blob: 190d4ebbb802abd9812764c601888996ea566227 [file] [log] [blame]
Tony Mack79a49c82013-06-15 23:51:57 -04001import threading
2import requests, json
Sapan Bhatia66f4e612013-07-02 12:12:38 -04003
Scott Baker3bd7c862015-02-04 16:54:10 -08004from planetstack.config import Config, XOS_DIR
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
Scott Baker3bd7c862015-02-04 16:54:10 -080019 if (random_client_id is None) and os.path.exists(XOS_DIR + "/random_client_id"):
Scott Baker3d5a2782014-05-13 09:58:02 -070020 # try to use the last one we used, if we saved it
21 try:
Scott Baker3bd7c862015-02-04 16:54:10 -080022 random_client_id = open(XOS_DIR+"/random_client_id","r").readline().strip()
Scott Baker3d5a2782014-05-13 09:58:02 -070023 print "get_random_client_id: loaded %s" % random_client_id
24 except:
Scott Baker3bd7c862015-02-04 16:54:10 -080025 print "get_random_client_id: failed to read " + XOS_DIR + "/random_client_id"
Scott Baker3d5a2782014-05-13 09:58:02 -070026
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:
Scott Baker3bd7c862015-02-04 16:54:10 -080033 open(XOS_DIR + "/random_client_id","w").write("%s\n" % random_client_id)
Scott Baker3d5a2782014-05-13 09:58:02 -070034 except:
Scott Baker3bd7c862015-02-04 16:54:10 -080035 print "get_random_client_id: failed to write " + XOS_DIR + "/random_client_id"
Scott Baker3d5a2782014-05-13 09:58:02 -070036
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()
Sapan Bhatiafc69f3d2014-07-21 20:09:06 -040074 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
Sapan Bhatia1b84d662014-04-29 23:45:48 -040088 def handle_event(self, payload):
89 payload_dict = json.loads(payload)
Sapan Bhatia43c3a772013-07-03 11:19:07 -040090
Sapan Bhatia1b84d662014-04-29 23:45:48 -040091 if (self.wake_up):
92 self.wake_up()
Sapan Bhatiadbaf1932013-09-03 11:28:52 -040093
Sapan Bhatia1b84d662014-04-29 23:45:48 -040094 def run(self):
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
Scott Bakerc1c45f82014-01-21 16:23:51 -080097
Sapan Bhatia1b84d662014-04-29 23:45:48 -040098 try:
99 user = Config().feefie_client_user
100 except:
101 user = 'pl'
Sapan Bhatia66f4e612013-07-02 12:12:38 -0400102
Sapan Bhatia1b84d662014-04-29 23:45:48 -0400103 try:
104 clid = Config().feefie_client_id
105 except:
106 clid = get_random_client_id()
107 print "EventListener: no feefie_client_id configured. Using random id %s" % clid
Scott Bakerc1c45f82014-01-21 16:23:51 -0800108
Sapan Bhatia1b84d662014-04-29 23:45:48 -0400109 f = Fofum(user=user)
110
111 listener_thread = threading.Thread(target=f.listen_for_event,args=(clid,self.handle_event))
112 listener_thread.start()