def send_channel_update(category, clients=None): game_ent = get_current_game() players = [x for x in ndb.get_multi(game_ent.players)] player_names = [x.name for x in players] recipients = player_names if clients is None else clients message = {'category': category} if category == 'players': message['player_count'] = len(player_names) message['player_names'] = player_names # always tell everyone about 'players' updates recipients = player_names # TODO: consider making a separate 'nextround' event elif category == 'leader' or category == 'follower': # handle common data first message['round'] = game_ent.round_ message['sequence_length'] = game_ent.sequence_length if category == 'follower': message['leader'] = game_ent.leader elif category == 'copysequence': message['sequence'] = g.sequence elif category == 'sequencematch': pass elif category == 'sequencemismatch': pass else: abort(404) # slow, doing lots of membership testing channels_to_send = [x.channel_token for x in players if x.name in recipients and x.channel_token] for channel_token in channels_to_send: channel.send_message(channel_token, json.dumps(message))
def get(self): # every 1 minute feeds = {"nrg": "http://rss.nrg.co.il/newsflash/", "וואלה!": "http://rss.walla.co.il/?w=/1/22/0/@rss", "ynet": "http://www.ynet.co.il/Integration/StoryRss1854.xml", "mako": "http://rcs.mako.co.il/rss/news-israel.xml"} updates = [] for name, url in feeds.iteritems(): for entry in feedparser.parse(url).entries: if Update.get_by_key_name(entry.title) is None: entry = Update(key_name=entry.title, content=utils.force_unicode(entry.title), date=datetime.datetime.fromtimestamp(time.mktime(entry.date_parsed)), source=utils.force_unicode(name), description=utils.force_unicode(entry.description)); entry.put() updates.append(entry) updates.sort(key=lambda item:item.date, reverse=True) for client_id in clients: try: channel.send_message(client_id, utils.updates_to_json(updates)) except: clients.remove(client_id)
def run(self, auth_pair, photos, locs=None): if not photos or not len(photos): return photoGroup = photos[0] if not locs: locs = self.getGroupLocs(auth_pair, photoGroup) # this took some time, schedule it for new run deferred.defer(self.run, auth_pair, photos, locs) return photo = photoGroup[0] loc = self.getClosestLoc(locs, photo.exif.time.text) if loc : #logging.info("updating photo: "+photo.title.text) self.updatePhoto(auth_pair, photo, loc) msg = { 'title': photo.title.text, 'thumbnail': photo.media.thumbnail[0].url, 'lat':loc['latitude'], 'lng': loc['longitude'] }; channel.send_message(auth_pair.token, json.dumps(msg)) photoGroup.pop(0) if len(photoGroup) == 0: photos.pop(0) locs = None if not len(photos) == 0: deferred.defer(self.run, auth_pair, photos, locs)
def send_to_all(message, tokens=None): if not tokens: tokens = memcache.get('tokens') if tokens: for token, id in tokens.iteritems(): if isinstance(id ,int): id = 'anonymous(%s)' %id channel.send_message(id, message)
def update_web(self, channel, txt): from google.appengine.api.channel import channel as gchan chan = ChannelBase(channel, botname="gae-web") logging.warn("%s - webchannels are %s" % (self.cfg.name, chan.data.webchannels)) remove = [] for c in chan.data.webchannels: try: if c: logging.warn("%s - sending %s to channel %s" % (self.cfg.name, txt, c)) gchan.send_message(c, txt) except gchan.InvalidChannelClientIdError: remove.append(c) if remove: for c in remove: chan.data.webchannels.remove(c) ; logging.warn("%s - closing channel %s" % (self.cfg.name, c)) chan.save()
def post(self): """ TODO: not yet commented. """ suite = Suite(key_name=uuid.uuid4().__str__()) suite.put() # run the test suite is_compliant = True suite.service_uri = self.request.get('url') for elem in dir(tests): if elem.find('ctf_') != -1: func = getattr(tests, elem) # initialize a new test object test = Test(suite=suite) test.put() test.name = func.__name__ test.description = func.__doc__.strip() # run the individual test auth = self.request.get('auth') if auth: token = base64.b64encode(self.request.get('user') + ':' + self.request.get('pass')) test.result = func(test, suite.service_uri, token) else: test.result = func(test, suite.service_uri) is_compliant &= test.result # store test to database and add to result set test.put() channel.send_message(self.request.get('client'), simplejson.dumps(test.to_dict())) suite.is_compliant = is_compliant suite.put() #channel.send_message(self.request.get('client'), simplejson.dumps(suite.to_dict())) self.response.set_status(202) self.response.headers['Content-type'] = 'application/json' self.response.headers.add_header('Location', self.request.url + '/archive/' + suite.key().name()) self.response.out.write(simplejson.dumps(suite.to_dict())) # eof
def post(self): """ TODO: not yet commented. """ suite = model.Suite(key_name=uuid.uuid4().__str__()) user = users.get_current_user() if user: suite.user = user suite.put() # run the test suite is_compliant = True suite.service_uri = self.request.get('url') for ctf in tests.ctfs: # initialize a new test object test = model.Test(suite=suite) test.put() test.name = ctf.__name__ test.description = ctf.__doc__.strip() # run the individual test auth = self.request.get('auth') if auth: token = base64.b64encode(self.request.get('user') + ':' + self.request.get('pass')) # XXX: add better handling for HTTP Basic Auth test.result = ctf(test, suite.service_uri, 'Basic ' + token) else: test.result = ctf(test, suite.service_uri) is_compliant &= test.result # store test to database and add to result set test.put() channel.send_message(self.request.get('client'), simplejson.dumps(test.to_dict())) suite.is_compliant = is_compliant suite.put() #channel.send_message(self.request.get('client'), simplejson.dumps(suite.to_dict())) self.response.set_status(202) self.response.headers['Content-type'] = str('application/json') self.response.headers.add_header(str('Location'), str(self.request.url + '/archive/' + suite.key().name())) self.response.out.write(simplejson.dumps(suite.to_dict(flatten_date=True)))
def update_web(self, channel, txt, end="<br>"): from google.appengine.api.channel import channel as gchan chan = ChannelBase(channel, botname="gae-web") #logging.warn("%s - webchannels are %s" % (self.name, chan.data.webchannels)) remove = [] for c in chan.data.webchannels: try: if c: logging.debug("%s - sending to channel %s" % (self.name, chan)) gchan.send_message(c, txt + end) except gchan.InvalidChannelClientIdError: remove.append(c) if remove: for c in remove: chan.data.webchannels.remove(c) chan.save()
def push_to_channels(room, message): client_set = get_clients(room) unencrypted_message = message for u in client_set: if u.connected: encrypted_message = Message() client_e = u.e client_n = u.n if client_e: client_e = float(client_e) client_n = float(client_n) encrypted_message.text = crypto_helper.encrypt(None, client_n, client_e, message.text) else: encrypted_message.text = message.text encrypted_message.sender_email = message.sender_email encrypted_message.room_name = message.room_name json = encrypted_message.to_json() channel.send_message(u.client_id, json)
def post(self): if not self.requireParameters(POST_MSG_PEER, POST_MSG_CONTENTS, POST_MSG_PUBLCIKEY_CALCULATED, POST_MSG_PUBLICKEY_USED): return receiver = self.user_model.get_by_auth_id(self.request.POST[POST_MSG_PEER]) if not receiver: self.response.write(RESPONSE_MSG_NO_PEER) return if not receiver.loginToken: self.response.write(RESPONSE_PEER_INACTIVE) return usedPublicKey = self.request.POST[POST_MSG_PUBLICKEY_USED] calculatedPublicKey = self.request.POST[POST_MSG_PUBLCIKEY_CALCULATED] contents = self.request.POST[POST_MSG_CONTENTS] signature = None if POST_MSG_SM_SIGNATURE in self.request.POST: signature = self.request.POST[POST_MSG_SM_SIGNATURE] MessageModelV2.create(receiver.getId(), self.user.auth_ids[0], contents, usedPublicKey, calculatedPublicKey, signature=signature) if hasattr(receiver, "GCMKey"): receiverGCM = receiver.GCMKey else: receiverGCM = None if hasattr(receiver, "WNSUri"): receiverWNS = receiver.WNSUri else: receiverWNS = None if hasattr(receiver, "channelActive"): receiverChannel = receiver.auth_ids[0] else: receiverChannel = None if (receiverGCM is not None) and (receiverGCM != ""): GCMNotify(receiverGCM, self.request.POST[POST_MSG_PEER]) if (receiverWNS is not None) and (receiverWNS != ""): WNSNotify(receiverWNS, self.request.POST[POST_MSG_PEER]) if receiverChannel is not None: channel.send_message(receiverChannel, receiverChannel) self.response.write(RESPONSE_OK)