So I am trying to do this project for a class, where I simulate a distributed network of computers using sockets. And then each computer will write an event object that contains its ID and timestamp, and each computer will also read incoming event objects and compare the timestamp between its own and the one sent.
I thought I got everything working properly, until I look into my logs, and something seemed strange. I notice that the computer is reading the same object over and over, even though it should of received new events generated by other computers.
Here's the code for generating and writing events to the socket
private void generateEvents(Random rand, int numOfEvents) throws IOException {
int temp;
while (eventCount < numOfEvents) {
int choice = rand.nextInt(5);
synchronized (timestamp) {
if (choice == 0) {
temp = timestamp.get(identifier);
++temp;
timestamp.set(identifier, temp);
log.write(eventCount + ": " + "Sent to myself. " + timestamp.toString());
} else {
int randC = rand.nextInt(outputClients.size());
ClientSocket cc = outputClients.get(randC);
Event sentEvent = new Event(identifier, timestamp);
log.write(sentEvent.from + " " + sentEvent.timestamp);
cc.out.writeObject(sentEvent);
log.write(eventCount + ": " + "Sent to " + randC + ": " + timestamp.toString());
}
}
eventCount++;
// Add sleep for breathing room
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
This code is ran by the main computer thread.
Here's the code for reading the object
while (isAlive) {
Event event;
try {
event = (Event) in.readObject();
if (event != null) {
log.write("Reading from " + event.from + ": " + event.timestamp.toString());
log.write("Old timestamp: " + timestamp.toString());
synchronized (timestamp) {
int temp;
for (int i = 0; i < timestamp.size(); ++i) {
if (event.getTimestamp().get(i) > timestamp.get(i)) {
timestamp.set(i, event.getTimestamp().get(i));
}
}
temp = timestamp.get(event.getFromID());
++temp;
timestamp.set(event.getFromID(), temp);
}
log.write("Updated timestamp: " + timestamp.toString());
}
} catch (ClassNotFoundException e) {
// Do nothing
} catch (EOFException e) {
// Do nothing
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
This code is ran by separate threads that was created by the Computer class.
You can check out my full code here: https://github.com/nollidnosnhoj/time-table-exchange
So when I ran the program, I start seeing convenient patterns:
Computer is starting!
[0, 0, 0, 0, 0]
Connecting to other computers in network
Connected to computers. Start reading and writing events
0 [0, 0, 0, 0, 0]
0: Sent to 2: [0, 0, 0, 0, 0]
0 [0, 0, 0, 0, 0]
1: Sent to 2: [0, 0, 0, 0, 0]
Reading from 4: [0, 0, 0, 0, 0]
Old timestamp: [0, 0, 0, 0, 0]
Updated timestamp: [0, 0, 0, 0, 1]
0 [0, 0, 0, 0, 1]
2: Sent to 2: [0, 0, 0, 0, 1]
Reading from 1: [0, 0, 1, 0, 0]
Old timestamp: [0, 0, 0, 0, 1]
Updated timestamp: [0, 1, 1, 0, 1]
Reading from 3: [1, 1, 0, 2, 0]
Old timestamp: [0, 1, 1, 0, 1]
Updated timestamp: [1, 1, 1, 3, 1]
0 [1, 1, 1, 3, 1]
3: Sent to 1: [1, 1, 1, 3, 1]
0 [1, 1, 1, 3, 1]
4: Sent to 2: [1, 1, 1, 3, 1]
0 [1, 1, 1, 3, 1]
5: Sent to 3: [1, 1, 1, 3, 1]
0 [1, 1, 1, 3, 1]
6: Sent to 3: [1, 1, 1, 3, 1]
0 [1, 1, 1, 3, 1]
7: Sent to 1: [1, 1, 1, 3, 1]
Reading from 4: [0, 0, 0, 0, 0]
Old timestamp: [1, 1, 1, 3, 1]
Updated timestamp: [1, 1, 1, 3, 2]
0 [1, 1, 1, 3, 2]
8: Sent to 2: [1, 1, 1, 3, 2]
Reading from 1: [0, 0, 1, 0, 0]
Old timestamp: [1, 1, 1, 3, 2]
Updated timestamp: [1, 2, 1, 3, 2]
0 [1, 2, 1, 3, 2]
9: Sent to 1: [1, 2, 1, 3, 2]
10: Sent to myself. [2, 2, 1, 3, 2]
11: Sent to myself. [3, 2, 1, 3, 2]
0 [3, 2, 1, 3, 2]
12: Sent to 2: [3, 2, 1, 3, 2]
Reading from 2: [3, 4, 3, 5, 2]
Old timestamp: [3, 2, 1, 3, 2]
Updated timestamp: [3, 4, 4, 5, 2]
0 [3, 4, 4, 5, 2]
13: Sent to 2: [3, 4, 4, 5, 2]
14: Sent to myself. [4, 4, 4, 5, 2]
Reading from 4: [0, 0, 0, 0, 0]
Old timestamp: [4, 4, 4, 5, 2]
Updated timestamp: [4, 4, 4, 5, 3]
0 [4, 4, 4, 5, 3]
15: Sent to 1: [4, 4, 4, 5, 3]
Reading from 1: [0, 0, 1, 0, 0]
Old timestamp: [4, 4, 4, 5, 3]
Updated timestamp: [4, 5, 4, 5, 3]
0 [4, 5, 4, 5, 3]
16: Sent to 3: [4, 5, 4, 5, 3]
0 [4, 5, 4, 5, 3]
17: Sent to 0: [4, 5, 4, 5, 3]
0 [4, 5, 4, 5, 3]
18: Sent to 1: [4, 5, 4, 5, 3]
Reading from 2: [3, 4, 3, 5, 2]
Old timestamp: [4, 5, 4, 5, 3]
Updated timestamp: [4, 5, 5, 5, 3]
0 [4, 5, 5, 5, 3]
19: Sent to 3: [4, 5, 5, 5, 3]
20: Sent to myself. [5, 5, 5, 5, 3]
Reading from 3: [1, 1, 0, 2, 0]
Old timestamp: [5, 5, 5, 5, 3]
Updated timestamp: [5, 5, 5, 6, 3]
0 [5, 5, 5, 6, 3]
21: Sent to 2: [5, 5, 5, 6, 3]
0 [5, 5, 5, 6, 3]
22: Sent to 1: [5, 5, 5, 6, 3]
Reading from 4: [0, 0, 0, 0, 0]
Old timestamp: [5, 5, 5, 6, 3]
Updated timestamp: [5, 5, 5, 6, 4]
Reading from 1: [0, 0, 1, 0, 0]
Old timestamp: [5, 5, 5, 6, 4]
Updated timestamp: [5, 6, 5, 6, 4]
23: Sent to myself. [6, 6, 5, 6, 4]
0 [6, 6, 5, 6, 4]
24: Sent to 3: [6, 6, 5, 6, 4]
0 [6, 6, 5, 6, 4]
25: Sent to 3: [6, 6, 5, 6, 4]
0 [6, 6, 5, 6, 4]
26: Sent to 3: [6, 6, 5, 6, 4]
Reading from 2: [3, 4, 3, 5, 2]
Old timestamp: [6, 6, 5, 6, 4]
Updated timestamp: [6, 6, 6, 6, 4]
0 [6, 6, 6, 6, 4]
27: Sent to 1: [6, 6, 6, 6, 4]
Reading from 3: [1, 1, 0, 2, 0]
Old timestamp: [6, 6, 6, 6, 4]
Updated timestamp: [6, 6, 6, 7, 4]
0 [6, 6, 6, 7, 4]
28: Sent to 1: [6, 6, 6, 7, 4]
0 [6, 6, 6, 7, 4]
29: Sent to 2: [6, 6, 6, 7, 4]
Reading from 4: [0, 0, 0, 0, 0]
Old timestamp: [6, 6, 6, 7, 4]
Updated timestamp: [6, 6, 6, 7, 5]
0 [6, 6, 6, 7, 5]
30: Sent to 2: [6, 6, 6, 7, 5]
Reading from 1: [0, 0, 1, 0, 0]
Old timestamp: [6, 6, 6, 7, 5]
Updated timestamp: [6, 7, 6, 7, 5]
0 [6, 7, 6, 7, 5]
31: Sent to 1: [6, 7, 6, 7, 5]
32: Sent to myself. [7, 7, 6, 7, 5]
0 [7, 7, 6, 7, 5]
33: Sent to 0: [7, 7, 6, 7, 5]
Reading from 2: [3, 4, 3, 5, 2]
Old timestamp: [7, 7, 6, 7, 5]
Updated timestamp: [7, 7, 7, 7, 5]
0 [7, 7, 7, 7, 5]
34: Sent to 1: [7, 7, 7, 7, 5]
0 [7, 7, 7, 7, 5]
35: Sent to 1: [7, 7, 7, 7, 5]
Reading from 3: [1, 1, 0, 2, 0]
Old timestamp: [7, 7, 7, 7, 5]
Updated timestamp: [7, 7, 7, 8, 5]
0 [7, 7, 7, 8, 5]
36: Sent to 1: [7, 7, 7, 8, 5]
0 [7, 7, 7, 8, 5]
37: Sent to 1: [7, 7, 7, 8, 5]
Reading from 4: [0, 0, 0, 0, 0]
Old timestamp: [7, 7, 7, 8, 5]
Updated timestamp: [7, 7, 7, 8, 6]
Reading from 1: [0, 0, 1, 0, 0]
Old timestamp: [7, 7, 7, 8, 6]
Updated timestamp: [7, 8, 7, 8, 6]
0 [7, 8, 7, 8, 6]
38: Sent to 0: [7, 8, 7, 8, 6]
0 [7, 8, 7, 8, 6]
39: Sent to 2: [7, 8, 7, 8, 6]
0 [7, 8, 7, 8, 6]
40: Sent to 3: [7, 8, 7, 8, 6]
0 [7, 8, 7, 8, 6]
41: Sent to 3: [7, 8, 7, 8, 6]
Reading from 2: [3, 4, 3, 5, 2]
Old timestamp: [7, 8, 7, 8, 6]
Updated timestamp: [7, 8, 8, 8, 6]
42: Sent to myself. [8, 8, 8, 8, 6]
Reading from 3: [1, 1, 0, 2, 0]
Old timestamp: [8, 8, 8, 8, 6]
Updated timestamp: [8, 8, 8, 9, 6]
0 [8, 8, 8, 9, 6]
43: Sent to 3: [8, 8, 8, 9, 6]
0 [8, 8, 8, 9, 6]
44: Sent to 2: [8, 8, 8, 9, 6]
Reading from 4: [0, 0, 0, 0, 0]
Old timestamp: [8, 8, 8, 9, 6]
Updated timestamp: [8, 8, 8, 9, 7]
0 [8, 8, 8, 9, 7]
45: Sent to 0: [8, 8, 8, 9, 7]
Reading from 1: [0, 0, 1, 0, 0]
Old timestamp: [8, 8, 8, 9, 7]
Updated timestamp: [8, 9, 8, 9, 7]
0 [8, 9, 8, 9, 7]
46: Sent to 3: [8, 9, 8, 9, 7]
47: Sent to myself. [9, 9, 8, 9, 7]
0 [9, 9, 8, 9, 7]
48: Sent to 3: [9, 9, 8, 9, 7]
Reading from 2: [3, 4, 3, 5, 2]
Old timestamp: [9, 9, 8, 9, 7]
Updated timestamp: [9, 9, 9, 9, 7]
0 [9, 9, 9, 9, 7]
49: Sent to 0: [9, 9, 9, 9, 7]
0 [9, 9, 9, 9, 7]
50: Sent to 0: [9, 9, 9, 9, 7]
Reading from 3: [1, 1, 0, 2, 0]
Old timestamp: [9, 9, 9, 9, 7]
Updated timestamp: [9, 9, 9, 10, 7]
51: Sent to myself. [10, 9, 9, 10, 7]
0 [10, 9, 9, 10, 7]
52: Sent to 2: [10, 9, 9, 10, 7]
Reading from 4: [0, 0, 0, 0, 0]
Old timestamp: [10, 9, 9, 10, 7]
Updated timestamp: [10, 9, 9, 10, 8]
Reading from 1: [0, 0, 1, 0, 0]
Old timestamp: [10, 9, 9, 10, 8]
Updated timestamp: [10, 10, 9, 10, 8]
53: Sent to myself. [11, 10, 9, 10, 8]
0 [11, 10, 9, 10, 8]
54: Sent to 3: [11, 10, 9, 10, 8]
0 [11, 10, 9, 10, 8]
55: Sent to 1: [11, 10, 9, 10, 8]
56: Sent to myself. [12, 10, 9, 10, 8]
Reading from 2: [3, 4, 3, 5, 2]
Old timestamp: [12, 10, 9, 10, 8]
Updated timestamp: [12, 10, 10, 10, 8]
0 [12, 10, 10, 10, 8]
57: Sent to 1: [12, 10, 10, 10, 8]
0 [12, 10, 10, 10, 8]
58: Sent to 2: [12, 10, 10, 10, 8]
Reading from 3: [1, 1, 0, 2, 0]
Old timestamp: [13, 10, 10, 10, 8]
Reading from 2: [3, 4, 3, 5, 2]
Old timestamp: [13, 10, 10, 10, 8]
Reading from 1: [0, 0, 1, 0, 0]
Old timestamp: [13, 10, 10, 10, 8]
Reading from 4: [0, 0, 0, 0, 0]
Old timestamp: [13, 10, 10, 10, 8]
59: Sent to myself. [13, 10, 10, 10, 8]
Updated timestamp: [13, 10, 10, 10, 9]
Updated timestamp: [13, 11, 10, 10, 9]
Updated timestamp: [13, 11, 11, 11, 9]
Updated timestamp: [13, 11, 11, 11, 9]
0 [13, 11, 11, 11, 9]
60: Sent to 3: [13, 11, 11, 11, 9]
0 [13, 11, 11, 11, 9]
61: Sent to 0: [13, 11, 11, 11, 9]
0 [13, 11, 11, 11, 9]
62: Sent to 2: [13, 11, 11, 11, 9]
0 [13, 11, 11, 11, 9]
63: Sent to 0: [13, 11, 11, 11, 9]
0 [13, 11, 11, 11, 9]
64: Sent to 2: [13, 11, 11, 11, 9]
Reading from 3: [1, 1, 0, 2, 0]
Old timestamp: [13, 11, 11, 11, 9]
Updated timestamp: [13, 11, 11, 12, 9]
0 [13, 11, 11, 12, 9]
65: Sent to 2: [13, 11, 11, 12, 9]
Reading from 4: [0, 0, 0, 0, 0]
Old timestamp: [13, 11, 11, 12, 9]
Updated timestamp: [13, 11, 11, 12, 10]
Reading from 1: [0, 0, 1, 0, 0]
Old timestamp: [13, 11, 11, 12, 10]
Updated timestamp: [13, 12, 11, 12, 10]
Reading from 2: [3, 4, 3, 5, 2]
Old timestamp: [13, 12, 11, 12, 10]
Updated timestamp: [13, 12, 12, 12, 10]
0 [13, 12, 12, 12, 10]
66: Sent to 2: [13, 12, 12, 12, 10]
0 [13, 12, 12, 12, 10]
67: Sent to 1: [13, 12, 12, 12, 10]
0 [13, 12, 12, 12, 10]
68: Sent to 2: [13, 12, 12, 12, 10]
69: Sent to myself. [14, 12, 12, 12, 10]
0 [14, 12, 12, 12, 10]
70: Sent to 1: [14, 12, 12, 12, 10]
0 [14, 12, 12, 12, 10]
71: Sent to 3: [14, 12, 12, 12, 10]
Reading from 3: [1, 1, 0, 2, 0]
Old timestamp: [14, 12, 12, 12, 10]
Updated timestamp: [14, 12, 12, 13, 10]
0 [14, 12, 12, 13, 10]
72: Sent to 2: [14, 12, 12, 13, 10]
Reading from 4: [0, 0, 0, 0, 0]
Old timestamp: [14, 12, 12, 13, 10]
Updated timestamp: [14, 12, 12, 13, 11]
Reading from 2: [3, 4, 3, 5, 2]
Old timestamp: [14, 12, 12, 13, 11]
Updated timestamp: [14, 12, 13, 13, 11]
Reading from 1: [0, 0, 1, 0, 0]
Old timestamp: [14, 12, 13, 13, 11]
Updated timestamp: [14, 13, 13, 13, 11]
0 [14, 13, 13, 13, 11]
73: Sent to 2: [14, 13, 13, 13, 11]
0 [14, 13, 13, 13, 11]
74: Sent to 2: [14, 13, 13, 13, 11]
0 [14, 13, 13, 13, 11]
75: Sent to 1: [14, 13, 13, 13, 11]
76: Sent to myself. [15, 13, 13, 13, 11]
77: Sent to myself. [16, 13, 13, 13, 11]
0 [16, 13, 13, 13, 11]
78: Sent to 0: [16, 13, 13, 13, 11]
0 [16, 13, 13, 13, 11]
79: Sent to 2: [16, 13, 13, 13, 11]
Reading from 3: [1, 1, 0, 2, 0]
Old timestamp: [16, 13, 13, 13, 11]
Updated timestamp: [16, 13, 13, 14, 11]
0 [16, 13, 13, 14, 11]
80: Sent to 0: [16, 13, 13, 14, 11]
Reading from 4: [0, 0, 0, 0, 0]
Old timestamp: [16, 13, 13, 14, 11]
Updated timestamp: [16, 13, 13, 14, 12]
Reading from 2: [3, 4, 3, 5, 2]
Old timestamp: [16, 13, 13, 14, 12]
Updated timestamp: [16, 13, 14, 14, 12]
Reading from 1: [0, 0, 1, 0, 0]
Old timestamp: [16, 13, 14, 14, 12]
Updated timestamp: [16, 14, 14, 14, 12]
0 [16, 14, 14, 14, 12]
81: Sent to 1: [16, 14, 14, 14, 12]
0 [16, 14, 14, 14, 12]
82: Sent to 2: [16, 14, 14, 14, 12]
0 [16, 14, 14, 14, 12]
83: Sent to 3: [16, 14, 14, 14, 12]
84: Sent to myself. [17, 14, 14, 14, 12]
85: Sent to myself. [18, 14, 14, 14, 12]
0 [18, 14, 14, 14, 12]
86: Sent to 2: [18, 14, 14, 14, 12]
0 [18, 14, 14, 14, 12]
87: Sent to 1: [18, 14, 14, 14, 12]
Reading from 3: [1, 1, 0, 2, 0]
Old timestamp: [18, 14, 14, 14, 12]
Updated timestamp: [18, 14, 14, 15, 12]
Reading from 4: [0, 0, 0, 0, 0]
Old timestamp: [18, 14, 14, 15, 12]
Updated timestamp: [18, 14, 14, 15, 13]
Reading from 2: [3, 4, 3, 5, 2]
Old timestamp: [18, 14, 14, 15, 13]
Updated timestamp: [18, 14, 15, 15, 13]
Reading from 1: [0, 0, 1, 0, 0]
Old timestamp: [18, 14, 15, 15, 13]
Updated timestamp: [18, 15, 15, 15, 13]
0 [18, 15, 15, 15, 13]
88: Sent to 1: [18, 15, 15, 15, 13]
0 [18, 15, 15, 15, 13]
89: Sent to 1: [18, 15, 15, 15, 13]
0 [18, 15, 15, 15, 13]
90: Sent to 0: [18, 15, 15, 15, 13]
0 [18, 15, 15, 15, 13]
91: Sent to 1: [18, 15, 15, 15, 13]
0 [18, 15, 15, 15, 13]
92: Sent to 1: [18, 15, 15, 15, 13]
0 [18, 15, 15, 15, 13]
93: Sent to 3: [18, 15, 15, 15, 13]
0 [18, 15, 15, 15, 13]
94: Sent to 1: [18, 15, 15, 15, 13]
Reading from 3: [1, 1, 0, 2, 0]
Old timestamp: [18, 15, 15, 15, 13]
Updated timestamp: [18, 15, 15, 16, 13]
0 [18, 15, 15, 16, 13]
95: Sent to 0: [18, 15, 15, 16, 13]
Reading from 4: [0, 0, 0, 0, 0]
Old timestamp: [18, 15, 15, 16, 13]
Updated timestamp: [18, 15, 15, 16, 14]
Reading from 2: [3, 4, 3, 5, 2]
Old timestamp: [18, 15, 15, 16, 14]
Updated timestamp: [18, 15, 16, 16, 14]
Reading from 1: [0, 0, 1, 0, 0]
Old timestamp: [18, 15, 16, 16, 14]
Updated timestamp: [18, 16, 16, 16, 14]
96: Sent to myself. [19, 16, 16, 16, 14]
0 [19, 16, 16, 16, 14]
97: Sent to 2: [19, 16, 16, 16, 14]
0 [19, 16, 16, 16, 14]
98: Sent to 1: [19, 16, 16, 16, 14]
0 [19, 16, 16, 16, 14]
99: Sent to 2: [19, 16, 16, 16, 14]
Finished generating events
Reading from 3: [1, 1, 0, 2, 0]
Old timestamp: [19, 16, 16, 16, 14]
Updated timestamp: [19, 16, 16, 17, 14]
Reading from 4: [0, 0, 0, 0, 0]
Old timestamp: [19, 16, 16, 17, 14]
Updated timestamp: [19, 16, 16, 17, 15]
Reading from 2: [3, 4, 3, 5, 2]
Old timestamp: [19, 16, 16, 17, 15]
Updated timestamp: [19, 16, 17, 17, 15]
Reading from 1: [0, 0, 1, 0, 0]
Old timestamp: [19, 16, 17, 17, 15]
Updated timestamp: [19, 17, 17, 17, 15]
Notifying controller
Reading from 2: [3, 4, 3, 5, 2]
Old timestamp: [19, 17, 17, 17, 15]
Updated timestamp: [19, 17, 18, 17, 15]
Reading from 3: [1, 1, 0, 2, 0]
Old timestamp: [19, 17, 18, 17, 15]
Updated timestamp: [19, 17, 18, 18, 15]
Continuing reading...
Reading from 4: [0, 0, 0, 0, 0]
Old timestamp: [19, 17, 18, 18, 15]
Updated timestamp: [19, 17, 18, 18, 16]
Computer is tearing down.
Just find all the statement that starts with "Reading from" and you will see the pattern.
I assume that this has to do something with the object writing into a socket, and the reading the wrong data from the socket. I honestly I have no idea, and this is just getting super frustrating for me.
If you have any questions, I will definitely answer them!
Found the solution to my problem. I have to add the objectoutputstream reset method before I write my object into the output stream, so that it resets the stream and does not backtrack from previous objects.
You can read more about it here: Java socket/serialization, object won't update
The power of researching for the solution!