Diferență între revizuiri ale paginii „OOP Lab Task 5”

De la WikiLabs
Jump to navigationJump to search
Linia 16: Linia 16:
 
== Requirements ==
 
== Requirements ==
  
Coming soon!
+
* Modify class '''ClientPeer''' written for the previous assignment, making it into a thread which, in parallel with the main execution thread, has to read objects of type '''Message''' from the server and write them on the screen.
 +
* Modify class '''ServerPeer''' written for the previous assignment, making it into a thread. This new thread must read objects of type '''Message''' and '''PrivateMessage''' from the associated client and correctly distribute them to the other clients.
 +
* Modify class '''Server''' written for the previous assignment, such that when a new client connects, the Server main thread should create a new '''ServerPeer''' and start it as a Thread, and then return to blocking in method '''ServerSocket'''.''accept()'', waiting for a new incoming connection.
 +
* The server should not accept a new connection if the current number of connected clients is already equal to property MAX_CLIENTS read from the ''server.conf'' configuration file by the class '''ServerConfig'''.
 +
 
 +
Notes:
 +
* Class '''Server''' should keep a list of all the connected clients (objects of type '''ServerPeer'''). For this, you can use class [http://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html java.util.ArrayList].
 +
* This list should be kept consistent, meaning new clients should be added, and disconnected clients should be removed.
 +
* This list should be available to the '''ServerPeer''' execution threads through synchronized methods.
 +
* To check if an object is instance of a specific class, you can use operator '''instanceof''':
 +
<syntaxhighlight lang="java">
 +
Object o = ois.readOject();
 +
if(o instanceof String){
 +
    String s = (String)o;
 +
    System.out.print(s);
 +
}else{
 +
    System.out.print("The object is not a String!");
 +
}
 +
</syntaxhighlight>

Versiunea de la data 18 decembrie 2014 20:45

Required Tutorials

Requirements

  • Modify class ClientPeer written for the previous assignment, making it into a thread which, in parallel with the main execution thread, has to read objects of type Message from the server and write them on the screen.
  • Modify class ServerPeer written for the previous assignment, making it into a thread. This new thread must read objects of type Message and PrivateMessage from the associated client and correctly distribute them to the other clients.
  • Modify class Server written for the previous assignment, such that when a new client connects, the Server main thread should create a new ServerPeer and start it as a Thread, and then return to blocking in method ServerSocket.accept(), waiting for a new incoming connection.
  • The server should not accept a new connection if the current number of connected clients is already equal to property MAX_CLIENTS read from the server.conf configuration file by the class ServerConfig.

Notes:

  • Class Server should keep a list of all the connected clients (objects of type ServerPeer). For this, you can use class java.util.ArrayList.
  • This list should be kept consistent, meaning new clients should be added, and disconnected clients should be removed.
  • This list should be available to the ServerPeer execution threads through synchronized methods.
  • To check if an object is instance of a specific class, you can use operator instanceof:
Object o = ois.readOject();
if(o instanceof String){
    String s = (String)o;
    System.out.print(s);
}else{
    System.out.print("The object is not a String!");
}