/
kiwi_sched.py
66 lines (54 loc) · 1.97 KB
/
kiwi_sched.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
import sys
import json
import socket
import bases
from sys import maxsize
# this file contains:
# agent skeleton
# TODO:scheduling algorithms
class agent:
# fixed len msgs
MSGLEN = 2048
listenport = 2014
sendport = 2015
bindhost = '0.0.0.0'
states = {'idle':0, 'scheduling':1, 'communicating':2}
def __init__(self, name):
# name is the identifier
self.name = name
self.state = self.states['idle']
sndsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sndsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
recsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
recsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.sndsock = sndsock
self.recsock = recsock
def receive(self):
(bytes_info, cli_addr) = self.recsock.recvfrom(self.MSGLEN)
print ('receiving from', cli_addr)
return bytes_info
def json_decode(self, msgreceived):
decoded_msg = json.loads(msgreceived.decode('utf-8').strip())
# a dict of subgraph info
return decoded_msg
# reconstruct local taskgraph motif
def restore_motif(self, wfdict):
wf = bases.wfgraph('local_taskgraph')
for k,v in wfdict.items():
tmpmod = bases.mod(k, v['res'])
if v['host_id']!=None:
tmpmod.host_id = v['host_id']
wf.add(tmpmod)
for k,v in wfdict.items():
if len(v['suc'])>0:
for l,c in v['suc'].items():
if l in wfdict.keys():
bases.mod_join(wf.get(k), wf.get(l), c)
return wf
# msgtosend: a updated dict of subgraph info
def json_encode(self, msgtosend):
encoded_msg = bytes( json.dumps(msgtosend), encoding='utf-8' )
# a ready-to-send bytestring
return encoded_msg
def send(self, msg, desthost, port):
self.sndsock.sendto(msg, (desthost, port))