|
1 | 1 | # coding=utf8 |
2 | 2 |
|
3 | 3 | import os |
| 4 | +import sys |
| 5 | +import signal |
| 6 | +import socket |
| 7 | +import asyncore |
| 8 | +import time |
| 9 | +import json |
| 10 | +import threading |
4 | 11 |
|
5 | | -def generate_seeds(n): |
| 12 | +import message |
6 | 13 |
|
| 14 | +#import net.service as service |
| 15 | +from net.service import * |
| 16 | + |
| 17 | +clients = {} |
| 18 | +ips_fixed = [] |
| 19 | +validation_public_keys = [] |
| 20 | +deploy_normal_network = False |
| 21 | + |
| 22 | +class client(asyncore.dispatcher_with_send): |
| 23 | + __host = None |
| 24 | + __buffer = '' |
| 25 | + def handle_close(self): |
| 26 | + if self.__host is not None: |
| 27 | + if self.__host in clients: |
| 28 | + del clients[self.__host] |
| 29 | + self.close() |
| 30 | + print 'close %s ' % self.__host |
| 31 | + |
| 32 | + def handle_read(self): |
| 33 | + global ips_fixed |
| 34 | + global validation_public_keys |
| 35 | + global deploy_normal_network |
| 36 | + data = self.recv(8192) |
| 37 | + try: |
| 38 | + jdata = json.loads(data) |
| 39 | + if jdata['cmd'] == 'join': |
| 40 | + |
| 41 | + if jdata.has_key('conf') \ |
| 42 | + and jdata['conf'].has_key('port_peer') \ |
| 43 | + and jdata['conf']['port_peer'].has_key('port'): |
| 44 | + ips_fixed.append('%s:%d' %(self.__host.split(':')[0], jdata['conf']['port_peer']['port'])) |
| 45 | + |
| 46 | + if jdata.has_key('conf') \ |
| 47 | + and jdata['conf'].has_key('validation') \ |
| 48 | + and jdata['conf']['validation'].has_key('validation_public_key'): |
| 49 | + validation_public_keys.append(jdata['conf']['validation']['validation_public_key']) |
| 50 | + |
| 51 | + self.send(message.success('join')) |
| 52 | + |
| 53 | + elif jdata['cmd'] == 'start': |
| 54 | + del clients[self.__host] |
| 55 | + deploy_normal_network = True |
| 56 | + self.send(message.success('start')) |
| 57 | + except ValueError as e: |
| 58 | + print 'Handle read data failure. %s' % e |
| 59 | + print data |
| 60 | + self.send(message.failure(e)) |
| 61 | + except keyError as e: |
| 62 | + print 'Handle read data failure. %s' % e |
| 63 | + print data |
| 64 | + self.send(message.failure(e)) |
| 65 | + |
| 66 | + def set_host(self, host): |
| 67 | + self.__host = host |
| 68 | + |
| 69 | + def sendmsg(self, msg): |
| 70 | + self.send(msg) |
| 71 | + |
| 72 | +class initNormalNetWorkHandler: |
| 73 | + def __init__(self): |
| 74 | + pass |
| 75 | + |
| 76 | + def dispatch(self, newClient): |
| 77 | + socket, addr = newClient |
| 78 | + print 'A new client come from %s' % repr(addr) |
| 79 | + c = client(socket) |
| 80 | + key = '%s:%d' % (addr[0], addr[1]) |
| 81 | + c.set_host(key) |
| 82 | + clients[key] = c |
| 83 | + |
| 84 | + |
| 85 | +def handler_signal(signum, frame): |
| 86 | + if signum == signal.SIGINT: |
| 87 | + sys.exit(0) |
| 88 | + |
| 89 | +class worker(threading.Thread): |
| 90 | + |
| 91 | + def __init__(self, name = 'worker_thread'): |
| 92 | + self._stopevent = threading.Event() |
| 93 | + self._sleepperiod = 0.1 |
| 94 | + threading.Thread.__init__(self, name = name) |
| 95 | + |
| 96 | + def run(self): |
| 97 | + global ips_fixed |
| 98 | + global validation_public_keys |
| 99 | + global deploy_normal_network |
| 100 | + while not self._stopevent.isSet(): |
| 101 | + if deploy_normal_network == True: |
| 102 | + for key, client in clients.iteritems(): |
| 103 | + ip = key.split(':')[0] |
| 104 | + ips = filter(lambda e: e.find(ip) == -1, ips_fixed) |
| 105 | + deployCmd = message.deployRequest(ips, validation_public_keys) |
| 106 | + client.sendmsg(deployCmd) |
| 107 | + deploy_normal_network = False |
| 108 | + self._stopevent.wait(self._sleepperiod) |
| 109 | + |
| 110 | + print '%s ends' % self.getName() |
| 111 | + |
| 112 | + |
| 113 | + def join(self, timeout = None): |
| 114 | + self._stopevent.set() |
| 115 | + threading.Thread.join(self, timeout) |
| 116 | + |
| 117 | +def init_normal_network(listen_tcp_port = 7670): |
| 118 | + signal.signal(signal.SIGINT, handler_signal) |
| 119 | + server = TCPServer(('0.0.0.0', listen_tcp_port), initNormalNetWorkHandler()) |
| 120 | + w = worker() |
| 121 | + w.start() |
| 122 | + |
| 123 | + print 'listen on %d' % listen_tcp_port |
| 124 | + |
| 125 | + server.run_forever() |
| 126 | + w.join() |
| 127 | + |
| 128 | + |
| 129 | + |
| 130 | +def test(): |
| 131 | + init_normal_network() |
| 132 | + |
| 133 | +if __name__ == '__main__': |
| 134 | + test() |
0 commit comments