mirror of
https://github.com/balkian/SOJA.git
synced 2025-05-11 22:19:07 +00:00
Added event subscription for asynchronous events
This commit is contained in:
parent
27b74831fd
commit
7860e57974
@ -8,6 +8,7 @@ import io.socket.SocketIOException;
|
|||||||
import jason.asSyntax.Literal;
|
import jason.asSyntax.Literal;
|
||||||
|
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import net.sf.json.JSONArray;
|
import net.sf.json.JSONArray;
|
||||||
@ -33,8 +34,11 @@ public class SocketIOConnector implements AsyncWebServiceConnector{
|
|||||||
/** */
|
/** */
|
||||||
private Logger logger = Logger.getLogger("Web40SOJason." + SocketIOConnector.class.getName());
|
private Logger logger = Logger.getLogger("Web40SOJason." + SocketIOConnector.class.getName());
|
||||||
|
|
||||||
private static SocketIO socket;
|
private SocketIO socket;
|
||||||
static IOCallback callback = new IOCallback() {
|
|
||||||
|
private SOModel model;
|
||||||
|
|
||||||
|
IOCallback callback = new IOCallback() {
|
||||||
|
|
||||||
public void onMessage(JSONObject json, IOAcknowledge ack) {
|
public void onMessage(JSONObject json, IOAcknowledge ack) {
|
||||||
System.out.println("Message:" + json.toString());
|
System.out.println("Message:" + json.toString());
|
||||||
@ -62,7 +66,20 @@ public class SocketIOConnector implements AsyncWebServiceConnector{
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void on(String event, IOAcknowledge ack, Object... args) {
|
public void on(String event, IOAcknowledge ack, Object... args) {
|
||||||
|
logger.info("Package ack'ed");
|
||||||
|
JSONObject jso = new JSONObject();
|
||||||
|
String name = (String) jso.get("id");
|
||||||
|
String agName = "";
|
||||||
|
if(agentsID.containsKey(name)){
|
||||||
|
agName = agentsID.get(name);
|
||||||
|
}
|
||||||
|
else if(agentsEvent.containsKey(event)){
|
||||||
|
agName = agentsEvent.get(event);
|
||||||
|
}
|
||||||
|
String str = CollectionUtils.toPercepts(jso);
|
||||||
|
Literal lit = Literal.parseLiteral(str);
|
||||||
|
model.setDataInbox(agName, lit);
|
||||||
|
logger.info("DataInbox set to "+str);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -71,12 +88,18 @@ public class SocketIOConnector implements AsyncWebServiceConnector{
|
|||||||
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
private HashMap<String,String> agentsID;
|
||||||
|
private HashMap<String,String> agentsEvent;
|
||||||
|
|
||||||
|
|
||||||
//TODO Remove try-catch
|
//TODO Remove try-catch
|
||||||
/** Constructor */
|
/** Constructor */
|
||||||
public SocketIOConnector(String serviceUrl) {
|
public SocketIOConnector(String serviceUrl, SOModel model) {
|
||||||
|
this.model = model;
|
||||||
this.serviceUrl = serviceUrl;
|
this.serviceUrl = serviceUrl;
|
||||||
|
this.agentsID = new HashMap<String,String>();
|
||||||
|
this.agentsEvent = new HashMap<String,String>();
|
||||||
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
socket = new SocketIO();
|
socket = new SocketIO();
|
||||||
@ -131,4 +154,48 @@ public class SocketIOConnector implements AsyncWebServiceConnector{
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void addAgentForID(String name, String id){
|
||||||
|
agentsID.put(name, id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeAgentForID(String name){
|
||||||
|
agentsID.remove(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void removeAgentsForID(String id){
|
||||||
|
for(String key : agentsID.keySet()){
|
||||||
|
if(agentsID.get(key).equals(id)){
|
||||||
|
agentsID.remove(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void addAgentForEvent(String name, String id){
|
||||||
|
agentsEvent.put(name, id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeAgentForEvent(String name){
|
||||||
|
agentsEvent.remove(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeAgentsForEvent(String id){
|
||||||
|
for(String key : agentsEvent.keySet()){
|
||||||
|
if(agentsEvent.get(key).equals(id)){
|
||||||
|
agentsEvent.remove(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void removeAgentsByName(String name){
|
||||||
|
for(String key : agentsEvent.keySet()){
|
||||||
|
if(agentsEvent.get(key).equals(name)){
|
||||||
|
agentsEvent.remove(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for(String key : agentsID.keySet()){
|
||||||
|
if(agentsID.get(key).equals(name)){
|
||||||
|
agentsID.remove(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user