Advertisement
Guest User

PSI tcp

a guest
Nov 11th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.10 KB | None | 0 0
  1. package robot;
  2.  
  3. import java.net.*;
  4. import java.io.*;
  5. import java.util.Date;
  6. import java.util.logging.Level;
  7. import java.util.logging.Logger;
  8.  
  9. class Client implements Runnable{
  10.    
  11.     PrintWriter out = null;
  12.     BufferedReader in = null;
  13.     Socket clientSocket = null;
  14.     Boolean running=true;
  15.     private String state = "start";
  16.     private String username="";
  17.     private long startTime;
  18.    
  19.     public Client( Socket clientSocket) throws IOException {    
  20.         this.clientSocket=clientSocket;
  21.         startTime = System.currentTimeMillis();
  22.     }
  23.  
  24.     @Override
  25.     public void run() {
  26.         try {
  27.             out = new PrintWriter(clientSocket.getOutputStream(), true);
  28.             in = new BufferedReader(
  29.                 new InputStreamReader(
  30.                 clientSocket.getInputStream()));
  31.         } catch (IOException e) {
  32.             System.err.println("Couldn't get I/O.");
  33.             System.exit(1);      
  34.         }
  35.         String outputLine="";
  36.         while (running) {
  37.             try {
  38.                 outputLine=getOutput(in);
  39.             } catch (IOException ex) {
  40.                 System.err.println("Couldn't get I/O.");
  41.             }
  42.             if(outputLine.equals("break")){
  43.                 stop();
  44.             }else{
  45.                 if((new Date()).getTime() - startTime>=45*1000){
  46.                     outputLine="502 TIMEOUT";
  47.                     stop();
  48.                 }
  49.                 out.print(outputLine + System.getProperty("line.separator"));
  50.                 out.flush();
  51.             }
  52.         }
  53.     System.out.println("thread ending");
  54.         out.close();
  55.         try {
  56.             in.close();
  57.             clientSocket.close();
  58.         } catch (IOException ex) {
  59.             Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
  60.         }
  61.        
  62.     }  
  63.    
  64.     public String getInput(BufferedReader in) throws IOException{
  65.         String inputLine="";
  66.         char ch;
  67.         while (!inputLine.contains("\r\n")){
  68.             ch = (char)in.read();
  69.             if(ch != -1 ){
  70.                 inputLine+=ch;
  71.             } else{
  72.                 inputLine="break";
  73.                 break;
  74.             }          
  75.         }
  76.         return inputLine.substring(0, inputLine.length()-2);
  77.     }
  78.    
  79.     public String getOk(BufferedReader in) throws IOException{
  80.         String inputLine="",outputLine="";
  81.         char ch;
  82.         for(int i=0;i<5;i++){
  83.             if((ch = (char)in.read())!=-1){
  84.                 inputLine+=ch;
  85.             } else{
  86.                 inputLine="break";
  87.                 break;
  88.             }                        
  89.         }        
  90.         if(inputLine.equals("FOTO ")){
  91.             ch =(char)in.read();
  92.             String numbers="";
  93.             while(ch!=' '){
  94.                 numbers+=ch;
  95.                 ch = (char)in.read();
  96.             }
  97.             int number=Integer.parseInt(numbers);
  98.             String foto=null;
  99.             for(int i=0;i<number;i++){
  100.                 ch = (char)in.read();
  101.                 foto+=ch;
  102.             }
  103.             String control = "";
  104.             int num;
  105.             for(int i=0;i<8;i++){
  106.                 ch = (char)in.read();
  107.                 num = (int)ch;
  108.                 control+=num;
  109.             }
  110.            
  111.             outputLine=processFoto(foto,control);
  112.         } else if(inputLine.equals("INFO ")) {
  113.             while (!inputLine.contains("\r\n")){
  114.                 ch = (char)in.read();
  115.                 if(ch != -1 ){
  116.                     inputLine+=ch;
  117.                 } else{
  118.                     inputLine="break";
  119.                     break;
  120.                 }          
  121.             }
  122.             outputLine=processInfo(inputLine);
  123.         } else{
  124.             outputLine="501 SYNTAX ERROR";
  125.             stop();
  126.         }
  127.         return outputLine;    
  128.     }
  129.    
  130.     public String getOutput(BufferedReader in) throws IOException{
  131.         String input,output="";
  132.         switch(state){
  133.             case "start" :  
  134.                             output="200 LOGIN";
  135.                             state="username";
  136.                             break;
  137.             case "username" : if((input = getInput(in))=="break"){
  138.                                 output=input;
  139.                               } else{
  140.                                 output="201 PASSWORD";
  141.                               }
  142.                               state="password";
  143.                               username=input;
  144.                               break;
  145.             case "password" : if((input = getInput(in))=="break"){
  146.                                 output=input;
  147.                                } else{
  148.                                 output = getPassword(input);
  149.                                }                                                          
  150.                                state="ok";
  151.                                break;
  152.             case "ok" : output=getOk(in);
  153.         }
  154.         return output;
  155.     }
  156.    
  157.     public String getPassword(String input){
  158.         int pass=0;
  159.         String output;
  160.         for(char ch : username.toCharArray()){
  161.             pass+=(int) ch;
  162.         }
  163.         if(pass==Integer.parseInt(input)){
  164.             output="202 OK";
  165.         } else{
  166.             output="500 LOGIN FAILED";
  167.             stop();
  168.         }
  169.         return output;
  170.     }
  171.    
  172.     public String processInfo(String input){
  173.         //ulozit do logu      
  174.         return "202 OK";
  175.     }
  176.    
  177.     public String processFoto(String input,String control){
  178.         int number=0;
  179.         for(char ch:input.toCharArray()){
  180.             number+=(int)ch;
  181.         }
  182.         String outputLine="";
  183.         if(number==Integer.parseInt(control)){
  184.             outputLine="202 OK";
  185.             //ulozeni
  186.         } else{
  187.             outputLine="300 BAD CHECKSUM";
  188.         }
  189.         return outputLine;
  190.     }
  191.    
  192.     private void stop(){
  193.         running=false;
  194.     }
  195.  
  196. }
  197.  
  198.  
  199. public class Robot {  
  200.     public static void main(String[] args) throws IOException {
  201.     if (args.length == 0) {
  202.             System.err.println("Server: java robot.Robot <port>");
  203.             System.exit(1);
  204.     } else {
  205.             System.out.println("Starting server...\n");
  206.                 Server server=new Server(Integer.parseInt( args[0]));
  207.     }
  208.     }
  209.    
  210. }
  211.  
  212. class Server{
  213.  
  214.     private ServerSocket serverSocket = null;
  215.     private int port = 3645;
  216.     boolean running=true;
  217.    
  218.     public Server(int port) throws IOException {
  219.         try {
  220.             serverSocket = new ServerSocket( port);
  221.         } catch (IOException e) {
  222.             System.err.println("Could not listen on port: " + port);
  223.             System.exit(1);
  224.         }    
  225.         while(running){
  226.             Thread thr;
  227.             try {
  228.                 Socket clientSocket = serverSocket.accept();
  229.                 Client client = new Client(clientSocket);
  230.                 thr = new Thread(client);
  231.                 thr.start();
  232.             } catch (IOException e) {
  233.                 System.err.println("Accept failed.");
  234.                 System.exit(1);
  235.             }
  236.         }
  237.         serverSocket.close();
  238.     }
  239.        
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement