package classes.holonControlUnit; import classes.Holon; import classes.holonControlUnit.messages.MergeMsg; import classes.holonControlUnit.messages.Message; import classes.holonControlUnit.messages.NeighborhoodMsg; import classes.holonControlUnit.messages.OrderMsg; import classes.holonControlUnit.messages.SplitMsg; import classes.holonControlUnit.messages.StateMsg; import classes.holonControlUnit.messages.StateRequestMsg; import classes.holonControlUnit.messages.MergeOrderMsg; import java.util.ArrayList; import com.google.gson.Gson; /** * this class is responsable for communication between holons * @author Jonas * */ public class CommunicationModule { private HolonControlUnit hcu; private Gson gson; public CommunicationModule(HolonControlUnit owner) { this.hcu = owner; this.gson = new Gson(); } public void sendMsg(String receiver, Message.Type type, String body) { //send msg to holon receiver Message msg = new Message(this.hcu.getHolon().getUniqueID(), receiver, type, body); //get receiver through his uniqueID Holon h = this.hcu.getHolon().model.getHolonsByID().get(receiver); if(h == null) { System.err.println("Could not find Holon: "+receiver+"\t in holons: "+this.hcu.getHolon().model.getHolonsByID()); return; } h.holonControlUnit.getCommunicator().receiveMsg(gson.toJson(msg)); } public void receiveMsg(String message) { Message msg = this.gson.fromJson(message, Message.class); if(!msg.getReceiver().equals(this.hcu.getHolon().getUniqueID())) throw new RuntimeException("Missleaded message:\n"+message); switch (msg.getType()) { case ORDER: this.hcu.getStateAssembler().setOrder(this.gson.fromJson(msg.getBody(), OrderMsg.class), msg.getSender()); break; case NEIGHBORHOOD: receiveNeighborhoodMsg(msg); break; case STATE_REQUEST: this.hcu.receiveStateRequest(msg.getSender(), this.gson.fromJson(msg.getBody(), StateRequestMsg.class)); break; case STATE: this.hcu.getStateEstimator().receiveState(msg.getSender(), this.gson.fromJson(msg.getBody(), StateMsg.class)); break; case MERGE: receiveMergeMsg(msg); break; case SPLIT: receiveSplitMsg(msg); break; case MERGE_ORDER: this.hcu.getHierarchyController().receiveMergeOrder(this.gson.fromJson(msg.getBody(), MergeOrderMsg.class), msg.getSender()); break; default: throw new RuntimeException("Unknown message type:\n"+message); } } private void receiveNeighborhoodMsg(Message msg) { NeighborhoodMsg nMsg = this.gson.fromJson(msg.getBody(), NeighborhoodMsg.class); ArrayList neighbors = nMsg.getNeighbors(); switch(nMsg.getType()) { case NEW_VIRTUAL_NEIGHBOR: this.hcu.getHierarchyController().addVirtualNeighbors(neighbors); break; case REMOVE_VIRTUAL_NEIGHBOR: this.hcu.getHierarchyController().removeVirtualNeighbors(neighbors); break; // case NEW_PHYSICAL_NEIGHBOR: // break; // case REMOVE_PHYSICAL_NEIGHBOR: // break; default: System.err.println("Unknown neighborhood type:\n"+msg); } } private void receiveMergeMsg(Message msg) { MergeMsg mMsg = this.gson.fromJson(msg.getBody(), MergeMsg.class); switch(mMsg.getType()) { case REQ: this.hcu.getHierarchyController().receiveMergeReq(mMsg, msg.getSender()); break; case ACK: this.hcu.getHierarchyController().receiveMergeAck(mMsg, msg.getSender()); break; case ACK_II: this.hcu.getHierarchyController().receiveMergeAckII(mMsg, msg.getSender()); break; default: System.err.println("Unknown neighborhood type:\n"+msg); } } private void receiveSplitMsg(Message msg) { SplitMsg split = this.gson.fromJson(msg.getBody(), SplitMsg.class); this.hcu.getHierarchyController().receiveSplitMsg(split, msg.getSender()); } public Gson getGson() { return this.gson; } }