forked from thomasjungblut/HamaStreaming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BSPPeer.py
179 lines (145 loc) · 5.21 KB
/
BSPPeer.py
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
"""
The BSPPeer handles the incoming protocol requests and forwards it to the BSP class.
Basically you can register the to be executed BSP class into this peer,
it will then callback the according methods.
"""
from BspJobConfiguration import BspJobConfiguration
from sys import stdout, stdin
from BinaryProtocol import BinaryProtocol as bp
class BSPPeer:
PROTOCOL_VERSION = 0
def __init__(self, bspClass):
self.config = BspJobConfiguration()
self.bspClass = bspClass;
self.initialize()
def initialize(self):
"""
INIT protocol works as follows:
START OP_CODE
PROTOCOL_NUMBER
SET_BSPJOB_CONF OP_CODE
NUMBER OF CONF ITEMS (#KEY + #VALUES)
N-LINES, where line is key and the following the value
"""
# parse our initial values
line = readLine()
# start code is the first
if line == bp.getProtocolString(bp.START):
# check the protocol compatibility
protocolNumber = int(readLine())
if protocolNumber != self.PROTOCOL_VERSION:
raise RuntimeError(
"Protocol version mismatch: Expected: " + str(self.PROTOCOL_VERSION) +
" but got: " + str(protocolNumber))
line = readLine()
# parse the configurations
if line == bp.getProtocolString(bp.SET_BSPJOB_CONF):
numberOfItems = readLine()
key = None
value = None
for i in range(0, int(numberOfItems), 2):
key = readLine()
value = readLine()
self.config.put(key, value)
self.ack(bp.START)
def send(self, peer, msg):
println(bp.getProtocolString(bp.SEND_MSG))
println(peer)
println(msg)
def getCurrentMessage(self):
println(bp.getProtocolString(bp.GET_MSG))
line = readLine()
# if no message is send it will send %%-1%%
if line == "%%-1%%":
return False
# TODO
# Problem reported by Roman:
# If I send any message of the length L, I receive the message with
# additional (L-1)/2 '^@' symbols after it.
# return line;
return line[:len(line)-len(line)//3]
def getAllMessages(self):
msgs = []
numMessages = self.getNumCurrentMessages()
for i in range(int(numMessages)):
msgs.append(self.getCurrentMessage())
return msgs
def getNumCurrentMessages(self):
println(bp.getProtocolString(bp.GET_MSG_COUNT))
return readLine()
def sync(self):
println(bp.getProtocolString(bp.SYNC))
# this should block now until we get a response
line = readLine()
if line != (bp.getProtocolString(bp.SYNC) + "_SUCCESS"):
raise RuntimeError(
"Barrier sync failed!")
def getSuperstepCount(self):
println(bp.getProtocolString(bp.GET_SUPERSTEP_COUNT))
return readLine()
def getPeerName(self):
return self.getPeerNameForIndex(-1)
def getPeerNameForIndex(self, index):
println(bp.getProtocolString(bp.GET_PEERNAME))
println(str(index));
return readLine()
def getPeerIndex(self):
println(bp.getProtocolString(bp.GET_PEER_INDEX))
return readLine()
def getAllPeerNames(self):
println(bp.getProtocolString(bp.GET_ALL_PEERNAME))
ln = readLine()
names = []
for i in range(int(ln)):
peerName = readLine()
names.append(peerName)
return names
def getNumPeers(self):
println(bp.getProtocolString(bp.GET_PEER_COUNT))
return readLine()
def clear(self):
println(bp.getProtocolString(bp.CLEAR))
def write(self, key, value):
println(bp.getProtocolString(bp.WRITE_KEYVALUE))
println(key)
println(value)
def readNext(self):
println(bp.getProtocolString(bp.READ_KEYVALUE))
line = readLine()
secondLine = readLine()
# if no message is send it will send %%-1%%
if line == "%%-1%%" and secondLine == "%%-1%%":
return False
return [line, secondLine]
def reopenInput(self):
println(bp.getProtocolString(bp.REOPEN_INPUT))
# TODO counter!
def runSetup(self):
line = readLine()
# start code is the first
if line.startswith(bp.getProtocolString(bp.RUN_SETUP)):
self.bspClass.setup(self);
self.ack(bp.RUN_SETUP)
def runBSP(self):
line = readLine()
# start code is the first
if line.startswith(bp.getProtocolString(bp.RUN_BSP)):
self.bspClass.bsp(self);
self.ack(bp.RUN_BSP)
def runCleanup(self):
line = readLine()
# start code is the first
if line.startswith(bp.getProtocolString(bp.RUN_CLEANUP)):
self.bspClass.cleanup(self);
self.ack(bp.RUN_CLEANUP)
def ack(self, code):
println(bp.getAckProtocolString(code))
def done(self):
println(bp.getProtocolString(bp.DONE))
def log(self, msg):
println(bp.getProtocolString(bp.LOG) + msg)
def readLine():
return stdin.readline().rstrip('\n')
def println(text):
print(text)
stdout.flush()