def _processIncoming(self, delivery, connector): link = proton.pn_delivery_link(delivery) ssn = proton.pn_link_session(link) msg = [] self.log.debug("Receiving '%s'", proton.pn_delivery_tag(delivery)) while True: rc, buff = proton.pn_link_recv(link, 1024) msg.append(buff) if rc == proton.PN_EOS: break msg = ''.join(msg) self.log.debug("Received '%s'", proton.pn_delivery_tag(delivery)) proton.pn_link_advance(link) proton.pn_delivery_update(delivery, proton.PN_ACCEPTED) proton.pn_delivery_settle(delivery) msgObj = proton.Message() msgObj.decode(msg) ctx = proton.pn_session_get_context(ssn) ctx._pushIncomingMessage(msgObj.body) # if more credit is needed, grant it if proton.pn_link_credit(link) == 0: proton.pn_link_flow(link, MBUFF_SIZE)
def _queueOutgoingDeliveries(self, conn): for ssn in self._iterSessions(conn, proton.PN_LOCAL_ACTIVE): ctx = proton.pn_session_get_context(ssn) sender = ctx.sender if sender is None: # No sender link sender = proton.pn_sender(ctx.session, "sender-%s" % str(uuid.uuid4())) ctx.sender = sender continue while proton.pn_link_credit(sender) > 0: try: data = ctx._popPendingMessage() except Empty: break else: msg = proton.Message() msg.body = data d = proton.pn_delivery(sender, "delivery-%s" % str(uuid.uuid4())) proton.pn_delivery_set_context(d, msg.encode()) self.log.debug("Queueing delivery (%s)", proton.pn_delivery_tag(d))
def _openLinks(self, conn): link = proton.pn_link_head(conn, proton.PN_LOCAL_UNINIT) while link: self.log.debug("Opening Link") proton.pn_terminus_copy(proton.pn_link_source(link), proton.pn_link_remote_source(link)) proton.pn_terminus_copy(proton.pn_link_target(link), proton.pn_link_remote_target(link)) ssn = proton.pn_link_session(link) client = proton.pn_session_get_context(ssn) if proton.pn_link_is_sender(link): if client.sender != link: self.log.debug("Already have a sender opened for session") proton.pn_link_close(link) else: self.log.debug("Opening Link to send messages") proton.pn_link_open(link) elif proton.pn_link_is_receiver(link): self.log.debug("Opening Link to recv messages") proton.pn_link_open(link) proton.pn_link_flow(link, MBUFF_SIZE) link = proton.pn_link_next(link, proton.PN_LOCAL_UNINIT)
def _cleanSessions(self, conn): ssn = proton.pn_session_head(conn, (proton.PN_LOCAL_ACTIVE | proton.PN_REMOTE_CLOSED)) while ssn: self.log.debug("Closing Session") proton.pn_session_close(ssn) self._sessionContexts.remove(proton.pn_session_get_context(ssn)) ssn = proton.pn_session_next(ssn, (proton.PN_LOCAL_ACTIVE | proton.PN_REMOTE_CLOSED))
def _cleanLinks(self, conn): link = proton.pn_link_head(conn, (proton.PN_LOCAL_ACTIVE | proton.PN_REMOTE_CLOSED)) while link: self.log.debug("Closing Link") proton.pn_link_close(link) ssn = proton.pn_link_session(link) client = proton.pn_session_get_context(ssn) if link == client.sender: client.sender = None link = proton.pn_link_next(link, (proton.PN_LOCAL_ACTIVE | proton.PN_REMOTE_CLOSED))
def _cleanLinks(self, conn): link = proton.pn_link_head( conn, (proton.PN_LOCAL_ACTIVE | proton.PN_REMOTE_CLOSED)) while link: self.log.debug("Closing Link") proton.pn_link_close(link) ssn = proton.pn_link_session(link) client = proton.pn_session_get_context(ssn) if link == client.sender: client.sender = None link = proton.pn_link_next( link, (proton.PN_LOCAL_ACTIVE | proton.PN_REMOTE_CLOSED))
def _processIncoming(self, delivery, connector): link = proton.pn_delivery_link(delivery) ssn = proton.pn_link_session(link) msg = [] rc, buff = proton.pn_link_recv(link, 1024) while rc >= 0: msg.append(buff) rc, buff = proton.pn_link_recv(link, 1024) msg = ''.join(msg) proton.pn_delivery_update(delivery, proton.PN_ACCEPTED) msgObj = proton.Message() msgObj.decode(msg) ctx = proton.pn_session_get_context(ssn) ctx._pushIncomingMessage(msgObj.body) proton.pn_delivery_settle(delivery) proton.pn_link_advance(link) # if more credit is needed, grant it if proton.pn_link_credit(link) == 0: proton.pn_link_flow(link, 1)