Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 1 | import threading |
| 2 | import requests, json |
| 3 | |
| 4 | from planetstack.config import Config |
| 5 | |
| 6 | import uuid |
| 7 | import os |
| 8 | import imp |
| 9 | import inspect |
| 10 | import base64 |
| 11 | from fofum import Fofum |
| 12 | import json |
| 13 | import traceback |
| 14 | |
Sapan Bhatia | b726219 | 2014-07-22 00:30:16 -0400 | [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 |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 40 | def event(func): |
| 41 | setattr(func, 'event', func.__name__) |
Sapan Bhatia | b726219 | 2014-07-22 00:30:16 -0400 | [diff] [blame] | 42 | return func |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 43 | |
| 44 | class EventHandler: |
| 45 | # This code is currently not in use. |
| 46 | def __init__(self): |
Sapan Bhatia | b726219 | 2014-07-22 00:30:16 -0400 | [diff] [blame] | 47 | pass |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 48 | |
| 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 Bhatia | b726219 | 2014-07-22 00:30:16 -0400 | [diff] [blame] | 61 | |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 62 | |
| 63 | class 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 Bhatia | b726219 | 2014-07-22 00:30:16 -0400 | [diff] [blame] | 73 | clid = get_random_client_id() |
| 74 | print "EventSender: no feefie_client_id configured. Using random id %s" % clid |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 75 | |
| 76 | self.fofum = Fofum(user=user) |
| 77 | self.fofum.make(clid) |
| 78 | |
| 79 | def fire(self,**kwargs): |
Sapan Bhatia | b726219 | 2014-07-22 00:30:16 -0400 | [diff] [blame] | 80 | kwargs["uuid"] = str(uuid.uuid1()) |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 81 | self.fofum.fire(json.dumps(kwargs)) |
| 82 | |
| 83 | class 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 Bhatia | 743bed1 | 2014-07-22 00:48:09 -0400 | [diff] [blame] | 91 | if (self.wake_up): |
Sapan Bhatia | b726219 | 2014-07-22 00:30:16 -0400 | [diff] [blame] | 92 | self.wake_up() |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 93 | |
| 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 Bhatia | b726219 | 2014-07-22 00:30:16 -0400 | [diff] [blame] | 106 | clid = get_random_client_id() |
| 107 | print "EventListener: no feefie_client_id configured. Using random id %s" % clid |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 108 | |
| 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() |