Java Reference
In-Depth Information
40
41
try { // Close socket
42
clntSock.close();
43
} catch (IOException e) {
44
entry.add("Exception="+ e.getMessage());
45
}
46
47
logger.writeEntry(entry);
48
}
49 }
EchoProtocol.java
1. Declaration of implementation of the Runnable interface: line 4
2. Member variables and constructor: lines 7-13
Each instance of EchoProtocol contains a socket for the connection and a reference to the
logger.
3. run() : lines 15-48
Implement the echo protocol:
Write the client and thread information to a buffer: lines 16-20
ArrayList is a dynamically sized container of Object s. The add() method of ArrayList
inserts the specified object at the end of the list. In this case, the inserted object is a
String . Each element of the ArrayList represents a line of output to the logger.
Execute the echo protocol: lines 22-45
Write the elements (one per line) of the ArrayList instance to the logger: line 47
The logger allows for synchronized reporting of thread creation and client completion,
so that entries from different threads are not interleaved. This facility is defined by the Logger
interface, which has methods for writing strings or object collections.
Logger.java
0 import java.util.*; // for Collection
1
2 public interface Logger {
3
public void writeEntry(Collection entry); // Write list of lines
4
public void writeEntry(String entry);
// Write single line
5 }
Logger.java
Search WWH ::




Custom Search