/
tough_call.py
182 lines (139 loc) · 4.64 KB
/
tough_call.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
173
174
175
176
177
178
179
180
181
182
# Routing python file will call server based on their availability
import random
import socket
import bluelet
import pickle
import video as vd
import i_am_testing as ml
import numpy as np
"""
class video(object):
def __init__(self,id,duration,bitrate,bitrate_video,height,width,frame_rate,codec):
self.id = id
self.duration = duration
self.bitrate = bitrate
self.bitrate_video = bitrate_video
self.height = height
self.width = width
self.frame_rate = frame_rate
self.codec = codec
def v_id(self):
return self.id
def v_codec(self):
return self.codec
"""
def echoer(conn):
data = yield conn.recv(1024)
data1.append(pickle.loads(data))
#print(l.id)
while data:
data = yield conn.recv(1024)
if not data:
break
data1.append(pickle.loads(data))
#print(l.id)
#Task types (templates for periodic tasks)
class TaskType(object):
#Constructor
def __init__(self,execution,name):
"""
self.period = period
self.release = release
self.deadline = deadline
"""
self.execution = execution
self.name = name
class Server:
def __init__(self,capacity,occ,port,name):
self.capacity = capacity
self.port = port
self.name = name.replace("\n", "")
self.occupied = occ
self.available = capacity-occ # initially available is capacity as none of the slot has been used yet
self.id = int(random.random() * 10000)
def avail(self):
return self.available
def port_num(self):
return self.port
def assign(self,time):
if self.available >= time: # weighted transcoding time is assigned to server
self.occupied += time
self.available -= time
return True
else:
return False
#Get name as Name + # + id ; Random id for every Server
def get_unique_name(self):
return str(self.name)
def capacity_cmp(self, other):
if self.available < other.available:
return 1
if self.available > other.available:
return -1
return 0
if __name__ == '__main__':
host = socket.gethostname() # '127.0.0.1' can also be used
serverfile = open('server.txt')
lines = serverfile.readlines()
#taskfile = open('test.txt')
#tlines = taskfile.readlines()
server_s = []
task_types = []
data1 = []
bluelet.run(bluelet.server('', 9001, echoer))
for line in lines:
line = line.split(' ')
for i in range (0,3):
line[i] = int(line[i])
if len(line) == 4:
name = line[3]
elif len(line) == 3:
name = 'Server'
else:
raise Exception('Invalid Server.txt file structure')
if int(line[0])>0:
server_s.append(Server(capacity=line[0], occ=line[1], port=line[2], name=name))
temp2 = []
for i in data1:
temp2.append(i.request())
y_rbf = ml.machine_learn(temp2)
for i in range(len(y_rbf)):
print str(y_rbf[i])
y_rbf = y_rbf.tolist()
task_id = []
task_time = []
for i in data1:
task_id.append(i.id)
for i in range(len(y_rbf)):
task_time.append(float(y_rbf[i]))
for i in range(len(task_id)):
task_types.append(TaskType(task_time[i],task_id[i]))
server_s = sorted(server_s, capacity_cmp)
#print "Sorted"
server_assign = server_s[0]
#sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
for tline in task_types:
for s in server_s:
if s.available > server_assign.available:
server_assign = s
if(server_assign.assign(tline.execution)):
#print tline.name+" assigned to Server : "+server_assign.get_unique_name()
#port = server_assign.port
print server_assign.name
print " assigned " +tline.name
port = server_assign.port
#9999
# Creating the socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#Connecting to socket
sock.connect((host, port))
#connect takes tuple of host and port
#data = sock.recv(1024)
#print "hello this is data "+data
msg =str(tline.execution) +" "+ tline.name
msg += "\n"
print msg
sock.send('HI! I am client. '+msg)
sock.close()
else:
print "All the servers are busy please try after some time"