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 | |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 4 | from planetstack.config import Config |
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 | |
| 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 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() |
Scott Baker | 3d5a278 | 2014-05-13 09:58:02 -0700 | [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 | |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 88 | |
Sapan Bhatia | 1b84d66 | 2014-04-29 23:45:48 -0400 | [diff] [blame] | 89 | def handle_event(self, payload): |
| 90 | payload_dict = json.loads(payload) |
Sapan Bhatia | 43c3a77 | 2013-07-03 11:19:07 -0400 | [diff] [blame] | 91 | |
Sapan Bhatia | 1b84d66 | 2014-04-29 23:45:48 -0400 | [diff] [blame] | 92 | if (self.wake_up): |
| 93 | self.wake_up() |
Sapan Bhatia | dbaf193 | 2013-09-03 11:28:52 -0400 | [diff] [blame] | 94 | |
Sapan Bhatia | 1b84d66 | 2014-04-29 23:45:48 -0400 | [diff] [blame] | 95 | 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 Baker | c1c45f8 | 2014-01-21 16:23:51 -0800 | [diff] [blame] | 98 | |
Sapan Bhatia | 1b84d66 | 2014-04-29 23:45:48 -0400 | [diff] [blame] | 99 | try: |
| 100 | user = Config().feefie_client_user |
| 101 | except: |
| 102 | user = 'pl' |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 103 | |
Sapan Bhatia | 1b84d66 | 2014-04-29 23:45:48 -0400 | [diff] [blame] | 104 | 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 Baker | c1c45f8 | 2014-01-21 16:23:51 -0800 | [diff] [blame] | 109 | |
Sapan Bhatia | 1b84d66 | 2014-04-29 23:45:48 -0400 | [diff] [blame] | 110 | f = Fofum(user=user) |
| 111 | |
| 112 | listener_thread = threading.Thread(target=f.listen_for_event,args=(clid,self.handle_event)) |
| 113 | listener_thread.start() |