blob: 190d4ebbb802abd9812764c601888996ea566227 [file] [log] [blame]
Sapan Bhatia26d40bc2014-05-12 15:28:02 -04001import threading
2import requests, json
3
Scott Bakera0a86e62015-02-04 23:30:19 -08004from planetstack.config import Config, XOS_DIR
Sapan Bhatia26d40bc2014-05-12 15:28:02 -04005
6import uuid
7import os
8import imp
9import inspect
10import base64
11from fofum import Fofum
12import json
13import traceback
14
Sapan Bhatiab7262192014-07-22 00:30:16 -040015random_client_id=None
16def get_random_client_id():
17 global random_client_id
18
Scott Bakera0a86e62015-02-04 23:30:19 -080019 if (random_client_id is None) and os.path.exists(XOS_DIR + "/random_client_id"):
Sapan Bhatiab7262192014-07-22 00:30:16 -040020 # try to use the last one we used, if we saved it
21 try:
Scott Bakera0a86e62015-02-04 23:30:19 -080022 random_client_id = open(XOS_DIR+"/random_client_id","r").readline().strip()
Sapan Bhatiab7262192014-07-22 00:30:16 -040023 print "get_random_client_id: loaded %s" % random_client_id
24 except:
Scott Bakera0a86e62015-02-04 23:30:19 -080025 print "get_random_client_id: failed to read " + XOS_DIR + "/random_client_id"
Sapan Bhatiab7262192014-07-22 00:30:16 -040026
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 Bakera0a86e62015-02-04 23:30:19 -080033 open(XOS_DIR + "/random_client_id","w").write("%s\n" % random_client_id)
Sapan Bhatiab7262192014-07-22 00:30:16 -040034 except:
Scott Bakera0a86e62015-02-04 23:30:19 -080035 print "get_random_client_id: failed to write " + XOS_DIR + "/random_client_id"
Sapan Bhatiab7262192014-07-22 00:30:16 -040036
37 return random_client_id
38
39# decorator that marks dispatachable event methods
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040040def event(func):
41 setattr(func, 'event', func.__name__)
Sapan Bhatiab7262192014-07-22 00:30:16 -040042 return func
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040043
44class EventHandler:
45 # This code is currently not in use.
46 def __init__(self):
Sapan Bhatiab7262192014-07-22 00:30:16 -040047 pass
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040048
49 @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
57
58 def dispatch(self, event, *args, **kwds):
59 if hasattr(self, event):
60 return getattr(self, event)(*args, **kwds)
Sapan Bhatiab7262192014-07-22 00:30:16 -040061
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040062
63class EventSender:
64 def __init__(self,user=None,clientid=None):
65 try:
66 user = Config().feefie_client_user
67 except:
68 user = 'pl'
69
70 try:
71 clid = Config().feefie_client_id
72 except:
Sapan Bhatiab7262192014-07-22 00:30:16 -040073 clid = get_random_client_id()
74 print "EventSender: no feefie_client_id configured. Using random id %s" % clid
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040075
76 self.fofum = Fofum(user=user)
77 self.fofum.make(clid)
78
79 def fire(self,**kwargs):
Sapan Bhatiab7262192014-07-22 00:30:16 -040080 kwargs["uuid"] = str(uuid.uuid1())
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040081 self.fofum.fire(json.dumps(kwargs))
82
83class EventListener:
84 def __init__(self,wake_up=None):
85 self.handler = EventHandler()
86 self.wake_up = wake_up
87
88 def handle_event(self, payload):
89 payload_dict = json.loads(payload)
90
Sapan Bhatia743bed12014-07-22 00:48:09 -040091 if (self.wake_up):
Sapan Bhatiab7262192014-07-22 00:30:16 -040092 self.wake_up()
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040093
94 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
97
98 try:
99 user = Config().feefie_client_user
100 except:
101 user = 'pl'
102
103 try:
104 clid = Config().feefie_client_id
105 except:
Sapan Bhatiab7262192014-07-22 00:30:16 -0400106 clid = get_random_client_id()
107 print "EventListener: no feefie_client_id configured. Using random id %s" % clid
Sapan Bhatia26d40bc2014-05-12 15:28:02 -0400108
109 f = Fofum(user=user)
110
111 listener_thread = threading.Thread(target=f.listen_for_event,args=(clid,self.handle_event))
112 listener_thread.start()