OOP Lab Task 4: Diferență între versiuni

De la WikiLabs
Jump to navigationJump to search
Fără descriere a modificării
Linia 10: Linia 10:
* [[Exception Handling]]
* [[Exception Handling]]
* [[Input/Output Streams]]
* [[Input/Output Streams]]
* [[Serialization]]
* [[Network Sockets]]


== Requirements ==
== Requirements ==


# Implement a class called '''ClientPeer''' with the following characteristics:
# Write a class called '''ServerConfig''' which has the role of reading configuration values from a text file. This class will be used later on during the development of the server application, and needs to meet the following requirements:
#* The class constructor takes as arguments a String (the user/sender name) and a reference to a pre-initialized Socket.
#* The class needs to have two constructors:
#* The class implements two methods:
#*# One with an argument of type String, which represents the file to be read
#** <code>void sendMessage(String message)</code> - creates a Message object and serializes it, utilising the available Socket.
#*# One without any arguments, which will implicitly consider the configuration file to be ''server.conf''.
#** <code>void sendMessage(String message, String receiver)</code> - creates a PrivateMessage object and serializes it, utilising the available Socket.
#* Objects of type '''ServerConfig''' have to read the file, line by line, and then expose the parameters that were read through methods.
# Implement a class called '''TextClient''', which is executable (has a main method), with the following characteristics:
#* The methods in class '''ServerConfig''' must throw exceptions if any issues arise when reading or parsing the file:
#* The main method opens a Socket on the localhost (127.0.0.1) on port 9000
#*# '''IOException''' if the file is missing or it cannot be opened (this exception is already being thrown by stream related classes, so you only need to rethrow it further up the execution stack).
#* 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!").
#*# '''InvalidFormatException''' if at least one line from the file doesn't match the expected pattern (this type of exception doesn't exist and it needs to be created).
# Implement a class called '''ServerPeer''' with the following characteristics:
#*# '''UnknownKeyException''' if an unknown property is present in the file (this type of exception doesn't exist and it needs to be created).
#* The class constructor takes as argument a reference to a pre-initialized Socket.
#*# '''MissingKeyException''' if one of the expected properties is missing from the file (this type of exception doesn't exist and it needs to be created).
#* While the socket connection is active, it deserializes objects of type Message and prints their formatted contents to the screen.
#* The class must be immutable.
# Implement a class called '''Server''', which is executable (has a main method), with the following characteristics:
# The following properties are the only ones valid and must exist in the configuration file:
#* The main method must utilize an object of type '''ServerConfig''' to read a port number from a configuration file
#* TCP_PORT - representing the TCP port on which the server will be listening on.
#* Utilizing the identified port number, opens a ServerSocket and listens to connections,
#* MAX_CLIENTS - the maximum number of clients that the server an serve.
#* 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.
# Write a test for this class (which should open a file, read its contents and print the read parameters on the screen) and add it to the main method in class '''Main'''.
# Modify the Message and PrivateMessage classes such that they are serializable.
 
 
The configuration file that class '''ServerConfig''' is reading is a text file, containing lines of the following form:
<syntaxhighlight lang="xorg_conf">
PROPERTY=value
</syntaxhighlight>
* Any line starting with thr character #, preceded or not by spaces or "tab" characters is considered to be a comment and it will be ignored.
* Lines that do not contain any printable character but only spaces and/ or tabs will also be ignored.
* Spaces or tabs from the beginning and end of lines are ignored.
An example of a configuration file:
 
<syntaxhighlight lang="xorg_conf">
#this is a config file for the chat server!
      #  this is made by ME!
MAX_CLIENT=100
      TCP_PORT=9000
</syntaxhighlight>
 
 
'''Notes:'''
* The three types of exceptions which must be defined have very simple implementations. You need to define them as classes that extend class '''Exception''' and they only need to contain a constructor with an argument of type String that only calls the constructor of the super-class of the same type.
* The submitted files will be only Java implementations of the following classes: '''ServerConfig''', '''Main''', '''Message''', '''PrivateMessage''', '''InvalidFormatException''', '''UnknownKeyException''' and '''MissingKeyException'''.
* The following classes and methods may be of use in implementing this assignment. Look them up in the API documentation:
** String.trim
** String.charAt
** String.contains
** String.startsWith
** String.equals
** String.split
** Integer.parseInt
** BufferedReader.readLine
* Because you can have two different objects of type '''String''' (with different references) which contain the same character sequence, and because operator == is used to compare references, the correct way to compare two Strings is by calling method equals:
<syntaxhighlight lang="java">
String a = "abc";
String b = new String(a);
if(a == b) { // <- wrong, this is evaluated to false
  //...
}
if(a.equals(b)) { // <- correct, this is evaluated to true
  //...
}
 
</syntaxhighlight >
 
Submitting:
* The assignment is submitted attaching the Java source code files exclusively to an e-mail sent to radu.hobincu@upb.ro.
* The subject of the e-mail will be [POO_3]
* The body of the e-mail will contain the name and group of the student and any additional command you want to make.
* <font color="red">'''Attention'''</font>: Any deviation from these instructions may lead to the loss of the entire amount of points.

Versiunea de la data 18 noiembrie 2015 00:02

Required Tutorials

Requirements

  1. Write a class called ServerConfig which has the role of reading configuration values from a text file. This class will be used later on during the development of the server application, and needs to meet the following requirements:
    • The class needs to have two constructors:
      1. One with an argument of type String, which represents the file to be read
      2. One without any arguments, which will implicitly consider the configuration file to be server.conf.
    • Objects of type ServerConfig have to read the file, line by line, and then expose the parameters that were read through methods.
    • The methods in class ServerConfig must throw exceptions if any issues arise when reading or parsing the file:
      1. IOException if the file is missing or it cannot be opened (this exception is already being thrown by stream related classes, so you only need to rethrow it further up the execution stack).
      2. InvalidFormatException if at least one line from the file doesn't match the expected pattern (this type of exception doesn't exist and it needs to be created).
      3. UnknownKeyException if an unknown property is present in the file (this type of exception doesn't exist and it needs to be created).
      4. MissingKeyException if one of the expected properties is missing from the file (this type of exception doesn't exist and it needs to be created).
    • The class must be immutable.
  2. The following properties are the only ones valid and must exist in the configuration file:
    • TCP_PORT - representing the TCP port on which the server will be listening on.
    • MAX_CLIENTS - the maximum number of clients that the server an serve.
  3. Write a test for this class (which should open a file, read its contents and print the read parameters on the screen) and add it to the main method in class Main.


The configuration file that class ServerConfig is reading is a text file, containing lines of the following form:

PROPERTY=value
  • Any line starting with thr character #, preceded or not by spaces or "tab" characters is considered to be a comment and it will be ignored.
  • Lines that do not contain any printable character but only spaces and/ or tabs will also be ignored.
  • Spaces or tabs from the beginning and end of lines are ignored.

An example of a configuration file:

#this is a config file for the chat server!
      #   this is made by ME!
MAX_CLIENT=100
       TCP_PORT=9000


Notes:

  • The three types of exceptions which must be defined have very simple implementations. You need to define them as classes that extend class Exception and they only need to contain a constructor with an argument of type String that only calls the constructor of the super-class of the same type.
  • The submitted files will be only Java implementations of the following classes: ServerConfig, Main, Message, PrivateMessage, InvalidFormatException, UnknownKeyException and MissingKeyException.
  • The following classes and methods may be of use in implementing this assignment. Look them up in the API documentation:
    • String.trim
    • String.charAt
    • String.contains
    • String.startsWith
    • String.equals
    • String.split
    • Integer.parseInt
    • BufferedReader.readLine
  • Because you can have two different objects of type String (with different references) which contain the same character sequence, and because operator == is used to compare references, the correct way to compare two Strings is by calling method equals:
String a = "abc";
String b = new String(a);
if(a == b) { // <- wrong, this is evaluated to false
  //...
}
if(a.equals(b)) { // <- correct, this is evaluated to true
  //...
}

Submitting:

  • The assignment is submitted attaching the Java source code files exclusively to an e-mail sent to radu.hobincu@upb.ro.
  • The subject of the e-mail will be [POO_3]
  • The body of the e-mail will contain the name and group of the student and any additional command you want to make.
  • Attention: Any deviation from these instructions may lead to the loss of the entire amount of points.