CMSC 433 - Project 1 - Java exercise
Due Feb. 13, 6:00PM
Part 1 - Counter
Write myCounter, a counter class. It should
support the following functionality:
public class myCounter {
Integer getNextID();
}
- The values returned by getNextID() should start at 0, increase by 1 each
time, and be distinct across all myCounter instances (i.e. all myCounter
instances should share the same internal counter).
- If the value will exceed Integer.MAX_VALUE, return -1 on this and all
future calls.
Part 2 - Timestamper
Write myTimestamper, a timestamping class.
It should support the following functionality:
public class myTimestamper {
java.util.Date getTimestamp();
}
- The return value of getTimestamp() should indicate the time of day at which the
function was called.
Part 3 - Logging server and Logging client
Write myLoggingServer and myLoggingClient, server and client classes that
support the following
functionality:
public class myLoggingServer {
myLoggingServer(int port, java.io.Writer outStream);
void writeRecord(String record);
String getRecords(long windowSize);
void flush();
}
-
myLoggingServer opens a socket on localhost at port number port and
establishes outStream as the place to which it writes its output.
Once established, myLoggingServer loops forever, doing the following:
-
accepts a connection from a myLoggingClient on port,
-
reads a command name ("writeRecord" + "\n",
"getRecords" + "\n", or "flush" + "\n") from the socket
-
reads one record ((ID + "\n" + timestamp
+ "\n"
+ event + "\n") or (windowSize)) from the socket,
-
calls the appropriate method passing the data read from
the socket, appropriately converted,
-
if command was getRecords returns records in original
format (ID + "\n" + timestamp
+ "\n"
+ event + "\n")
-
closes the connection.
- The field separator for all fields will be the newline (so newlines are
not allowed in ID or event strings).
- For the purpose of passing a java.util.Date object (either way) between
the logging client and server (i.e. as part of a record), the String
representation of a Date object should be a value of type long that
represents the number of milliseconds since the beginning of Unix time, January 1, 1970, 00:00:00 GMT
(see Date.getTime() and the wrapper class Long).
- writeRecord stores record on server.
- getRecords returns all records that are no older than windowSize
milliseconds that have not yet been flushed. Make sure you print out the
records in ascending order of their timestamps with one blank line in
between each record. The timestamps should be human-readable (use
java.util.Date.toString() ).
- flush writes all records to outStream and removes them from the
server's stored records. Make sure you print out the records in ascending
order of their timestamps with one blank line in between each record. The
timestamps should be printed using the long format, as for passing them
between the client and server.
- The public static main() method for your server should get the port number
to listen on from the command line (e.g., start the server with java -Dport=portnum
myLoggingServer). The port number can be extracted in your
server with the Integer.getInteger() method. For testing purposes, so everyone uses
different ports, test your server using port x33yy, where x is your section number and yy is your account number.
public class myLoggingClient {
myLoggingClient(InetAddress host, int port);
void writeRecord(String ID, String timestamp, String event);
void viewRecords(long windowSize);
void flush();
}
- myLoggingClient stores information needed to connect to the myLoggingServer instance
(i.e. host host at port port).
- writeRecord() sends its parameters to the myLoggingServer instance
previously associated with this myLoggingClient instance, with each
parameter string followed by a newline ("\n").
- viewRecords() creates a new window on the host machine and displays all
records whose timestamp is no older than windowSize
milliseconds. See file TestPane.java
for a sample program using Swing that displays text in a scrolling window.
- flush() sends a flush command to the server.
- The driver for the client should get the host and port number of the
server to connect to from the command line. For the client driver we
provide, ProducerConsumerTest, start with java -Dhost=hostname -Dport=portnum
ProducerConsumerTest). As show in the driver, the hostname can be
extracted with the System.getProperty() method.
Details
Here is driver code that we will use to test your classes: ProducerConsumerTest.java,
Producer.java, Consumer.java,
and CubbyHole.java . For these drivers,
sample contents for the client viewRecords() window is here
, and sample output for the server flush() command is here
. Note that your output will not match exactly because of thread behavior
and values of timestamps.
Submission details
Submission will be handled using the submit program as project 1 (i.e. submit
1 *.java).
Web Accessibility