-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGrpcNode.kt
More file actions
263 lines (223 loc) · 8.47 KB
/
Copy pathGrpcNode.kt
File metadata and controls
263 lines (223 loc) · 8.47 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package com.aditshah.distributed.infrastructure.node;
import com.aditshah.distributed.*
import com.aditshah.distributed.infrastructure.common.Coordinate
import com.aditshah.distributed.infrastructure.common.CoordinateArea
import com.aditshah.distributed.infrastructure.common.MapSharedInfo
import com.aditshah.distributed.infrastructure.common.WeightsMap
import com.google.api.kgax.grpc.ClientStreamingCall
import com.google.protobuf.Empty
import io.grpc.ManagedChannelBuilder
import kotlinx.coroutines.runBlocking
import kotlinx.io.core.Closeable
import kotlin.concurrent.thread
/**
* This class is an implementation of Node, which uses a gRPC communications backend to simulate the communication
* between nodes. The goal is to have the communications happen in the background as seamlessly as possible
*
*/
class GrpcNode(
private val startingLocation: Coordinate,
private val coordinateArea: CoordinateArea,
private val weightMap: WeightsMap,
private val host: String,
private val port: Int
) : Node {
private var isRegistered: Boolean = false
private val client = NodeClient()
private val info =
MapSharedInfo(coordinateArea, weightMap)
// private val logger = KotlinLogging.logger {}
private var stopped: Boolean = false
private var id = -1
/*
* This function connects the node to the server, receives an ID, sends its stating location, and subscribes to future
* updates to the Location and Weights, each in their own thread
*/
override fun registerNode(): Int {
client.connect()
id = client.registerNode()
client.setInitialWeights()
isRegistered = true;
putLocation(startingLocation);
thread { client.subscribeLocation() }
thread { client.subscribeWeights() }
return id;
}
override fun shutdownNode() {
stopped = true;
client.close();
}
class NodeNotRegisteredException(message: String = "") : RuntimeException()
/*
* The logic for this appears convoluted, but it exists to account for the diferrent posibilities if the id has
* not been assigned.
*/
override fun getID(): Int {
if (id == -1) {
if (!isRegistered) {
val e = NodeNotRegisteredException("Attempted to get ID, but node not registered")
// logger.error(e) { "Attempted to get ID, but node not registered" }
throw e
} else {
throw RuntimeException("node registered, but ID not assigned")
}
} else {
return id
}
}
override fun getAllIDs(): MutableSet<Int> {
return info.locationMap.keys
}
override fun getCoordinateArea(): CoordinateArea {
return coordinateArea
}
private fun checkRegistered() {
if (!isRegistered) throw NodeNotRegisteredException("")
}
override fun putLocation(coord: Coordinate) {
checkRegistered()
require(info.coordinateArea.contains(coord)) { "Location $coord not in area ${info.coordinateArea}" }
client.putLocation(coord)
info.putLocation(id, coord)
// logger.info { "Moving to $coord" }
}
override fun getLocation(id: Int): Coordinate {
return info.getLocation(id)
}
override fun getWeight(coord: Coordinate): Double {
return info.getWeight(coord)
}
override fun putWeight(coord: Coordinate, value: Double) {
checkRegistered();
info.putWeight(coord, value)
client.putWeight(coord)
}
override fun isStopped(): Boolean {
return stopped
}
override fun getInfo(): MapSharedInfo {
return info
}
/**
* This class has the gRPC calls to the Communication Server. They are in an inner class to avoid cluttering the main
* communication logic
*/
private inner class NodeClient : Closeable {
private lateinit var client: CommunicationServiceClient;
private lateinit var locCall: ClientStreamingCall<CoordinateIDMessage, Empty>;
private lateinit var weightCall: ClientStreamingCall<WeightMessage, Empty>
// private val client: NodeInfoServiceClient;
private var isConnected: Boolean = false;
fun connect() {
// logger.info { "Connecting to server at $host:$port" }
client = CommunicationServiceClient.create(
channel = ManagedChannelBuilder
.forAddress(host, port)
.usePlaintext()
.build()
)
// logger.info { "Connected to server" }
locCall = client.putLocation()
// logger.debug { "Location stream established" }
weightCall = client.putWeight()
// logger.debug { "Weight stream established" }
isConnected = true
}
fun registerNode(): Int {
// logger.info { "Registering node" }
return runBlocking {
val id = client.registerNode()
// logger.info { "Node registered. id = ${id.value}" }
id.value;
}
}
fun setInitialWeights() {
runBlocking {
client.setInitialWeights(
initialWeightsMessage {
val weightList = mutableListOf<WeightMessage>()
for ((coordinate, weight) in weightMap.map) {
weightList.add(weightMessage {
this.coordinate = coordinateMessage {
coordinate.also {
this.x = it.X
this.y = it.Y
this.z = it.Z
}
}
this.value = weight
})
}
this.weights = weightList
}
)
}
}
fun putLocation(coord: Coordinate) {
val ID = id;
// logger.debug { "Sending location $coord for id $id to server" }
val call = this.locCall;
runBlocking {
call.requests.send(
coordinateIDMessage {
this.id = nodeID { this.value = ID };
this.coordinate = coordinateMessage {
this.x = coord.X;
this.y = coord.Y;
this.z = coord.Z
}
}
)
}
}
fun subscribeLocation() {
val replies = client.subscribeLocations(nodeID { this.value = id })
.responses
runBlocking {
for (reply in replies) {
val id = reply.id.value
val coordinate = Coordinate(reply.coordinate)
// logger.debug { "Received location $coordinate from id $id" }
info.putLocation(
id, coordinate
)
}
}
}
fun putWeight(coord: Coordinate) {
val weight = info.getWeight(coord)
// logger.debug { "Putting weight $weight for coord $coord" }
val call = this.weightCall
runBlocking {
call.requests.send(
weightMessage {
this.coordinate = coordinateMessage {
this.x = coord.X
this.y = coord.Y
this.z = coord.Z
}
this.value = weight
}
)
}
}
fun subscribeWeights() {
val replies = client.subscribeWeights(nodeID { this.value = id })
.responses
runBlocking {
for (reply in replies) {
val coordinate = Coordinate(reply.coordinate)
val weight = reply.value
// logger.debug { "Got weight $weight for coordinate $coordinate" }
info.putWeight(
coordinate, weight
)
}
}
}
override fun close() {
client.shutdownChannel();
}
//fun toJson() = Json.stringify(serializer(), locationMap.toMap())
}
}