def transmit(self, frame): """ Convert a frame object to a frame string and transmit to the server. """ for listener in self.listeners.values(): if not listener: continue if not hasattr(listener, 'on_send'): continue listener.on_send(frame) lines = utils.convert_frame_to_lines(frame) packed_frame = pack(lines) log.info("Sending frame %s", lines) if self.socket is not None: try: self.__socket_semaphore.acquire() try: self.send_over_socket(encode(packed_frame)) finally: self.__socket_semaphore.release() except Exception: _, e, _ = sys.exc_info() log.error("Error sending frame", exc_info=1) raise e else: raise exception.NotConnectedException()
def send(self, destination, body, content_type=None, headers={}, **keyword_headers): assert destination is not None, "'destination' is required" assert body is not None, "'body' is required" headers = utils.merge_headers([headers, keyword_headers]) headers[HDR_DESTINATION] = destination if content_type: headers[HDR_CONTENT_TYPE] = content_type body = encode(body) # if HDR_CONTENT_LENGTH not in headers: # headers[HDR_CONTENT_LENGTH] = len(body) self.__send_frame(CMD_SEND, headers, body)
def __send_frame(self, command, headers={}, payload=''): """ Send a STOMP frame. \param command the frame command \param headers a map of headers (key-val pairs) \param payload the message payload """ if type(payload) == dict: headers["transformation"] = "jms-map-xml" payload = self.__convert_dict(payload) if payload: payload = encode(payload) if hasbyte(0, payload): headers.update({'content-length': len(payload)}) if self.__socket is not None: try: frame = [] if command is not None: frame.append(command + '\n') for key, val in headers.items(): frame.append('%s:%s\n' % (key, val)) frame.append('\n') if payload: frame.append(payload) if command is not None: # only send the terminator if we're sending a command (heartbeats have no term) frame.append(NULL) frame = pack(frame) self.__socket_semaphore.acquire() try: socksend(self.__socket, frame) log.debug("Sent frame: type=%s, headers=%r, body=%r" % (command, headers, payload)) finally: self.__socket_semaphore.release() except Exception: _, e, _ = sys.exc_info() log.error("Error sending frame: %s" % e) raise e else: raise exception.NotConnectedException()
def send(self, destination, body, content_type=None, headers={}, **keyword_headers): assert destination is not None, "'destination' is required" assert body is not None, "'body' is required" headers = utils.merge_headers([headers, keyword_headers]) headers[HDR_DESTINATION] = destination if content_type: headers[HDR_CONTENT_TYPE] = content_type body = encode(body) #if HDR_CONTENT_LENGTH not in headers: # headers[HDR_CONTENT_LENGTH] = len(body) self.send_frame(CMD_SEND, headers, body)
def __send_frame(self, command, headers={}, payload=''): """ Send a STOMP frame. \param command the frame command \param headers a map of headers (key-val pairs) \param payload the message payload """ if type(payload) == dict: headers["transformation"] = "jms-map-xml" payload = self.__convert_dict(payload) if payload: payload = encode(payload) if hasbyte(0, payload): headers.update({'content-length': len(payload)}) if self.__socket is not None: try: frame = [ ] if command is not None: frame.append(command + '\n') for key, val in headers.items(): frame.append('%s:%s\n' % (key, val)) frame.append('\n') if payload: frame.append(payload) if command is not None: # only send the terminator if we're sending a command (heartbeats have no term) frame.append(NULL) frame = pack(frame) self.__socket_semaphore.acquire() try: socksend(self.__socket, frame) log.debug("Sent frame: type=%s, headers=%r, body=%r" % (command, headers, payload)) finally: self.__socket_semaphore.release() except Exception: _, e, _ = sys.exc_info() log.error("Error sending frame: %s" % e) raise e else: raise exception.NotConnectedException()
def send_frame(self, frame): """ Convert a frame object to a frame string and transmit to the server. """ for listener in self.listeners.values(): if not listener: continue if not hasattr(listener, 'on_send'): continue listener.on_send(frame) lines = [ ] if frame.cmd: lines.append(frame.cmd) lines.append("\n") for key, vals in sorted(frame.headers.items()): if type(vals) != tuple: vals = ( vals, ) for val in vals: lines.append('%s:%s\n' % (key, val)) lines.append('\n') if frame.body: lines.append(frame.body) if frame.cmd: lines.append(NULL) packed_frame = pack(lines) log.info("Sending frame %s" % lines) if self.__socket is not None: try: self.__socket_semaphore.acquire() try: self.__socket.sendall(encode(packed_frame)) finally: self.__socket_semaphore.release() except Exception: _, e, _ = sys.exc_info() log.error("Error sending frame: %s" % e) raise e else: raise exception.NotConnectedException()