Advertisement
Wolan1995

SAM | SessionHandler (09.06.2017)

Jun 9th, 2017
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.14 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.util.HashMap;
  3. import java.util.HashSet;
  4. import java.util.Map;
  5. import java.util.Set;
  6. import java.util.logging.Level;
  7. import java.util.logging.Logger;
  8. import javax.websocket.EncodeException;
  9. import javax.websocket.Session;
  10.  
  11. import org.json.simple.JSONObject;
  12. import org.json.simple.parser.JSONParser;
  13. import org.json.simple.parser.ParseException;
  14.  
  15. /*
  16.  * To change this license header, choose License Headers in Project Properties.
  17.  * To change this template file, choose Tools | Templates
  18.  * and open the template in the editor.
  19.  */
  20.  
  21. /**
  22.  *
  23.  * @author student_10
  24.  */
  25. public class SessionHandler {
  26.    
  27.     static int i = 0;
  28.     static int licznik = 0;
  29.     static int licznikSES =0;
  30.     static String [] sesje = new String[4];
  31.     static int[] wartosci = new int[2];
  32.     static int losowa = 1 + (int)(Math.random() * 10);
  33.     static int losowa2 = 1 + (int)(Math.random() * 10);
  34.     int  x=0;
  35.  
  36.     static String losowaSTR;
  37.     static String losowaSTR2;
  38.    
  39.     private static final Set<Session> sessions = new HashSet<>();
  40.     static Map<String, Session> openSessions = new HashMap();
  41.     static Map<String, Session> klienci = new HashMap();
  42.  
  43.     static JSONObject jsonObject = new JSONObject();
  44.     static JSONObject jsonObject2 = new JSONObject();    
  45.  
  46.  
  47.    
  48.     public static void addSession(Session session){
  49.        
  50.         sesje[licznikSES] = session.getId();
  51.                         licznikSES++;
  52.         sessions.add(session);
  53.     }
  54.    
  55.     public static void removeSession(Session session){
  56.         sessions.remove(session);
  57.     }
  58.    
  59.     public static void sendToSession(Session session, String message){
  60.  
  61.        
  62.         String janek = "janusz";
  63.        
  64.  
  65.         i++;
  66.         losowaSTR = String.valueOf(losowa);
  67.         losowaSTR2 = String.valueOf(losowa2);
  68.        
  69.         //System.out.println(licznikSES);
  70.  
  71.        
  72.        
  73.        try {
  74.  
  75.            if(licznikSES == 4 ) {
  76.                                                JSONParser parser = new JSONParser();
  77.  
  78.                try {
  79.                     Object obj = parser.parse(message);
  80.                     JSONObject jsonObj = (JSONObject) obj;
  81.                    
  82.                    
  83.                     if(jsonObj.get("Info").equals("gotowy")) {
  84.                         JSONObject wysylkaJson = new JSONObject();
  85.                         for (Session s : sessions) {
  86.                             if (s.isOpen()&& s.getId().equals(sesje[0])) {
  87.                                 wysylkaJson.put("Info", "start");
  88.                                 wysylkaJson.put("Gracz", "0");
  89.                                 s.getBasicRemote().sendObject(wysylkaJson);
  90.                                
  91.                                 //s.getBasicRemote().sendObject(0);
  92.                                 //s.getBasicRemote().sendObject("start");
  93.  
  94.                             }
  95.                             if (s.isOpen()&& s.getId().equals(sesje[1])) {
  96.                                 wysylkaJson.put("Info", "start");
  97.                                 wysylkaJson.put("Gracz", "1");
  98.                                 s.getBasicRemote().sendObject(wysylkaJson);
  99.                                 //s.getBasicRemote().sendObject(1);
  100.                                 //s.getBasicRemote().sendObject("start");
  101.                             }
  102.                             if (s.isOpen()&& s.getId().equals(sesje[2])) {
  103.                                 wysylkaJson.put("Info", "start");
  104.                                 wysylkaJson.put("Gracz", "2");
  105.                                 s.getBasicRemote().sendObject(wysylkaJson);
  106.                                 //s.getBasicRemote().sendObject(2);
  107.                                 //s.getBasicRemote().sendObject("start");
  108.                             }
  109.                             if (s.isOpen()&& s.getId().equals(sesje[3])) {
  110.                                 wysylkaJson.put("Info", "start");
  111.                                 wysylkaJson.put("Gracz", "3");
  112.                                 s.getBasicRemote().sendObject(wysylkaJson);
  113.                                 //s.getBasicRemote().sendObject(3);
  114.                                 //s.getBasicRemote().sendObject("start");
  115.                             }
  116.                         }
  117.                     }
  118.                     else if (jsonObj.get("Info").equals("ruch")) {
  119.                         for (Session s : sessions) {                      
  120.                             if (s.isOpen()) {
  121.                                     if(jsonObj.get("Gracz").equals("0")) {
  122.                                         s.getBasicRemote().sendObject(message);
  123.                                     }
  124.                                     if(jsonObj.get("Gracz").equals("1")) {
  125.                                         s.getBasicRemote().sendObject(message);
  126.  
  127.                                     }
  128.                                     if(jsonObj.get("Gracz").equals("2")) {
  129.                                         s.getBasicRemote().sendObject(message);
  130.  
  131.                                     }
  132.                                     if(jsonObj.get("Gracz").equals("3")) {
  133.                                         s.getBasicRemote().sendObject(message);
  134.                                     }
  135.                             }
  136.                         }
  137.                    
  138.                     } else if (jsonObj.get("Info").equals("strzal")) {
  139.                    
  140.                     }
  141.                     else if (jsonObj.get("Info").equals("koniec")) {
  142.                    
  143.                     }
  144.  
  145.                    
  146.                } catch (ParseException ex) {
  147.                     Logger.getLogger(SessionHandler.class.getName()).log(Level.SEVERE, null, ex);
  148.                }
  149.                
  150.                    /* if (message.equals("halo")){
  151.                         for (Session s : sessions) {
  152.                             if (s.isOpen()&& s.getId().equals(sesje[0])) {
  153.                                 s.getBasicRemote().sendObject(0);
  154.                                 s.getBasicRemote().sendObject("start");
  155.  
  156.                             }
  157.                             if (s.isOpen()&& s.getId().equals(sesje[1])) {
  158.                                 s.getBasicRemote().sendObject(1);
  159.                                 s.getBasicRemote().sendObject("start");
  160.                             }
  161.                             if (s.isOpen()&& s.getId().equals(sesje[2])) {
  162.                                 s.getBasicRemote().sendObject(2);
  163.                                 s.getBasicRemote().sendObject("start");
  164.                             }
  165.                             if (s.isOpen()&& s.getId().equals(sesje[3])) {
  166.                                 s.getBasicRemote().sendObject(3);
  167.                                 s.getBasicRemote().sendObject("start");
  168.                             }
  169.                            
  170.                         }      
  171.                     }
  172.                     else {
  173.  
  174.                         for (Session s : sessions) {
  175.                             if (s.isOpen()) {
  176.                                 System.out.println(message);
  177.                                 //JSONParser parser = new JSONParser();
  178.                                 try {
  179.                                     Object obj = parser.parse(message);
  180.                                     JSONObject jsonObj = (JSONObject) obj;
  181.                                     if(jsonObj.get("ID").equals("0")) {
  182.                                         s.getBasicRemote().sendObject(message);
  183.                                     }
  184.                                     if(jsonObj.get("ID").equals("1")) {
  185.                                         s.getBasicRemote().sendObject(message);
  186.  
  187.                                     }
  188.                                     if(jsonObj.get("ID").equals("2")) {
  189.                                         s.getBasicRemote().sendObject(message);
  190.  
  191.                                     }
  192.                                     if(jsonObj.get("ID").equals("3")) {
  193.                                         s.getBasicRemote().sendObject(message);
  194.  
  195.                                     }
  196.                                 } catch (ParseException ex) {
  197.                                    // Logger.getLogger(SessionHandler.class.getName()).log(Level.SEVERE, null, ex);
  198.                                 }
  199.                             }
  200.                         }
  201.                        
  202.                        
  203.                     }*/
  204.  
  205.  
  206.            }
  207.         } catch (IOException ex) {
  208.             sessions.remove(session);
  209.             Logger.getLogger(SessionHandler.class.getName()).log(Level.SEVERE, null, ex);
  210.         } catch (EncodeException ex) {
  211.             Logger.getLogger(SessionHandler.class.getName()).log(Level.SEVERE, null, ex);
  212.         }
  213.     }
  214.    
  215.     public static void sendToallConnectedSessions(String message){
  216.          for (Session session : sessions) {
  217.             sendToSession(session, message);
  218.         }
  219.     }
  220.    
  221.     public static void sendToallConnectedSessionsInRoom(String roomID, String message){
  222.          for (Session session : sessions) {
  223.             sendToSession(session, message);
  224.         }
  225.     }
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement