-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
180 lines (155 loc) · 5.4 KB
/
client.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
from concurrent import futures
import time
import grpc
import pr_pb2
import pr_pb2_grpc
import thread
import sys
import json
import socket
from pymongo import MongoClient
_ONE_DAY_IN_SECONDS = 60 * 60 * 24
CENTRAL_SERVER_IP = ""
ACCESS_POINT = ""
SELF_IP=[l for l in ([ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if not ip.startswith("127.")][:1], [[(s.connect(('8.8.8.8', 53)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]]) if l][0][0]
if len(sys.argv) < 2 :
print "ERROR : Enter the port for access point server...exiting"
exit()
port = sys.argv[1]
self_ip = str(SELF_IP)+":"+str(port)
class Client(pr_pb2_grpc.PublishTopicServicer):
def forwardBackup(self, request_iterator, context):
for request in request_iterator :
print "\nReceived new data..."
print request
dataDump.insert_one({"topic":request.topic,"data":request.data})
return pr_pb2.acknowledge(ack="Data received by the client...")
def serve():
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
pr_pb2_grpc.add_PublishTopicServicer_to_server(Client(), server)
server.add_insecure_port(self_ip)
server.start()
try:
while True:
time.sleep(_ONE_DAY_IN_SECONDS)
except KeyboardInterrupt:
server.stop(0)
def subscribe_topic(topic,self_ip):
if subscribedTopics.find({"topic":topic}).count() > 0 :
print "Already subscribed to the topic :",topic
else :
channel = grpc.insecure_channel(ACCESS_POINT)
stub = pr_pb2_grpc.PublishTopicStub(channel)
response = stub.subscribeRequest(pr_pb2.topicSubscribe(topic=topic,client_ip=self_ip))
subscribedTopics.insert_one({"topic":topic})
def push_topic(topic,data):
channel = grpc.insecure_channel(ACCESS_POINT)
stub = pr_pb2_grpc.PublishTopicStub(channel)
response = stub.publishRequest(pr_pb2.topicData(topic=topic, data=data))
print("Ack received: " + response.ack)
def get_front_ip():
channel = grpc.insecure_channel(CENTRAL_SERVER_IP)
stub = pr_pb2_grpc.PublishTopicStub(channel)
response = stub.getFrontIp(pr_pb2.empty())
print("Frontend server Ip alloted: " + response.ip)
if response.ip == "NONE" :
print "No frontend servers active ...extiting..."
exit()
return response.ip
def generateTopics(lst,client_ip):
for topic in lst :
yield pr_pb2.topicSubscribe(topic=topic,client_ip=client_ip)
if __name__ == '__main__':
thread.start_new_thread(serve,())
mongoClient = MongoClient("localhost", 27017)
mongoClient.drop_database('Client'+port)
db = mongoClient['Client'+port]
subscribedTopics = db["subscribedTopics"]
dataDump = db["dataDump"]
a = json.load(open("options","r"))
CENTRAL_SERVER_IP = a["virtualServer"]
ACCESS_POINT = get_front_ip()
while (True) :
print "Type 1 for publish\nType 2 for subscribe\nType 3 for unsubscribe\nType 4 for exit"
response = raw_input()
if response == "1" :
print "Enter topic"
topic = raw_input()
print "Enter data"
data = raw_input()
push_topic(topic,data)
elif response == "2" :
channel = grpc.insecure_channel(CENTRAL_SERVER_IP)
stub = pr_pb2_grpc.PublishTopicStub(channel)
responses = stub.querryTopics(pr_pb2.empty())
topicList = []
i = 0
for response in responses :
i+=1
topicList.append(response.topic)
cursor = subscribedTopics.find({})
subscribedTopicsList = []
for document in cursor:
subscribedTopicsList.append(document["topic"])
newTopicList = list(set(topicList) - set(subscribedTopicsList))
i=0
for topic in newTopicList :
print i,": ",topic
i+=1
if len(newTopicList) > 0 :
print "Select available unsubscribed topic from above choices :"
selectedNumber = raw_input()
try :
if int(selectedNumber) < len(newTopicList) :
subscribe_topic(newTopicList[int(selectedNumber)],self_ip)
else :
print "Invalid option selected ..."
except :
print "Invalid option selected ..."
else :
print "No new topics found ..."
elif response == "3" :
cursor = subscribedTopics.find({})
lst = []
i = 0
for document in cursor:
print i,": "+document["topic"]
i+=1
lst.append(document["topic"])
unsubscribeList = []
if len(lst) > 0 :
print "Select topics from above choices, seperated by spaces:"
selectedNumbers = raw_input().split()
for selectedNumber in selectedNumbers :
try :
if int(selectedNumber) < len(lst) :
unsubscribeList.append(str(lst[int(selectedNumber)]))
else :
print "Invalid options selected ..."
unsubscribeList = []
break
except :
unsubscribeList = []
print "Invalid options selected ..."
else :
print "No topics subscribed to ..."
if len(unsubscribeList) > 0 :
channel = grpc.insecure_channel(ACCESS_POINT)
stub = pr_pb2_grpc.PublishTopicStub(channel)
response = stub.unsubscribeRequest(generateTopics(unsubscribeList,str(SELF_IP)+":"+port))
for topic in unsubscribeList :
subscribedTopics.delete_one({"topic":topic})
print "unsubscribed from topics :",unsubscribeList
elif response == "4" :
cursor = subscribedTopics.find({})
lst = []
for document in cursor:
lst.append(document["topic"])
channel = grpc.insecure_channel(ACCESS_POINT)
stub = pr_pb2_grpc.PublishTopicStub(channel)
response = stub.unsubscribeRequest(generateTopics(lst,str(SELF_IP)+":"+port))
mongoClient.drop_database('Client'+port)
print "exiting now..."
exit()
else :
print "Invalid option selected, try again..."