-
Notifications
You must be signed in to change notification settings - Fork 0
/
publisher.py
178 lines (139 loc) · 7.54 KB
/
publisher.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
#!/usr/bin/python
# requirements
import sys
from xml.etree import ElementTree as ET
import socket, select, string, sys
from termcolor import colored
import random
import uuid
from lib.SSAPLib import *
# basic info
node_id = str(uuid.uuid4())
heading = "\n" + colored("Publisher> ", "blue", attrs=["bold"])
# requests
join_requests = {}
leave_requests = {}
#main function
if __name__ == "__main__":
if(len(sys.argv) < 3) :
print 'Usage : python publisher.py vsib_hostname vsib_port realsib_hostname realsib_port'
sys.exit()
vsib_host = sys.argv[1]
vsib_port = int(sys.argv[2])
realsib_host = sys.argv[3]
realsib_port = int(sys.argv[4])
vs = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
vs.settimeout(2)
# connect to remote host
try:
vs.connect((vsib_host, vsib_port))
except :
print 'Unable to connect to the virtual sib'
sys.exit()
print 'Connected to remote host. Sending register request!'
# building and sending the register request
space_id = "X"
transaction_id = random.randint(0, 1000)
register_msg = SSAP_MESSAGE_REQUEST_TEMPLATE%(node_id,
space_id,
"REGISTER",
transaction_id, "")
vs.send(register_msg)
request_sockets = []
# connect to the real sib specified as a parameter
# rs = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# try :
# rs.connect((realsib_host, realsib_port))
# except :
# print 'Unable to connect to the real sib'
# sys.exit()
# # building and sending a join request to the real sib
# join_msg = SSAP_MESSAGE_TEMPLATE%(node_id,
# space_id,
# "JOIN",
# transaction_id, "")
# rs.send(join_msg)
# main loop
while 1:
socket_list = [sys.stdin, vs]
for i in request_sockets:
socket_list.append(i)
# Get the list sockets which are readable
read_sockets, write_sockets, error_sockets = select.select(socket_list , [], [])
for sock in read_sockets:
# incoming message from the real sib or from the virtual sib
if sock in read_sockets: # [vs, rs]:
try:
ssap_msg = sock.recv(4096)
if ssap_msg and ssap_msg != " ":
print colored("Ricevuto un messaggio ", "red", attrs=["bold"])
#ssap_list = ssap_msg.split("\n")
#ssap_msg = "".join(ssap_list)
# parse the ssap message
try:
root = ET.fromstring(ssap_msg)
info = {}
for child in root:
if child.attrib.has_key("name"):
k = child.tag + "_" + str(child.attrib["name"])
else:
k = child.tag
info[k] = child.text
#########################################################
##
## from the virtual sib to the real sib
##
#########################################################
if sock == vs:
print colored("Il messaggio ci e' stato inviato dalla vsib", "red", attrs=["bold"])
print colored("Il messaggio e' una ", "red", attrs=["bold"]) + str(info["transaction_type"]) + " " + str(info["message_type"])
### NEW PART ###
if info["message_type"] == "REQUEST": # and info["transaction_type"] != "SUBSCRIBE":
try :
print "Creazione connessione"
rs = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
rs.connect((realsib_host, realsib_port))
except :
print str(sys.exc_info())
print 'Unable to connect to the real sib'
sys.exit()
request_sockets.append(rs)
# if it's not a register confirmation, we have to forward the message
# sent by the virtual sib to the real sib
print heading + "Received the following " + colored(info["message_type"], "blue", attrs=["bold"]) + " message from the " + colored("VIRTUAL SIB", "blue", attrs=["bold"])
print ssap_msg
if not(info["transaction_type"] == "REGISTER"):
print "Invio alla sib reale"
rs.send(ssap_msg)
#########################################################
##
## from the real sib to the virtual sib
##
#########################################################
else: # sock in request_sockets:
print "Messaggio da una request socket (real sib)"
### NEW PART
vs.send(ssap_msg)
if info["message_type"] == "CONFIRM" and info["transaction_type"] != "SUBSCRIBE":
sock.close()
request_sockets.remove(sock)
# rs.close()
# rs = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# try :
# rs.connect((realsib_host, realsib_port))
# except :
# print 'Unable to connect to the real sib'
# sys.exit()
# if it's not a register confirmation, we have to forward the message
# sent by the virtual sib to the real sib
print heading + "Received the following " + colored(info["message_type"], "blue", attrs=["bold"]) + " message from the " + colored("REAL SIB", "blue", attrs=["bold"])
print ssap_msg
# if not(info["transaction_type"] in ["JOIN", "LEAVE"]):
print "send to vs"
# vs.send(ssap_msg)
except ET.ParseError:
print "Parse Error" + str(ssap_msg)
pass
except socket.error:
print "Socket Error"
pass