forked from szrlee/Mapreducer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
182 lines (154 loc) · 5.14 KB
/
client.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
180
181
import asynchat
import cPickle as pickle
import types
import socket
import asyncore
import sys
import marshal
import shelve
import reduceworker
import os
import thread
import time
import threading
class Client(asynchat.async_chat, object):
def __init__(self, base):
asynchat.async_chat.__init__(self)
self.buffer = []
self.set_terminator('\n\r')
self.globemapfn = self.reducefn = None
self.results_base = base
self.ac_in_buffer_size = 999999
def conn(self, server, port):
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.connect((server, port))
def collect_incoming_data(self, data):
self.buffer.append(data)
def found_terminator(self):
try:
command, length, pdata = (''.join(self.buffer)).split(':',2)
data = pickle.loads(pdata)
self.buffer = []
self.process_command(command, data)
self.set_terminator('\n\r')
except EOFError:
self.done(command, None)
def process_command(self, command, data=None):
commands = {
'mapfn': self.set_mapfn,
'reducefn': self.set_reducefn,
'map': self.call_mapfn,
'reduce': self.call_reducefn,
'done': self.done
}
if command in commands:
commands[command](command, data)
def set_mapfn(self, command, data):
self.mapfn = types.FunctionType(marshal.loads(data), globals(), 'mapfn')
def set_reducefn(self, command, data):
self.reducefn = types.FunctionType(marshal.loads(data), globals(), 'reducefn')
def call_mapfn(self, command, data):
results = {}
for k, v in self.mapfn(data[0], data[1]):
if k not in results:
results[k] = []
results[k].append(v)
K = str(data[0])
metex.acquire()
if data[0] in results.keys():
if not self.results_base[K]== results:
self.results_base[K] = results
else :
pass
else:
self.results_base[K] = results
metex.release()
self.send_command('mapdone', (data[0], results.keys()))
def call_reducefn(self, command, data):
reducer = reduceworker.reduceworker(data, self)
reducer.work()
def send_command(self, command, data):
if data:
pdata = pickle.dumps(data)
length = str(len(pdata))
self.push(command +':'+length+':'+pdata+'\n\r')
else:
self.push(command +':'+'0:'+'\n\r')
def done(self, command, data):
if os.path.isfile('results.dat'):
os.remove('results.dat')
self.close_when_done()
quit()
class peer_server(asyncore.dispatcher):
def __init__(self, port, base):
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.bind(('',port))
self.listen(5)
self.base = base
def handle_accept(self):
acception= self.accept()
if acception:
peer_conn, addr = acception
pconnect = peer_server_conn(peer_conn, self.base)
class peer_server_conn(asynchat.async_chat):
def __init__(self, conn, base):
asynchat.async_chat.__init__(self, conn)
self.buffer = []
self.set_terminator('\n\r')
self.base = base
def collect_incoming_data(self, data):
self.buffer.append(data)
def found_terminator(self):
command, length, pdata = (''.join(self.buffer)).split(':',2)
if int(length) == len(pdata):
data = pickle.loads(pdata)
self.buffer = []
if data:
self.process_command(command, data)
else:
print error
self.set_terminator('\n\r')
# def handle_close(self):
# self.close()
# def handle_error(self):
# self.close()
def send_command(self, command, data):
if data:
pdata = pickle.dumps(data)
length = str(len(pdata))
self.push(command +':'+length+':'+pdata+'\n\r')
else:
self.push(command +':'+'\n\r')
def process_command(self, command, data):
commands ={
'collect': self.collect
}
if command in commands:
commands[command](command, data)
def collect(self, command, data):
K = str(data[0])
collect_base = self.base
collect_values = []
if K in collect_base.keys():
collect_values.extend(collect_base[K][data[1]])
else:
print 'The key:%s isn\'t here'%K
self.send_command('collectdone', (data[0], collect_values))
def start_client(base):
client = Client(base)
client.conn('127.0.0.1', 20000)
metex = threading.RLock()
def main():
try:
base = shelve.open('results.dat', 'c')
peerserver = peer_server(21111, base)
for i in range(3):
# map = {}
thread.start_new_thread(start_client, (base,))
time.sleep(1)
asyncore.loop()
except EOFError:
quit()
if __name__ == '__main__':
main()