This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Logger.java
174 lines (144 loc) · 4.67 KB
/
Logger.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package cnt5106c.p2p_file_sharing;
import java.io.PrintWriter;
import java.io.FileNotFoundException;
import java.time.LocalDateTime;
import java.util.List;
public class Logger {
private final String peerID;
private final String logFileName;
private PrintWriter writer;
private boolean consoleDisplay;
static enum Direction {
CONNECT_TO,
CONNECT_FROM;
}
Logger(String peerID) throws FileNotFoundException {
this.peerID = peerID;
logFileName = "log_peer_" + peerID + ".log";
writer = new PrintWriter(logFileName);
consoleDisplay = false;
}
void logTCPConnection(String partnerID, Direction dir) {
String currentTime = getCurrentTime();
String log;
if(dir == Direction.CONNECT_TO)
log = currentTime + ": Peer " + peerID + " makes a connection to Peer " + partnerID + ".";
else
log = currentTime + ": Peer " + peerID + " is connected from Peer " + partnerID + ".";
synchronized(writer) {
writer.println(log);
writer.flush();
}
if(consoleDisplay)
System.out.println(log);
}
void logChangePreferredNeighbor(List<String> neighbors) {
String currentTime = getCurrentTime();
String log = currentTime + ": Peer " + peerID + " has the preferred neighbors ";
if(!neighbors.isEmpty()) {
for(int i = 0; i < neighbors.size() - 1; i++)
log += neighbors.get(i) + ",";
log += neighbors.get(neighbors.size() - 1);
}
log += ".";
synchronized(writer) {
writer.println(log);
writer.flush();
}
if(consoleDisplay)
System.out.println(log);
}
void logChangeOptimisticallyUnchokedNeighbor(String neighborID) {
String currentTime = getCurrentTime();
String log = currentTime + ": Peer " + peerID + " has the optimistically unchoked neighbor " + neighborID + ".";
synchronized(writer) {
writer.println(log);
writer.flush();
}
if(consoleDisplay)
System.out.println(log);
}
void logUnchoking(String neighborID) {
String currentTime = getCurrentTime();
String log = currentTime + ": Peer " + peerID + " is unchoked by " + neighborID + ".";
synchronized(writer) {
writer.println(log);
writer.flush();
}
if(consoleDisplay)
System.out.println(log);
}
void logChoking(String neighborID) {
String currentTime = getCurrentTime();
String log = currentTime + ": Peer " + peerID + " is choked by " + neighborID + ".";
synchronized(writer) {
writer.println(log);
writer.flush();
}
if(consoleDisplay)
System.out.println(log);
}
void logReceiveHave(String partner, int pieceIndex) {
String currentTime = getCurrentTime();
String log = currentTime + ": Peer " + peerID + " received the 'have' message from " + partner + " for the piece " + pieceIndex + ".";
synchronized(writer) {
writer.println(log);
writer.flush();
}
if(consoleDisplay)
System.out.println(log);
}
void logReceiveInterested(String partner) {
String currentTime = getCurrentTime();
String log = currentTime + ": Peer " + peerID + " received the 'interested' message from " + partner + ".";
synchronized(writer) {
writer.println(log);
writer.flush();
}
if(consoleDisplay)
System.out.println(log);
}
void logReceiveNotInterested(String partner) {
String currentTime = getCurrentTime();
String log = currentTime + ": Peer " + peerID + " received the 'not interested' message from " + partner + ".";
synchronized(writer) {
writer.println(log);
writer.flush();
}
if(consoleDisplay)
System.out.println(log);
}
void logDownloadPiece(String partner, int pieceIndex, int totalPieces) {
String currentTime = getCurrentTime();
String log = currentTime + ": Peer " + peerID + " has downloaded the piece " + String.format("%5d", pieceIndex) + " from " + partner + ". ";
log += "Now the number of pieces it has is " + String.format("%5d", totalPieces) + ".";
synchronized(writer) {
writer.println(log);
writer.flush();
}
if(consoleDisplay)
System.out.println(log);
}
void logDownloadComplete() {
String currentTime = getCurrentTime();
String log = currentTime + ": Peer " + peerID + " has downloaded the complete file.";
synchronized(writer) {
writer.println(log);
writer.flush();
}
if(consoleDisplay)
System.out.println(log);
}
private String getCurrentTime() {
LocalDateTime time = LocalDateTime.now();
String currentTime = time.getYear() + "-" + String.format("%2s", time.getMonthValue()) + "-" + String.format("%2s", time.getDayOfMonth()) + " "
+ String.format("%2s", time.getHour()) + ":" + String.format("%2s", time.getMinute()) + ":" + String.format("%2s", time.getSecond());
return currentTime;
}
void toggleConsoleDisplay() {
consoleDisplay = (consoleDisplay) ? false : true;
}
void closeFile() {
writer.close();
}
}