Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 1 | import threading |
| 2 | import requests, json |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 3 | |
Scott Baker | 3bd7c86 | 2015-02-04 16:54:10 -0800 | [diff] [blame] | 4 | from planetstack.config import Config, XOS_DIR |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 5 | |
Scott Baker | c1c45f8 | 2014-01-21 16:23:51 -0800 | [diff] [blame] | 6 | import uuid |
Sapan Bhatia | 9182b32 | 2013-06-25 16:22:14 -0400 | [diff] [blame] | 7 | import os |
Scott Baker | 6ecd426 | 2014-01-21 23:15:21 -0800 | [diff] [blame] | 8 | import imp |
| 9 | import inspect |
Sapan Bhatia | 9182b32 | 2013-06-25 16:22:14 -0400 | [diff] [blame] | 10 | import base64 |
Tony Mack | 5c0c455 | 2013-07-03 09:36:51 -0400 | [diff] [blame] | 11 | from fofum import Fofum |
Sapan Bhatia | dbaf193 | 2013-09-03 11:28:52 -0400 | [diff] [blame] | 12 | import json |
Scott Baker | 6ecd426 | 2014-01-21 23:15:21 -0800 | [diff] [blame] | 13 | import traceback |
Sapan Bhatia | 9182b32 | 2013-06-25 16:22:14 -0400 | [diff] [blame] | 14 | |
Scott Baker | 3d5a278 | 2014-05-13 09:58:02 -0700 | [diff] [blame] | 15 | random_client_id=None |
| 16 | def get_random_client_id(): |
| 17 | global random_client_id |
| 18 | |
Scott Baker | 3bd7c86 | 2015-02-04 16:54:10 -0800 | [diff] [blame] | 19 | if (random_client_id is None) and os.path.exists(XOS_DIR + "/random_client_id"): |
Scott Baker | 3d5a278 | 2014-05-13 09:58:02 -0700 | [diff] [blame] | 20 | # try to use the last one we used, if we saved it |
| 21 | try: |
Scott Baker | 3bd7c86 | 2015-02-04 16:54:10 -0800 | [diff] [blame] | 22 | random_client_id = open(XOS_DIR+"/random_client_id","r").readline().strip() |
Scott Baker | 3d5a278 | 2014-05-13 09:58:02 -0700 | [diff] [blame] | 23 | print "get_random_client_id: loaded %s" % random_client_id |
| 24 | except: |
Scott Baker | 3bd7c86 | 2015-02-04 16:54:10 -0800 | [diff] [blame] | 25 | print "get_random_client_id: failed to read " + XOS_DIR + "/random_client_id" |
Scott Baker | 3d5a278 | 2014-05-13 09:58:02 -0700 | [diff] [blame] | 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: |
Scott Baker | 3bd7c86 | 2015-02-04 16:54:10 -0800 | [diff] [blame] | 33 | open(XOS_DIR + "/random_client_id","w").write("%s\n" % random_client_id) |
Scott Baker | 3d5a278 | 2014-05-13 09:58:02 -0700 | [diff] [blame] | 34 | except: |
Scott Baker | 3bd7c86 | 2015-02-04 16:54:10 -0800 | [diff] [blame] | 35 | print "get_random_client_id: failed to write " + XOS_DIR + "/random_client_id" |
Scott Baker | 3d5a278 | 2014-05-13 09:58:02 -0700 | [diff] [blame] | 36 | |
| 37 | return random_client_id |
| 38 | |
| 39 | # decorator that marks dispatachable event methods |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 40 | def event(func): |
Sapan Bhatia | 1b84d66 | 2014-04-29 23:45:48 -0400 | [diff] [blame] | 41 | setattr(func, 'event', func.__name__) |
| 42 | return func |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 43 | |
| 44 | class EventHandler: |
Sapan Bhatia | 1b84d66 | 2014-04-29 23:45:48 -0400 | [diff] [blame] | 45 | # This code is currently not in use. |
| 46 | def __init__(self): |
| 47 | pass |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 48 | |
Sapan Bhatia | 1b84d66 | 2014-04-29 23:45:48 -0400 | [diff] [blame] | 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 |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 57 | |
Sapan Bhatia | 1b84d66 | 2014-04-29 23:45:48 -0400 | [diff] [blame] | 58 | def dispatch(self, event, *args, **kwds): |
| 59 | if hasattr(self, event): |
| 60 | return getattr(self, event)(*args, **kwds) |
| 61 | |
Scott Baker | 3d5a278 | 2014-05-13 09:58:02 -0700 | [diff] [blame] | 62 | |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 63 | class EventSender: |
Sapan Bhatia | 1b84d66 | 2014-04-29 23:45:48 -0400 | [diff] [blame] | 64 | def __init__(self,user=None,clientid=None): |
| 65 | try: |
| 66 | user = Config().feefie_client_user |
| 67 | except: |
| 68 | user = 'pl' |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 69 | |
Sapan Bhatia | 1b84d66 | 2014-04-29 23:45:48 -0400 | [diff] [blame] | 70 | try: |
| 71 | clid = Config().feefie_client_id |
| 72 | except: |
| 73 | clid = get_random_client_id() |
Sapan Bhatia | fc69f3d | 2014-07-21 20:09:06 -0400 | [diff] [blame] | 74 | print "EventSender: no feefie_client_id configured. Using random id %s" % clid |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 75 | |
Sapan Bhatia | 1b84d66 | 2014-04-29 23:45:48 -0400 | [diff] [blame] | 76 | self.fofum = Fofum(user=user) |
| 77 | self.fofum.make(clid) |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 78 | |
Sapan Bhatia | 1b84d66 | 2014-04-29 23:45:48 -0400 | [diff] [blame] | 79 | def fire(self,**kwargs): |
| 80 | kwargs["uuid"] = str(uuid.uuid1()) |
| 81 | self.fofum.fire(json.dumps(kwargs)) |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 82 | |
| 83 | class EventListener: |
Sapan Bhatia | 1b84d66 | 2014-04-29 23:45:48 -0400 | [diff] [blame] | 84 | def __init__(self,wake_up=None): |
| 85 | self.handler = EventHandler() |
| 86 | self.wake_up = wake_up |
Scott Baker | 6ecd426 | 2014-01-21 23:15:21 -0800 | [diff] [blame] | 87 | |
Sapan Bhatia | 1b84d66 | 2014-04-29 23:45:48 -0400 | [diff] [blame] | 88 | def handle_event(self, payload): |
| 89 | payload_dict = json.loads(payload) |
Sapan Bhatia | 43c3a77 | 2013-07-03 11:19:07 -0400 | [diff] [blame] | 90 | |
Sapan Bhatia | 1b84d66 | 2014-04-29 23:45:48 -0400 | [diff] [blame] | 91 | if (self.wake_up): |
| 92 | self.wake_up() |
Sapan Bhatia | dbaf193 | 2013-09-03 11:28:52 -0400 | [diff] [blame] | 93 | |
Sapan Bhatia | 1b84d66 | 2014-04-29 23:45:48 -0400 | [diff] [blame] | 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 |
Scott Baker | c1c45f8 | 2014-01-21 16:23:51 -0800 | [diff] [blame] | 97 | |
Sapan Bhatia | 1b84d66 | 2014-04-29 23:45:48 -0400 | [diff] [blame] | 98 | try: |
| 99 | user = Config().feefie_client_user |
| 100 | except: |
| 101 | user = 'pl' |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 102 | |
Sapan Bhatia | 1b84d66 | 2014-04-29 23:45:48 -0400 | [diff] [blame] | 103 | 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 Baker | c1c45f8 | 2014-01-21 16:23:51 -0800 | [diff] [blame] | 108 | |
Sapan Bhatia | 1b84d66 | 2014-04-29 23:45:48 -0400 | [diff] [blame] | 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() |