-
Notifications
You must be signed in to change notification settings - Fork 0
/
naive_Cloudlet.py
69 lines (60 loc) · 1.5 KB
/
naive_Cloudlet.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
import time
import threading
import socket
import thread
import urllib2
mobile_list = {}
file = open("IP.txt", "r")
rpid = file.readline()
host = file.readline()
port = 12345
def factorial(num):
sum = 1
for x in xrange(1,num):
sum *=(x+1)
return sum
def processing(load):
print "Processing Load..."
time.sleep(15)
value = factorial(int(load))
print value
return value
def spl_return_req(connection1,value,t0):
fresult = processing(int(z))
connection1.send(value)
t1 = time.time() - t0
print "Time taken to process: " + str(t1)+ " ms"
connection1.send(t1)
def convert_procid(term):
term = term.strip('()')
t = term.split(',')
q = t[0].strip('\'')
p = q+':'+t[1].strip()
return p
s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host, port))
s.listen(10)
def mobile_recv(ss, procid):
while 1:
try:
data = ss.recv(1024)
except:
break
if not data:
break
t0 = time.time()
thread.start_new_thread( spl_return_req, (ss,data,t0, ) )
print("Disconnected...Removing Request...")
ss.close()
thread.exit()
while True:
mobile_conn, ipaddr = s.accept() # mobile connection
procid = convert_procid(str(ipaddr))
print ('Recieved connection from mobile with IP - ', procid)
mobile_conn.send('Dear Mobile user, you are connected!')
mobile_list[procid] = mobile_conn
try:
thread.start_new_thread( mobile_recv, (mobile_conn,procid,) )
except:
print "Error: unable to start thread"