OOP Lab Task 5: Diferență între versiuni

De la WikiLabs
Jump to navigationJump to search
Linia 15: Linia 15:
== Requirements ==
== Requirements ==


# Implement a class called <span style="color:green;">ClientPeer</span> with the following characteristics:
# Create a new project, named '''oop_lab5'''.
#* The class constructor takes as arguments a String (the user/sender name) and a reference to a pre-initialized Socket.
# For this task you need the previously designed class '''Message''', from OOP Lab Task 1. Copy its source .java file inside this package (letting NetBeans to refactor it or manually changing its package statement to make the class part of this package).
#* The class implements two methods:
# Create a class called <code style="color:green;">ClientPeer</code> with the following features:
#** <code>void sendMessage(String message)</code> - creates a Message object and serializes it, utilising the available Socket.
#* The class constructor takes as arguments a string (the user/sender name) and a reference to a Socket object, that is supposed to be already instantiated and connected, and saves them in private fields.
#** <code>void sendMessage(String message, String receiver)</code> - creates a PrivateMessage object and serializes it, utilising the available Socket.
#* The method <code style="color:green;">void sendMessage(String message)</code>, that creates a '''Message''' object with the content taken from the method's argument and the sender's name read from the class' fields, and sends the newly created object through the available Socket.
#** In order to be able to send a '''Message''' object through a stream (to serialize that object), it must be serializable.
#** The object is sent through the output stream of the Socket. You get the reference to that stream using the Socket's method <code style="color:green;">getOutputStream</code>, but to be able to send objects through this stream, which is only a basic stream of bytes, you need to enclose it in a filter stream of type <code style="color:green;">ObjectOutputStream</code>.
#** Both the constructor and the method employ a socket and stream. Their methods may throw various errors of type '''IOException'''. You do not catch them, but, because they are checked exceptions, you must declare them as being thrown by the constructor and the method.
# Implement a class called '''TextClient''', which is executable (has a main method), with the following characteristics:
# 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
#* The main method opens a Socket on the localhost (127.0.0.1) on port 9000

Versiunea de la data 15 decembrie 2015 21:52

Required Tutorials

Requirements

  1. Create a new project, named oop_lab5.
  2. For this task you need the previously designed class Message, from OOP Lab Task 1. Copy its source .java file inside this package (letting NetBeans to refactor it or manually changing its package statement to make the class part of this package).
  3. Create a class called ClientPeer with the following features:
    • The class constructor takes as arguments a string (the user/sender name) and a reference to a Socket object, that is supposed to be already instantiated and connected, and saves them in private fields.
    • The method void sendMessage(String message), that creates a Message object with the content taken from the method's argument and the sender's name read from the class' fields, and sends the newly created object through the available Socket.
      • In order to be able to send a Message object through a stream (to serialize that object), it must be serializable.
      • The object is sent through the output stream of the Socket. You get the reference to that stream using the Socket's method getOutputStream, but to be able to send objects through this stream, which is only a basic stream of bytes, you need to enclose it in a filter stream of type ObjectOutputStream.
      • Both the constructor and the method employ a socket and stream. Their methods may throw various errors of type IOException. You do not catch them, but, because they are checked exceptions, you must declare them as being thrown by the constructor and the method.
  4. 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!").
  5. 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.
  6. 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.
  7. Modify the Message and PrivateMessage classes such that they are serializable.

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 5 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.