package com.creative.service;

import java.util.concurrent.ConcurrentHashMap;

import com.creative.context.Context;
import com.creative.disruptor.DisruptorEvent;
import org.json.JSONException;

public class StateService implements GeneralService {
	public StateService(){
	}
	ConcurrentHashMap <String,String> messageList = new ConcurrentHashMap<String, String>();

	public boolean canHandle(String command) {
		if(command == null || "".equals(command)) return false;
		return command.startsWith("STATE_");
	}

	@Override
	public void onEvent(DisruptorEvent event) throws Exception {
		Context context = event.context;
		String command;
		try{
			command = context.getRequest().get(GeneralService.COMMAND);
		}catch(JSONException e){
			return;
		}
		if(!canHandle(command)) return;
		String result = "";
		switch(command){
		case "STATE_GET":
			result = messageList.get(context.getRequest().get(GeneralService.FROM));
			break;
		default:
			messageList.put(context.getRequest().get(GeneralService.TO), context.getRequest().toString());
			result = "{STATE:OK}";
			break;
		}
		if(result == null || "".equals(result)) result = "{COMMAND:STATE_SET;FROM:nil;TO:"+ context.getRequest().get(GeneralService.FROM) +";DATA:nil}";
		context.setResponse(result);

	}
}

@@ -929,15 +929,136 @@
 ;%0A%09%09
-default
+case %22STATE_INFO%22:%0A%09%09%09result = getInfo();%0A%09%09%09break;%0A%09%09case %22STATE_STATUS%22:%0A%09%09%09result = getStatus();%0A%09%09%09break;%0A%09%09case %22STATE_SET%22
 :%0A%09%09
@@ -1374,11 +1374,181 @@
 lt);%0A%0A%09%7D
+%0A%0A%09private String getStatus() %7B%0A%09%09// TODO Auto-generated method stub%0A%09%09return null;%0A%09%7D%0A%0A%09private String getInfo() %7B%0A%09%09// TODO Auto-generated method stub%0A%09%09return null;%0A%09%7D
 %0A%7D%0A

add more features to State service