Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.Socket;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Map;
- public class ServerThread extends Thread {
- private Socket socket;
- private Map<String, String> accountInfo;
- SimpleProtocol p;
- public ServerThread(Socket socket) {
- this.socket = socket;
- p = new SimpleProtocol();
- accountInfo = new HashMap<String, String>();
- }
- @Override
- public void run() {
- try {
- System.out.println("processing request");
- BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- DataOutputStream out = new DataOutputStream(socket.getOutputStream());
- out.writeBytes(p.createMessage("sign-up", "true", "welcome to server") + "\n");
- System.out.println("butts");
- while (true) {
- String instring = in.readLine();
- if (instring != null && getType(instring).equals("sign-in")) {
- // while((instring=in.readLine())!= null){
- // System.out.print(instring);
- whichType(instring);
- out.writeBytes(p.createMessage(getType(instring), signIn(instring), "please") + "\n");
- //break;
- // socket.close();
- } else {// wasnt in older version
- if (instring != null && getType(instring).equals("sign-up")) {
- // System.out.print(instring);
- whichType(instring);
- out.writeBytes(p.createMessage(getType(instring), signUp(instring), "Wut") + "\n");
- //break;
- //socket.close();
- }
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public void whichType(String message) {
- String parts[] = p.decodeMessage(message);
- String type = parts[0];
- if (type.equals("sign-up")) {
- signUp(message);
- } else {
- if (type.equals("sign-in")) {
- signIn(message);
- } else {
- if (type.equals("get_message")) {
- getMessage(message);
- } else {
- if (type.equals("send-message")) {
- sendMessage();
- }
- }
- }
- }
- }
- private String signUp(String info) {
- String parts[] = p.decodeMessage(info);
- String username = parts[1];
- String password = parts[2];
- if (!getAccount().containsKey(username) && username.length() >= 5 && username.length() <= 20
- && password.length() >= 5 && password.length() <= 32) {
- accountInfo.put(username, password);
- return "true";
- // }
- // try {
- // //socket.close();
- // } catch (IOException e) {
- // // TODO Auto-generated catch block
- // e.printStackTrace();
- }else{
- if(getAccount().containsKey(username)){
- Iterator<String> keySetIterator = getAccount().keySet().iterator();
- while(keySetIterator.hasNext()){
- String key = keySetIterator.next();
- System.out.println("key: " + key + " value: " + accountInfo.get(key));
- }
- return "false";
- }
- }// create restrictions for what is acceptable signup info
- return username;
- }
- private String signIn(String info) {// was boolean
- // create restrictions or checks for existing account
- String parts[] = p.decodeMessage(info);
- String username = parts[1];
- String password = parts[2];
- if (getAccount().containsKey(username) && getAccount().containsValue(password)) {
- return "true";
- } else {
- System.out.println("Incorrect Username and/or Password");
- // try {
- //
- // socket.close();
- // } catch (IOException e) {
- // // TODO Auto-generated catch block
- // e.printStackTrace();
- // }
- }
- return "false";
- }
- private void getMessage(String info) {
- String parts[] = p.decodeMessage(info);
- String type = parts[0];
- String offset = parts[1];
- String username = parts[2];
- String timeStamp = parts[3];
- String message = parts[4];
- }
- private void sendMessage() {
- }
- public String getType(String input) {
- String parts[] = p.decodeMessage(input);
- return parts[0];
- }
- public Map<String, String> getAccount()
- {
- return this.accountInfo;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement