Advertisement
Guest User

Untitled

a guest
Aug 27th, 2014
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.63 KB | None | 0 0
  1.  
  2. import java.io.IOException;
  3. import java.util.concurrent.ExecutorService;
  4. import java.util.concurrent.Executors;
  5.  
  6. import models.IronArgusFlow;
  7. import models.IronMitigationEvent;
  8.  
  9. import org.apache.http.client.ClientProtocolException;
  10. import org.atmosphere.annotation.Broadcast;
  11. import org.atmosphere.cpr.Broadcaster;
  12. import org.atmosphere.jersey.Broadcastable;
  13. import org.atmosphere.jersey.SuspendResponse;
  14.  
  15. import com.ironnet.Hermes;
  16. import com.ironnet.exceptions.PublishException;
  17. import com.ironnet.models.type.IronType;
  18.  
  19. import Runnables.ArgusFlowRunnable;
  20. import Runnables.IronMitigationRunnable;
  21. import Runnables.IronScoreBaselineRunnable;
  22. import Runnables.IronScoreClusterRunnable;
  23.  
  24. import javax.ws.rs.FormParam;
  25. import javax.ws.rs.GET;
  26. import javax.ws.rs.POST;
  27. import javax.ws.rs.Path;
  28. import javax.ws.rs.PathParam;
  29. import javax.ws.rs.Produces;
  30.  
  31. /**
  32. * Simple PubSub resource that demonstrate many functionality supported by
  33. * Atmosphere Javascript and Atmosphere Jersey extension.
  34. *
  35. * @author Jeanfrancois Arcand
  36. */
  37. @Path("/pubsub/{topic}")
  38. public class JerseyPubSub {
  39.  
  40. private
  41. @PathParam("topic")
  42. Broadcaster topic;
  43.  
  44. @GET
  45. public SuspendResponse<String> subscribe() {
  46. import java.io.IOException;
  47. import java.util.concurrent.ExecutorService;
  48. import java.util.concurrent.Executors;
  49.  
  50. import models.IronArgusFlow;
  51. import models.IronMitigationEvent;
  52.  
  53. import org.apache.http.client.ClientProtocolException;
  54. import org.atmosphere.annotation.Broadcast;
  55. import org.atmosphere.cpr.Broadcaster;
  56. import org.atmosphere.jersey.Broadcastable;
  57. import org.atmosphere.jersey.SuspendResponse;
  58.  
  59. import com.ironnet.Hermes;
  60. import com.ironnet.exceptions.PublishException;
  61. import com.ironnet.models.type.IronType;
  62.  
  63. import Runnables.ArgusFlowRunnable;
  64. import Runnables.IronMitigationRunnable;
  65. import Runnables.IronScoreBaselineRunnable;
  66. import Runnables.IronScoreClusterRunnable;
  67.  
  68. import javax.ws.rs.FormParam;
  69. import javax.ws.rs.GET;
  70. import javax.ws.rs.POST;
  71. import javax.ws.rs.Path;
  72. import javax.ws.rs.PathParam;
  73. import javax.ws.rs.Produces;
  74.  
  75. /**
  76. * Simple PubSub resource that demonstrate many functionality supported by
  77. * Atmosphere Javascript and Atmosphere Jersey extension.
  78. *
  79. * @author Jeanfrancois Arcand
  80. */
  81. @Path("/pubsub/{topic}")
  82. public class JerseyPubSub {
  83.  
  84. private
  85. @PathParam("topic")
  86. Broadcaster topic;
  87.  
  88. @GET
  89. public SuspendResponse<String> subscribe() {
  90.  
  91. Broadcaster b = topic;
  92.  
  93. // ArgusFlowRunnable afr = new ArgusFlowRunnable(topic);
  94. IronScoreClusterRunnable iscr = new IronScoreClusterRunnable(topic);
  95. // IronScoreBaselineRunnable isbr = new IronScoreBaselineRunnable(topic);
  96. // IronMitigationRunnable imr = new IronMitigationRunnable (topic);
  97.  
  98. // b.getBroadcasterConfig().getExecutorService().execute(afr);
  99. b.getBroadcasterConfig().getExecutorService().execute(iscr);
  100. // b.getBroadcasterConfig().getExecutorService().execute(isbr);
  101. // b.getBroadcasterConfig().getExecutorService().execute(imr);
  102.  
  103.  
  104. return new SuspendResponse.SuspendResponseBuilder<String>()
  105. .broadcaster(topic)
  106. .outputComments(true)
  107. .addListener(new EventsLogger())
  108. .build();
  109. }
  110.  
  111. @POST
  112. @Broadcast
  113. @Produces("text/json")
  114. public Broadcastable publish(String message) {
  115.  
  116. IronType type;
  117. try {
  118. type = Hermes.createIronType(IronMitigationEvent.getSchema());
  119. } catch (ClientProtocolException e1) {
  120. // TODO Auto-generated catch block
  121. e1.printStackTrace();
  122. } catch (IOException e1) {
  123. // TODO Auto-generated catch block
  124. e1.printStackTrace();
  125. }
  126.  
  127. // Set up to get the topic in the KV
  128. if (Hermes.getIronTopic("mitigation_recommended") == null)
  129. {
  130. try {
  131. Hermes.createIronTopic("mitigation_recommended", type.getGuid(), null);
  132. } catch (ClientProtocolException e) {
  133. // TODO Auto-generated catch block
  134. e.printStackTrace();
  135. } catch (IOException e) {
  136. // TODO Auto-generated catch block
  137. e.printStackTrace();
  138. }
  139. }
  140.  
  141. long start = System.currentTimeMillis();
  142.  
  143. // The server should've already defined the "triplet list" for the
  144. // data they will be sending. We are sending ServerUser objects, and
  145. // have defined the triplets within that class.
  146. for (int i = 0; i < 1000; i++)
  147. {
  148. IronArgusFlow flow = new IronArgusFlow();
  149. flow.timeSinceEpoch = System.currentTimeMillis();
  150. flow.saddr = Integer.toString(i);
  151.  
  152. System.out.println(flow.saddr);
  153. // A static method on the data type seems like an easy way
  154. // to always have access to the triplet list.
  155. //
  156. // "testtopic" just happens to be the topic we are producing on,
  157. // but in the real scenario, we would've created a new topic
  158. // with a GUID and called "createTopic". This is just here to
  159. // show how to publish data sans KV
  160. try
  161. {
  162. Hermes.publishData(type, flow, "argus_flow");
  163. }
  164. catch (IOException | PublishException e)
  165. {
  166. e.printStackTrace();
  167. }
  168. }
  169.  
  170. long end = System.currentTimeMillis();
  171.  
  172. System.out.println("Time elapsed: " + (end - start));
  173.  
  174. System.out.println("recvd: " + message);
  175. return new Broadcastable(message, "", topic);
  176. }
  177. }
  178.  
  179. Broadcaster b = topic;
  180.  
  181. // ArgusFlowRunnable afr = new ArgusFlowRunnable(topic);
  182. IronScoreClusterRunnable iscr = new IronScoreClusterRunnable(topic);
  183. // IronScoreBaselineRunnable isbr = new IronScoreBaselineRunnable(topic);
  184. // IronMitigationRunnable imr = new IronMitigationRunnable (topic);
  185.  
  186. // b.getBroadcasterConfig().getExecutorService().execute(afr);
  187. b.getBroadcasterConfig().getExecutorService().execute(iscr);
  188. // b.getBroadcasterConfig().getExecutorService().execute(isbr);
  189. // b.getBroadcasterConfig().getExecutorService().execute(imr);
  190.  
  191.  
  192. return new SuspendResponse.SuspendResponseBuilder<String>()
  193. .broadcaster(topic)
  194. .outputComments(true)
  195. .addListener(new EventsLogger())
  196. .build();
  197. }
  198.  
  199. @POST
  200. @Broadcast
  201. @Produces("text/json")
  202. public Broadcastable publish(String message) {
  203.  
  204. IronType type;
  205. try {
  206. type = Hermes.createIronType(IronMitigationEvent.getSchema());
  207. } catch (ClientProtocolException e1) {
  208. // TODO Auto-generated catch block
  209. e1.printStackTrace();
  210. } catch (IOException e1) {
  211. // TODO Auto-generated catch block
  212. e1.printStackTrace();
  213. }
  214.  
  215. // Set up to get the topic in the KV
  216. if (Hermes.getIronTopic("mitigation_recommended") == null)
  217. {
  218. try {
  219. Hermes.createIronTopic("mitigation_recommended", type.getGuid(), null);
  220. } catch (ClientProtocolException e) {
  221. // TODO Auto-generated catch block
  222. e.printStackTrace();
  223. } catch (IOException e) {
  224. // TODO Auto-generated catch block
  225. e.printStackTrace();
  226. }
  227. }
  228.  
  229. long start = System.currentTimeMillis();
  230.  
  231. // The server should've already defined the "triplet list" for the
  232. // data they will be sending. We are sending ServerUser objects, and
  233. // have defined the triplets within that class.
  234. for (int i = 0; i < 1000; i++)
  235. {
  236. IronArgusFlow flow = new IronArgusFlow();
  237. flow.timeSinceEpoch = System.currentTimeMillis();
  238. flow.saddr = Integer.toString(i);
  239.  
  240. System.out.println(flow.saddr);
  241. // A static method on the data type seems like an easy way
  242. // to always have access to the triplet list.
  243. //
  244. // "testtopic" just happens to be the topic we are producing on,
  245. // but in the real scenario, we would've created a new topic
  246. // with a GUID and called "createTopic". This is just here to
  247. // show how to publish data sans KV
  248. try
  249. {
  250. Hermes.publishData(type, flow, "argus_flow");
  251. }
  252. catch (IOException | PublishException e)
  253. {
  254. e.printStackTrace();
  255. }
  256. }
  257.  
  258. long end = System.currentTimeMillis();
  259.  
  260. System.out.println("Time elapsed: " + (end - start));
  261.  
  262. System.out.println("recvd: " + message);
  263. return new Broadcastable(message, "", topic);
  264. }
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement