Advertisement
Guest User

Untitled

a guest
Apr 28th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.85 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 void loginUser(@QueryParam("session") String sesId) {
  146. String user = "";
  147.  
  148.  
  149. for (String u : jmsColl.getUserSession().keySet()) {
  150. if (jmsColl.getUserSession().get(u).getId().equals(sesId)) {
  151. user = u;
  152. break;
  153. }
  154. }
  155. ResteasyClient client = new ResteasyClientBuilder().build();
  156. ResteasyWebTarget target = client.target("http://192.168.0.12:8080/ClientWeb/rest/user/logout?user=" + user);
  157. Response response = target.request().get();
  158.  
  159. String myAddress = "";
  160.  
  161. try {
  162. myAddress = Inet4Address.getLocalHost().getHostAddress().toString();
  163. } catch (UnknownHostException e) {
  164. // TODO Auto-generated catch block
  165. e.printStackTrace();
  166. }
  167.  
  168. refreshUsers();
  169. for (String h : collection.getHost().keySet()) {
  170. if(!h.equals(myAddress)){
  171. client = new ResteasyClientBuilder().build();
  172. target = client.target("http://"+h+":8080/ClientWeb/rest/client/refreshUsers");
  173. response = target.request(MediaType.APPLICATION_JSON).get();
  174. }
  175. }
  176.  
  177. }
  178.  
  179. @GET
  180. @Path("/removeUser")
  181. public void removeUser(@QueryParam("user")String user){
  182. jmsColl.getUserSession().remove(user);
  183. }
  184.  
  185. @GET
  186. @Path("/sendMsg")
  187. public void sendMsg(@QueryParam("msg") String msg, @QueryParam("socketId") String socketId) {
  188. String myAddress = "";
  189. try {
  190. myAddress = Inet4Address.getLocalHost().getHostAddress().toString();
  191. } catch (UnknownHostException e) {
  192. // TODO Auto-generated catch block
  193. e.printStackTrace();
  194. }
  195. String f = "";
  196. for (String u : jmsColl.getUserSession().keySet()) {
  197. if (jmsColl.getUserSession().get(u).getId().equals(socketId)) {
  198. f = u;
  199. break;
  200. }
  201. }
  202. postMsg(f, msg);
  203.  
  204. for (String h : collection.getHost().keySet()) {
  205. if(!h.equals(myAddress)){
  206. ResteasyClient client = new ResteasyClientBuilder().build();
  207. ResteasyWebTarget target = client.target("http://"+h+":8080/ClientWeb/rest/client/postMsg?user="+f+"&msg="+msg);
  208. Response response = target.request().get();
  209. }
  210. }
  211.  
  212. // ResteasyClient client = new ResteasyClientBuilder().build();
  213. // ResteasyWebTarget target = client.target("http://192.168.0.12:8080/ClientWeb/rest/user/logout?user=" + user);
  214. // Response response = target.request().get();
  215.  
  216. }
  217.  
  218. @GET
  219. @Path("/postMsg")
  220. public void postMsg(@QueryParam("user") String user, @QueryParam("msg") String msg){
  221. String[] splitted = msg.split(",");
  222. System.out.println(msg);
  223. if(splitted[2].equals("public")){
  224. for (Session ses : jmsColl.getUserSession().values()) {
  225. User from = callUser().get(user);
  226.  
  227. Message mes = new Message(from, null, new Date(), "", splitted[1]);
  228. try {
  229. Thread.sleep(50);
  230. ses.getBasicRemote().sendText("text," + mes.toString());
  231. System.out.println("POSLAO JE NA SESIJU REFRESH!");
  232. } catch (Exception e) {
  233. // TODO Auto-generated catch block
  234. e.printStackTrace();
  235. }
  236.  
  237.  
  238. }
  239. }else
  240. {
  241. User from = callUser().get(user);
  242. Message mes = new Message(from, callUser().get(splitted[2]), new Date(), "", splitted[1]);
  243. for (String u : callUser().keySet()) {
  244. if (u.equals(splitted[2]) || u.equals(from.getUsername())) {
  245. Session ses = jmsColl.getUserSession().get(u);
  246.  
  247. try {
  248. Thread.sleep(50);
  249. ses.getBasicRemote().sendText("text," + mes.toString());
  250. System.out.println("POSLAO JE NA SESIJU REFRESH!");
  251. } catch (Exception e) {
  252. // TODO Auto-generated catch block
  253. e.printStackTrace();
  254. }
  255. }
  256. }
  257. }
  258. }
  259.  
  260. @GET
  261. @Path("/registerUser")
  262. public String registerUser(@QueryParam("user") String user, @QueryParam("pass") String pass) {
  263. ResteasyClient client = new ResteasyClientBuilder().build();
  264. ResteasyWebTarget target = client
  265. .target("http://192.168.0.12:8080/ClientWeb/rest/user/register?user=" + user + "&pass=" + pass);
  266. Response response = target.request().get();
  267. return response.readEntity(String.class);
  268. }
  269.  
  270. @GET
  271. @Path("/refreshUsers")
  272. public void refreshUsers() {
  273.  
  274. HashMap<String, User> users = callUser();
  275. String address = "";
  276.  
  277. try {
  278. address = Inet4Address.getLocalHost().getHostAddress();
  279. } catch (UnknownHostException e) {
  280. // TODO Auto-generated catch block
  281. e.printStackTrace();
  282. }
  283. System.out.println(users.toString());
  284. for (String u : users.keySet()) {
  285. System.out.println(users.get(u).getHost().getAddress());
  286. if(users.get(u).getHost().getAddress().equals(address)){
  287. Session ses = jmsColl.getUserSession().get(u);
  288.  
  289. try {
  290. ses.getBasicRemote().sendText("clear");
  291. System.out.println("POSLAO JE NA SESIJU REFRESH!");
  292. } catch (IOException e) {
  293. // TODO Auto-generated catch block
  294. e.printStackTrace();
  295. }
  296.  
  297.  
  298. for (String usr : users.keySet()) {
  299. try {
  300. Thread.sleep(50);
  301. ses.getBasicRemote().sendText("add," + usr);
  302. System.out.println("POSLAO JE NA SESIJU REFRESH!");
  303. } catch (Exception e) {
  304. // TODO Auto-generated catch block
  305. e.printStackTrace();
  306. }
  307. }
  308. }
  309. }
  310.  
  311. }
  312. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement