PJF16

Server

Jan 25th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.63 KB | None | 0 0
  1. package dslab.test;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.Closeable;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.io.PrintWriter;
  8. import java.net.ServerSocket;
  9. import java.net.Socket;
  10. import java.rmi.NoSuchObjectException;
  11. import java.rmi.registry.LocateRegistry;
  12. import java.rmi.registry.Registry;
  13. import java.rmi.server.UnicastRemoteObject;
  14. import java.util.ArrayList;
  15. import java.util.HashMap;
  16. import java.util.List;
  17. import java.util.Map;
  18.  
  19. /**
  20.  * NOTE: DO *NOT* CHANGE THIS FILE!
  21.  */
  22. public class Server implements Runnable, Closeable, IServerRemote {
  23.  
  24.     private int tcpPort;
  25.     private ServerSocket serverSocket;
  26.     private Socket clientSocket;
  27.  
  28.     private int registryPort;
  29.     private String bindingName;
  30.     private Registry registry;
  31.  
  32.     private List<String> solvedStages = new ArrayList<String>();
  33.  
  34.     public Server(int tcpPort, int registryPort, String bindingName) {
  35.         this.tcpPort = tcpPort;
  36.         this.registryPort = registryPort;
  37.         this.bindingName = bindingName;
  38.     }
  39.  
  40.     public void init() {
  41.         try {
  42.             registry = LocateRegistry.createRegistry(registryPort);
  43.  
  44.             IServerRemote remote = (IServerRemote) UnicastRemoteObject
  45.                     .exportObject(this, 0);
  46.  
  47.             registry.bind(bindingName, remote);
  48.  
  49.             serverSocket = new ServerSocket(tcpPort);
  50.         } catch (Exception e) {
  51.             try {
  52.                 close();
  53.             } catch (IOException e1) {
  54.             }
  55.         }
  56.     }
  57.  
  58.     @Override
  59.     public String bye() {
  60.         solvedStages.add(BYE[0]);
  61.         return "!see_you";
  62.     }
  63.  
  64.     @Override
  65.     public void run() {
  66.  
  67.         try {
  68.             clientSocket = serverSocket.accept();
  69.  
  70.             solvedStages.add(HELLO[0]);
  71.  
  72.             BufferedReader reader = new BufferedReader(new InputStreamReader(
  73.                     clientSocket.getInputStream()));
  74.             PrintWriter writer = new PrintWriter(
  75.                     clientSocket.getOutputStream(), true);
  76.  
  77.             String request;
  78.             while ((request = reader.readLine()) != null) {
  79.                 if (request.equals("!hello")) {
  80.                     solvedStages.add(HELLO[1]);
  81.                     writer.println("!welcome");
  82.                 } else
  83.                     writer.println("!wrongRequest");
  84.             }
  85.             solvedStages.add(HELLO[2]);
  86.  
  87.         } catch (IOException e) {
  88.             try {
  89.                 close();
  90.             } catch (IOException e1) {
  91.             }
  92.         }
  93.     }
  94.  
  95.     @Override
  96.     public void close() throws IOException {
  97.  
  98.         if (serverSocket != null && !serverSocket.isClosed())
  99.             serverSocket.close();
  100.  
  101.         if (clientSocket != null && !clientSocket.isClosed())
  102.             clientSocket.close();
  103.  
  104.         try {
  105.             UnicastRemoteObject.unexportObject(this, true);
  106.         } catch (NoSuchObjectException e) {
  107.         }
  108.  
  109.         if (registry != null) {
  110.             try {
  111.                 // unbind remote object
  112.                 registry.unbind(bindingName);
  113.  
  114.                 // stop registry
  115.                 UnicastRemoteObject.unexportObject(registry, true);
  116.             } catch (Exception e) {
  117.             }
  118.         }
  119.     }
  120.  
  121.     public Result getResult(String id) {
  122.         boolean succeeded = true;
  123.         String message = "\nYou didn't pass the following stages: \n";
  124.         String[] stages = idMap.get(id);
  125.         for (String stage : stages) {
  126.             if (!solvedStages.contains(stage)) {
  127.                 succeeded = false;
  128.                 message += "* " + stageMap.get(stage) + "\n";
  129.             }
  130.         }
  131.  
  132.         return new Result(succeeded, message);
  133.     }
  134.  
  135.     private static Map<String, String> stageMap = new HashMap<String, String>();
  136.     private static Map<String, String[]> idMap = new HashMap<String, String[]>();
  137.     private final static String[] HELLO = { "hello_01", "hello_02", "hello_03"};
  138.     private final static String[] BYE = { "bye_01" };
  139.  
  140.     static {
  141.         idMap.put("hello", HELLO);
  142.         idMap.put("bye", BYE);
  143.  
  144.         stageMap.put(HELLO[0], "connect to server");
  145.         stageMap.put(HELLO[1], "send !hello to server");
  146.         stageMap.put(HELLO[2], "close connection");
  147.  
  148.         stageMap.put(BYE[0], "invoke bye() on ServerRemote");
  149.     }
  150.  
  151. }
Advertisement
Add Comment
Please, Sign In to add comment