daily pastebin goal
49%
SHARE
TWEET

server

a guest Dec 15th, 2018 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package jur.pawel.server;
  2.  
  3. import jur.pawel.model.IModel;
  4. import jur.pawel.model.Model;
  5. import jur.pawel.model.generation.WrongParametersException;
  6.  
  7. import java.io.*;
  8. import java.net.*;
  9. import java.util.Arrays;
  10. import java.util.Properties;
  11.  
  12. public class Server {
  13.  
  14.     private IModel model = new Model();
  15.     private Properties properties = new Properties();
  16.     private ServerSocket server;
  17.     private Socket service;
  18.  
  19.     public Server() {
  20.         try (FileInputStream in = new FileInputStream(".properties")) {
  21.             properties.load(in);
  22.             server = new ServerSocket(Integer.parseInt(properties.getProperty("port")));
  23.         } catch (IOException e) {
  24.             e.printStackTrace();
  25.         }
  26.     }
  27.  
  28.     public void run() {
  29.         while (true) {
  30.             try {
  31.                 service = server.accept();
  32.                 runService(service);
  33.             } catch (IOException e) {
  34.                 e.printStackTrace();
  35.             }
  36.         }
  37.     }
  38.  
  39.     private void runService(Socket service) throws IOException {
  40.         BufferedReader input = new BufferedReader(new InputStreamReader(service.getInputStream()));
  41.         PrintWriter output = new PrintWriter(service.getOutputStream(), true);
  42.  
  43.         String receivedString;
  44.         while(true) {
  45.             receivedString = input.readLine();
  46.             if(receivedString != null) {
  47.                 String[] splitReceived = receivedString.trim().toLowerCase().split("[ ]");
  48.  
  49.                 ReceivedCommand command = interpretReceived(splitReceived);
  50.  
  51.                 switch (command) {
  52.                     case Validation:
  53.                     case Help:
  54.                     case Generation:
  55.                         output.println("200");
  56.                     default:
  57.                         output.println("500");
  58.                         break;
  59.                 }
  60.  
  61.                 String[] parameters = Arrays.copyOfRange(splitReceived, 1, splitReceived.length);
  62.                 output.println(execute(command, parameters));
  63.             }
  64.             else break;
  65.         }
  66.     }
  67.  
  68.     private String execute(ReceivedCommand command, String[] parameters) {
  69.         String response = "";
  70.         switch(command){
  71.             case Help:
  72.                 response = "available commands: v <number> (validation of palindrome), g <min number> <max number> (generating a palindrome)";
  73.                 break;
  74.             case Validation:
  75.                 response = model.validatePalindrome(parameters[0]).toString();
  76.                 break;
  77.             case Generation:
  78.                 try {
  79.                     response = model.generatePalindrome(Integer.parseInt(parameters[0]), Integer.parseInt(parameters[1]));
  80.                 } catch (WrongParametersException e) {
  81.                     response = e.getMessage();
  82.                 }
  83.                 break;
  84.             case Fail:
  85.                 response = "Wrong parameters, send \"help\" for more information";
  86.                 break;
  87.         }
  88.         return response;
  89.     }
  90.  
  91.     private ReceivedCommand interpretReceived(String[] input) {
  92.         if(input[0].equals("v") && input.length >= 2)
  93.             return ReceivedCommand.Validation;
  94.         if(input[0].equals("g") && input.length >= 3)
  95.             return ReceivedCommand.Generation;
  96.         if(input[0].equals("help"))
  97.             return ReceivedCommand.Help;
  98.         return ReceivedCommand.Fail;
  99.     }
  100. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top