Esempio n. 1
0
 def test_roundtrip_format_unicode(self):
     for _ in xrange(500):
         v = get_random_object(unicode=True)
         self.assertEqual(
             v, tnetstring.loads(tnetstring.dumps(v, "utf8"), "utf8"))
         self.assertEqual((v, ""),
                          tnetstring.pop(tnetstring.dumps(v, "utf16"),
                                         "utf16"))
Esempio n. 2
0
def thrash_tnetstring():
    for obj, tns, json, msh in TESTS:
        if TEST_DUMP_ONLY:
            tnetstring.dumps(obj)
        elif TEST_LOAD_ONLY:
            assert tnetstring.loads(tns) == obj
        else:
            assert tnetstring.loads(tnetstring.dumps(obj)) == obj
Esempio n. 3
0
	def _send_to_zmq(self, content, channel):
		self._lock.acquire()
		if self._push_sock:
			content[_ensure_utf8('channel')] = channel
			self._push_sock.send(tnetstring.dumps(content))
		else:
			self._pub_controller.publish(channel,
					tnetstring.dumps(content))
		self._lock.release()
Esempio n. 4
0
 def test_unicode_handling(self):
     self.assertRaises(ValueError,tnetstring.dumps,u"hello")
     self.assertEquals(tnetstring.dumps(u"hello","utf8"),"5:hello,")
     self.assertEquals(type(tnetstring.loads("5:hello,")),str)
     self.assertEquals(type(tnetstring.loads("5:hello,","utf8")),unicode)
     ALPHA = u"\N{GREEK CAPITAL LETTER ALPHA}lpha"
     self.assertEquals(tnetstring.dumps(ALPHA,"utf8"),"6:"+ALPHA.encode("utf8")+",")
     self.assertEquals(tnetstring.dumps(ALPHA,"utf16"),"12:"+ALPHA.encode("utf16")+",")
     self.assertEquals(tnetstring.loads("12:\xff\xfe\x91\x03l\x00p\x00h\x00a\x00,","utf16"),ALPHA)
Esempio n. 5
0
 def test_unicode_handling(self):
     self.assertRaises(ValueError, tnetstring.dumps, u"hello")
     self.assertEquals(tnetstring.dumps(u"hello", "utf8"), "5:hello,")
     self.assertEquals(type(tnetstring.loads("5:hello,")), str)
     self.assertEquals(type(tnetstring.loads("5:hello,", "utf8")), unicode)
     ALPHA = u"\N{GREEK CAPITAL LETTER ALPHA}lpha"
     self.assertEquals(tnetstring.dumps(ALPHA, "utf8"),
                       "6:" + ALPHA.encode("utf8") + ",")
     self.assertEquals(tnetstring.dumps(ALPHA, "utf16"),
                       "12:" + ALPHA.encode("utf16") + ",")
     self.assertEquals(
         tnetstring.loads("12:\xff\xfe\x91\x03l\x00p\x00h\x00a\x00,",
                          "utf16"), ALPHA)
Esempio n. 6
0
 def __init__ (self, sip_comm, sip_data, rtp_flow):
     for uuid, cnt in sip_comm.iteritems ():
         path = "%s/%s/"%(main_path, uuid)
         print 'Dumping SIP uuid:\'%s\' (%d packets) in %s'%(uuid, cnt, path)
         dialog, arr = sip_data [uuid]
         i = 0
         filearr = []
         try:
             os.makedirs (path)
         except OSError, e:
             # directory might already exist
             pass
         listpath = "%s/list.txt"%path
         listfd = open (listpath, "w+")
         listfd.write (
             "# This file contains the listing of %d's dumped packets\n"%cnt)
         listfd_legs = []
         for timestamp, addr, pkt, legs_n in arr:
             if not listfd_legs:
                 timestamppath = "%s/timestamp.txt"%path
                 timestampfd = open (timestamppath, "w+")
                 timestampfd.write (str (timestamp))
                 timestampfd.close ()
                 
             if legs_n in listfd_legs:
                 continue
             listfd_legs += [legs_n]
             src, dst = (addr [0], addr [1]), (addr [2], addr [3])
             listfd.write ("# legs_%d from %s to %s\n"%(legs_n,
                                                      str (src),
                                                      str (dst)))
         for timestamp, addr, pkt, legs_n in arr:
             src, dst = (addr [0], addr [1]), (addr [2], addr [3])
             dirpath = "%s/legs_%d"%(path, legs_n)
             filename = "%d.txt"%i
             filepath = "%s/%s"%(dirpath, filename)
             filearr += [tnetstring.dumps ([filepath, timestamp, 
                                            list (addr)])]
             listfd.write ("legs_%d/%d.txt\n"%(legs_n, i))
             try:
                 os.makedirs (dirpath)
             except OSError, e:
                 # directory might already exist
                 pass
             fd = open (filepath, "w+")
             fd.write (tnetstring.dumps ([timestamp, list (addr), str (pkt)],
                                         'iso-8859-15'))
             fd.close ()
             i += 1
Esempio n. 7
0
 def server_rtp_create (_redis, ckey, s_id, port, bind_ip, rr):
     _redis.lpush (redis_server_inst%rr,
                   tnetstring.dumps (["CREATE_RTP_SOCK",
                                      [s_id,
                                       bind_ip, port], ckey],
                                     'iso-8859-15'))
     return
Esempio n. 8
0
 def server_sip_create (_redis, ckey, s_id, bind_ip, port, rr, transport):
     _redis.lpush (redis_server_inst%rr,
                   tnetstring.dumps (["CREATE_SIP%s_SOCK"%transport,
                                      [s_id,
                                       bind_ip, port], ckey],
                                     'iso-8859-15'))
     return
Esempio n. 9
0
	def _respond(self, value):
		resp = dict()
		if self.req_id:
			resp['id'] = self.req_id
		resp['success'] = True
		resp['value'] = ensure_utf8(value)
		self.rep_sock.send(tnetstring.dumps(resp))
Esempio n. 10
0
	def _respond_error(self, condition):
		resp = dict()
		if self.req_id:
			resp['id'] = self.req_id
		resp['success'] = False
		resp['condition'] = ensure_utf8(condition)
		self.rep_sock.send(tnetstring.dumps(resp))
Esempio n. 11
0
	def _respond(self, value):
		resp = dict()
		if self.req_id:
			resp['id'] = self.req_id
		resp['success'] = True
		resp['value'] = ensure_utf8(value)
		self.rep_sock.send(tnetstring.dumps(resp))
Esempio n. 12
0
	def publish(self, channel, id, prev_id, rheaders=None, rbody=None, sbody=None, code=None, reason=None):
		self.lock.acquire()

		if self.pubs is None:
			self.pubs = list()
			for p in self.proxies:
				pub = gripcontrol.GripPubControl(p['control_uri'])
				if 'control_iss' in p:
					pub.set_auth_jwt({'iss': p['control_iss']}, p['key'])
				self.pubs.append(pub)

		formats = list()
		if rbody is not None:
			formats.append(gripcontrol.HttpResponseFormat(code=code, reason=reason, headers=rheaders, body=rbody))
		if sbody is not None:
			formats.append(gripcontrol.HttpStreamFormat(sbody))

		item = pubcontrol.Item(formats, id, prev_id)

		zitem = item.export()
		zitem['channel'] = channel
		zitem['http-response'] = convert_json_transport(zitem['http-response'])
		self.sock.send(tnetstring.dumps(zitem))

		for pub in self.pubs:
			pub.publish_async(channel, item)

		self.lock.release()
Esempio n. 13
0
	def _respond_error(self, condition):
		resp = dict()
		if self.req_id:
			resp['id'] = self.req_id
		resp['success'] = False
		resp['condition'] = ensure_utf8(condition)
		self.rep_sock.send(tnetstring.dumps(resp))
	def test_send_to_zmq(self):
		client = ZmqPubControlClientTestClass2('uri')
		client._push_sock = ZmqSocketTestClass()
		client._send_to_zmq({'content'.encode('utf-8'):
				'content'.encode('utf-8')}, 'chan'.encode('utf-8'))
		self.assertEquals(client._push_sock.send_data, tnetstring.dumps(
				{'content'.encode('utf-8'): 'content'.encode('utf-8'),
				'channel'.encode('utf-8'): 'chan'.encode('utf-8')}))
		client = ZmqPubControlClientTestClass2('uri')
		client._pub_controller = ZmqPubControllerTestClass()
		client._send_to_zmq({'content'.encode('utf-8'):
				'content'.encode('utf-8')}, 'chan'.encode('utf-8'))
		self.assertEquals(client._pub_controller.publish_channel,
				'chan'.encode('utf-8'))
		self.assertEquals(client._pub_controller.publish_content,
				tnetstring.dumps({'content'.encode('utf-8'):
				'content'.encode('utf-8')}))
Esempio n. 15
0
 def _send_to_zmq(self, channel, item):
     self._lock.acquire()
     if self._zmq_pub_controller:
         channel = _ensure_utf8(channel)
         content = item.export(True, True)
         self._zmq_pub_controller.publish(channel,
                                          tnetstring.dumps(content))
     self._lock.release()
Esempio n. 16
0
    def send_msg(self, msg):
        msg[b'from'] = instance_id
        msg[b'id'] = self.rid[1]
        msg[b'seq'] = self.seq
        self.seq += 1

        print('OUT {} {}'.format(self.rid[0], msg))
        out_sock.send(self.rid[0] + b' T' + tnetstring.dumps(msg))
	def test_send_to_zmq(self):
		client = ZmqPubControlClientTestClass2('uri')
		client._push_sock = ZmqSocketTestClass()
		client._send_to_zmq({'content'.encode('utf-8'):
				'content'.encode('utf-8')}, 'chan'.encode('utf-8'))
		self.assertEqual(client._push_sock.send_data, tnetstring.dumps(
				{'content'.encode('utf-8'): 'content'.encode('utf-8'),
				'channel'.encode('utf-8'): 'chan'.encode('utf-8')}))
		client = ZmqPubControlClientTestClass2('uri')
		client._pub_controller = ZmqPubControllerTestClass()
		client._send_to_zmq({'content'.encode('utf-8'):
				'content'.encode('utf-8')}, 'chan'.encode('utf-8'))
		self.assertEqual(client._pub_controller.publish_channel,
				'chan'.encode('utf-8'))
		self.assertEqual(client._pub_controller.publish_content,
				tnetstring.dumps({'content'.encode('utf-8'):
				'content'.encode('utf-8')}))
Esempio n. 18
0
	def _send_to_zmq(self, channel, item):
		self._lock.acquire()
		if self._zmq_pub_controller:
			channel = _ensure_utf8(channel)
			content = item.export(True, True)
			self._zmq_pub_controller.publish(channel,
					tnetstring.dumps(content))
		self._lock.release()
Esempio n. 19
0
def add_test(v):
    #  These modules have a few round-tripping problems...
    try:
        assert cjson.decode(cjson.encode(v)) == v
        assert yajl.loads(yajl.dumps(v)) == v
    except Exception:
        pass
    else:
        TESTS.append((v,tnetstring.dumps(v),cjson.encode(v)))
Esempio n. 20
0
 def makePickle(self, record):
     """ Create message in TNetStrings format.
     """
     msg = {}
     self.format(record) # render 'message' attribute and others
     for k in self.send_fields:
         msg[k] = record.__dict__[k]
     tnetstr = tnetstrings.dumps(msg)
     return tnetstr
Esempio n. 21
0
 def makePickle(self, record):
     """ Create message in TNetStrings format.
     """
     msg = {}
     self.format(record)  # render 'message' attribute and others
     for k in self.send_fields:
         msg[k] = record.__dict__[k]
     tnetstr = tnetstrings.dumps(msg)
     return tnetstr
Esempio n. 22
0
	def call(self, method, args=dict()):
		req = dict()
		req['id'] = str(uuid.uuid4())
		req['method'] = ensure_utf8(method)
		req['args'] = ensure_utf8(args)
		req_raw = tnetstring.dumps(req)
		try:
			if not self.sock.poll(30000, zmq.POLLOUT):
				raise CallError('send-timeout')
			m_list = list()
			m_list.append('')
			m_list.append(req_raw)
			self.sock.send_multipart(m_list)
		except zmq.ZMQError as e:
			raise CallError('send-failed', e.message)
		start = int(time.clock() * 1000)
		while True:
			elapsed = max(int(time.clock() * 1000) - start, 0)
			try:
				if not self.sock.poll(max(30000 - elapsed, 0), zmq.POLLIN):
					raise CallError('receive-timeout')
				m_list = self.sock.recv_multipart()
			except zmq.ZMQError as e:
				raise CallError('receive-failed', e.message)
			if len(m_list) != 2:
				print "response has wrong number of parts, skipping"
				continue
			if len(m_list[0]) > 0:
				print "response first part is not empty, skipping"
				continue
			resp_raw = m_list[1]
			try:
				resp = tnetstring.loads(resp_raw)
			except:
				print "failed to parse response as tnetstring, skipping"
				continue
			if 'id' not in resp:
				print "response missing id field, skipping"
				continue
			if resp['id'] != req['id']:
				print "unexpected response id, skipping"
				continue
			break
		if 'success' not in resp:
			raise CallError('invalid-response', 'missing success field')
		if resp['success']:
			if 'value' not in resp:
				raise CallError('invalid-response', 'missing value field')
			return resp['value']
		else:
			if 'condition' not in resp:
				raise CallError('invalid-response', 'missing condition field')
			raise CallError(resp['condition'])
Esempio n. 23
0
	def call(self, method, args, timeout=10000):
		req = dict()
		req['id'] = str(uuid.uuid4())
		req['method'] = ensure_utf8(method)
		req['args'] = ensure_utf8(args)
		req_raw = tnetstring.dumps(req)
		start = int(time.clock() * 1000)
		try:
			if not self.sock.poll(timeout, zmq.POLLOUT):
				raise CallError('send-timeout')
			m_list = list()
			m_list.append('')
			m_list.append(req_raw)
			self.sock.send_multipart(m_list)
		except zmq.ZMQError as e:
			raise CallError('send-failed', e.message)
		while True:
			elapsed = max(int(time.clock() * 1000) - start, 0)
			try:
				if not self.sock.poll(max(timeout - elapsed, 0), zmq.POLLIN):
					raise CallError('receive-timeout')
				m_list = self.sock.recv_multipart()
			except zmq.ZMQError as e:
				raise CallError('receive-failed', e.message)
			if len(m_list) != 2:
				print "response has wrong number of parts, skipping"
				continue
			if len(m_list[0]) > 0:
				print "response first part is not empty, skipping"
				continue
			resp_raw = m_list[1]
			try:
				resp = tnetstring.loads(resp_raw)
			except:
				print "failed to parse response as tnetstring, skipping"
				continue
			if 'id' not in resp:
				print "response missing id field, skipping"
				continue
			if resp['id'] != req['id']:
				print "unexpected response id, skipping"
				continue
			break
		if 'success' not in resp:
			raise CallError('invalid-response', 'missing success field')
		if resp['success']:
			if 'value' not in resp:
				raise CallError('invalid-response', 'missing value field')
			return resp['value']
		else:
			if 'condition' not in resp:
				raise CallError('invalid-response', 'missing condition field')
			raise CallError(resp['condition'])
Esempio n. 24
0
def keepalive_worker():
	lock.acquire()
	for id, s in sessions.iteritems():
		resp = dict()
		resp['from'] = client_id
		resp['id'] = id
		resp['seq'] = s.out_seq
		s.out_seq += 1
		resp['type'] = 'credit'
		resp['credits'] = 0

		print 'OUT %s' % resp
		out_sock.send(s.to_address + ' T' + tnetstring.dumps(resp))
	lock.release()
Esempio n. 25
0
def send_body(to_addr, conns, data):
    ids = []
    for c in conns:
        ids.append({b'id': c.rid[1], b'seq': c.seq})
        c.seq += 1

    msg = {}
    msg[b'from'] = instance_id
    msg[b'id'] = ids
    msg[b'body'] = data
    msg[b'more'] = True

    print('OUT {} {}'.format(to_addr, msg))
    out_sock.send(to_addr + b' T' + tnetstring.dumps(msg))
Esempio n. 26
0
 def pkt_send (self, _id, data, host, port, rtp = False):
     if not rtp:
         self._last_packet ['_id'] = _id
         self._last_packet ['data'] = data
         self._last_packet ['host'] = host
         self._last_packet ['port'] = port
     try:
         self._redis.lpush ('send_%s'%str (_id),
                            tnetstring.dumps ([host, port, data],
                                              'iso-8859-15'))
         if self._debug:
             print "===> sending to %s:%d at %s"%(host, int (port),
                                                  time.ctime (time.time ()))
             print str (data)
     except:
         return True
     return False
Esempio n. 27
0
    def publish(self,
                channel,
                id,
                prev_id,
                rheaders=None,
                rbody=None,
                sbody=None,
                code=None,
                reason=None):
        self.lock.acquire()

        if self.pubs is None:
            self.pubs = list()
            for p in self.proxies:
                pub = gripcontrol.GripPubControl(p['control_uri'])
                if 'control_iss' in p:
                    pub.set_auth_jwt({'iss': p['control_iss']}, p['key'])
                self.pubs.append(pub)

        formats = list()
        if rbody is not None:
            formats.append(
                gripcontrol.HttpResponseFormat(code=code,
                                               reason=reason,
                                               headers=rheaders,
                                               body=rbody))
        if sbody is not None:
            formats.append(gripcontrol.HttpStreamFormat(sbody))

        item = pubcontrol.Item(formats, id, prev_id)

        zitem = item.export()
        zitem['channel'] = channel
        zitem['http-response'] = convert_json_transport(zitem['http-response'])
        self.sock.send(tnetstring.dumps(zitem))

        for pub in self.pubs:
            pub.publish_async(channel, item)

        self.lock.release()
Esempio n. 28
0
 def _handle_sip (self, date, data, addr, s_id):
     try:
         sip_pkt = SIPpacket ()
         sip_pkt.fromString (data)
         self._logger.debug (
             "%s: received SIP message (%d bytes)"% (addr, len (data)))
         redis_data = tnetstring.dumps ([s_id, date, list (addr), data],
                                        'iso-8859-15')
         valid = True
         try:
             # This is the SIP parser routine
             # There is no totag and it is a request : push in 'method's list
             # or compute session_id : 'fromtag:callid:branch'
             # when method is BYE 'fromtag' from 'To' and branch from 'CSeq'
             sip_pkt.to.enable ()
             tp = sip_pkt.tp
             if not tp and None is not sip_pkt.rm:
                 return [sip_pkt.rm, redis_data]
             if "BYE" == sip_pkt.rm:
                 sip_pkt.to.enable ()
                 fp = sip_pkt.tp
                 branch = sip_pkt.cseq.seq
             else:
                 sip_pkt._from.enable ()
                 fp = sip_pkt.fp
                 sip_pkt.via.enable ()
                 branch = sip_pkt.vb
             callid = sip_pkt.callid
             sess_id = "%s:%s:%s"% (fp, callid, branch)
             return [sess_id, redis_data]
         except KeyError, h:
             self._logger.warn (
                 "Missing header '%s', droping packet"%str (h))
     except Exception, e:
         self._logger.exception (e)
         self._logger.error (
             "Unable to parse SIP request, dumping")
         self._dump (data, suffix = "error")
Esempio n. 29
0
	def _discover_uris(self):
		self._thread_cond.acquire()
		if self._discovery_in_progress:
			self._thread_cond.wait()
			self._thread_cond.release()
			self._verify_discovered_uris()
			return
		else:
			self._discovery_in_progress = True
			self._thread_cond.release()
		if (self.uri is None or self._discovery_completed or
				(self.pub_uri and self.push_uri)):
			self._end_discovery(False)
			return
		sock = self._context.socket(zmq.REQ)
		sock.linger = 0
		sock.connect(self.uri)
		start = int(timeit.default_timer() * 1000)
		if not sock.poll(3000, zmq.POLLOUT):
			sock.close()
			self._end_discovery(False)
			raise ValueError('uri discovery request failed: pollout timeout')
		req = {_ensure_utf8('method'): _ensure_utf8('get-zmq-uris')}
		sock.send(tnetstring.dumps(req))
		elapsed = max(int(timeit.default_timer() * 1000) - start, 0)
		if not sock.poll(max(3000 - elapsed, 0), zmq.POLLIN):
			sock.close()
			self._end_discovery(False)
			raise ValueError('uri discovery request failed: pollin timeout')
		resp = tnetstring.loads(sock.recv())
		sock.close()
		if (not resp.get(_ensure_utf8('success')) or
				not resp.get(_ensure_utf8('value'))):
			self._end_discovery(False)
			raise ValueError('uri discovery request failed: %s' % resp)
		self._set_discovered_uris(resp[_ensure_utf8('value')])
		self._end_discovery(True)
		self._verify_discovered_uris()
Esempio n. 30
0
 def test_roundtrip_format_examples(self):
     for data, expect in FORMAT_EXAMPLES.items():
         self.assertEqual(expect, tnetstring.loads(data))
         self.assertEqual(expect,
                          tnetstring.loads(tnetstring.dumps(expect)))
         self.assertEqual((expect, ""), tnetstring.pop(data))
Esempio n. 31
0
 def save(self):
     (self.store.open(self.path, 'w', createdirs=True)
      .write(tnetstring.dumps(self.data)))
     self.store.save()
Esempio n. 32
0
 def server_remove_sip (_redis, ckey, bind_ip, port):
     _redis.lpush (redis_server_command,
                   tnetstring.dumps (['REMOVE_SIP', [bind_ip, port], ckey],
                                     'iso-8859-15'))
     return
Esempio n. 33
0
poller.register(sock, zmq.POLLIN)
start = int(time.time() * 1000)
while True:
	elapsed = int(time.time() * 1000) - start
	if elapsed >= 500:
		# give up
		break
	socks = dict(poller.poll(500 - elapsed))
	if socks.get(sock) == zmq.POLLIN:
		m = sock.recv()
		if m[0] == '\x01' and m[1:] == channel:
			# subscription ready
			break

hr = dict()
hr['body'] = content + '\n'
hs = dict()
hs['content'] = content + '\n'
ws = dict()
ws['content'] = content
formats = dict()
formats['http-response'] = hr
formats['http-stream'] = hs
formats['ws-message'] = ws
item = dict()
item['formats'] = formats

sock.send_multipart([channel, tnetstring.dumps(item)])

print 'Published'
Esempio n. 34
0
            s.out_seq += 1
            resp['code'] = 200
            resp['reason'] = 'OK'
            headers = list()
            if s.content_type:
                headers.append(['Content-Type', s.content_type])
            headers.append(['Content-Length', str(len(s.data))])
            resp['headers'] = headers
            resp['body'] = s.take_data()
            if s.offset < len(s.data):
                resp['more'] = True
            else:
                del sessions[s.id]

            print 'OUT %s' % resp
            out_sock.send(req['from'] + ' T' + tnetstring.dumps(resp))
        else:
            if len(body) > 0:
                # send credits
                resp = dict()
                resp['from'] = client_id
                resp['id'] = s.id
                resp['seq'] = s.out_seq
                s.out_seq += 1
                resp['type'] = 'credit'
                if not s.sent_credits:
                    resp['credits'] = 200000
                    s.sent_credits = True
                else:
                    resp['credits'] = len(body)
Esempio n. 35
0
ctx = zmq.Context()
out_sock = ctx.socket(zmq.PUSH)
out_sock.connect('ipc:///tmp/zurl-in')
out_stream_sock = ctx.socket(zmq.ROUTER)
out_stream_sock.connect('ipc:///tmp/zurl-in-stream')
in_sock = ctx.socket(zmq.SUB)
in_sock.setsockopt(zmq.SUBSCRIBE, client_id)
in_sock.connect('ipc:///tmp/zurl-out')

time.sleep(0.5)

rid = str(uuid.uuid4())
inseq = 0
outseq = 0
out_sock.send('T' + tnetstring.dumps({'from': client_id, 'id': rid, 'seq': outseq, 'method': 'GET', 'uri': sys.argv[1], 'stream': True, 'credits': 230000}))
outseq += 1

while True:
	buf = in_sock.recv()
	at = buf.find(' ')
	receiver = buf[:at]
	indata = tnetstring.loads(buf[at + 2:])
	if indata['id'] != rid:
		continue
	print 'IN: %s' % indata
	assert(indata['seq'] == inseq)
	inseq += 1
	if ('type' in indata and (indata['type'] == 'error' or indata['type'] == 'cancel')) or ('type' not in indata and 'more' not in indata):
		break
	raddr = indata['from']
Esempio n. 36
0
# this program sends a response to a certain request ID

import sys
import time
import tnetstring
import zmq

body = sys.argv[1]
addr = sys.argv[2].encode()
rid = sys.argv[3].encode()

ctx = zmq.Context()
sock = ctx.socket(zmq.PUB)
sock.connect('ipc://client-in')

# await subscription
time.sleep(0.01)

resp = {}
resp[b'from'] = b'sendresp'
resp[b'id'] = rid
resp[b'code'] = 200
resp[b'reason'] = b'OK'
resp[b'headers'] = [[b'Content-Type', b'text/plain']]
resp[b'body'] = '{}\n'.format(body).encode()

m = [addr + b' T' + tnetstring.dumps(resp)]

sock.send_multipart(m)
Esempio n. 37
0
elif args.content and not args.patch:
    formats['http-stream'] = {'content': args.content + '\n'}

if args.content and not args.patch:
    formats['ws-message'] = {'content': args.content}

if not formats:
    print 'error: nothing to send'
    sys.exit(1)

if args.sender:
    meta['sender'] = args.sender

item = {'channel': args.channel, 'formats': formats}

if args.id:
    item['id'] = args.id
if args.prev_id:
    item['prev-id'] = args.prev_id

if meta:
    item['meta'] = meta

ctx = zmq.Context()
sock = ctx.socket(zmq.PUSH)
sock.connect(args.spec)

sock.send(tnetstring.dumps(item))

print 'Published'
Esempio n. 38
0
File: get.py Progetto: namezis/zurl
if len(sys.argv) < 2:
    print 'usage: %s [url]' % sys.argv[0]
    sys.exit(1)

ctx = zmq.Context()
sock = ctx.socket(zmq.REQ)
sock.connect('ipc:///tmp/zurl-req')

req = dict()
req['id'] = str(uuid.uuid4())
req['method'] = 'GET'
req['uri'] = sys.argv[1]
#req['follow-redirects'] = True
#req['ignore-tls-errors'] = True
sock.send('T' + tnetstring.dumps(req))

resp = tnetstring.loads(sock.recv()[1:])
if 'type' in resp and resp['type'] == 'error':
    print 'error: %s' % resp['condition']
    sys.exit(1)

print 'code=%d reason=[%s]' % (resp['code'], resp['reason'])
for h in resp['headers']:
    print '%s: %s' % (h[0], h[1])

if 'body' in resp:
    print '\n%s' % resp['body']
else:
    print '\n'
Esempio n. 39
0
 def server_rtp_create_reply (_redis, ckey, s_id):
     _redis.lpush (redis_client_reply%ckey,
                   tnetstring.dumps (["CREATE_RTP_REPLY",
                                      [s_id]],
                                     'iso-8859-15'))
     return
Esempio n. 40
0
 def server_sip_create_reply (_redis, ckey, s_id, transport):
     _redis.lpush (redis_client_reply%ckey,
                   tnetstring.dumps (["CREATE_SIP%s_REPLY"%transport,
                                      [s_id]],
                                     'iso-8859-15'))
     return
	def recv(self):
		return tnetstring.dumps(discovery_result)
Esempio n. 42
0
 def test_roundtrip_format_random(self):
     for _ in xrange(500):
         v = get_random_object()
         self.assertEqual(v, tnetstring.loads(tnetstring.dumps(v)))
         self.assertEqual((v, ""), tnetstring.pop(tnetstring.dumps(v)))
	def test_discover_uris(self):
		global pollin_response
		global pollout_response
		global discovery_result
		global failed_discovery_result1
		global failed_discovery_result2
		time.clock = time.time
		client = ZmqPubControlClientTestClass4('uri', 'push_uri', 'pub_uri')
		client._discover_uris()
		self.assertFalse(client._discovery_completed)
		client = ZmqPubControlClientTestClass4('uri', 'push_uri', 'pub_uri')
		client.uri = None
		client.pub_uri = None
		client.push_uri = None
		client._discover_uris()
		self.assertFalse(client._discovery_completed)
		client = ZmqPubControlClientTestClass4('tcp://localhost:5563',
				'push_uri', 'pub_uri')
		client.pub_uri = None
		client.push_uri = None
		client._discovery_completed = True
		client._discover_uris()
		self.assertTrue(client._discovery_completed)
		self.assertFalse(hasattr(client._context, '.last_socket_created'))
		client = ZmqPubControlClientTestClass4('tcp://localhost:5563',
				'push_uri', 'pub_uri')
		client.pub_uri = None
		client.push_uri = None
		pollin_response = 1
		pollout_response = 1
		client._context = ZmqContextTestClass()
		client._discover_uris()
		self.assertTrue(client._discovery_completed)
		self.assertEquals(client._context.socket_type, zmq.REQ)
		self.assertEquals(client._context.last_socket_created.
				connect_uri, 'tcp://localhost:5563')
		self.assertEquals(client._context.last_socket_created.
				poll_params[0][0], 3000)
		self.assertEquals(client._context.last_socket_created.
				poll_params[0][1], zmq.POLLOUT)
		self.assertTrue(client._context.last_socket_created.
				poll_params[1][0] < 2000 and
				client._context.last_socket_created.poll_params[1][0] > 1950)
		self.assertEquals(client._context.last_socket_created.
				poll_params[1][1], zmq.POLLIN)
		self.assertTrue(client._context.last_socket_created.closed)
		self.assertEquals(client._context.last_socket_created.send_data,
				tnetstring.dumps({'method'.encode('utf-8'):
				'get-zmq-uris'.encode('utf-8')}))
		self.assertEquals(client.resolve_uris[0], 'publish-pull')
		self.assertEquals(client.resolve_uris[1], 'publish-sub')
		self.assertEquals(client.resolve_hosts[0], 'localhost')
		self.assertEquals(client.resolve_hosts[1], 'localhost')
		self.assertEquals(client.push_uri, 'publish-pull')
		self.assertEquals(client.pub_uri, 'publish-sub')
		client = ZmqPubControlClientTestClass4('tcp://localhost:5563',
				'push_uri', 'pub_uri')
		client.pub_uri = None
		client.push_uri = None
		client._context = ZmqContextTestClass()
		pollin_response = 1
		pollout_response = 0
		with self.assertRaises(ValueError):
			client._discover_uris()
		self.assertFalse(client._discovery_completed)
		self.assertEquals(client._context.last_socket_created.
				poll_params[0][0], 3000)
		self.assertEquals(client._context.last_socket_created.
				poll_params[0][1], zmq.POLLOUT)
		self.assertTrue(client._context.last_socket_created.closed)
		client = ZmqPubControlClientTestClass4('tcp://localhost:5563',
				'push_uri', 'pub_uri')
		client.pub_uri = None
		client.push_uri = None
		client._context = ZmqContextTestClass()
		pollin_response = 0
		pollout_response = 1
		client._discover_uris_async()
		client = ZmqPubControlClientTestClass4('tcp://localhost:5563',
				'push_uri', 'pub_uri')
		client.pub_uri = None
		client.push_uri = None
		client._context = ZmqContextTestClass()
		pollin_response = 0
		pollout_response = 1
		with self.assertRaises(ValueError):
			client._discover_uris()
		self.assertFalse(client._discovery_completed)
		self.assertEquals(client._context.last_socket_created.
				poll_params[0][0], 3000)
		self.assertEquals(client._context.last_socket_created.
				poll_params[0][1], zmq.POLLOUT)
		self.assertTrue(client._context.last_socket_created.closed)
		pollin_response = 1
		pollout_response = 1
		client = ZmqPubControlClientTestClass4('uri',
				'push_uri', 'pub_uri')
		client.pub_uri = None
		client.push_uri = None
		client._context = ZmqContextTestClass()
		client._discover_uris()
		self.assertEquals(client._context.socket_type, zmq.REQ)
		self.assertEquals(client._context.last_socket_created.
				connect_uri, 'uri')
		self.assertTrue(client._context.last_socket_created.closed)
		self.assertEquals(client.resolve_uris[0], 'publish-pull')
		self.assertEquals(client.resolve_uris[1], 'publish-sub')
		self.assertEquals(client.resolve_hosts[0], None)
		self.assertEquals(client.resolve_hosts[1], None)
		client = ZmqPubControlClientTestClass4('tcp://localhost:5563',
				'push_uri', 'pub_uri')
		client.pub_uri = None
		client.push_uri = None
		client._context = ZmqContextTestClass()
		discovery_result = failed_discovery_result1
		client.resp = False
		with self.assertRaises(ValueError):
			client._discover_uris()
		self.assertTrue(client._context.last_socket_created.closed)
		client = ZmqPubControlClientTestClass4('tcp://localhost:5563',
				'push_uri', 'pub_uri')
		client.pub_uri = None
		client.push_uri = None
		client._context = ZmqContextTestClass()
		discovery_result = failed_discovery_result2
		client.resp = False
		with self.assertRaises(ValueError):
			client._discover_uris()
		self.assertTrue(client._context.last_socket_created.closed)
Esempio n. 44
0
File: post.py Progetto: namezis/zurl
time.sleep(0.5)

pos = 0
file = open(sys.argv[2], 'r').read()

rid = str(uuid.uuid4())
outseq = 0
outcredits = 0
out_sock.send(
    'T' + tnetstring.dumps({
        'from': client_id,
        'id': rid,
        'seq': outseq,
        'method': 'POST',
        'uri': sys.argv[1],
        'headers': [['Content-Length', str(len(file))]],
        'stream': True,
        'credits': 200000,
        'more': True
    }))
outseq += 1

raddr = None

while True:
    buf = in_sock.recv()
    at = buf.find(' ')
    receiver = buf[:at]
    data = tnetstring.loads(buf[at + 2:])
    print 'IN: %s %s' % (receiver, data)
Esempio n. 45
0
import zmq

if len(sys.argv) < 2:
	print 'usage: %s [url]' % sys.argv[0]
	sys.exit(1)

ctx = zmq.Context()
sock = ctx.socket(zmq.REQ)
sock.connect('ipc:///tmp/zurl-req')

req = dict()
req['id'] = str(uuid.uuid4())
req['method'] = 'GET'
req['uri'] = sys.argv[1]
#req['ignore-tls-errors'] = True
sock.send('T' + tnetstring.dumps(req))

resp = tnetstring.loads(sock.recv()[1:])
if 'type' in resp and resp['type'] == 'error':
	print 'error: %s' % resp['condition']
	sys.exit(1)

print 'code=%d reason=[%s]' % (resp['code'], resp['reason'])
for h in resp['headers']:
	print '%s: %s' % (h[0], h[1])

if 'body' in resp:
	print '\n%s' % resp['body']
else:
	print '\n'
Esempio n. 46
0
headers = []
if args.header:
	for h in args.header:
		k, v = h.split(':')
		headers.append([k, v.lstrip()])

ctx = zmq.Context()
sock = ctx.socket(zmq.PUSH)
sock.connect(args.spec)

hr = {'body': args.content + '\n'}
if args.code is not None:
	hr['code'] = args.code
if headers:
	hr['headers'] = headers
hs = {'content': args.content + '\n'}
ws = {'content': args.content}

item = {
	'channel': args.channel,
	'formats': {
		'http-response': hr,
		'http-stream': hs,
		'ws-message': ws
	}
}

sock.send(tnetstring.dumps(item))

print 'Published'
Esempio n. 47
0
def _publish_chunk(sock, data):
    m = {'channel': 'audio', 'formats': {'http-stream': {'content': data}}}
    sock.send(tnetstring.dumps(m), zmq.DONTWAIT)
Esempio n. 48
0
        if not header and not payload:
            break
        i += 1
        ethernet = eth_decoder.decode (payload)
        if ethernet.get_ether_type () == Packets.IP.ethertype:
            ip = ip_decoder.decode (payload[ethernet.get_header_size ():])
            if ip.get_ip_p () == Packets.UDP.protocol: 
                udp = udp_decoder.decode (
                    payload[ethernet.get_header_size ()+ip.get_header_size ():])
                # print "IPv4 UDP packet %s:%d->%s:%d" % (ip.get_ip_src (),
                #                                         udp.get_uh_sport (),
                #                                         ip.get_ip_dst (),
                #                                         udp.get_uh_dport ()),
                udph_length = 8
                udp_data = payload [ ethernet.get_header_size () + 
                                     ip.get_header_size () + udph_length:]
                # print len (udp_data)
                pkt_list += [udp_data]
    except pcapy.PcapError:
        break
    except KeyboardInterrupt:
        print "dumping now"
        break
if not i:
    print "No packet match"
    sys.exit (1)
fd = open (outfile, "w+")
fd.write (tnetstring.dumps (pkt_list, 'iso-8859-15'))
fd.close ()
print "dumped %d's packets"%i
Esempio n. 49
0
out_stream_sock = ctx.socket(zmq.ROUTER)
out_stream_sock.connect('ipc:///tmp/zurl-in-stream')
in_sock = ctx.socket(zmq.SUB)
in_sock.setsockopt(zmq.SUBSCRIBE, client_id)
in_sock.connect('ipc:///tmp/zurl-out')

time.sleep(0.5)

rid = str(uuid.uuid4())
inseq = 0
outseq = 0
out_sock.send('T' + tnetstring.dumps({
    'from': client_id,
    'id': rid,
    'seq': outseq,
    'method': 'GET',
    'uri': sys.argv[1],
    'stream': True,
    'credits': 230000
}))
outseq += 1

while True:
    buf = in_sock.recv()
    at = buf.find(' ')
    receiver = buf[:at]
    indata = tnetstring.loads(buf[at + 2:])
    if indata['id'] != rid:
        continue
    print 'IN: %s' % indata
    assert (indata['seq'] == inseq)
Esempio n. 50
0
def thrash_tnetstring():
    for obj, tns, json in TESTS:
        assert tnetstring.loads(tns) == obj
        assert tnetstring.loads(tnetstring.dumps(obj)) == obj
Esempio n. 51
0
 def save(self):
     self.open('index.gz', 'w').write(tnetstring.dumps(self.index))
Esempio n. 52
0
 def test_roundtrip_big_integer(self):
     i1 = math.factorial(30000)
     s = tnetstring.dumps(i1)
     i2 = tnetstring.loads(s)
     self.assertEquals(i1, i2)
	def recv(self):
		return tnetstring.dumps(discovery_result)
Esempio n. 54
0
 def server_sip_remove (_redis, ckey, rr):
     _redis.lpush (redis_server_inst%rr,
                   tnetstring.dumps (["REMOVE_SIP_SOCK",
                                      [s_id, bind_ip, port], ckey],
                                     'iso-8859-15'))
     return
Esempio n. 55
0
		headers = list()
		headers.append(['Content-Type', 'text/plain'])
		headers.append(['Content-Length', str(os.stat(filename).st_size)])
		resp['headers'] = headers
		if len(body) > 0:
			resp['body'] = body
		if not eof:
			resp['more'] = True

		s.out_seq += 1

		if eof:
			del sessions[id]

		print 'OUT %s' % resp
		out_sock.send(req['from'] + ' ' + tnetstring.dumps(resp))
	elif socks.get(in_stream_sock) == zmq.POLLIN:
		parts = in_stream_sock.recv_multipart()
		req = tnetstring.loads(parts[1])
		print 'IN stream %s' % req

		# we are only streaming output, so subsequent input messages must be credits
		if 'type' not in req or req['type'] != 'credit':
			print 'wrong packet type'
			continue

		id = req['id']

		s = sessions.get(id)
		if s is None:
			continue
	def test_discover_uris(self):
		global pollin_response
		global pollout_response
		global discovery_result
		global failed_discovery_result1
		global failed_discovery_result2
		time.clock = time.time
		client = ZmqPubControlClientTestClass4('uri', 'push_uri', 'pub_uri')
		client._discover_uris()
		self.assertFalse(client._discovery_completed)
		client = ZmqPubControlClientTestClass4('uri', 'push_uri', 'pub_uri')
		client.uri = None
		client.pub_uri = None
		client.push_uri = None
		client._discover_uris()
		self.assertFalse(client._discovery_completed)
		client = ZmqPubControlClientTestClass4('tcp://localhost:5563',
				'push_uri', 'pub_uri')
		client.pub_uri = None
		client.push_uri = None
		client._discovery_completed = True
		client._discover_uris()
		self.assertTrue(client._discovery_completed)
		self.assertFalse(hasattr(client._context, '.last_socket_created'))
		client = ZmqPubControlClientTestClass4('tcp://localhost:5563',
				'push_uri', 'pub_uri')
		client.pub_uri = None
		client.push_uri = None
		pollin_response = 1
		pollout_response = 1
		client._context = ZmqContextTestClass()
		client._discover_uris()
		self.assertTrue(client._discovery_completed)
		self.assertEqual(client._context.socket_type, zmq.REQ)
		self.assertEqual(client._context.last_socket_created.
				connect_uri, 'tcp://localhost:5563')
		self.assertEqual(client._context.last_socket_created.
				poll_params[0][0], 3000)
		self.assertEqual(client._context.last_socket_created.
				poll_params[0][1], zmq.POLLOUT)
		self.assertTrue(client._context.last_socket_created.
				poll_params[1][0] < 2000 and
				client._context.last_socket_created.poll_params[1][0] > 1950)
		self.assertEqual(client._context.last_socket_created.
				poll_params[1][1], zmq.POLLIN)
		self.assertTrue(client._context.last_socket_created.closed)
		self.assertEqual(client._context.last_socket_created.send_data,
				tnetstring.dumps({'method'.encode('utf-8'):
				'get-zmq-uris'.encode('utf-8')}))
		self.assertEqual(client.resolve_uris[0], 'publish-pull')
		self.assertEqual(client.resolve_uris[1], 'publish-sub')
		self.assertEqual(client.resolve_hosts[0], 'localhost')
		self.assertEqual(client.resolve_hosts[1], 'localhost')
		self.assertEqual(client.push_uri, 'publish-pull')
		self.assertEqual(client.pub_uri, 'publish-sub')
		client = ZmqPubControlClientTestClass4('tcp://localhost:5563',
				'push_uri', 'pub_uri')
		client.pub_uri = None
		client.push_uri = None
		client._context = ZmqContextTestClass()
		pollin_response = 1
		pollout_response = 0
		with self.assertRaises(ValueError):
			client._discover_uris()
		self.assertFalse(client._discovery_completed)
		self.assertEqual(client._context.last_socket_created.
				poll_params[0][0], 3000)
		self.assertEqual(client._context.last_socket_created.
				poll_params[0][1], zmq.POLLOUT)
		self.assertTrue(client._context.last_socket_created.closed)
		client = ZmqPubControlClientTestClass4('tcp://localhost:5563',
				'push_uri', 'pub_uri')
		client.pub_uri = None
		client.push_uri = None
		client._context = ZmqContextTestClass()
		pollin_response = 0
		pollout_response = 1
		client._discover_uris_async()
		client = ZmqPubControlClientTestClass4('tcp://localhost:5563',
				'push_uri', 'pub_uri')
		client.pub_uri = None
		client.push_uri = None
		client._context = ZmqContextTestClass()
		pollin_response = 0
		pollout_response = 1
		with self.assertRaises(ValueError):
			client._discover_uris()
		self.assertFalse(client._discovery_completed)
		self.assertEqual(client._context.last_socket_created.
				poll_params[0][0], 3000)
		self.assertEqual(client._context.last_socket_created.
				poll_params[0][1], zmq.POLLOUT)
		self.assertTrue(client._context.last_socket_created.closed)
		pollin_response = 1
		pollout_response = 1
		client = ZmqPubControlClientTestClass4('uri',
				'push_uri', 'pub_uri')
		client.pub_uri = None
		client.push_uri = None
		client._context = ZmqContextTestClass()
		client._discover_uris()
		self.assertEqual(client._context.socket_type, zmq.REQ)
		self.assertEqual(client._context.last_socket_created.
				connect_uri, 'uri')
		self.assertTrue(client._context.last_socket_created.closed)
		self.assertEqual(client.resolve_uris[0], 'publish-pull')
		self.assertEqual(client.resolve_uris[1], 'publish-sub')
		self.assertEqual(client.resolve_hosts[0], None)
		self.assertEqual(client.resolve_hosts[1], None)
		client = ZmqPubControlClientTestClass4('tcp://localhost:5563',
				'push_uri', 'pub_uri')
		client.pub_uri = None
		client.push_uri = None
		client._context = ZmqContextTestClass()
		discovery_result = failed_discovery_result1
		client.resp = False
		with self.assertRaises(ValueError):
			client._discover_uris()
		self.assertTrue(client._context.last_socket_created.closed)
		client = ZmqPubControlClientTestClass4('tcp://localhost:5563',
				'push_uri', 'pub_uri')
		client.pub_uri = None
		client.push_uri = None
		client._context = ZmqContextTestClass()
		discovery_result = failed_discovery_result2
		client.resp = False
		with self.assertRaises(ValueError):
			client._discover_uris()
		self.assertTrue(client._context.last_socket_created.closed)