Advertisement
Guest User

Untitled

a guest
May 27th, 2015
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.06 KB | None | 0 0
  1. program design, a brief description of how your system works
  2. and your message design. Also discuss any design tradeoffs considered and made. Describe
  3. possible improvements and extensions to your program and indicate how you could realise them.
  4. If your program does not work under any particular circumstances please report this here. Also
  5. indicate any segments of code that you have borrowed from the Web or other books.
  6.  
  7.  
  8.  
  9. Design/Trade-offs.
  10.  
  11. For the first steps of the program, i used UDP as required for the delivery and receiving of ping messages between peers. I chose to perform this whole task within a single thread. I acheived this by implementing a 'delay' after each sending and receiving of messages. While this allowed it to function within a single thread, there was a clear trade off in effeciency and response times for response messages. Nonetheless, it is still able to constantly monitor whether the peers were alive periodically.
  12.  
  13. Step 3 required TCP to transport the messages and because it requires a set up connection, i chose to implement it using multithreads to allow each peer to simulatenously send and listen for both responses and standard input commands. I chose to implement an "ExecutorService" which essensially ran a private class in another thread to handle the messages and response separate from the server listening for connections to accept. I got this idea from stackOverflow, referenced below.
  14.  
  15.  
  16. ========================================================================================================
  17. //Aug 24 '13 at 11:59 - Shaggy
  18. //on stackoverflow "http://stackoverflow.com/questions/18418326/get-data-from-client-in-server-socket-java"
  19. public class ServerListener {
  20.  
  21. public static void main(String[] args) {
  22. new ServerListener().startServer();
  23. }
  24.  
  25. public void startServer() {
  26. final ExecutorService clientProcessingPool = Executors
  27. .newFixedThreadPool(10);
  28.  
  29. Runnable serverTask = new Runnable() {
  30. @Override
  31. public void run() {
  32. try {
  33. ServerSocket serverSocket = new ServerSocket(8000);
  34. System.out.println("Waiting for clients to connect...");
  35. while (true) {
  36. Socket clientSocket = serverSocket.accept();
  37. clientProcessingPool
  38. .submit(new ClientTask(clientSocket));
  39. }
  40. } catch (IOException e) {
  41. System.err.println("Unable to process client request");
  42. e.printStackTrace();
  43. }
  44. }
  45. };
  46. Thread serverThread = new Thread(serverTask);
  47. serverThread.start();
  48. }
  49.  
  50. private class ClientTask implements Runnable {
  51. private final Socket clientSocket;
  52.  
  53. private ClientTask(Socket clientSocket) {
  54. this.clientSocket = clientSocket;
  55. }
  56.  
  57. ========================================================================================================
  58.  
  59.  
  60.  
  61. For step 4, i found the predecessor peers by looking at the messages in the ping messages, since they contained the ID of the peer is came from. I ordered the peer predecessors using several if cases to deal with corner cases such as peer 15(first predecessor) and peer 1 (second predecessor) for peer 3.
  62.  
  63.  
  64. Improvements/extension
  65. My program could be improved particualrly in step 2 where the UDP ping messages and response messages could be more responsive. While at the moment, one request and one response are sent at the same period of time followed by a delay to separate the next round of request/response. This could be acheived by implementing a second thread in which the program will listen for UDP packets to receive. This will mean the response messages will be send directly after receiving a request without delay. This will also allow a more controlled/delayed timing for when to send request, without implying other peers are dead.
  66.  
  67.  
  68. Notes on running
  69. The program initializes in about 1 minute, it will start pinging straight away and begin determining it's predecessors.
  70. It will find initial predecessors although they may be incorrect at first, but it will self correct itself to finally determine it's correct 'first predecessor' and 'second predecessor'. after which it is ready to accept 'request commands'.
  71.  
  72. The ping messages come in pairs of 1 request and 1 response. The delayed response is due to the delay function as part of the implementation so sometimes after a request has been sent, the corresponding response may not arrive until approximately 30 seconds later.
  73.  
  74. When testing step 3, while inputing the commands into standard input, a ping message may interrupt the standard input line. However, you may continue to finish the initial command as if it were not interrupted and the command should still work.
  75.  
  76. The program still has not yet fully implemented 'quit' yet although initial messages for quit have been implemented, the actual reallocation of predecessors and sucessors have not been implemneted.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement