-
Notifications
You must be signed in to change notification settings - Fork 0
/
logWriting.java
executable file
·135 lines (120 loc) · 4.21 KB
/
logWriting.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
import java.io.*;
import java.util.*;
import java.text.*;
import java.util.concurrent.CopyOnWriteArrayList;
public class logWriting {
private int currentPeerID;
private FileWriter logFile;
private StringBuffer logInfo;
//constructor
public logWriting(int id) throws IOException {
currentPeerID = id;
String filenm = "log_peer_" + id + ".log";
logFile = new FileWriter(filenm);
}
private static String getCurrentTime() {
SimpleDateFormat time;
time = new SimpleDateFormat("[yyyy-MM-dd HH:mm:ss]: ");
return time.format(new Date());
}
public void connect (int peerID) throws IOException {
logInfo = new StringBuffer();
logInfo.append(getCurrentTime() + "Peer " + currentPeerID + " makes a connection to Peer " + peerID + "\n");
synchronized(logFile) {
logFile.write(logInfo.toString());
logFile.flush();
}
}
public void connected (int peerID) throws IOException {
logInfo = new StringBuffer();
logInfo.append(getCurrentTime() + "Peer " + currentPeerID + " is connected from Peer " + peerID + "\n");
synchronized(logFile) {
logFile.write(logInfo.toString());
logFile.flush();
}
}
public void changePreferredNeighbors (CopyOnWriteArrayList<Integer> preferNbList) throws IOException {
logInfo = new StringBuffer();
logInfo.append(getCurrentTime() + "Peer " + currentPeerID + " has the preferred neighbors ");
if (preferNbList.isEmpty()) {
logInfo.append("of null."+ "\n");
}
else {
for(int i=0;i<preferNbList.size();i++)
if (i == preferNbList.size()-1)
logInfo.append(preferNbList.get(i) + "."+ "\n");
else
logInfo.append(preferNbList.get(i) + ", ");
}
synchronized(logFile) {
logFile.write(logInfo.toString());
logFile.flush();
}
}
public void changeOptimisticNeighbor (int peerID) throws IOException {
logInfo = new StringBuffer();
logInfo.append(getCurrentTime() + "Peer " + currentPeerID + " has the optimistically unchoked neighbor " + peerID+ "\n");
synchronized(logFile) {
logFile.write(logInfo.toString());
logFile.flush();
}
}
public void unchokedNeighbor (int peerID) throws IOException {
logInfo = new StringBuffer();
logInfo.append(getCurrentTime() + "Peer " + currentPeerID + " is unchoked by " + peerID+ "\n");
synchronized(logFile) {
logFile.write(logInfo.toString());
logFile.flush();
}
}
public void chokedNeighbor (int peerID) throws IOException {
logInfo = new StringBuffer();
logInfo.append(getCurrentTime() + "Peer " + currentPeerID + " is choked by " + peerID+ "\n");
synchronized(logFile) {
logFile.write(logInfo.toString());
logFile.flush();
}
}
public void receiveHave (int peerID, int pieceIndex) throws IOException {
logInfo = new StringBuffer();
logInfo.append(getCurrentTime() + "Peer " + currentPeerID + " received the 'have' message from " + peerID
+ " for the piece " + pieceIndex+ "\n");
synchronized(logFile) {
logFile.write(logInfo.toString());
logFile.flush();
}
}
public void receiveInterested (int peerID) throws IOException {
logInfo = new StringBuffer();
logInfo.append(getCurrentTime() + "Peer " + currentPeerID + " received the 'interested' message from " + peerID+ "\n");
synchronized(logFile) {
logFile.write(logInfo.toString());
logFile.flush();
}
}
public void receiveNotInterested (int peerID) throws IOException {
logInfo = new StringBuffer();
logInfo.append(getCurrentTime() + "Peer " + currentPeerID + " received the 'not interested' message from " + peerID+ "\n");
synchronized(logFile) {
logFile.write(logInfo.toString());
logFile.flush();
}
}
public void downloadPiece (int peerID, int pieceIndex, int pieceNum) throws IOException {
logInfo = new StringBuffer();
logInfo.append(getCurrentTime() + "Peer " + currentPeerID + " has downloaded the piece " + pieceIndex
+ " from " + peerID + ". Now the number of pieces it has is " + pieceNum+ "\n");
synchronized(logFile) {
logFile.write(logInfo.toString());
logFile.flush();
}
}
public void completeDownload () throws IOException {
logInfo = new StringBuffer();
logInfo.append(getCurrentTime() + "Peer " + currentPeerID + " has downloaded the complete file."+ "\n");
synchronized(logFile) {
logFile.write(logInfo.toString());
logFile.flush();
}
}
}