OOP Lab Task 5: Diferență între versiuni

De la WikiLabs
Jump to navigationJump to search
Fără descriere a modificării
Linia 12: Linia 12:
* [[Serialization]]
* [[Serialization]]
* [[Network Sockets]]
* [[Network Sockets]]
* [[Concurrent Programming - Threads]]


== Requirements ==
== 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.
# Implement a class called '''ClientPeer''' with the following characteristics:
* 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.
#* The class constructor takes as arguments a String (the user/sender name) and a reference to a pre-initialized Socket.
* 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 class implements two methods:
* 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'''.
#** <code>void sendMessage(String message)</code> - creates a Message object and serializes it, utilising the available Socket.
 
#** <code>void sendMessage(String message, String receiver)</code> - creates a PrivateMessage object and serializes it, utilising the available Socket.
Notes:
# Implement a class called '''TextClient''', which is executable (has a main method), with the following characteristics:
* 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].
#* The main method opens a Socket on the localhost (127.0.0.1) on port 9000
* This list should be kept consistent, meaning new clients should be added, and disconnected clients should be removed.
#* While the socket connection is active, read strings of characters from the keyboard (utilizing the Console class - see [https://docs.oracle.com/javase/8/docs/api/java/lang/System.html#console-- System.console()]) and call the sendMessage methods specified above (Note: You may choose any formatting to identify a private message, but please describe this format in the homework submission e-mail; one example is <code>/w John Hello!</code> which identifies a private message to user John, with the content "Hello!").
* This list should be available to the '''ServerPeer''' execution threads through synchronized methods.
# Implement a class called '''ServerPeer''' with the following characteristics:
* To check if an object is instance of a specific class, you can use operator '''instanceof''':
#* The class constructor takes as argument a reference to a pre-initialized Socket.
<syntaxhighlight lang="java">
#* While the socket connection is active, it deserializes objects of type Message and prints their formatted contents to the screen.
Object o = ois.readOject();
# Implement a class called '''Server''', which is executable (has a main method), with the following characteristics:
if(o instanceof String){
#* The main method must utilize an object of type '''ServerConfig''' to read a port number from a configuration file
    String s = (String)o;
#* Utilizing the identified port number, opens a ServerSocket and listens to connections,
    System.out.print(s);
#* Once a client is connected, creates a ServerPeer object, which utilizing the Socket obtained from the ServerSocket, reads objects from the stream, according to point 3 above.
}else{
# Modify the Message and PrivateMessage classes such that they are serializable.
    System.out.print("The object is not a String!");
}
</syntaxhighlight>

Versiunea de la data 18 noiembrie 2015 00:02

Required Tutorials

Requirements

  1. Implement a class called ClientPeer with the following characteristics:
    • The class constructor takes as arguments a String (the user/sender name) and a reference to a pre-initialized Socket.
    • The class implements two methods:
      • void sendMessage(String message) - creates a Message object and serializes it, utilising the available Socket.
      • void sendMessage(String message, String receiver) - creates a PrivateMessage object and serializes it, utilising the available Socket.
  2. Implement a class called TextClient, which is executable (has a main method), with the following characteristics:
    • The main method opens a Socket on the localhost (127.0.0.1) on port 9000
    • While the socket connection is active, read strings of characters from the keyboard (utilizing the Console class - see System.console()) and call the sendMessage methods specified above (Note: You may choose any formatting to identify a private message, but please describe this format in the homework submission e-mail; one example is /w John Hello! which identifies a private message to user John, with the content "Hello!").
  3. Implement a class called ServerPeer with the following characteristics:
    • The class constructor takes as argument a reference to a pre-initialized Socket.
    • While the socket connection is active, it deserializes objects of type Message and prints their formatted contents to the screen.
  4. Implement a class called Server, which is executable (has a main method), with the following characteristics:
    • The main method must utilize an object of type ServerConfig to read a port number from a configuration file
    • Utilizing the identified port number, opens a ServerSocket and listens to connections,
    • Once a client is connected, creates a ServerPeer object, which utilizing the Socket obtained from the ServerSocket, reads objects from the stream, according to point 3 above.
  5. Modify the Message and PrivateMessage classes such that they are serializable.