CommunicationModule.java 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package classes.holonControlUnit;
  2. import classes.Holon;
  3. import classes.holonControlUnit.messages.Message;
  4. import classes.holonControlUnit.messages.NeighborhoodMsg;
  5. import classes.holonControlUnit.messages.StateMsg;
  6. import classes.holonControlUnit.messages.StateRequestMsg;
  7. import java.util.ArrayList;
  8. import java.util.HashSet;
  9. import java.util.Set;
  10. import com.google.gson.Gson;
  11. /**
  12. * this class is responsable for communication between holons
  13. * @author Jonas
  14. *
  15. */
  16. public class CommunicationModule {
  17. private HolonControlUnit hcu;
  18. private Gson gson;
  19. public CommunicationModule(HolonControlUnit owner) {
  20. this.hcu = owner;
  21. this.gson = new Gson();
  22. }
  23. public void sendMsg(String receiver, Message.Type type, String body) {
  24. //send msg to holon receiver
  25. Message msg = new Message(this.hcu.getHolon().getUniqueID(), receiver, type, body);
  26. //get receiver through his uniqueID
  27. Holon h = this.hcu.getHolon().model.getHolonsByID().get(receiver);
  28. if(h == null) {
  29. System.err.println("Could not find Holon: "+receiver+"\t in holons: "+this.hcu.getHolon().model.getHolonsByID());
  30. return;
  31. }
  32. h.holonControlUnit.getCommunicator().receiveMsg(gson.toJson(msg));
  33. }
  34. public void receiveMsg(String message) {
  35. Message msg = this.gson.fromJson(message, Message.class);
  36. if(!msg.getReceiver().equals(this.hcu.getHolon().getUniqueID()))
  37. throw new RuntimeException("Missleaded message:\n"+message);
  38. switch (msg.getType()) {
  39. case ORDER:
  40. break;
  41. case NEIGHBORHOOD:
  42. receiveNeighborhoodMsg(msg);
  43. break;
  44. case STATE_REQUEST:
  45. this.hcu.getStateEstimator().receiveStateRequest(msg.getSender(), this.gson.fromJson(msg.getBody(), StateRequestMsg.class));
  46. break;
  47. case STATE:
  48. this.hcu.getStateEstimator().receiveState(msg.getSender(), this.gson.fromJson(msg.getBody(), StateMsg.class));
  49. break;
  50. default:
  51. throw new RuntimeException("Unknown message type:\n"+message);
  52. }
  53. }
  54. private void receiveNeighborhoodMsg(Message msg) {
  55. NeighborhoodMsg nMsg = this.gson.fromJson(msg.getBody(), NeighborhoodMsg.class);
  56. ArrayList<String> neighbors = nMsg.getNeighbors();
  57. switch(nMsg.getType()) {
  58. case NEW_VIRTUAL_NEIGHBOR:
  59. this.hcu.getHierarchyController().addVirtualNeighbors(neighbors);
  60. break;
  61. case REMOVE_VIRTUAL_NEIGHBOR:
  62. this.hcu.getHierarchyController().removeVirtualNeighbors(neighbors);
  63. break;
  64. case NEW_PHYSICAL_NEIGHBOR:
  65. break;
  66. case REMOVE_PHYSICAL_NEIGHBOR:
  67. break;
  68. default:
  69. throw new RuntimeException("Unknown neighborhood type:\n"+msg);
  70. }
  71. }
  72. public Gson getGson() {
  73. return this.gson;
  74. }
  75. }