def connect(self): super(OutboundEventSocket, self).connect() # Starts event handler for this client/session. self.start_event_handler() # Sends connect and sets timeout while connecting. timer = Timeout(self.transport.get_connect_timeout()) timer.start() try: connect_response = self._protocol_send("connect") if not connect_response.is_success(): raise ConnectError("Error while connecting") except Timeout: raise ConnectError("Timeout connecting") finally: timer.cancel() # Sets channel and channel unique id from this event self._channel = connect_response self._uuid = connect_response.get_header("Unique-ID") # Set connected flag to True self.connected = True # Sets event filter or raises ConnectError if self._filter: if self._is_eventjson: self.trace("using eventjson") filter_response = self.eventjson(self._filter) else: self.trace("using eventplain") filter_response = self.eventplain(self._filter) if not filter_response.is_success(): raise ConnectError("Event filter failure")
def run(self): super(InboundEventSocket, self).connect() # Connects transport, if connection fails, raise ConnectError try: self.transport.connect() except Exception, e: raise ConnectError("Transport failure: %s" % str(e))
def read_raw(self, event): ''' Reads raw data based on Event Content-Length. Returns raw string or None if not found. ''' length = event.get_content_length() # Reads length bytes if length > 0 if length: res = self.transport.read(int(length)) if not res or len(res) != int(length): raise ConnectError("no more data in read_raw !") return res return None
def _wait_auth_request(self): ''' Waits until auth/request event is received. ''' # Sets timeout to wait for auth/request timer = Timeout(self.transport.get_connect_timeout()) timer.start() try: # When auth/request is received, # _auth_request method will wake up async result # so we will just wait this event here. return self._wait_auth_event.get() except Timeout: raise ConnectError("Timeout waiting auth/request") finally: timer.cancel()
def read_event(self): ''' Reads one Event from socket until EOL. Returns Event instance. Raises LimitExceededError if MAXLINES_PER_EVENT is reached. ''' buff = '' for x in range(MAXLINES_PER_EVENT): line = self.transport.read_line() if line == '': self.trace("no more data in read_event !") raise ConnectError("connection closed") elif line == EOL: # When matches EOL, creates Event and returns it. return Event(buff) else: # Else appends line to current buffer. buff = "%s%s" % (buff, line) raise LimitExceededError("max lines per event (%d) reached" % MAXLINES_PER_EVENT)
self.connected = True # Be sure command pool is empty before starting self._flush_commands() # Starts handling events self.start_event_handler() # Waits for auth/request, if timeout, raises ConnectError self._wait_auth_request() # We are ready now ! # Authenticate or raise ConnectError auth_response = self.auth(self.password) if not auth_response.is_reply_text_success(): raise ConnectError("Auth failure") # Sets event filter or raises ConnectError if self._filter: if self._is_eventjson: filter_response = self.eventjson(self._filter) else: filter_response = self.eventplain(self._filter) if not filter_response.is_reply_text_success(): raise ConnectError("Event filter failure") return def serve_forever(self): """ Starts waiting for events in endless loop. """
def write(self, data): if self.closed: raise ConnectError('not connected') self.sockfd.write(data) self.sockfd.flush()