Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Protocol;
- import java.io.BufferedReader;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.Socket;
- import java.util.ArrayList;
- import java.util.HashMap;
- public class SocketThread extends Thread{
- private Socket socket;
- private SimpleProtocol simpleprotocol;
- private HashMap<String,String> userDetails;
- private ArrayList <String> messages;
- public SocketThread(Socket socket) {
- this.socket = socket;
- simpleprotocol = new SimpleProtocol();
- userDetails = new HashMap<String,String>();
- }
- public void run(){
- String inputLine;
- try
- {
- //input stream reader
- //get socket writing and reading streams
- BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- DataOutputStream out = new DataOutputStream(socket.getOutputStream());
- out.writeBytes(simpleprotocol.createMessage("", "Welcome to my Server") + "\n");
- //Now start reading input from client
- while((inputLine = in.readLine()) != null) {
- //reply with the same message, adding some text
- chooseFunction(inputLine);
- if(getFunction(inputLine).equals("sign-up")){
- out.writeBytes(simpleprotocol.createMessage(getFunction(inputLine), userSignUp(inputLine),
- "User successfully signed up") + "\n");
- }else if(getFunction(inputLine).equals("sign-in")){
- out.writeBytes(simpleprotocol.createMessage(getFunction(inputLine), userSignIn(inputLine),
- "User successfully signed in") + "\n");{
- }
- }
- }
- //client disconnected, so close socket
- // socket.close();
- }
- catch (IOException e)
- {
- System.out.println("IOException on socket : " + e);
- e.printStackTrace();
- }
- }
- public void chooseFunction(String message){
- String[] received = simpleprotocol.decodeMessage(message);
- String function = received[0];
- if(function.equals("sign-up")){
- userSignUp(message);
- }else if(function.equals("sign-in")){
- userSignIn(message);
- }else if(function.equals("get-message")){
- getMessage(message);
- }else if(function.equals("send-message")){
- sendMessage(message);
- }
- }
- public String userSignUp(String credentials){
- String[] received = simpleprotocol.decodeMessage(credentials);
- String username = received[1];
- String password = received[2];
- if(!(userDetails.containsKey(username))){
- if(username.length() >=5 && username.length() <=20){
- if(password.length() >= 8 && password.length() <=32){
- userDetails.put(username, password);
- }
- }
- }
- return simpleprotocol.createMessage("sign-up", "true","User successfully signed up");
- // try{
- // socket.close();
- // }catch (IOException e){
- // e.printStackTrace();
- // }
- }
- public String userSignIn(String credentials){
- String[] received = simpleprotocol.decodeMessage(credentials);
- String username = received[1];
- String password = received[2];
- if(userDetails.containsKey(username) && userDetails.get(username).equals(password)){
- return "true";
- }else
- // try{
- // socket.close();
- // }catch(IOException e){
- // e.printStackTrace();
- // }
- return "false";
- }
- public void getMessage(String message){
- String[] received = simpleprotocol.decodeMessage(message);
- String offset = received[1];
- //
- // if(offset.equals("-1")){
- // for(String m: messages){
- // m.get
- // }
- // } else()
- }
- public void sendMessage(String message){
- }
- public String getFunction(String input) {
- String parts[] = simpleprotocol.decodeMessage(input);
- return parts[0];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement