-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.java
More file actions
141 lines (114 loc) · 2.65 KB
/
Node.java
File metadata and controls
141 lines (114 loc) · 2.65 KB
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
package com.ashishp.dc.assignment.cl.entity;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import org.jetbrains.annotations.NotNull;
import com.ashishp.dc.assignment.cl.handler.StorageHandler;
import com.ashishp.dc.assignment.cl.main.BankTransfer;
import com.google.common.base.MoreObjects;
/**
*
* @author <a href="http://www.linkedin.com/in/aashishpanchal">Aashish</a>
*
*/
public final class Node implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* Positive integer to determine position in the graph
*/
private final int id;
/**
* IP address of the server node
*/
@NotNull
private final String host;
/**
* Current state of the bank
*/
@NotNull
private final Item item;
/**
* Snapshot record of the bank
*/
@NotNull
private final Snapshot snapshot;
/**
* All known nodes in the graph, including itself
* <p>
* Map<NodeId, Host>
*/
@NotNull
private final Map<Integer, String> nodes = new HashMap<>();
public Node() {
this(0, "");
}
public Node(int id, @NotNull String host) {
this.id = id;
this.host = host;
item = new Item(BankTransfer.INITIAL_BALANCE);
snapshot = new Snapshot();
nodes.put(id, host);
}
public int getId() {
return id;
}
@NotNull
public Item getItem() {
return item;
}
@NotNull
public Snapshot getSnapshot() {
return snapshot;
}
/**
* Starts distributed snapshot by capturing local balance and waiting for marker
* from other nodes
*/
public void startSnapshotRecording() {
snapshot.startSnapshotRecording(id, item.getBalance(), nodes);
}
public void stopSnapshotRecording() {
StorageHandler.write(this);
snapshot.stopSnapshotRecording();
}
public void putNodes(@NotNull Map<Integer, String> nodes) {
this.nodes.putAll(nodes);
}
public void putNode(int id, @NotNull String host) {
nodes.put(id, host);
}
@NotNull
public String getHost() {
return host;
}
public Map<Integer, String> getNodes() {
return Collections.unmodifiableMap(nodes);
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
if (o instanceof Node) {
Node object = (Node) o;
return Objects.equals(id, object.id);
}
return false;
}
@Override
public int hashCode() {
return Objects.hash(id);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this).add("id", id).add("host", host).add("item", item)
.add("snapshot", snapshot).add("nodes", Arrays.toString(nodes.entrySet().toArray())).toString();
}
}