def serialize(self, serialization): if not serialization in self.serialized_cache: serialized = ztreamy.serialize_events(self.events, serialization=serialization) self.serialized_cache[serialization] = serialized else: serialized = self.serialized_cache[serialization] return serialized
def publish(self, event, callback=None): """Publishes a new event.""" logger.logger.event_published(event) body = ztreamy.serialize_events([event]) sys.stdout.write(body) sys.stdout.flush() if callback is not None: def new_callback(): callback(_FakeResponse()) self.ioloop.add_callback(new_callback)
def send_initial_events(self, evs): serialized = ztreamy.serialize_events( \ evs, serialization=self.properties.serialization) if self.properties.encoding == ClientProperties.ENCODING_PLAIN: data = serialized elif self.properties.encoding == ClientProperties.ENCODING_ZLIB: data = dispatchers.compress_zlib(serialized) elif self.properties.encoding == ClientProperties.ENCODING_GZIP: data = dispatchers.compress_gzip(serialized) self.send(data)
def publish_events(self, events, callback=None): """Publishes a list of events. The events in the list 'events' are sent to the server in a new HTTP request. If a 'callback' is given, it will be called when the response is received from the server. The callback receives a tornado.httpclient.HTTPResponse parameter. """ body = ztreamy.serialize_events(events) self._send_request(body, callback=callback)
def publish_events(self, events): """Publishes a list of events. The events in the list 'events' are sent to the server in a new HTTP request. """ body = ztreamy.serialize_events(events, serialization=self.serialization_type) conn = httplib.HTTPConnection(self.hostname, self.port) conn.request('POST', self.path, body, self.headers) response = conn.getresponse() if response.status == 200: return True else: logging.error(str(response.status) + ' ' + response.reason) return False
def _body_producer(self, write): while True: if self.pending_events: data = ztreamy.serialize_events(self.pending_events, serialization=self.serialization_type) yield write(data) self.reconnection.notify_success() self.pending_events = [] if self.running: self.next_publication += self.buffering_time delay = self.next_publication - self.io_loop.time() if delay <= 0: self.next_publication = (self.io_loop.time() + self.buffering_time) delay = self.buffering_time yield tornado.gen.sleep(delay) else: break
def publish_events(self, events, callback=None): """Publishes a list of events. The events in the list 'events' are sent to the server in a new HTTP request. If a 'callback' is given, it will be called when the response is received from the server. The callback receives a tornado.httpclient.HTTPResponse parameter. """ body = ztreamy.serialize_events(events) conn = httplib.HTTPConnection(self.hostname, self.port) conn.request('POST', self.path, body, SynchronousEventPublisher._headers) response = conn.getresponse() if response.status == 200: return True else: logging.error(str(response.status) + ' ' + response.reason) return False
def publish_events(self, events): """Publishes a list of events. The events in the list 'events' are sent to the server in a new HTTP request. """ body = ztreamy.serialize_events(events) logging.info("Connecting to " + self.hostname + " on port " + str(self.port)) conn = httplib.HTTPConnection(self.hostname, self.port) conn.request('POST', self.path, body, ZtreamyClient._headers) response = conn.getresponse() if response.status == 200: logging.info("Got 200 status from " + self.path) logging.info("Sent :" + body) return True else: logging.error(str(response.status) + ' ' + response.reason) return False
def register_client(self, client, last_event_seen=None): if client.streaming: if client.priority: self.priority_clients.append(client) elif client.compress: self.unsynced_compressed_streaming_clients.append(client) else: self.streaming_clients.append(client) logging.info('Streaming client registered; stream: %i; comp: %i'\ %(client.streaming, client.compress)) if last_event_seen != None: # Send the available events after the last seen event evs, none_lost = self.recent_events.newer_than(last_event_seen) if len(evs) > 0: client.send(ztreamy.serialize_events(evs)) if not client.streaming: client.close() if client.local: self.local_clients.append(client) elif not client.streaming and not client.closed: self.one_time_clients.append(client)
def dispatch(self, evs): num_clients = (len(self.streaming_clients) + len(self.one_time_clients) + len(self.unsynced_compressed_streaming_clients) + len(self.compressed_streaming_clients) + len(self.local_clients)) logging.info('Sending %r events to %r clients', len(evs), num_clients) self.recent_events.append_events(evs) self._next_client_cleanup -= 1 if self._next_client_cleanup == 0: self.clean_closed_clients() if isinstance(evs, list): if evs == []: self._periods_since_last_event += 1 if self._periods_since_last_event > 20 and self._auto_finish: logger.logger.server_closed(num_clients) self.close() self.ioloop.stop() # Use the following line for the experiments ## if False: elif ((num_clients > 0 or len(self.priority_clients) > 0) and self._periods_since_last_event > 20): logging.info('Sending Test-Connection event') evs = [events.Command('', 'ztreamy-command', 'Test-Connection')] self._periods_since_last_event = 0 self.dispatch_priority(evs) else: return else: raise ZtreamyException('Bad event type', 'send_event') self._periods_since_last_event = 0 if len(self.unsynced_compressed_streaming_clients) > 0: if (len(self.compressed_streaming_clients) == 0 or self._num_events_since_sync > param_max_events_sync): self._sync_compressor() if num_clients > 0: logging.info('Compressed clients: %d synced; %d unsynced'%\ (len(self.compressed_streaming_clients), len(self.unsynced_compressed_streaming_clients))) serialized = ztreamy.serialize_events(evs) for client in self.streaming_clients: self._send(serialized, client) for client in self.unsynced_compressed_streaming_clients: self._send(serialized, client) for client in self.local_clients: if not client.closed: client._send_events(evs) for client in self.one_time_clients: self._send(serialized, client) client.close() if len(self.compressed_streaming_clients) > 0: compressed_data = (self._compressor.compress(serialized) + self._compressor.flush(zlib.Z_SYNC_FLUSH)) for client in self.compressed_streaming_clients: self._send(compressed_data, client) for e in evs: logger.logger.event_dispatched(e) self.one_time_clients = [] self._num_events_since_sync += len(evs)
def dispatch_priority(self, evs): if len(self.priority_clients) > 0 and len(evs) > 0: serialized = ztreamy.serialize_events(evs) for client in self.priority_clients: self._send(serialized, client)
def dispatch(self, evs): num_clients = (len(self.streaming_clients) + len(self.one_time_clients) + len(self.unsynced_compressed_streaming_clients) + len(self.compressed_streaming_clients) + len(self.local_clients)) logging.info('Sending %r events to %r clients', len(evs), num_clients) self.recent_events.append_events(evs) self._next_client_cleanup -= 1 if self._next_client_cleanup == 0: self.clean_closed_clients() if isinstance(evs, list): if evs == []: self._periods_since_last_event += 1 if self._periods_since_last_event > 20 and self._auto_finish: logger.logger.server_closed(num_clients) self.close() self.ioloop.stop() # Use the following line for the experiments ## if False: elif ((num_clients > 0 or len(self.priority_clients) > 0) and self._periods_since_last_event > 20): logging.info('Sending Test-Connection event') evs = [ events.Command('', 'ztreamy-command', 'Test-Connection') ] self._periods_since_last_event = 0 self.dispatch_priority(evs) else: return else: raise ZtreamyException('Bad event type', 'send_event') self._periods_since_last_event = 0 if len(self.unsynced_compressed_streaming_clients) > 0: if (len(self.compressed_streaming_clients) == 0 or self._num_events_since_sync > param_max_events_sync): self._sync_compressor() if num_clients > 0: logging.info('Compressed clients: %d synced; %d unsynced'%\ (len(self.compressed_streaming_clients), len(self.unsynced_compressed_streaming_clients))) serialized = ztreamy.serialize_events(evs) for client in self.streaming_clients: self._send(serialized, client) for client in self.unsynced_compressed_streaming_clients: self._send(serialized, client) for client in self.local_clients: if not client.closed: client._send_events(evs) for client in self.one_time_clients: self._send(serialized, client) client.close() if len(self.compressed_streaming_clients) > 0: compressed_data = (self._compressor.compress(serialized) + self._compressor.flush(zlib.Z_SYNC_FLUSH)) for client in self.compressed_streaming_clients: self._send(compressed_data, client) for e in evs: logger.logger.event_dispatched(e) self.one_time_clients = [] self._num_events_since_sync += len(evs)