def new_message(client_id,message,channel_name,system): """ publish a new message into the channel message queue client_id - the publishing client id message - a string or object to publish,if it is a object it is sent as a json string channel_name - the name of the channel to publish this message system - true for system messages(like unsubscribe because of GC) """ if not (type(message) is str or type(message) is unicode): message = dumps(message) get_channel(channel_name).new_message(client_id,message,system) return 1
def get_channel_clients(channel_name): """ get all the clients subscribed to the channel channel_name - name of the channel """ return get_channel(channel_name).get_active_clients()
def get_updates(clients): """ get all updates from the channels and waits until one channel have new information """ #channel name -> messages updates = {} #events to wait for events = [] #cursors for each channel cursors = [] for client in clients: client.touch() for channel_name,cursor in client.channels.iteritems(): if channel_exist(channel_name): cursors.append(ChannelCursor(get_channel(channel_name),cursor)) #check for available messages for cc in cursors: update = cc.channel.get_updates(cc.cursor) if type(update) is Event: events.append(update) else: updates[cc.channel.name] = update #found messages,so just return them if len(updates) > 0: return updates #no messages,so wait for any event elif len(events) > 0: fired = False try: #mark the client as waiting so the GC doesn't collect them for client in clients: client.mark_waiting() #wait for any event to get fired up to COMET_TIMEOUT seconds with Timeout(COMET_TIMEOUT,False) as timeout: waitany(events + [client.channels_event for client in clients]) fired = True finally: #clear the marking for client in clients: client.touch() client.clear_waiting() if fired: return get_updates(clients) else:#timeout return {} #no channels else: return {}