package com.futureplatforms.kirin.controllers;

import java.util.HashMap;
import java.util.Map;

import com.futureplatforms.kirin.dependencies.StaticDependencies;
import com.futureplatforms.kirin.dependencies.TimerTask;
import com.futureplatforms.kirin.dependencies.StaticDependencies.NetworkDelegate.HttpVerb;
import com.futureplatforms.kirin.dependencies.StaticDependencies.NetworkDelegate.NetworkResponse;

public class NetworkHelper {
    
    private final StaticDependencies _Dependencies;
    private boolean _Cancelled = false;
    
    public NetworkHelper(StaticDependencies dependencies) {
        this._Dependencies = dependencies;
    }

    public final void execute(HttpVerb verb, String url, NetworkResponse callback) {
        execute(verb, url, null, new HashMap<String, String>(), callback);
    }
    
    public final void execute(HttpVerb verb, String url, String payload, NetworkResponse callback) {
        execute(verb, url, payload, new HashMap<String, String>(), callback);
    }
    
    public final void execute(HttpVerb verb, String url, String payload, Map<String, String> headers, final NetworkResponse callback) {
        final TimerTask tt = new TimerTask(_Dependencies) {

            @Override
            public void run() {
                _Cancelled = true;
                callback.onFail("timeout");
            }
            
        };
        tt.schedule(30000);
        _Dependencies.getLogDelegate().log("connecting to " + url);
        _Dependencies.getNetworkDelegate().doHttp(verb, url, payload, headers, new NetworkResponse() {
            
            public void onSuccess(int res, String result, Map<String, String> headers) {
                if (!_Cancelled) {
                    callback.onSuccess(res, result, headers);
                }
            }
            
            public void onFail(String code) {
                if (!_Cancelled) {
                    callback.onFail(code);
                }
            }
        });
    }
    
    public final void cancel() {
        _Cancelled = true;
    }
}

@@ -1264,28 +1264,58 @@
             
-    
+%09if (!_Cancelled) %7B%0A            %09%09
 _Cancelled =
@@ -1325,36 +1325,34 @@
 ue;%0A            
-    
+%09%09
 callback.onFail(
@@ -1363,16 +1363,31 @@
 eout%22);%0A
+            %09%7D%0A
         
@@ -1448,76 +1448,8 @@
 0);%0A
-        _Dependencies.getLogDelegate().log(%22connecting to %22 + url);%0A
     
@@ -1645,24 +1645,50 @@
  headers) %7B%0A
+            %09tt.cancel();%0A
             
@@ -1859,24 +1859,50 @@
 ing code) %7B%0A
+            %09tt.cancel();%0A
             

Remove network helper timeout weirdness