-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimple-Distributed-Blackboard.repy
More file actions
executable file
·77 lines (61 loc) · 3.21 KB
/
Simple-Distributed-Blackboard.repy
File metadata and controls
executable file
·77 lines (61 loc) · 3.21 KB
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
#mycontext implementation of global variables
mycontext['text_area_comment'] = ''
mycontext['vessels'] = (['83.230.127.124','192.1.249.137','130.104.72.213','204.85.191.10','128.2.211.115','130.79.48.55','128.208.4.199','128.10.18.53','137.189.98.213'])
#create a list for the ip address of the vessels
remove_my_ip= mycontext['vessels'].index(getmyip())
del mycontext['vessels'][remove_my_ip]
#board function definition
def board(ip, port, sockobj, thiscommhandle, listencommhandle):
msgheader = sockobj.recv(1024) # Receive message,
# React depending on message type: HTTP GET or POST, or some other type of communication.
#display this board as a default when the browser is first opened
if msgheader.startswith( 'GET' ):
htmlresponse = """<html> <head>
<meta content="text/html; charset=utf-8" http-equiv="content-type">
<title>Blackboard Group 5</title> </head> <body><h2> Board Content</h2>
<p>"""+ mycontext['text_area_comment'] + """</p><br>
<h3>Submit to board<h3>
<textarea rows="4" cols="50" name="comment" form="usrform"></textarea>
<form action="" id="usrform" method="post"> <input type="submit"> </form></body> </html>"""
sockobj.send("HTTP/1.1 200 OK\r\nContent-type: text/html\r\n" "Content-length: %i\r\n\r\n%s" % (len(htmlresponse), htmlresponse))
stopcomm(thiscommhandle)
#display this board when a post is made.
elif msgheader.startswith( 'POST' ):
#extract the posted content from the msgheader
posted_comment= msgheader[msgheader.find('comment') + 8:]
#update the mycontxt comment variable with the posted comment and a break
mycontext['text_area_comment'] += posted_comment + '<br>'
#display the updated mycontxt comment variable in the browser
htmlresponse = """<html> <head>
<meta content="text/html; charset=utf-8" http-equiv="content-type">
<title>Blackboard Group 5</title> </head> <body><h2> Board Content</h2>
<p>"""+ mycontext['text_area_comment'] +"""</p><br>
<h3>Submit to board<h3>
<textarea rows="4" cols="50" name="comment" form="usrform"></textarea>
<form action="" id="usrform" method="post"> <input type="submit"> </form></body> </html>"""
sockobj.send("HTTP/1.1 200 OK\r\nContent-type: text/html\r\n" + \
"Content-length: %i\r\n\r\n%s" % (len(htmlresponse), htmlresponse))
#open tcp connection to my neigbors ip address and send the posted comment
for i in mycontext['vessels']:
sockobject = openconn(i, 63153)
sockobject.send(posted_comment)
stopcomm(thiscommhandle)
#if the sent message above is received on another browser, update the mycontxt comment variable with this msgheader
else:
mycontext['text_area_comment'] += msgheader + '<br>'
stopcomm(thiscommhandle)
#the program starts from here
if callfunc == 'initialize':
if len(callargs) > 1:
raise Exception("Too many call arguments")
# Running remotely (assuming that we pass input argument only remotely):
# whenever this vessel gets a connection on its IPaddress:Clearinghouseport it'll call function board
elif len(callargs) == 1:
port = int(callargs[0])
ip = getmyip()
# Running locally:
# whenever we get a connection on 127.0.0.1:12345 we'll call board
else:
port = 12345
ip = '127.0.0.1'
listencommhandle = waitforconn(ip,port,board)