CommunicationModule.java 3.5 KB

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