Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main.java.pl.mn.model;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.Socket;
- /**
- * Created by mn on 02.02.16.
- * Klasa odpowiedzialna za obsługę protokołu komunikacji
- */
- public class CommunicationProtocol {
- private Socket command;
- private Socket file;
- private boolean logged;
- private BufferedReader in;
- private PrintWriter out;
- private SQLClientHandler dbCom;
- public CommunicationProtocol(Socket command, Socket file){
- this.command=command;
- this.file=file;
- logged=false;
- dbCom = new SQLClientHandler();
- try {
- in = new BufferedReader(new InputStreamReader(command.getInputStream()));
- out = new PrintWriter(command.getOutputStream(), true);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /**
- * Metoda odpowiedzialna za logowanie użytkownika po połączeniu
- */
- public void login() {
- String[] com = readCommand();
- while(!logged){
- if(com[0]=="USER"){
- out.println("331 Password required");
- String username = com[1];
- com = readCommand();
- if(com[0]=="PASS"){
- String password = com[1];
- if(dbCom.verifyPassword(username, password)) {
- out.println("230 User logged in");
- logged = true;
- } else {
- out.println("231 Wrong password or username");
- }
- } else {
- out.println("503 Bad sequence of commands");
- }
- } else {
- out.println("503 Bad sequence of commands");
- }
- }
- }
- private String[] readCommand(){
- String[] result = new String[2];
- try {
- String inputLine;
- if((inputLine = in.readLine())!=null){
- result = inputLine.split(" ");
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- return result;
- }
- /**
- * Metoda odpowiedzialna za obsługę poleceń oprócz logowania
- */
- public boolean handleCommands() {
- String[] com = readCommand();
- if(logged){
- switch(com[0]){
- case "QUIT":
- logged = false;
- out.println("Bye");
- case "NOOP":
- out.println("200 Command succesfull");
- case "PASV":
- case "STOR":
- case "RETR":
- case "APPE":
- case "ABOR":
- case "DELE":
- case "RMD":
- case "MKD":
- case "PWD":
- case "LIST":
- case "CWD":
- case "CHMOD":
- }
- }
- return logged;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement