PageRenderTime 39ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/Java/OPSJLib/src/ops/Manager.java

http://ops.googlecode.com/
Java | 239 lines | 175 code | 37 blank | 27 comment | 16 complexity | aa8af4d190525aa9e10d07f9a2846e1d MD5 | raw file
  1. /**
  2. *
  3. * Copyright (C) 2006-2009 Anton Gravestam.
  4. *
  5. * This file is part of OPS (Open Publish Subscribe).
  6. *
  7. * OPS (Open Publish Subscribe) is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. * OPS (Open Publish Subscribe) is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with OPS (Open Publish Subscribe). If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package ops;
  20. import java.io.IOException;
  21. import java.net.DatagramPacket;
  22. import java.net.InetAddress;
  23. import java.net.MulticastSocket;
  24. import java.net.UnknownHostException;
  25. import java.util.TreeMap;
  26. import java.util.Vector;
  27. import ops.*;
  28. /**
  29. *
  30. * @author Anton
  31. */
  32. public class Manager implements Runnable
  33. {
  34. private static Manager theManager;
  35. public static int MAX_SIZE = 65000;
  36. public static int MANAGER_PORT = 14000;
  37. public static int TOPIC_BASE_PORT = 14001;
  38. public static String handShake =":-) ";
  39. private MulticastSocket multicastSocket;
  40. private Vector<Topic> topics = new Vector<Topic>();
  41. private byte[] readBytes;
  42. private Thread thread;
  43. /** Creates a new instance of Manager */
  44. private Manager()
  45. {
  46. try
  47. {
  48. multicastSocket = new MulticastSocket(MANAGER_PORT);
  49. multicastSocket.joinGroup(InetAddress.getByName("235.5.5.5"));
  50. sendHandShake();
  51. }
  52. catch (UnknownHostException ex)
  53. {
  54. ex.printStackTrace();
  55. }
  56. catch (IOException ex)
  57. {
  58. ex.printStackTrace();
  59. }
  60. thread = new Thread(this);
  61. thread.start();
  62. try
  63. {
  64. System.out.println("Handshaking...");
  65. Thread.sleep(1000);
  66. System.out.println("Handshaking done.");
  67. }
  68. catch (InterruptedException ex)
  69. {
  70. ex.printStackTrace();
  71. }
  72. }
  73. public int getTopicPort(String topicKey)
  74. {
  75. for (Topic t : topics)
  76. {
  77. if(t.getName().equals(topicKey))
  78. return t.getPort();
  79. }
  80. return -1;
  81. }
  82. //By singelton pattern
  83. public static Manager getManager()
  84. {
  85. if(theManager == null)
  86. theManager = new Manager();
  87. return theManager;
  88. }
  89. public void run()
  90. {
  91. while(true)
  92. {
  93. readBytes = new byte[Manager.MAX_SIZE];
  94. DatagramPacket p = new DatagramPacket(readBytes, Manager.MAX_SIZE);
  95. try
  96. {
  97. multicastSocket.receive(p);
  98. String s = new String(p.getData());
  99. String[] sArray = s.split(" ");
  100. if(sArray[0].equals(":-)"))
  101. {
  102. sendTopics();
  103. }
  104. else if(sArray[0].equals("&"))
  105. {
  106. if(getTopicPort(sArray[1]) == -1)
  107. {
  108. //topics.add(new Topic(sArray[1], Integer.parseInt(sArray[2])));
  109. System.out.println("Topic " + sArray[1] + " updated at port " + getTopicPort(sArray[1]));
  110. }
  111. }
  112. else
  113. {
  114. if(getTopicPort(sArray[0]) != -1)
  115. {
  116. System.out.println("Topic " + sArray[0] + " found at port " + getTopicPort(sArray[0]));
  117. }
  118. else
  119. {
  120. //topics.add(new Topic(sArray[0], TOPIC_BASE_PORT + topics.size()));
  121. System.out.println("Topic " + sArray[0] + " registered at port " + (TOPIC_BASE_PORT + topics.size()-1));
  122. }
  123. }
  124. }
  125. catch (IOException ex)
  126. {
  127. ex.printStackTrace();
  128. }
  129. catch (ArrayIndexOutOfBoundsException e)
  130. {
  131. System.out.println("Topic refused");
  132. e.printStackTrace();
  133. }
  134. }
  135. }
  136. private void sendHandShake()
  137. {
  138. try
  139. {
  140. multicastSocket.send(new DatagramPacket(handShake.getBytes(), handShake.length(),InetAddress.getByName("235.5.5.5"),Manager.MANAGER_PORT));
  141. }
  142. catch (IOException ex)
  143. {
  144. ex.printStackTrace();
  145. }
  146. }
  147. private void sendTopics()
  148. {
  149. for(Topic t : topics)
  150. {
  151. String message = "& " + t.getName() + " " + t.getPort() + " ";
  152. try
  153. {
  154. multicastSocket.send(new DatagramPacket(message.getBytes(), message.length(),InetAddress.getByName("235.5.5.5"),Manager.MANAGER_PORT));
  155. }
  156. catch (IOException ex)
  157. {
  158. ex.printStackTrace();
  159. }
  160. }
  161. }
  162. public void registerSubscriber(String topic)
  163. {
  164. if(getTopicPort(topic) == -1)
  165. {
  166. registerTopic(topic);
  167. }
  168. }
  169. public void registerPublisher(String topic)
  170. {
  171. if(getTopicPort(topic) == -1)
  172. {
  173. registerTopic(topic);
  174. }
  175. }
  176. private void registerTopic(String topic)
  177. {
  178. try
  179. {
  180. multicastSocket = new MulticastSocket(Manager.MANAGER_PORT);
  181. multicastSocket.joinGroup(InetAddress.getByName("235.5.5.5"));
  182. }
  183. catch (UnknownHostException ex)
  184. {
  185. ex.printStackTrace();
  186. }
  187. catch (IOException ex)
  188. {
  189. ex.printStackTrace();
  190. }
  191. String managerString = topic + " " + "TestClass" + " ";//data.getClass().getName();
  192. try
  193. {
  194. multicastSocket.send(new DatagramPacket(managerString.getBytes(), managerString.length(),InetAddress.getByName("235.5.5.5"),Manager.MANAGER_PORT));
  195. }
  196. catch (IOException ex)
  197. {
  198. ex.printStackTrace();
  199. }
  200. }
  201. }