Advertisement
Guest User

Untitled

a guest
Apr 28th, 2016
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.47 KB | None | 0 0
  1. package session;
  2.  
  3. import java.io.IOException;
  4. import java.net.Inet4Address;
  5. import java.net.UnknownHostException;
  6. import java.util.Date;
  7. import java.util.HashMap;
  8.  
  9. import javax.ejb.EJB;
  10. import javax.ejb.LocalBean;
  11. import javax.ejb.Stateless;
  12. import javax.websocket.Session;
  13. import javax.ws.rs.GET;
  14. import javax.ws.rs.Path;
  15. import javax.ws.rs.Produces;
  16. import javax.ws.rs.QueryParam;
  17. import javax.ws.rs.core.GenericType;
  18. import javax.ws.rs.core.MediaType;
  19. import javax.ws.rs.core.Response;
  20.  
  21. import org.jboss.resteasy.client.jaxrs.ResteasyClient;
  22. import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
  23. import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
  24.  
  25. @LocalBean
  26. @Path("/client")
  27. @Stateless
  28. public class ClientApp implements ClientRemote {
  29.  
  30. @EJB
  31. CollectionsBean collection;
  32. @EJB
  33. JMSCollection jmsColl;
  34.  
  35. @GET
  36. @Path("/test")
  37. public String test() {
  38. // Host host = new Host("192.168.0.12", "master");
  39. // Host host = collection.getHost();
  40. // collection.addHost(new Host("192.168.0.12", "master"));
  41. // collection.addHost(new Host("192.168.0.12", "kmet1"));
  42. new JMSQueue("oj jmsu", "proba");
  43. return collection.getHost().toString();
  44. }
  45.  
  46. @GET
  47. @Path("/register")
  48. @Produces(MediaType.APPLICATION_JSON)
  49. public HashMap<String, Host> register(@QueryParam("ip") String ip, @QueryParam("ali") String ali) {
  50. Host newHost = new Host(ip, ali);
  51. collection.addHost(newHost);
  52. System.out.println("==========================REGISTER========================");
  53. System.out.println("==========================REGISTER========================");
  54. System.out.println("==========================REGISTER========================");
  55. System.out.println("==========================REGISTER========================");
  56. System.out.println("==========================REGISTER========================");
  57. return collection.getHost();
  58.  
  59. }
  60.  
  61. @GET
  62. @Path("/addUser")
  63. public void addUser(@QueryParam("user") String user, @QueryParam("sessionId") String sesionId){
  64. Session ses = jmsColl.getSessions().get(sesionId);
  65. jmsColl.getUserSession().put(user, jmsColl.getSessions().get(sesionId));
  66.  
  67. try {
  68. ses.getBasicRemote().sendText("true");
  69. System.out.println("POSLAO JE NA SESIJU REFRESH!");
  70. } catch (IOException e) {
  71. // TODO Auto-generated catch block
  72. e.printStackTrace();
  73. }
  74. }
  75.  
  76. @GET
  77. @Path("/registerSelf")
  78. public String registerSelf(/* Host host */) {
  79. // collection.addHost(new Host("192.168.0.12", "kmet1"));
  80. Host temp = new Host();
  81. try {
  82. temp = collection.getHost().get(Inet4Address.getLocalHost().getHostAddress().toString());
  83. } catch (UnknownHostException e) {
  84. // TODO Auto-generated catch block
  85. e.printStackTrace();
  86. }
  87. if (temp == null) {
  88. temp = new Host();
  89. }
  90. ResteasyClient client = new ResteasyClientBuilder().build();
  91. ResteasyWebTarget target = client.target("http://192.168.0.12:8080/ClientWeb/rest/client/register?ip="
  92. + temp.getAddress() + "&ali=" + temp.getAlias());
  93. Response response = target.request(MediaType.APPLICATION_JSON).get();
  94. collection.setHost(response.readEntity(HashMap.class));
  95. return collection.getHost().toString();
  96. }
  97.  
  98. @GET
  99. @Path("/unregister")
  100. public String/* HashMap<String, Host> */ unregister(/* Host host */) {
  101. // collection.removeHost(new Host("192.168.0.12", "kmet1"));
  102.  
  103. return collection.getHost().toString();
  104. }
  105.  
  106. @GET
  107. @Path("/callUser")
  108. @Produces(MediaType.APPLICATION_JSON)
  109. public HashMap<String, User> callUser() {
  110. ResteasyClient client = new ResteasyClientBuilder().build();
  111. ResteasyWebTarget target = client.target("http://192.168.0.12:8080/ClientWeb/rest/user/hello");
  112. Response response = target.request(MediaType.APPLICATION_JSON).get();
  113. return response.readEntity(new GenericType<HashMap<String, User>>(){});
  114. }
  115.  
  116. @GET
  117. @Path("/loginUser")
  118. public String loginUser(@QueryParam("user") String user, @QueryParam("pass") String pass,
  119. @QueryParam("sessionId") String sesionId, @QueryParam("address") String address) {
  120. String myAddress = "";
  121. try {
  122. myAddress = Inet4Address.getLocalHost().getHostAddress().toString();
  123. } catch (UnknownHostException e) {
  124. // TODO Auto-generated catch block
  125. e.printStackTrace();
  126. }
  127. ResteasyClient client = new ResteasyClientBuilder().build();
  128. ResteasyWebTarget target = client.target("http://192.168.0.12:8080/ClientWeb/rest/user/login?user=" + user
  129. + "&pass=" + pass + "&host=" + address + "&sessionId=" + sesionId);
  130. Response response = target.request(MediaType.APPLICATION_JSON).get();
  131. refreshUsers();
  132. for (String h : collection.getHost().keySet()) {
  133. if(!h.equals(myAddress)){
  134. client = new ResteasyClientBuilder().build();
  135. target = client.target("http://"+h+":8080/ClientWeb/rest/client/refreshUsers");
  136. response = target.request(MediaType.APPLICATION_JSON).get();
  137. }
  138. }
  139.  
  140. return "";
  141. }
  142.  
  143. @GET
  144. @Path("/logoutUser")
  145. public String loginUser(@QueryParam("user") String user) {
  146.  
  147. ResteasyClient client = new ResteasyClientBuilder().build();
  148. ResteasyWebTarget target = client.target("http://192.168.0.12:8080/ClientWeb/rest/user/logout?user=" + user);
  149. Response response = target.request().get();
  150. return response.readEntity(String.class);
  151. }
  152.  
  153. @GET
  154. @Path("/sendMsg")
  155. public void sendMsg(@QueryParam("msg") String msg, @QueryParam("socketId") String socketId) {
  156. String myAddress = "";
  157. try {
  158. myAddress = Inet4Address.getLocalHost().getHostAddress().toString();
  159. } catch (UnknownHostException e) {
  160. // TODO Auto-generated catch block
  161. e.printStackTrace();
  162. }
  163. String f = "";
  164. for (String u : jmsColl.getUserSession().keySet()) {
  165. if (jmsColl.getUserSession().get(u).getId().equals(socketId)) {
  166. f = u;
  167. break;
  168. }
  169. }
  170. postMsg(f, msg);
  171.  
  172. for (String h : collection.getHost().keySet()) {
  173. if(!h.equals(myAddress)){
  174. ResteasyClient client = new ResteasyClientBuilder().build();
  175. ResteasyWebTarget target = client.target("http://"+h+":8080/ClientWeb/rest/client/postMsg?user="+f+"&msg="+msg);
  176. Response response = target.request().get();
  177. }
  178. }
  179.  
  180. // ResteasyClient client = new ResteasyClientBuilder().build();
  181. // ResteasyWebTarget target = client.target("http://192.168.0.12:8080/ClientWeb/rest/user/logout?user=" + user);
  182. // Response response = target.request().get();
  183.  
  184. }
  185.  
  186. @GET
  187. @Path("/postMsg")
  188. public void postMsg(@QueryParam("user") String user, @QueryParam("msg") String msg){
  189. String[] splitted = msg.split(",");
  190. System.out.println(msg);
  191. if(splitted[2].equals("public")){
  192. for (Session ses : jmsColl.getUserSession().values()) {
  193. User from = callUser().get(user);
  194.  
  195. Message mes = new Message(from, null, new Date(), "", splitted[1]);
  196. try {
  197. Thread.sleep(50);
  198. ses.getBasicRemote().sendText("text," + mes.toString());
  199. System.out.println("POSLAO JE NA SESIJU REFRESH!");
  200. } catch (Exception e) {
  201. // TODO Auto-generated catch block
  202. e.printStackTrace();
  203. }
  204.  
  205.  
  206. }
  207. }
  208. }
  209.  
  210. @GET
  211. @Path("/registerUser")
  212. public String registerUser(@QueryParam("user") String user, @QueryParam("pass") String pass) {
  213. ResteasyClient client = new ResteasyClientBuilder().build();
  214. ResteasyWebTarget target = client
  215. .target("http://192.168.0.12:8080/ClientWeb/rest/user/register?user=" + user + "&pass=" + pass);
  216. Response response = target.request().get();
  217. return response.readEntity(String.class);
  218. }
  219.  
  220. @GET
  221. @Path("/refreshUsers")
  222. public void refreshUsers() {
  223.  
  224. HashMap<String, User> users = callUser();
  225. String address = "";
  226.  
  227. try {
  228. address = Inet4Address.getLocalHost().getHostAddress();
  229. } catch (UnknownHostException e) {
  230. // TODO Auto-generated catch block
  231. e.printStackTrace();
  232. }
  233. System.out.println(users.toString());
  234. for (String u : users.keySet()) {
  235. System.out.println(users.get(u).getHost().getAddress());
  236. if(users.get(u).getHost().getAddress().equals(address)){
  237. Session ses = jmsColl.getUserSession().get(u);
  238.  
  239. try {
  240. ses.getBasicRemote().sendText("clear");
  241. System.out.println("POSLAO JE NA SESIJU REFRESH!");
  242. } catch (IOException e) {
  243. // TODO Auto-generated catch block
  244. e.printStackTrace();
  245. }
  246.  
  247.  
  248. for (String usr : users.keySet()) {
  249. try {
  250. Thread.sleep(50);
  251. ses.getBasicRemote().sendText("add," + usr);
  252. System.out.println("POSLAO JE NA SESIJU REFRESH!");
  253. } catch (Exception e) {
  254. // TODO Auto-generated catch block
  255. e.printStackTrace();
  256. }
  257. }
  258. }
  259. }
  260.  
  261. }
  262. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement