4 from planetstack.config import Config
11 from fofum import Fofum
16 def get_random_client_id():
17 global random_client_id
19 if (random_client_id is None) and os.path.exists("/opt/planetstack/random_client_id"):
20 # try to use the last one we used, if we saved it
22 random_client_id = open("/opt/planetstack/random_client_id","r").readline().strip()
23 print "get_random_client_id: loaded %s" % random_client_id
25 print "get_random_client_id: failed to read /opt/planetstack/random_client_id"
27 if random_client_id is None:
28 random_client_id = base64.urlsafe_b64encode(os.urandom(12))
29 print "get_random_client_id: generated new id %s" % random_client_id
31 # try to save it for later (XXX: could race with another client here)
33 open("/opt/planetstack/random_client_id","w").write("%s\n" % random_client_id)
35 print "get_random_client_id: failed to write /opt/planetstack/random_client_id"
37 return random_client_id
39 # decorator that marks dispatachable event methods
41 setattr(func, 'event', func.__name__)
45 # This code is currently not in use.
52 for name in dir(EventHandler):
53 attribute = getattr(EventHandler, name)
54 if hasattr(attribute, 'event'):
55 events.append(getattr(attribute, 'event'))
58 def dispatch(self, event, *args, **kwds):
59 if hasattr(self, event):
60 return getattr(self, event)(*args, **kwds)
64 def __init__(self,user=None,clientid=None):
66 user = Config().feefie_client_user
71 clid = Config().feefie_client_id
73 clid = get_random_client_id()
74 print "EventSender: no feefie_client_id configured. Using random id %s" % clid
76 self.fofum = Fofum(user=user)
79 def fire(self,**kwargs):
80 kwargs["uuid"] = str(uuid.uuid1())
81 self.fofum.fire(json.dumps(kwargs))
84 def __init__(self,wake_up=None):
85 self.handler = EventHandler()
86 self.wake_up = wake_up
88 def handle_event(self, payload):
89 payload_dict = json.loads(payload)
95 # This is our unique client id, to be used when firing and receiving events
96 # It needs to be generated once and placed in the config file
99 user = Config().feefie_client_user
104 clid = Config().feefie_client_id
106 clid = get_random_client_id()
107 print "EventListener: no feefie_client_id configured. Using random id %s" % clid
111 listener_thread = threading.Thread(target=f.listen_for_event,args=(clid,self.handle_event))
112 listener_thread.start()