OOP Lab Task 6: Diferență între versiuni

De la WikiLabs
Jump to navigationJump to search
Linia 17: Linia 17:
== 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.
# Create a new project, named '''oop_lab6'''.
* 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.
# For this task you need the previously designed classes '''Message''', '''ClientPeer''' and '''Server''', from OOP Lab Task 5. Copy their source .java files inside this package (letting NetBeans to refactor them or manually changing their package statement to make these classes part of this package).
* 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.
# Modify the class '''Server''' so that its '''main''' method, instead of instantiating and running a '''ServerPeer''' object, instantiates a '''Client''' object. The '''Client''' arguments are a string (the name of the client on the server side) and the '''Socket''' reference, returned by the '''accept''' method called for the '''ServerSocket''' object.
* 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'''.
# The class '''ClientPeer''', which was used in the previous laboratory only to send messages, will manage a bidirectional connection, to send AND receive messages through it.
#* Beside the '''ObjectOutputStream''' you need an input stream to read '''Message''' objects from. The following things need to be added to this class:
#** a reference to an <code style="color:green;">ObjectInputStream</code> object;
#** The '''ObjectInputStream''' is a filter stream that encloses the input stream of the socket. You get the reference to that input stream using the '''Socket''''s method <code style="color:green;">getInputStream</code>. The filter stream will automatically deserialize the received '''Message''' objects. The input and output streams instantiations are done inside the constructor.
#** a method, named <code style="color:green;">readMessage</code>, without arguments, that returns the formatted string of the received '''Message''' object. Inside this method you read a single object from the input stream, using the proper method of the '''ObjectInputStream''' object, downcast the read object to a '''Message''' type and call the proper method of the received object to get the formatted string. This method may throw various exceptions of type '''IOException''' when called, and also an exception of type '''ClassNotFoundException''' when the downcasting is not possible. These are checked exceptions; though you do not catch them they need to be declared by the method.
#** The '''close''' method needs now to close both streams, the input stream and the output stream.
#* Remember! All fields are private, all methods are public.


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">
<syntaxhighlight lang="java">
Object o = ois.readOject();
Object o = ois.readOject();

Versiunea de la data 12 ianuarie 2016 20:21

Required Tutorials

Requirements

  1. Create a new project, named oop_lab6.
  2. For this task you need the previously designed classes Message, ClientPeer and Server, from OOP Lab Task 5. Copy their source .java files inside this package (letting NetBeans to refactor them or manually changing their package statement to make these classes part of this package).
  3. Modify the class Server so that its main method, instead of instantiating and running a ServerPeer object, instantiates a Client object. The Client arguments are a string (the name of the client on the server side) and the Socket reference, returned by the accept method called for the ServerSocket object.
  4. The class ClientPeer, which was used in the previous laboratory only to send messages, will manage a bidirectional connection, to send AND receive messages through it.
    • Beside the ObjectOutputStream you need an input stream to read Message objects from. The following things need to be added to this class:
      • a reference to an ObjectInputStream object;
      • The ObjectInputStream is a filter stream that encloses the input stream of the socket. You get the reference to that input stream using the Socket's method getInputStream. The filter stream will automatically deserialize the received Message objects. The input and output streams instantiations are done inside the constructor.
      • a method, named readMessage, without arguments, that returns the formatted string of the received Message object. Inside this method you read a single object from the input stream, using the proper method of the ObjectInputStream object, downcast the read object to a Message type and call the proper method of the received object to get the formatted string. This method may throw various exceptions of type IOException when called, and also an exception of type ClassNotFoundException when the downcasting is not possible. These are checked exceptions; though you do not catch them they need to be declared by the method.
      • The close method needs now to close both streams, the input stream and the output stream.
    • Remember! All fields are private, all methods are public.
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!");
}

Submitting

  • The assignment will be evaluated automatically by the Web-CAT platform.
  • You could access the Web-CAT platform using the username and the password with which you acces the electronica.curs.pub.ro intranet.
  • Select the OOP Lab Task 6 assignment.
  • Submit your work as a single .zip archive (give it whatever name you choose) containing only the Java source code files.
  • Attention Any deviation from these instructions may lead to the loss of the entire amount of points.