Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package dslab.test;
- import java.io.BufferedReader;
- import java.io.Closeable;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.rmi.NoSuchObjectException;
- import java.rmi.registry.LocateRegistry;
- import java.rmi.registry.Registry;
- import java.rmi.server.UnicastRemoteObject;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- /**
- * NOTE: DO *NOT* CHANGE THIS FILE!
- */
- public class Server implements Runnable, Closeable, IServerRemote {
- private int tcpPort;
- private ServerSocket serverSocket;
- private Socket clientSocket;
- private int registryPort;
- private String bindingName;
- private Registry registry;
- private List<String> solvedStages = new ArrayList<String>();
- public Server(int tcpPort, int registryPort, String bindingName) {
- this.tcpPort = tcpPort;
- this.registryPort = registryPort;
- this.bindingName = bindingName;
- }
- public void init() {
- try {
- registry = LocateRegistry.createRegistry(registryPort);
- IServerRemote remote = (IServerRemote) UnicastRemoteObject
- .exportObject(this, 0);
- registry.bind(bindingName, remote);
- serverSocket = new ServerSocket(tcpPort);
- } catch (Exception e) {
- try {
- close();
- } catch (IOException e1) {
- }
- }
- }
- @Override
- public String bye() {
- solvedStages.add(BYE[0]);
- return "!see_you";
- }
- @Override
- public void run() {
- try {
- clientSocket = serverSocket.accept();
- solvedStages.add(HELLO[0]);
- BufferedReader reader = new BufferedReader(new InputStreamReader(
- clientSocket.getInputStream()));
- PrintWriter writer = new PrintWriter(
- clientSocket.getOutputStream(), true);
- String request;
- while ((request = reader.readLine()) != null) {
- if (request.equals("!hello")) {
- solvedStages.add(HELLO[1]);
- writer.println("!welcome");
- } else
- writer.println("!wrongRequest");
- }
- solvedStages.add(HELLO[2]);
- } catch (IOException e) {
- try {
- close();
- } catch (IOException e1) {
- }
- }
- }
- @Override
- public void close() throws IOException {
- if (serverSocket != null && !serverSocket.isClosed())
- serverSocket.close();
- if (clientSocket != null && !clientSocket.isClosed())
- clientSocket.close();
- try {
- UnicastRemoteObject.unexportObject(this, true);
- } catch (NoSuchObjectException e) {
- }
- if (registry != null) {
- try {
- // unbind remote object
- registry.unbind(bindingName);
- // stop registry
- UnicastRemoteObject.unexportObject(registry, true);
- } catch (Exception e) {
- }
- }
- }
- public Result getResult(String id) {
- boolean succeeded = true;
- String message = "\nYou didn't pass the following stages: \n";
- String[] stages = idMap.get(id);
- for (String stage : stages) {
- if (!solvedStages.contains(stage)) {
- succeeded = false;
- message += "* " + stageMap.get(stage) + "\n";
- }
- }
- return new Result(succeeded, message);
- }
- private static Map<String, String> stageMap = new HashMap<String, String>();
- private static Map<String, String[]> idMap = new HashMap<String, String[]>();
- private final static String[] HELLO = { "hello_01", "hello_02", "hello_03"};
- private final static String[] BYE = { "bye_01" };
- static {
- idMap.put("hello", HELLO);
- idMap.put("bye", BYE);
- stageMap.put(HELLO[0], "connect to server");
- stageMap.put(HELLO[1], "send !hello to server");
- stageMap.put(HELLO[2], "close connection");
- stageMap.put(BYE[0], "invoke bye() on ServerRemote");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment