Advertisement
Guest User

Untitled

a guest
Mar 12th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.89 KB | None | 0 0
  1. package Protocol;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.DataOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.net.Socket;
  8. import java.util.ArrayList;
  9. import java.util.HashMap;
  10.  
  11. public class SocketThread extends Thread{
  12.  
  13.     private Socket socket;
  14.     private SimpleProtocol simpleprotocol;
  15.     private HashMap<String,String> userDetails;
  16.     private ArrayList <String> messages;
  17.  
  18.     public SocketThread(Socket socket) {
  19.         this.socket = socket;
  20.         simpleprotocol = new SimpleProtocol();
  21.         userDetails = new HashMap<String,String>();
  22.     }
  23.    
  24.     public void run(){
  25.         String inputLine;
  26.        
  27.         try
  28.         {
  29.             //input stream reader
  30.             //get socket writing and reading streams
  31.             BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  32.             DataOutputStream out = new DataOutputStream(socket.getOutputStream());
  33.            
  34.             out.writeBytes(simpleprotocol.createMessage("", "Welcome to my Server") + "\n");
  35.  
  36.             //Now start reading input from client
  37.             while((inputLine = in.readLine()) != null) {
  38.                 //reply with the same message, adding some text
  39.                 chooseFunction(inputLine);
  40.                     if(getFunction(inputLine).equals("sign-up")){
  41.                         out.writeBytes(simpleprotocol.createMessage(getFunction(inputLine), userSignUp(inputLine),
  42.                                 "User successfully signed up") + "\n");
  43.                     }else if(getFunction(inputLine).equals("sign-in")){
  44.                         out.writeBytes(simpleprotocol.createMessage(getFunction(inputLine), userSignIn(inputLine),
  45.                                 "User successfully signed in") + "\n");{
  46.                        
  47.                     }
  48.                     }
  49.                
  50.             }
  51.              
  52.             //client disconnected, so close socket
  53. //            socket.close();
  54.         }
  55.        
  56.         catch (IOException e)
  57.         {
  58.             System.out.println("IOException on socket : " + e);
  59.             e.printStackTrace();
  60.         }
  61.     }
  62.  
  63.    
  64.     public void chooseFunction(String message){
  65.         String[] received = simpleprotocol.decodeMessage(message);
  66.         String function = received[0];
  67.        
  68.         if(function.equals("sign-up")){
  69.             userSignUp(message);
  70.         }else if(function.equals("sign-in")){
  71.             userSignIn(message);
  72.         }else if(function.equals("get-message")){
  73.             getMessage(message);
  74.         }else if(function.equals("send-message")){
  75.             sendMessage(message);
  76.         }
  77.     }
  78.    
  79.    
  80.     public String userSignUp(String credentials){
  81.         String[] received = simpleprotocol.decodeMessage(credentials);
  82.         String username = received[1];
  83.         String password = received[2];
  84.        
  85.        
  86.             if(!(userDetails.containsKey(username))){
  87.                 if(username.length() >=5 && username.length() <=20){
  88.                     if(password.length() >= 8 && password.length() <=32){  
  89.                         userDetails.put(username, password);
  90.                     }
  91.                 }
  92.             }
  93.        
  94.         return simpleprotocol.createMessage("sign-up", "true","User successfully signed up");
  95.  
  96. //          try{
  97. //              socket.close();
  98. //          }catch (IOException e){
  99. //              e.printStackTrace();
  100. //          }
  101.     }
  102.    
  103.     public String userSignIn(String credentials){
  104.         String[] received = simpleprotocol.decodeMessage(credentials);
  105.         String username = received[1];
  106.         String password = received[2];
  107.        
  108.         if(userDetails.containsKey(username) && userDetails.get(username).equals(password)){
  109.             return "true";
  110.         }else
  111. //          try{
  112. //              socket.close();
  113. //          }catch(IOException e){
  114. //              e.printStackTrace();
  115. //          }
  116.         return "false";
  117.     }
  118.    
  119.    
  120.    
  121.     public void getMessage(String message){
  122.         String[] received = simpleprotocol.decodeMessage(message);
  123.         String offset = received[1];
  124. //     
  125. //      if(offset.equals("-1")){
  126. //          for(String m: messages){
  127. //              m.get
  128. //          }
  129. //      } else()
  130.     }
  131.    
  132.     public void sendMessage(String message){
  133.        
  134.     }
  135.    
  136.     public String getFunction(String input) {
  137.         String parts[] = simpleprotocol.decodeMessage(input);
  138.  
  139.         return parts[0];
  140.     }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement