Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package session;
- import java.io.IOException;
- import java.net.Inet4Address;
- import java.net.UnknownHostException;
- import java.util.Date;
- import java.util.HashMap;
- import javax.ejb.EJB;
- import javax.ejb.LocalBean;
- import javax.ejb.Stateless;
- import javax.websocket.Session;
- import javax.ws.rs.GET;
- import javax.ws.rs.Path;
- import javax.ws.rs.Produces;
- import javax.ws.rs.QueryParam;
- import javax.ws.rs.core.GenericType;
- import javax.ws.rs.core.MediaType;
- import javax.ws.rs.core.Response;
- import org.jboss.resteasy.client.jaxrs.ResteasyClient;
- import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
- import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
- @LocalBean
- @Path("/client")
- @Stateless
- public class ClientApp implements ClientRemote {
- @EJB
- CollectionsBean collection;
- @EJB
- JMSCollection jmsColl;
- @GET
- @Path("/test")
- public String test() {
- // Host host = new Host("192.168.0.12", "master");
- // Host host = collection.getHost();
- // collection.addHost(new Host("192.168.0.12", "master"));
- // collection.addHost(new Host("192.168.0.12", "kmet1"));
- new JMSQueue("oj jmsu", "proba");
- return collection.getHost().toString();
- }
- @GET
- @Path("/register")
- @Produces(MediaType.APPLICATION_JSON)
- public HashMap<String, Host> register(@QueryParam("ip") String ip, @QueryParam("ali") String ali) {
- Host newHost = new Host(ip, ali);
- collection.addHost(newHost);
- System.out.println("==========================REGISTER========================");
- System.out.println("==========================REGISTER========================");
- System.out.println("==========================REGISTER========================");
- System.out.println("==========================REGISTER========================");
- System.out.println("==========================REGISTER========================");
- return collection.getHost();
- }
- @GET
- @Path("/addUser")
- public void addUser(@QueryParam("user") String user, @QueryParam("sessionId") String sesionId){
- Session ses = jmsColl.getSessions().get(sesionId);
- jmsColl.getUserSession().put(user, jmsColl.getSessions().get(sesionId));
- try {
- ses.getBasicRemote().sendText("true");
- System.out.println("POSLAO JE NA SESIJU REFRESH!");
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- @GET
- @Path("/registerSelf")
- public String registerSelf(/* Host host */) {
- // collection.addHost(new Host("192.168.0.12", "kmet1"));
- Host temp = new Host();
- try {
- temp = collection.getHost().get(Inet4Address.getLocalHost().getHostAddress().toString());
- } catch (UnknownHostException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- if (temp == null) {
- temp = new Host();
- }
- ResteasyClient client = new ResteasyClientBuilder().build();
- ResteasyWebTarget target = client.target("http://192.168.0.12:8080/ClientWeb/rest/client/register?ip="
- + temp.getAddress() + "&ali=" + temp.getAlias());
- Response response = target.request(MediaType.APPLICATION_JSON).get();
- collection.setHost(response.readEntity(HashMap.class));
- return collection.getHost().toString();
- }
- @GET
- @Path("/unregister")
- public String/* HashMap<String, Host> */ unregister(/* Host host */) {
- // collection.removeHost(new Host("192.168.0.12", "kmet1"));
- return collection.getHost().toString();
- }
- @GET
- @Path("/callUser")
- @Produces(MediaType.APPLICATION_JSON)
- public HashMap<String, User> callUser() {
- ResteasyClient client = new ResteasyClientBuilder().build();
- ResteasyWebTarget target = client.target("http://192.168.0.12:8080/ClientWeb/rest/user/hello");
- Response response = target.request(MediaType.APPLICATION_JSON).get();
- return response.readEntity(new GenericType<HashMap<String, User>>(){});
- }
- @GET
- @Path("/loginUser")
- public String loginUser(@QueryParam("user") String user, @QueryParam("pass") String pass,
- @QueryParam("sessionId") String sesionId, @QueryParam("address") String address) {
- String myAddress = "";
- try {
- myAddress = Inet4Address.getLocalHost().getHostAddress().toString();
- } catch (UnknownHostException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- ResteasyClient client = new ResteasyClientBuilder().build();
- ResteasyWebTarget target = client.target("http://192.168.0.12:8080/ClientWeb/rest/user/login?user=" + user
- + "&pass=" + pass + "&host=" + address + "&sessionId=" + sesionId);
- Response response = target.request(MediaType.APPLICATION_JSON).get();
- refreshUsers();
- for (String h : collection.getHost().keySet()) {
- if(!h.equals(myAddress)){
- client = new ResteasyClientBuilder().build();
- target = client.target("http://"+h+":8080/ClientWeb/rest/client/refreshUsers");
- response = target.request(MediaType.APPLICATION_JSON).get();
- }
- }
- return "";
- }
- @GET
- @Path("/logoutUser")
- public void loginUser(@QueryParam("session") String sesId) {
- String user = "";
- for (String u : jmsColl.getUserSession().keySet()) {
- if (jmsColl.getUserSession().get(u).getId().equals(sesId)) {
- user = u;
- break;
- }
- }
- ResteasyClient client = new ResteasyClientBuilder().build();
- ResteasyWebTarget target = client.target("http://192.168.0.12:8080/ClientWeb/rest/user/logout?user=" + user);
- Response response = target.request().get();
- String myAddress = "";
- try {
- myAddress = Inet4Address.getLocalHost().getHostAddress().toString();
- } catch (UnknownHostException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- refreshUsers();
- for (String h : collection.getHost().keySet()) {
- if(!h.equals(myAddress)){
- client = new ResteasyClientBuilder().build();
- target = client.target("http://"+h+":8080/ClientWeb/rest/client/refreshUsers");
- response = target.request(MediaType.APPLICATION_JSON).get();
- }
- }
- }
- @GET
- @Path("/removeUser")
- public void removeUser(@QueryParam("user")String user){
- jmsColl.getUserSession().remove(user);
- }
- @GET
- @Path("/sendMsg")
- public void sendMsg(@QueryParam("msg") String msg, @QueryParam("socketId") String socketId) {
- String myAddress = "";
- try {
- myAddress = Inet4Address.getLocalHost().getHostAddress().toString();
- } catch (UnknownHostException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- String f = "";
- for (String u : jmsColl.getUserSession().keySet()) {
- if (jmsColl.getUserSession().get(u).getId().equals(socketId)) {
- f = u;
- break;
- }
- }
- postMsg(f, msg);
- for (String h : collection.getHost().keySet()) {
- if(!h.equals(myAddress)){
- ResteasyClient client = new ResteasyClientBuilder().build();
- ResteasyWebTarget target = client.target("http://"+h+":8080/ClientWeb/rest/client/postMsg?user="+f+"&msg="+msg);
- Response response = target.request().get();
- }
- }
- // ResteasyClient client = new ResteasyClientBuilder().build();
- // ResteasyWebTarget target = client.target("http://192.168.0.12:8080/ClientWeb/rest/user/logout?user=" + user);
- // Response response = target.request().get();
- }
- @GET
- @Path("/postMsg")
- public void postMsg(@QueryParam("user") String user, @QueryParam("msg") String msg){
- String[] splitted = msg.split(",");
- System.out.println(msg);
- if(splitted[2].equals("public")){
- for (Session ses : jmsColl.getUserSession().values()) {
- User from = callUser().get(user);
- Message mes = new Message(from, null, new Date(), "", splitted[1]);
- try {
- Thread.sleep(50);
- ses.getBasicRemote().sendText("text," + mes.toString());
- System.out.println("POSLAO JE NA SESIJU REFRESH!");
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }else
- {
- User from = callUser().get(user);
- Message mes = new Message(from, callUser().get(splitted[2]), new Date(), "", splitted[1]);
- for (String u : callUser().keySet()) {
- if (u.equals(splitted[2]) || u.equals(from.getUsername())) {
- Session ses = jmsColl.getUserSession().get(u);
- try {
- Thread.sleep(50);
- ses.getBasicRemote().sendText("text," + mes.toString());
- System.out.println("POSLAO JE NA SESIJU REFRESH!");
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
- }
- @GET
- @Path("/registerUser")
- public String registerUser(@QueryParam("user") String user, @QueryParam("pass") String pass) {
- ResteasyClient client = new ResteasyClientBuilder().build();
- ResteasyWebTarget target = client
- .target("http://192.168.0.12:8080/ClientWeb/rest/user/register?user=" + user + "&pass=" + pass);
- Response response = target.request().get();
- return response.readEntity(String.class);
- }
- @GET
- @Path("/refreshUsers")
- public void refreshUsers() {
- HashMap<String, User> users = callUser();
- String address = "";
- try {
- address = Inet4Address.getLocalHost().getHostAddress();
- } catch (UnknownHostException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- System.out.println(users.toString());
- for (String u : users.keySet()) {
- System.out.println(users.get(u).getHost().getAddress());
- if(users.get(u).getHost().getAddress().equals(address)){
- Session ses = jmsColl.getUserSession().get(u);
- try {
- ses.getBasicRemote().sendText("clear");
- System.out.println("POSLAO JE NA SESIJU REFRESH!");
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- for (String usr : users.keySet()) {
- try {
- Thread.sleep(50);
- ses.getBasicRemote().sendText("add," + usr);
- System.out.println("POSLAO JE NA SESIJU REFRESH!");
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement