def subscribe(self, col, spec): """Subscribe to updates of document in col identified by spec.""" data = [ "\x00\x00\x00\x00", _make_c_string("%s@%s" % (self._pubsub_id, col)), struct.pack('<ii', 0, 0), BSON.from_dict(spec), ] msg = "".join(data) yield self._put_request(OP_SUBSCRIBE, msg) yield response('')
def wait(self, collection=''): """Wait for events to be published on subscribed docs in collection. If no collection is given, the proxy will notify for all subscribed docs across collections. """ data = "\x00\x00\x00\x00%s%s" % ( _make_c_string(collection), self._pubsub_id, ) yield self._put_request(OP_WAIT, data) doclen = struct.unpack('<i', (yield bytes(4)))[0] rawdoc = yield bytes(doclen) objs = [] while rawdoc: obj, rawdoc = _bson_to_dict(rawdoc) objs.append(obj) yield response(objs)
def request(self, method, path, headers, body=None): '''Issues a `method` request to `path` on the connected server. Sends along `headers`, and body. Very low level--you must set "host" yourself, for example. It will set Content-Length, however. ''' req = HttpRequest(method, path, '1.1') if body: headers.set('Content-Length', len(body)) yield '%s\r\n%s\r\n\r\n' % (req.format(), headers.format()) if body: yield body resp_line = yield until_eol() version, code, status = resp_line.split(None, 2) code = int(code) header_block = yield until('\r\n\r\n') heads = HttpHeaders() heads.parse(header_block) if heads.get_one('Transfer-Encoding') == 'chunked': body = yield handle_chunks(heads) else: cl = int(heads.get_one('Content-Length', 0)) if cl: body = yield bytes(cl) else: body = None if version < '1.0' or heads.get_one('Connection') == 'close': self.close() yield response((code, heads, body))
def echo(self, message): yield message + '\r\n' back = yield until("\r\n") yield response(back)
def echo(self, message): yield message + "\r\n" back = yield until("\r\n") yield response(back)