blob: f7615eb9243a7a0a6e594a568b40b2b83761683d [file] [log] [blame]
Tony Mack79a49c82013-06-15 23:51:57 -04001import threading
2import requests, json
Sapan Bhatia66f4e612013-07-02 12:12:38 -04003
Sapan Bhatia66f4e612013-07-02 12:12:38 -04004from planetstack.config import Config
Tony Mack79a49c82013-06-15 23:51:57 -04005
Scott Bakerc1c45f82014-01-21 16:23:51 -08006import uuid
Sapan Bhatia9182b322013-06-25 16:22:14 -04007import os
Scott Baker6ecd4262014-01-21 23:15:21 -08008import imp
9import inspect
Sapan Bhatia9182b322013-06-25 16:22:14 -040010import base64
Tony Mack5c0c4552013-07-03 09:36:51 -040011from fofum import Fofum
Sapan Bhatiadbaf1932013-09-03 11:28:52 -040012import json
Scott Baker6ecd4262014-01-21 23:15:21 -080013import traceback
Sapan Bhatia9182b322013-06-25 16:22:14 -040014
Scott Baker3d5a2782014-05-13 09:58:02 -070015random_client_id=None
16def get_random_client_id():
17 global random_client_id
18
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
21 try:
22 random_client_id = open("/opt/planetstack/random_client_id","r").readline().strip()
23 print "get_random_client_id: loaded %s" % random_client_id
24 except:
25 print "get_random_client_id: failed to read /opt/planetstack/random_client_id"
26
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
30
31 # try to save it for later (XXX: could race with another client here)
32 try:
33 open("/opt/planetstack/random_client_id","w").write("%s\n" % random_client_id)
34 except:
35 print "get_random_client_id: failed to write /opt/planetstack/random_client_id"
36
37 return random_client_id
38
39# decorator that marks dispatachable event methods
Tony Mack79a49c82013-06-15 23:51:57 -040040def event(func):
Sapan Bhatia1b84d662014-04-29 23:45:48 -040041 setattr(func, 'event', func.__name__)
42 return func
Tony Mack79a49c82013-06-15 23:51:57 -040043
44class EventHandler:
Sapan Bhatia1b84d662014-04-29 23:45:48 -040045 # This code is currently not in use.
46 def __init__(self):
47 pass
Tony Mack79a49c82013-06-15 23:51:57 -040048
Sapan Bhatia1b84d662014-04-29 23:45:48 -040049 @staticmethod
50 def get_events():
51 events = []
52 for name in dir(EventHandler):
53 attribute = getattr(EventHandler, name)
54 if hasattr(attribute, 'event'):
55 events.append(getattr(attribute, 'event'))
56 return events
Tony Mack79a49c82013-06-15 23:51:57 -040057
Sapan Bhatia1b84d662014-04-29 23:45:48 -040058 def dispatch(self, event, *args, **kwds):
59 if hasattr(self, event):
60 return getattr(self, event)(*args, **kwds)
61
Scott Baker3d5a2782014-05-13 09:58:02 -070062
Sapan Bhatia66f4e612013-07-02 12:12:38 -040063class EventSender:
Sapan Bhatia1b84d662014-04-29 23:45:48 -040064 def __init__(self,user=None,clientid=None):
65 try:
66 user = Config().feefie_client_user
67 except:
68 user = 'pl'
Sapan Bhatia66f4e612013-07-02 12:12:38 -040069
Sapan Bhatia1b84d662014-04-29 23:45:48 -040070 try:
71 clid = Config().feefie_client_id
72 except:
73 clid = get_random_client_id()
Scott Baker3d5a2782014-05-13 09:58:02 -070074 print "EventSender: no feefie_client_id configured. Using random id %s" % clid
Sapan Bhatia66f4e612013-07-02 12:12:38 -040075
Sapan Bhatia1b84d662014-04-29 23:45:48 -040076 self.fofum = Fofum(user=user)
77 self.fofum.make(clid)
Sapan Bhatia9faf7b02013-10-09 10:27:14 -040078
Sapan Bhatia1b84d662014-04-29 23:45:48 -040079 def fire(self,**kwargs):
80 kwargs["uuid"] = str(uuid.uuid1())
81 self.fofum.fire(json.dumps(kwargs))
Tony Mack79a49c82013-06-15 23:51:57 -040082
83class EventListener:
Sapan Bhatia1b84d662014-04-29 23:45:48 -040084 def __init__(self,wake_up=None):
85 self.handler = EventHandler()
86 self.wake_up = wake_up
Scott Baker6ecd4262014-01-21 23:15:21 -080087
Tony Mack79a49c82013-06-15 23:51:57 -040088
Sapan Bhatia1b84d662014-04-29 23:45:48 -040089 def handle_event(self, payload):
90 payload_dict = json.loads(payload)
Sapan Bhatia43c3a772013-07-03 11:19:07 -040091
Sapan Bhatia1b84d662014-04-29 23:45:48 -040092 if (self.wake_up):
93 self.wake_up()
Sapan Bhatiadbaf1932013-09-03 11:28:52 -040094
Sapan Bhatia1b84d662014-04-29 23:45:48 -040095 def run(self):
96 # This is our unique client id, to be used when firing and receiving events
97 # It needs to be generated once and placed in the config file
Scott Bakerc1c45f82014-01-21 16:23:51 -080098
Sapan Bhatia1b84d662014-04-29 23:45:48 -040099 try:
100 user = Config().feefie_client_user
101 except:
102 user = 'pl'
Sapan Bhatia66f4e612013-07-02 12:12:38 -0400103
Sapan Bhatia1b84d662014-04-29 23:45:48 -0400104 try:
105 clid = Config().feefie_client_id
106 except:
107 clid = get_random_client_id()
108 print "EventListener: no feefie_client_id configured. Using random id %s" % clid
Scott Bakerc1c45f82014-01-21 16:23:51 -0800109
Sapan Bhatia1b84d662014-04-29 23:45:48 -0400110 f = Fofum(user=user)
111
112 listener_thread = threading.Thread(target=f.listen_for_event,args=(clid,self.handle_event))
113 listener_thread.start()