-
Notifications
You must be signed in to change notification settings - Fork 0
/
master3.py
executable file
·172 lines (153 loc) · 4.13 KB
/
master3.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
#!/usr/bin/python
import os
import sys
import ConfigParser
import tools
import socket
import json
import threading
import thread
import datetime
import time
import traceback
import string
from DBOperator import DBOperator
config_file = os.getcwd() + '/config.ini'
cf = ConfigParser.ConfigParser()
cf.read(config_file)
MIN_CLIENTS = int(cf.get('server','min_clients'),10)
TIMEOUT = int(cf.get('server','timeout'),10)
HOST = cf.get('server','ip')
PORT = int(cf.get('server','port'),10)
IP_STEP = int(cf.get('server','step'),10)
INTERVAL = int(cf.get('server','interval'),10)
LOG_FILE = os.path.join(os.getcwd(),cf.get('support','log_file'))
logger = tools.set_logger('master3.py',LOG_FILE)
def master():
global str_ip_start
str_ip_start = sys.argv[1]
global str_ip_end
str_ip_end = sys.argv[2]
global client_list
client_list = []
# global client_num
# client_num = 0
global start_time
start_time = tools.get_current_time()
global end_time
global t1
t1 = threading.Thread(target = serv, name = 'serv thread')
t1.setDaemon(1)
t1.start()
logger.info('Start serv thread.')
global t2
t2 = threading.Thread(target = allocate, name = 'allocate_ips')
t2.setDaemon(1)
t2.start()
logger.info('Start allocate thread.')
user_inter()
t1.join()
t2.join()
def user_inter():
while True:
instruction = raw_input()
if instruction == 'quit()':
logger.info('Master close.')
exit(0)
def allocate():
ip_start = tools.ip2long(str_ip_start)
ip_end = tools.ip2long(str_ip_end)
# client_num = len(client_list)
i = 0
if ip_start > ip_end:
logger.error('Invalid ip range.')
return
hosts = ip_end - ip_start + 1
while ip_end - ip_start > IP_STEP:
client_num = len(client_list)
if client_num <= 0:
time.sleep(INTERVAL)
continue
records = DBOperator().get_client_allocated_amount(client_list[i])
logger.debug('Records amount %d, client num %d.' % (records,client_num))
if records < IP_STEP*2:
#Insert allocate_queue
ip_list = tools.generate_ips2(ip_start,ip_start+IP_STEP)
DBOperator().insert_ips_to_allocate(ip_list,client_list[i])
logger.info('%s - %s allocated to %s.'%(ip_list[0],ip_list[-1],client_list[i]))
i = (i+1) % client_num
ip_start += IP_STEP
else:
time.sleep(INTERVAL)
i = (i+1)%client_num
continue
while ip_end - ip_start > 0:
records = DBOperator().get_allocate_queue_record_amount()
if records < client_num*IP_STEP*2 and client_num > 0:
#Insert allocate_queue
ip_list = tools.generate_ips2(ip_start,ip_end+1)
DBOperator().insert_ips_to_allocate(ip_list,client_list[i])
logger.info('%s - %s allocated to %s.'%(ip_list[0],ip_list[-1],client_list[i]))
i = (i+1) % client_num
ip_start = ip_end
else:
time.sleep(INTERVAL)
continue
end_time = tools.get_current_time()
print 'Time:%s , %s, Hosts: %d' %(start_time,end_time,hosts)
def serv():
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
s.bind((HOST,PORT))
s.listen(5)
while True:
logger.info('Ready to connect!')
try:
c,addr = s.accept()
logger.info('Got guest from %s' % (addr[0]) )
# print 'Got guest from ',addr
thread.start_new_thread(my_recv,(c,))
except:
traceback.print_exc()
logger.info('Time out.')
continue
def my_recv(socket2):
socket = socket2
try:
res = b''
res = res + socket.recv(4096)
data = json.loads(res)
except:
traceback.print_exc()
logger.error('Receive error.')
res = parse(data)
if len(res) > 0:
# status = res['result']
socket.send(pack_data(res))
def pack_data(data):
data = json.dumps(data)
res = ""
res = res + data
logger.debug('Send info is :' + res)
return res
def parse(data):
logger.debug('Received data is :' + str(data))
cmd = data['cmd']
if cmd == 'add_client':
client = data['client']
add_client(client)
result = {'cmd':'add_client','result':'success'}
elif cmd == 'del_client':
client = data['client']
del_client(client)
result = {'cmd':'del_client','result':'success'}
return result
def add_client(client):
client_list.append(client)
def del_client(client):
client_list.remove(client)
if __name__=="__main__":
try:
master()
except Exception,e:
print e