Advertisement
Azure47

OS Lab 2.4 - Incomplete

Mar 24th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.37 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.ServerSocket;
  3. import java.net.Socket;
  4. import java.util.HashMap;
  5.  
  6. class ClientStarterWorkerThread extends Thread {
  7.  
  8.     private int ID;
  9.     private DataInputStream inputStream;
  10.  
  11.     public ClientStarterWorkerThread(int clientID, DataInputStream inputStream) {
  12.         this.ID = clientID;
  13.         this.inputStream = inputStream;
  14.     }
  15.  
  16.     @Override
  17.     public void run() {
  18.         // todo: Handle listening to messages
  19.     }
  20. }
  21.  
  22. public class ClientStarer {
  23.  
  24.     private int ID;
  25.     private Socket s;
  26.     private PrintStream p;
  27.     //todo: init other required variables here
  28.  
  29.     ClientStarer(int id, String host, int port) throws IOException {
  30.         this.ID = id;
  31.  
  32.         // todo: Connect to server and send client ID
  33.         s = new Socket(host,port);
  34.         p = new PrintStream(s.getOutputStream());
  35.         p.println(ID);
  36.  
  37.         // todo: Listen for incoming messages
  38.         listen();
  39.     }
  40.  
  41.     // todo: Implement the sending message mechanism
  42.     void sendMessage(int idReceiver, String message) throws IOException {
  43.  
  44.     }
  45.  
  46.     // todo: end communication - send END to server
  47.     private void endCommunication() throws IOException {
  48.         p.println("END");
  49.         s.close();
  50.     }
  51.  
  52.     // todo: listen for icoming messages from the server.
  53.     // It should start a separate thread to handle listening
  54.     // and not block the execution
  55.     // Should start a new ClientStarterWorkerThread
  56.     private void listen() throws IOException{
  57.  
  58.     }
  59.  
  60.     public static void main(String[] args) throws IOException, InterruptedException {
  61.         //todo: Initialize and start 3 clients
  62.  
  63.         ClientStarer client1 = new ClientStarer(1,"127.0.0.1",9876);
  64.         ClientStarer client2 = new ClientStarer(2,"127.0.0.1",9876);
  65.         ClientStarer client3 = new ClientStarer(3,"127.0.0.1",9876);
  66.  
  67.         // Simulate chat
  68.         client1.sendMessage(2, "Hello from client 1");
  69.         Thread.sleep(1000);
  70.         client2.sendMessage(3, "Hello from client 2");
  71.         Thread.sleep(1000);
  72.         client1.sendMessage(3, "Hello from client 1");
  73.         Thread.sleep(1000);
  74.         client3.sendMessage(1, "Hello from client 3");
  75.         Thread.sleep(1000);
  76.         client3.sendMessage(2, "Hello from client 3");
  77.  
  78.         // Exit the chatroom
  79.         client1.endCommunication();
  80.         client2.endCommunication();
  81.         client3.endCommunication();
  82.     }
  83. }
  84.  
  85. class TCPServer {
  86.  
  87.     private ServerSocket server;
  88.     private HashMap<Integer, Socket> activeConnections;
  89.  
  90.     // todo: Get the required connection
  91.     public Socket getConnection(int id) throws IOException{
  92.         return activeConnections.get(id);
  93.     }
  94.  
  95.     // todo: Add connected client to the hash map
  96.     void addConnection(int id, Socket connection) {
  97.         activeConnections.put(id,connection);
  98.     }
  99.  
  100.     synchronized void endConnection(int id){
  101.         activeConnections.remove(id);
  102.     }
  103.  
  104.     //todo: Initialize server
  105.     TCPServer(int port) throws IOException {
  106.         server = new ServerSocket(port);
  107.     }
  108.  
  109.     // todo: Handle server listening
  110.     // todo: For each connection, start a separate
  111.     // todo: thread (ServerWorkerThread) to handle the communication
  112.     void listen() throws IOException {
  113.        
  114.     }
  115.  
  116.     public static void main(String[] args) throws IOException {
  117.         // todo: Start server
  118.  
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement