Beispiel #1
0
def load():
    global g_ip, g_win_no, g_types, cln
    try:
        with open('setting.json', 'r') as f:
            g_ip, g_win_no, g_types = json.load(f)
        cln = rpc.RpcClient(g_ip, 8900)
    except:
        pass
Beispiel #2
0
def login():
	try:
		g_.cln=rpc.RpcClient(g_.server_address,9090)
		b_ok,res=g_.cln.login(g_.user_name,g_.cry_password)
		if not b_ok:
			msgbox(res)
			return 0
		g_.token,g_.user_name,g_.department=res
		return 1
	except:
		msgbox('无法连接到服务器')
		return 0
Beispiel #3
0
def login():
    global cln, token
    try:
        cln = rpc.RpcClient(server_address, 9090)
        b_ok, res = cln.login(user_name, cry_password)
        if not b_ok:
            msgbox(res)
            return 0
        token = res
        return 1
    except:
        msgbox('无法连接到服务器')
        return 0
Beispiel #4
0
def setproxies_worker(c):
	last_specs = list()
	while True:
		specs = list()
		nodes = db.node_get_all()
		for id, node in nodes.iteritems():
			if node['type'] == 'edge':
				addr = node.get('public-addr')
				if addr:
					specs.append('tcp://%s:5560' % addr)
		if last_specs != specs:
			logger.info('sending proxy list to origin')
			client = rpc.RpcClient(['tcp://%s:10100' % origin_host], context=zmq_context)
			client.sock.linger = 0
			args = dict()
			args['specs'] = specs
			client.call('set-grip-proxies', args)
			client.close()
			last_specs = specs

		time.sleep(2)
Beispiel #5
0
import sys
import rpc

client = rpc.RpcClient(['tcp://%s:10100' % sys.argv[1]])
print client.call('send', {'message': sys.argv[2]})
Beispiel #6
0
import __main__
import rpc
cln=rpc.RpcClient('127.0.0.1',8900)

def print_ticket():
    import os,office
    wd=office.Word(0)
    doc=wd.open(os.getcwd()+'\\ticket.doc')
    doc.raw.PrintOut()


def get_title():
    fun=__main__.exe_fun__['list_add_column']
    for n,x in enumerate(cln.get_title()):
        fun(n,*x)
    return 0

def new_number():
    nm,_id,pn,tp=__main__.stack__[:4]
    tp=tp[0].upper()
    cln.new_number(nm,_id,pn,tp)
    return 0

refresh_token=0
def refresh():
    global refresh_token
    tk,pcs=cln.refresh(refresh_token)
    if tk==refresh_token:
        return
    refresh_token=tk
    __main__.exe_fun__['clear_list']()
Beispiel #7
0
import zmq
import redis_ops
import rpc

zmq_context = zmq.Context()
db = redis_ops.RedisOps()

nodes = db.node_get_all()

for id, node in nodes.iteritems():
    if node['type'] != 'client':
        continue
    if node.get('public-addr'):
        print 'resetting %s' % id
        if 'count' in node:
            del node['count']
        if 'count-cur' in node:
            del node['count-cur']
        if 'edge' in node:
            del node['edge']
        client = rpc.RpcClient(['tcp://%s:10100' % node['public-addr']],
                               context=zmq_context)
        client.sock.linger = 0
        args = dict()
        args['base-uri'] = ''
        args['count'] = 0
        args['connect-host'] = ''
        client.call('setup-clients', args)
        client.close()
        db.node_update(id, node)
Beispiel #8
0
	return time.strftime('%Y.%m.%d %H:%M:%S')
#server data
g_pieces=[]
g_cnt=1
g_sessions=dict()

#main client service
import rpc
svr=rpc.RpcSvr('0.0.0.0',8900)

try:
	ip=open('notifyip.dat').read()
except:
	ip=input('notify ip:')
open('notifyip.dat','w').write(ip)
cln=rpc.RpcClient(ip,8800)


refresh_token=time.time()#change if data modified.
def save_data():
	global refresh_token
	refresh_token=time.time()
	with open('serialize.dat','w') as f:
		json.dump((g_pieces,g_cnt,g_sessions),f)

def load_data():
	global g_pieces,g_cnt,g_sessions
	try:
		with open('serialize.dat','r') as f:
			g_pieces,g_cnt,g_sessions=json.load(f)
	except:
Beispiel #9
0
def nodemanage_worker(c):
	out_sock = zmq_context.socket(zmq.PUB)
	out_sock.linger = 0
	out_sock.connect(update_spec)

	need_status = True
	while True:
		count = db.config_get_count()

		nodes = db.node_get_all()
		edge_up = 0
		edge_running = 0
		edge_have = 0
		client_up = 0
		client_running = 0
		client_have = 0
		for id, node in nodes.iteritems():
			if node['type'] == 'edge':
				edge_have += 1
				if node.get('private-addr'):
					edge_running += 1
				if 'ping' in node:
					edge_up += 1
			elif node['type'] == 'client':
				client_have += 1
				if node.get('private-addr'):
					client_running += 1
				if 'ping' in node:
					client_up += 1

		if count > 0:
			edge_need = ((count - 1) / edge_capacity) + 1
			client_need = ((count - 1) / client_capacity) + 1
		else:
			edge_need = 0
			client_need = 0

		if need_status:
			out_sock.send('update nodes')
			logger.info('edge: %d/%d/%d/%d, client: %d/%d/%d/%d'
				% (edge_up, edge_running, edge_have, edge_need,
				client_up, client_running, client_have, client_need))
			need_status = False

		# FIXME: balance removal
		remove_id = None
		if edge_have > edge_need:
			for id, node in nodes.iteritems():
				if node['type'] == 'edge':
					remove_id = id
					break
		elif client_have > client_need:
			for id, node in nodes.iteritems():
				if node['type'] == 'client':
					remove_id = id
					break
		#else:
		#	# see if there's an old node
		#	now = int(time.time() * 1000)
		#	for id, node in nodes.iteritems():
		#		if now >= node['created'] + (5 * 60 * 1000):
		#			remove_id = id
		#			logger.info('%s has expired' % remove_id)
		#			break
		if remove_id:
			node = nodes[remove_id]
			logger.info('removing %s' % remove_id)
			terminate_node(node['region'], node['instance-id'])
			db.node_remove(remove_id)
			# if removing an edge node, remove any client binding
			if node['type'] == 'edge':
				for cid, cnode in nodes.iteritems():
					if cnode['type'] != 'client':
						continue
					if cnode.get('edge') == remove_id:
						del cnode['edge']
						db.node_update(cid, cnode)
			need_status = True
			continue

		if edge_have < edge_need:
			logger.info('creating edge node...')
			if default_region:
				region = default_region
			else:
				region = pick_region('edge', nodes)
			instance = create_node('edge', region)
			node = dict()
			node['type'] = 'edge'
			node['region'] = region
			node['instance-id'] = instance.id
			node['created'] = int(time.time() * 1000)
			id = db.node_add('edge-', node)
			logger.info('done: id=%s, region=%s, ec2-instance=%s' % (id, region, instance.id))
			need_status = True
			continue

		if client_have < client_need:
			logger.info('creating client node...')
			if default_region:
				region = default_region
			else:
				region = pick_region('client', nodes)
			instance = create_node('client', region)
			node = dict()
			node['type'] = 'client'
			node['region'] = region
			node['instance-id'] = instance.id
			node['created'] = int(time.time() * 1000)
			id = db.node_add('client-', node)
			logger.info('done: id=%s, region=%s, ec2-instance=%s' % (id, region, instance.id))
			need_status = True
			continue

		updated = False
		for id, node in nodes.iteritems():
			if not node.get('private-addr'):
				instance = get_node(node['region'], node['instance-id'])
				if instance.state == 'running':
					logger.info('node %s running, %s' % (id, instance.ip_address))
					node['private-addr'] = instance.private_ip_address
					node['public-addr'] = instance.ip_address
					instance.add_tag('Name', id)
					db.node_update(id, node)
					updated = True
					break
		if updated:
			need_status = True
			continue

		# set client sizes and assign to edge nodes
		if edge_running == edge_need and client_running == client_need:
			num_client_nodes = 0
			for id, node in nodes.iteritems():
				if node['type'] == 'client':
					num_client_nodes += 1
			for n, id in enumerate(order_keys(nodes.keys())):
				node = nodes[id]
				if node['type'] != 'client':
					continue
				changed = False
				node_client_count = count / num_client_nodes
				if n < count % num_client_nodes:
					node_client_count += 1
				if node.get('count') != node_client_count:
					node['count'] = node_client_count
					changed = True
				if not node.get('edge'):
					# find a free edge
					eid = pick_edge(node['region'], nodes)
					if eid:
						node['edge'] = eid
						logger.info('mapping %s (%s) -> %s (%s)' % (id, node['region'], eid, nodes[eid]['region']))
						changed = True
				if changed:
					db.node_update(id, node)

		# apply client settings
		changed = False
		for id, node in nodes.iteritems():
			if node['type'] != 'client':
				continue
			if 'ping' not in node or not node.get('count') or not node.get('edge'):
				continue
			node_client_count = node['count']
			if node.get('count-cur') != node_client_count:
				logger.info('applying settings to %s' % id)
				enode = nodes[node['edge']]
				client = rpc.RpcClient(['tcp://%s:10100' % node['public-addr']], context=zmq_context)
				client.sock.linger = 0
				args = dict()
				args['base-uri'] = client_base_uri
				args['count'] = node_client_count
				args['connect-host'] = enode['public-addr']
				client.call('setup-clients', args)
				client.close()
				node['count-cur'] = node_client_count
				db.node_update(id, node)
				changed = True
				break
		if changed:
			continue

		# any node need to be pinged?
		origin_info = db.get_origin_info()
		if origin_info is None:
			origin_info = dict()
		now = int(time.time() * 1000)
		oldest_id = None
		oldest_last_ping = None
		for id, node in nodes.iteritems():
			if not node.get('public-addr'):
				continue
			if 'last-ping' not in node:
				oldest_id = id
				oldest_last_ping = -1
				break
			elif oldest_last_ping is None or node['last-ping'] < oldest_last_ping:
				oldest_id = id
				oldest_last_ping = node['last-ping']
		if oldest_last_ping != -1:
			if 'last-ping' not in origin_info:
				oldest_id = 'origin'
				oldest_last_ping = -1
			elif oldest_last_ping is None or origin_info['last-ping'] < oldest_last_ping:
				oldest_id = 'origin'
				oldest_last_ping = origin_info['last-ping']
		if oldest_id == 'origin':
			if 'last-ping' not in origin_info or now >= origin_info['last-ping'] + (5 * 1000):
				logger.debug('pinging %s' % origin_host)
				origin_info['last-ping'] = now
				db.set_origin_info(origin_info)
				changed = True
				client = rpc.RpcClient(['tcp://%s:10100' % origin_host], context=zmq_context)
				client.sock.linger = 0
				success = False
				try:
					client.call('ping')
					start_time = int(time.time() * 1000)
					client.call('ping')
					end_time = int(time.time() * 1000)
					success = True
				except:
					logger.debug('ping failed')
				client.close()
				if success:
					origin_info['ping'] = end_time - start_time
					db.set_origin_info(origin_info)
					logger.debug('ping time: %d' % origin_info['ping'])
		elif oldest_id:
			id = oldest_id
			node = nodes[id]
			if 'last-ping' not in node or now >= node['last-ping'] + (5 * 1000):
				logger.debug('pinging %s' % id)
				node['last-ping'] = now
				db.node_update(id, node)
				changed = True
				client = rpc.RpcClient(['tcp://%s:10100' % node['public-addr']], context=zmq_context)
				client.sock.linger = 0
				success = False
				try:
					client.call('ping')
					start_time = int(time.time() * 1000)
					ret = client.call('ping')
					end_time = int(time.time() * 1000)
					success = True
				except:
					logger.debug('ping failed')
				client.close()
				if success:
					had_ping = 'ping' in node
					node['ping'] = end_time - start_time
					if node['type'] == 'client':
						node['manager-id'] = ret['id']
					db.node_update(id, node)
					logger.debug('ping time: %d' % node['ping'])
					if not had_ping:
						need_status = True
		if changed:
			continue

		time.sleep(5)
Beispiel #10
0
def save():
    global g_ip, g_win_no, g_types, cln
    g_ip, g_win_no, g_types = __main__.stack__[:3]
    cln = rpc.RpcClient(g_ip, 8900)
    with open('setting.json', 'w') as f:
        json.dump((g_ip, g_win_no, g_types), f)
Beispiel #11
0
import __main__
import rpc
import json

g_ip = ''
g_win_no = ''
g_types = ''
cln = rpc.RpcClient(g_ip, 8900)


def load():
    global g_ip, g_win_no, g_types, cln
    try:
        with open('setting.json', 'r') as f:
            g_ip, g_win_no, g_types = json.load(f)
        cln = rpc.RpcClient(g_ip, 8900)
    except:
        pass


def save():
    global g_ip, g_win_no, g_types, cln
    g_ip, g_win_no, g_types = __main__.stack__[:3]
    cln = rpc.RpcClient(g_ip, 8900)
    with open('setting.json', 'w') as f:
        json.dump((g_ip, g_win_no, g_types), f)


load()