Skip to content

Commit ca35c85

Browse files
committed
start normal network
1 parent 3cc6f33 commit ca35c85

15 files changed

Lines changed: 560 additions & 7 deletions

chainCtr

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import time
1010

1111
import script.config as config
1212
import script.unit as unit
13+
import script.join as join
14+
import script.init as init
1315

1416
def print_usage():
1517
print 'usage'
@@ -22,6 +24,7 @@ def print_usage():
2224
print ' eg: stop [--fake]'
2325
print ' init [options] initial env'
2426
print ' eg: init [--fake]'
27+
print ' join --host host join chainsql network'
2528
print ' config --json key:value ... -- [options] modify config'
2629
print ' eg: config --json sync_db.type:mysql sync.db:ripple -- --id chainsql_id'
2730
print ' test test chainsqld network'
@@ -33,6 +36,7 @@ def print_usage():
3336
print ' --seeds n generate n seeds'
3437
print ' --json configs modify config'
3538
print ' --id id chainsq\'s id'
39+
print ' --host host specify host that we should join'
3640
print ' --wait wait for network running successfully'
3741

3842

@@ -105,13 +109,31 @@ def init_fake_network():
105109
unit.stop_chainsqled()
106110

107111
def init_normal_network():
108-
print 'not impl'
112+
pwd = os.getcwd()
113+
os.chdir('%s/bin' % pwd)
114+
115+
config.set_rpc_host('0.0.0.0:7788')
116+
config.set_rpc_admin_ip('0.0.0.0')
117+
118+
unit.stop_chainsqled()
119+
unit.execute_chainsqld()
120+
121+
init.init_normal_network()
122+
109123

110124
def handle_config(argv):
111125
chainsqld_id = None
112126
for idx in range(len(argv)):
113127
print argv[idx]
114128

129+
def handle_join(argv):
130+
for idx in range(2, len(argv)):
131+
if argv[idx] == '--host':
132+
idx += 1
133+
host = argv[idx]
134+
v = host.split(':')
135+
join.join((v[0], int(v[1])))
136+
115137
def handle_test(argv):
116138
pwd = os.getcwd()
117139
os.chdir('%s/test/js' % pwd)
@@ -180,7 +202,7 @@ def start_fake_network(wait):
180202

181203

182204
def start_normal_network(wait):
183-
print 'not impl'
205+
join.start()
184206

185207
def handle_init(argv):
186208
fake = False
@@ -222,6 +244,8 @@ def handle_commands(argv):
222244
handle_init(argv)
223245
elif cmd == 'config':
224246
handle_config(argv)
247+
elif cmd == 'join':
248+
handle_join(argv)
225249
elif cmd == 'test':
226250
handle_test(argv)
227251

script/config.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ def set_host(protocol, host):
3030
cmd = 'sed -i "%d c ip = %s" %s' %(number, vector[0], chainsql_cfg)
3131
os.system(cmd)
3232

33+
def set_admin_ip(protocol, ip):
34+
protocol = '\[%s\]' % protocol
35+
number = get_line_number(protocol, chainsql_cfg)
36+
if number != -1:
37+
number += 3
38+
cmd = 'sed -i "%d c admin = %s" %s' %(number, ip, chainsql_cfg)
39+
os.system(cmd)
40+
41+
3342
def set_rpc_host(host='127.0.0.1:5005'):
3443
set_host('port_rpc_admin_local', host);
3544

@@ -39,6 +48,12 @@ def set_peer_host(host='127.0.0.1:51235'):
3948
def set_ws_host(host='127.0.0.1:6006'):
4049
set_host('port_ws_admin_local', host);
4150

51+
def set_rpc_admin_ip(ip = '127.0.0.1'):
52+
set_admin_ip('port_rpc_admin_local', ip)
53+
54+
def set_ws_admin_ip(ip = '127.0.0.1'):
55+
set_admin_ip('port_ws_admin_local', ip)
56+
4257
def set_validation_public_key(key):
4358
number = get_line_number('\[validation_public_key\]', chainsql_cfg)
4459
if number == -1:

script/init.py

Lines changed: 129 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,134 @@
11
# coding=utf8
22

33
import os
4+
import sys
5+
import signal
6+
import socket
7+
import asyncore
8+
import time
9+
import json
10+
import threading
411

5-
def generate_seeds(n):
12+
import message
613

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

Comments
 (0)