package org.encog.cloud;

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

public class EncogCloud {
	
	private String session;
	
	public String constructService(String service)
	{
		return "http://cloud.encog.com/" + service;
	}
	
	public CloudTask beginTask(String name)
	{
		CloudTask result = new CloudTask(this);
		result.init(name);
		
		return result;
	}
	
	public void connect(String uid, String pwd)
	{
		CloudRequest request = new CloudRequest();
		Map<String,String> args = new HashMap<String,String>();
		args.put("uid", uid);
		args.put("pwd", pwd);
		request.performURLPOST(false, constructService("login"), args);
		if( !"success".equals(request.getStatus()))
		{
			throw new EncogCloudError(request.getMessage());
		}
		this.session = request.getSession();
	}
	
	public void logout()
	{
		CloudRequest request = new CloudRequest();
		request.performURLGET(false, this.session + "logout");
		this.session = null;
	}
	
	public void validateSession(boolean failOnError)
	{
		int max;
		
		if( failOnError )
			max = 1;
		else
			max = 5;
		
		for(int i=0;i<max;i++) {
			CloudRequest request = new CloudRequest();
			request.performURLGET(false, this.session);
			if( "success".equals(request.getStatus()))
				return;
		}
		
		if( failOnError )
		{
			throw new EncogCloudError("Connection lost");
		}
	}
	
	public boolean isConnected()
	{
		return session!=null;
	}
	
	public String getSession()
	{
		return session;
	}
}

@@ -109,133 +109,340 @@
 %0D%0A%09p
-rivate String session;%0D%0A%09%0D%0A%09public String constructService(String service)%0D%0A%09%7B%0D%0A%09%09return %22http://cloud.encog.com/%22 + service;
+ublic final String DEFAULT_SERVER = %22http://cloud.encog.com/%22;%0D%0A%09private String session;%0D%0A%09private String server;%0D%0A%09%0D%0A%09public String constructService(String service)%0D%0A%09%7B%0D%0A%09%09return this.server + service;%0D%0A%09%7D%0D%0A%09%0D%0A%09public EncogCloud(String server)%0D%0A%09%7B%0D%0A%09%09this.server = server;%0D%0A%09%09if( !this.server.endsWith(%22/%22))%0D%0A%09%09%09this.server+='/';%09%09
 %0D%0A%09%7D

Allow cloud to use any server.