Advertisement
Guest User

Untitled

a guest
Feb 3rd, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.05 KB | None | 0 0
  1. package main.java.pl.mn.model;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.io.PrintWriter;
  7. import java.net.Socket;
  8.  
  9. /**
  10.  * Created by mn on 02.02.16.
  11.  * Klasa odpowiedzialna za obsługę protokołu komunikacji
  12.  */
  13. public class CommunicationProtocol {
  14.     private Socket command;
  15.     private Socket file;
  16.     private boolean logged;
  17.     private BufferedReader in;
  18.     private PrintWriter out;
  19.     private SQLClientHandler dbCom;
  20.  
  21.     public CommunicationProtocol(Socket command, Socket file){
  22.         this.command=command;
  23.         this.file=file;
  24.         logged=false;
  25.         dbCom = new SQLClientHandler();
  26.         try {
  27.             in = new BufferedReader(new InputStreamReader(command.getInputStream()));
  28.             out = new PrintWriter(command.getOutputStream(), true);
  29.         } catch (IOException e) {
  30.             e.printStackTrace();
  31.         }
  32.     }
  33.  
  34.     /**
  35.      * Metoda odpowiedzialna za logowanie użytkownika po połączeniu
  36.      */
  37.     public void login() {
  38.         String[] com = readCommand();
  39.         while(!logged){
  40.             if(com[0]=="USER"){
  41.                 out.println("331 Password required");
  42.                 String username = com[1];
  43.                 com = readCommand();
  44.                 if(com[0]=="PASS"){
  45.                     String password = com[1];
  46.                     if(dbCom.verifyPassword(username, password)) {
  47.                         out.println("230 User logged in");
  48.                         logged = true;
  49.                     } else {
  50.                         out.println("231 Wrong password or username");
  51.                     }
  52.                 } else {
  53.                     out.println("503 Bad sequence of commands");
  54.                 }
  55.             } else {
  56.                 out.println("503 Bad sequence of commands");
  57.             }
  58.         }
  59.     }
  60.  
  61.     private String[] readCommand(){
  62.         String[] result = new String[2];
  63.         try {
  64.             String inputLine;
  65.             if((inputLine = in.readLine())!=null){
  66.                 result = inputLine.split(" ");
  67.             }
  68.         } catch (IOException e) {
  69.             e.printStackTrace();
  70.         }
  71.         return result;
  72.     }
  73.  
  74.     /**
  75.      * Metoda odpowiedzialna za obsługę poleceń oprócz logowania
  76.      */
  77.     public boolean handleCommands() {
  78.         String[] com = readCommand();
  79.         if(logged){
  80.             switch(com[0]){
  81.                 case "QUIT":
  82.                     logged = false;
  83.                     out.println("Bye");
  84.                 case "NOOP":
  85.                     out.println("200 Command succesfull");
  86.                 case "PASV":
  87.                    
  88.                 case "STOR":
  89.                 case "RETR":
  90.                 case "APPE":
  91.                 case "ABOR":
  92.                 case "DELE":
  93.                 case "RMD":
  94.                 case "MKD":
  95.                 case "PWD":
  96.                 case "LIST":
  97.                 case "CWD":
  98.                 case "CHMOD":
  99.  
  100.             }
  101.         }
  102.         return logged;
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement