-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
executable file
·150 lines (106 loc) · 3.41 KB
/
server.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
#!/usr/bin/env python
from bottle import run, route, request, template, Bottle
from pymongo.connection import Connection
import json
import uuid
import pika
import random, string
try:
# Establishes the Connection with the MongoDB and Declares the Collation
col = Connection("localhost") # Connection 'c'
db = col['message_broker'] # Collection 'message_broker'
except Exception, e:
print str(e)
# Set the class Bottle
app = Bottle()
# Set the vector Alpha-Numeric for _id
# Create a Random '_id'
def randomId():
rand = ""
for i in range(1,24):
rand += random.choice(string.lowercase + string.digits)
return rand
# RabbitMQ Client
class RpcClient(object):
def __init__(self):
credentials = pika.PlainCredentials('guest', 'guest')
# Establishes the Connection with the RabbitMQ and Declares the Channel
self.queue_name = 'message_broker'
self.connection = pika.BlockingConnection(pika.ConnectionParameters('localhost', 5672, '/', credentials))
self.channel = self.connection.channel()
result = self.channel.queue_declare(exclusive=True)
self.callback_queue = result.method.queue
# Submit for the queue 'callback' and so receive RPC returns
self.channel.basic_consume(self.on_response, no_ack=True,
queue=self.callback_queue)
# For every response message, it checks if 'correlational_id' is being searched
# If it's true, it saves the response in 'self.response' and exits the loop
def on_response(self, ch, method, props, body):
if self.corr_id == props.correlation_id:
self.response = body
# Define the main function 'call' (it does the current RPC request)
def call(self, s):
self.response = None
# Generate a unique 'correlation_id' number and save it
# The callback function 'on_responde' will use this value to get the appropriate answer
self.corr_id = str(uuid.uuid4())
# Post the Request message
# With Two Properties: 'reply_to' and 'cerrelation_id'
self.channel.basic_publish(exchange='',
routing_key=self.queue_name,
properties=pika.BasicProperties(
reply_to=self.callback_queue,
correlation_id=self.corr_id,
),
body=str(s))
# Wait for the appropriate response to arrive
while self.response is None:
self.connection.process_data_events()
# Returns the response to the user
return str(self.response)
# Index
@app.route('/', method='GET')
def index():
print "Hello World"
return 0
# GET all data from MongoDB
@app.route('/lab/get/message_broker', method='GET')
def get_all_data():
# col = db[queue]
try:
cursor = db.message_broker.find()
for d in cursor:
print d
except Exception, e:
print str(e)
return 1
# GET data from MongoDB
@app.route('/lab/get/message_broker/<id>', method='GET')
def get_data(id):
# col = db[queue]
try:
data = db.message_broker.find( { "_id": id } )
print data
except Exception, e:
print str(e)
return json.dumps(data)
# SET data to MongoDB
@app.route('/lab/set/<queue>', method='POST')
def set_data(queue):
# Request the JSON from HTTP
data = request.json
# Add the Random ID to the JSON data
data['_id'] = randomId()
# Trasform the JSON data into 'Dictionary'
d = dict(data)
try:
# Create the RabbitMQ Client Object
mq_client = RpcClient()
mq_client.call(d)
# Save the 'data' JSON in Collation
col = db[queue]
col.insert(d)
except Exception, e:
print str(e)
return json.dumps(d)
run(app, host='0.0.0.0', port=8080, debug=True)