/
queueservice.py
46 lines (36 loc) · 1.51 KB
/
queueservice.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
# CMPT 474 Spring 2014, Assignment 6
# Utilities for reading and writing tuples to the serverQ.py
# Core libraries
import json
import pdb
# Libraries that must be installed via pip
import requests
# Libraries provided with assignment
from vectorclock import VectorClock
# Distinguished label to indicate a clock list
CLOCK_CODE = 'CLOCK_LIST_XXX'
class Queue(object):
def __init__(self, port):
self.port = port
def get(self, channel):
resp = requests.get('http://localhost:'+str(self.port)+'/q/'+channel,
headers={'content-type': 'application/json'})
jresp = resp.json()
if len(jresp) > 0:
for k in jresp:
if (isinstance(jresp[k],dict) and
jresp[k].keys() == [CLOCK_CODE]):
jresp[k] = [VectorClock.fromDict(dc) for dc in jresp[k][CLOCK_CODE]]
return jresp
else:
return None
def put(self, channel, dct):
if not isinstance(dct, dict):
raise Exception('Message to send is not a dict')
for k in dct:
if (isinstance(dct[k],(list, tuple)) and
isinstance(dct[k][0], VectorClock)):
dct[k] = {CLOCK_CODE: [vc.asDict() for vc in dct[k]]}
res = requests.put('http://localhost:'+str(self.port)+'/q/'+channel,
data=json.dumps(dct),
headers={'content-type': 'application/json'})