def alertrequest(self, event): """AlertManager event handler for incoming events :param event with incoming AlertManager message """ hfoslog("[ALERT] Event: '%s'" % event.__dict__) try: action = event.action data = event.data if action == 'mob': if data == True: hfoslog("[ALERT] MOB ALERT ACTIVATED.", lvl=critical) self.mobalert = True self._recordMobAlert() alertpacket = {'component': 'alert', 'action': 'mob', 'data': True} else: hfoslog("[ALERT] MOB deactivation requested by ", event.user.account.username, lvl=warn) self.mobalert = False alertpacket = {'component': 'alert', 'action': 'mob', 'data': False} else: hfoslog("[ALERT] Unsupported action requested: ", event, lvl=warn) return try: self.fireEvent(broadcast("users", alertpacket)) except Exception as e: hfoslog("[ALERT] Transmission error before broadcast: %s" % e, lvl=error) except Exception as e: hfoslog("[ALERT] Error: '%s' %s" % (e, type(e)), lvl=error)
def navdatapush(self): """ Pushes the current :referenceframe: out to clients. :return: """ try: self.fireEvent( referenceframe({ 'data': self.referenceframe, 'ages': self.referenceages }), "navdata") self.intervalcount += 1 if self.intervalcount == self.passiveinterval: self.fireEvent( broadcast( 'users', { 'component': 'navdata', 'action': 'update', 'data': { 'data': self.referenceframe.serializablefields(), 'ages': self.referenceages } }), "hfosweb") self.intervalcount = 0 hfoslog("[NAVDATA] Reference frame successfully pushed.", lvl=verbose) except Exception as e: hfoslog("[NAVDATA] Could not push referenceframe: ", e, type(e), lvl=critical)
def chatrequest(self, event): """Chat event handler for incoming events :param event: ChatRequest with incoming chat message """ hfoslog("[CHAT] Event: '%s'" % event.__dict__) try: action = event.action data = event.data username = self._getusername(event) if action == "say": chatpacket = { 'component': 'chat', 'action': 'broadcast', 'data': { 'sender': username, 'timestamp': time(), 'content': data.encode('utf-8') } } else: hfoslog("[CHAT] Unsupported action: ", action, event, lvl=warn) return try: self.fireEvent(broadcast("users", chatpacket)) except Exception as e: hfoslog("[CHAT] Transmission error before broadcast: %s" % e, lvl=error) except Exception as e: hfoslog("[CHAT] Error: '%s' %s" % (e, type(e)), lvl=error)
def chatrequest(self, event): """Chat event handler for incoming events :param event: ChatRequest with incoming chat message """ hfoslog("[CHAT] Event: '%s'" % event.__dict__) try: action = event.action data = event.data username = self._getusername(event) if action == "say": chatpacket = {'component': 'chat', 'action': 'broadcast', 'data': { 'sender': username, 'timestamp': time(), 'content': data.encode('utf-8') } } else: hfoslog("[CHAT] Unsupported action: ", action, event, lvl=warn) return try: self.fireEvent(broadcast("users", chatpacket)) except Exception as e: hfoslog("[CHAT] Transmission error before broadcast: %s" % e, lvl=error) except Exception as e: hfoslog("[CHAT] Error: '%s' %s" % (e, type(e)), lvl=error)
def alertrequest(self, event): """AlertManager event handler for incoming events :param event with incoming AlertManager message """ hfoslog("[ALERT] Event: '%s'" % event.__dict__) try: action = event.action data = event.data if action == 'mob': if data == True: hfoslog("[ALERT] MOB ALERT ACTIVATED.", lvl=critical) self.mobalert = True self._recordMobAlert() alertpacket = { 'component': 'alert', 'action': 'mob', 'data': True } else: hfoslog("[ALERT] MOB deactivation requested by ", event.user.account.username, lvl=warn) self.mobalert = False alertpacket = { 'component': 'alert', 'action': 'mob', 'data': False } else: hfoslog("[ALERT] Unsupported action requested: ", event, lvl=warn) return try: self.fireEvent(broadcast("users", alertpacket)) except Exception as e: hfoslog("[ALERT] Transmission error before broadcast: %s" % e, lvl=error) except Exception as e: hfoslog("[ALERT] Error: '%s' %s" % (e, type(e)), lvl=error)
def ping(self, *args): """Pings all connected clients with stupid ping/demo messages (for now) :param args: """ self._count += 1 hfoslog("[DEMO] Ping %i:" % self._count, args) data = {'component': 'ping', 'action': "Hello"} if (self._count % 2) == 0: data = { 'component': 'navdata', 'action': 'update', 'data': { 'true_course': randint(0, 359), 'spd_over_grnd': randint(0, 50) } } self.fireEvent(broadcast("clients", json.dumps(data)))
def ping(self, *args): """Pings all connected clients with stupid ping/demo messages (for now) :param args: """ self._count += 1 hfoslog("[DEMO] Ping %i:" % self._count, args) data = {'component': 'ping', 'action': "Hello" } if (self._count % 2) == 0: data = {'component': 'navdata', 'action': 'update', 'data': {'true_course': randint(0, 359), 'spd_over_grnd': randint(0, 50) } } self.fireEvent(broadcast("clients", json.dumps(data)))
def navdatapush(self): """ Pushes the current :referenceframe: out to clients. :return: """ try: self.fireEvent(referenceframe({'data': self.referenceframe, 'ages': self.referenceages}), "navdata") self.intervalcount += 1 if self.intervalcount == self.passiveinterval: self.fireEvent(broadcast('users', { 'component': 'navdata', 'action': 'update', 'data': { 'data': self.referenceframe.serializablefields(), 'ages': self.referenceages } }), "hfosweb") self.intervalcount = 0 hfoslog("[NAVDATA] Reference frame successfully pushed.", lvl=verbose) except Exception as e: hfoslog("[NAVDATA] Could not push referenceframe: ", e, type(e), lvl=critical)
def mapviewrequest(self, event): """ Handles new mapview category requests :param event: MapviewRequest with basic action types: * subscribe * unsubscribe * update * list * get """ hfoslog("[MVM] Event: '%s'" % event.__dict__, lvl=debug) try: try: userobj = event.user action = event.action data = event.data try: nickname = userobj.profile.nick except AttributeError: nickname = userobj.account.username useruuid = userobj.uuid hfoslog("[MVM] client: ", event.client.__dict__) clientuuid = event.client.uuid except Exception as e: raise ValueError("[MVM] Problem during event unpacking:", e, type(e)) if action == 'list': try: dblist = self._generatemapviewlist() self.fireEvent(send(clientuuid, {'component': 'mapview', 'action': 'list', 'data': dblist})) except Exception as e: hfoslog("[MVM] Listing error: ", e, type(e), lvl=error) return elif action == 'get': dbmapview = None try: dbmapview = mapviewobject.find_one({'uuid': useruuid}) except Exception as e: hfoslog("[MVM] Get for MapView failed, creating new one.", e) if not dbmapview: try: # TODO: Move this into a seperate operation (create) dbmapview = mapviewobject({'uuid': str(uuid4()), 'useruuid': useruuid, 'name': '%s Default MapView' % nickname, 'shared': True, }) hfoslog("[MVM] New mapviewobject: ", dbmapview) dbmapview.save() except Exception as e: hfoslog("[MVM] Storing new view failed: ", e, type(e), lvl=error) if dbmapview: self.fireEvent(send(clientuuid, {'component': 'mapview', 'action': 'get', 'data': dbmapview.serializablefields()})) return elif action == 'subscribe': try: dbmapview = mapviewobject.find_one({'uuid': data}) except Exception as e: hfoslog("[MVM] Get for MapView failed. Creating new one.", e) if not dbmapview: dbmapview = mapviewobject({'uuid': str(uuid4()), 'useruuid': useruuid, 'name': '%s Default MapView' % nickname, 'shared': True, }) if data in self._subscribers: if clientuuid not in self._subscribers[data]: self._subscribers[data].append(clientuuid) else: self._subscribers[data] = [clientuuid] hfoslog("[MVM] Subscription registered: ", data, clientuuid, dbmapview.to_dict()) self.fireEvent(send(clientuuid, {'component': 'mapview', 'action': 'get', 'data': dbmapview.serializablefields()})) return elif action == 'unsubscribe': self._unsubscribe(clientuuid, data) return # The following need a mapview object attached try: mapview = mapviewobject(data) mapview.validate() except Exception as e: hfoslog("[MVM] Only mapview related actions left, but no Mapviewobject.", e, type(e), lvl=warn) return if action == 'update': hfoslog("[MVM] Update begin") try: uuid = mapview.uuid dbmapview = mapviewobject.find_one({'uuid': uuid}) hfoslog("[MVM] Database Mapview dict: ", dbmapview.__dict__, lvl=error) except Exception as e: hfoslog("[MVM] Couldn't get mapview", (data, e, type(e)), lvl=error) return if dbmapview: try: dbmapview.update(mapview._fields) dbmapview.save() hfoslog("[MVM] Valid update stored.") except Exception as e: hfoslog("[MVM] Database mapview update failed: ", (uuid, e, type(e)), lvl=error) return else: try: hfoslog("[MVM] New MapView: ", mapview) mapview.uuid = uuid # make sure mapview.save() dbmapview = mapview hfoslog("[MVM] New MapView stored.") if mapview.shared: hfoslog("[MVM] Broadcasting list update for new mapview.") self.fireEvent(broadcast("users", self._generatemapviewlist())) except Exception as e: hfoslog("[MVM] MapView creation error: '%s' (%s)" % (e, type(e)), lvl=error) return try: hfoslog("[MVM] Subscriptions: ", self._subscribers) hfoslog("[MVM] dbmapview: ", mapview._fields) if dbmapview.shared: if dbmapview.uuid in self._subscribers: try: hfoslog("[MVM] Broadcasting mapview update to subscribers.") mapviewpacket = {'component': 'mapview', 'action': 'update', 'data': mapview.serializablefields() } self._broadcast(mapviewpacket, dbmapview.uuid) except Exception as e: hfoslog("[MVM] Transmission error before broadcast: %s" % e) else: hfoslog("[MVM] Not subscribed.") else: hfoslog("[MVM] Not shared.") except Exception as e: hfoslog("[MVM] Update error during final mapview handling", e) except Exception as e: hfoslog("[MVM] Global Error: '%s' %s" % (e, type(e)), lvl=error)
def mapviewrequest(self, event): """ Handles new mapview category requests :param event: MapviewRequest with basic action types: * subscribe * unsubscribe * update * list * get """ hfoslog("[MVM] Event: '%s'" % event.__dict__, lvl=debug) try: try: userobj = event.user action = event.action data = event.data try: nickname = userobj.profile.nick except AttributeError: nickname = userobj.account.username useruuid = userobj.uuid hfoslog("[MVM] client: ", event.client.__dict__) clientuuid = event.client.uuid except Exception as e: raise ValueError("[MVM] Problem during event unpacking:", e, type(e)) if action == 'list': try: dblist = self._generatemapviewlist() self.fireEvent( send( clientuuid, { 'component': 'mapview', 'action': 'list', 'data': dblist })) except Exception as e: hfoslog("[MVM] Listing error: ", e, type(e), lvl=error) return elif action == 'get': dbmapview = None try: dbmapview = mapviewobject.find_one({'uuid': useruuid}) except Exception as e: hfoslog("[MVM] Get for MapView failed, creating new one.", e) if not dbmapview: try: # TODO: Move this into a seperate operation (create) dbmapview = mapviewobject({ 'uuid': str(uuid4()), 'useruuid': useruuid, 'name': '%s Default MapView' % nickname, 'shared': True, }) hfoslog("[MVM] New mapviewobject: ", dbmapview) dbmapview.save() except Exception as e: hfoslog("[MVM] Storing new view failed: ", e, type(e), lvl=error) if dbmapview: self.fireEvent( send( clientuuid, { 'component': 'mapview', 'action': 'get', 'data': dbmapview.serializablefields() })) return elif action == 'subscribe': try: dbmapview = mapviewobject.find_one({'uuid': data}) except Exception as e: hfoslog("[MVM] Get for MapView failed. Creating new one.", e) if not dbmapview: dbmapview = mapviewobject({ 'uuid': str(uuid4()), 'useruuid': useruuid, 'name': '%s Default MapView' % nickname, 'shared': True, }) if data in self._subscribers: if clientuuid not in self._subscribers[data]: self._subscribers[data].append(clientuuid) else: self._subscribers[data] = [clientuuid] hfoslog("[MVM] Subscription registered: ", data, clientuuid, dbmapview.to_dict()) self.fireEvent( send( clientuuid, { 'component': 'mapview', 'action': 'get', 'data': dbmapview.serializablefields() })) return elif action == 'unsubscribe': self._unsubscribe(clientuuid, data) return # The following need a mapview object attached try: mapview = mapviewobject(data) mapview.validate() except Exception as e: hfoslog( "[MVM] Only mapview related actions left, but no Mapviewobject.", e, type(e), lvl=warn) return if action == 'update': hfoslog("[MVM] Update begin") try: uuid = mapview.uuid dbmapview = mapviewobject.find_one({'uuid': uuid}) hfoslog("[MVM] Database Mapview dict: ", dbmapview.__dict__, lvl=error) except Exception as e: hfoslog("[MVM] Couldn't get mapview", (data, e, type(e)), lvl=error) return if dbmapview: try: dbmapview.update(mapview._fields) dbmapview.save() hfoslog("[MVM] Valid update stored.") except Exception as e: hfoslog("[MVM] Database mapview update failed: ", (uuid, e, type(e)), lvl=error) return else: try: hfoslog("[MVM] New MapView: ", mapview) mapview.uuid = uuid # make sure mapview.save() dbmapview = mapview hfoslog("[MVM] New MapView stored.") if mapview.shared: hfoslog( "[MVM] Broadcasting list update for new mapview." ) self.fireEvent( broadcast("users", self._generatemapviewlist())) except Exception as e: hfoslog("[MVM] MapView creation error: '%s' (%s)" % (e, type(e)), lvl=error) return try: hfoslog("[MVM] Subscriptions: ", self._subscribers) hfoslog("[MVM] dbmapview: ", mapview._fields) if dbmapview.shared: if dbmapview.uuid in self._subscribers: try: hfoslog( "[MVM] Broadcasting mapview update to subscribers." ) mapviewpacket = { 'component': 'mapview', 'action': 'update', 'data': mapview.serializablefields() } self._broadcast(mapviewpacket, dbmapview.uuid) except Exception as e: hfoslog( "[MVM] Transmission error before broadcast: %s" % e) else: hfoslog("[MVM] Not subscribed.") else: hfoslog("[MVM] Not shared.") except Exception as e: hfoslog("[MVM] Update error during final mapview handling", e) except Exception as e: hfoslog("[MVM] Global Error: '%s' %s" % (e, type(e)), lvl=error)