Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 1 | import threading |
| 2 | import requests, json |
| 3 | from core.models import * |
| 4 | from openstack.manager import OpenStackManager |
| 5 | |
| 6 | # decorator that marks dispatachable event methods |
| 7 | def event(func): |
| 8 | setattr(func, 'event', func.__name__) |
| 9 | return func |
| 10 | |
| 11 | class EventHandler: |
| 12 | |
| 13 | def __init__(self): |
| 14 | self.manager = OpenStackManager() |
| 15 | |
| 16 | def get_events(self): |
| 17 | events = [] |
| 18 | for attrib in dir(self): |
| 19 | if hasattr(attrib, 'event'): |
| 20 | events.append(getattr(attrib, 'event')) |
| 21 | return events |
| 22 | |
| 23 | def dispatch(self, event, *args, **kwds): |
| 24 | if hasattr(self, event): |
| 25 | return getattr(self, event)(*args, **kwds) |
| 26 | |
| 27 | |
| 28 | @event |
| 29 | def save_site(self, id): |
| 30 | sites = Site.objects.filter(id=id) |
| 31 | if sites: |
| 32 | self.manager.save_site(sites[0]) |
| 33 | |
| 34 | @event |
| 35 | def delete_site(self, tenant_id): |
| 36 | self.manager.driver.delete_tenant(tenant_id) |
| 37 | |
| 38 | @event |
| 39 | def save_site_privilege(self, id): |
| 40 | site_privileges = SitePrivilege.objects.filter(id=id) |
| 41 | if site_privileges: |
| 42 | site_priv = self.manager.save_site_privilege(site_privileges[0]) |
| 43 | |
| 44 | @event |
| 45 | def delete_site_privilege(self, kuser_id, tenant_id, role_type): |
| 46 | self.manager.driver.delete_user_role(kuser_id, tenant_id, role_type) |
| 47 | |
| 48 | @event |
| 49 | def save_slice(self, id): |
| 50 | slices = Slice.objects.filter(id=id) |
| 51 | if slices: |
| 52 | self.manager.save_slice(slices[0]) |
| 53 | |
| 54 | @event |
| 55 | def delete_slice(self, tenant_id, network_id, router_id, subnet_id): |
| 56 | self.manager._delete_slice(tenant_id, network_id, router_id, subnet_id) |
| 57 | |
| 58 | @event |
| 59 | def save_user(self, id): |
| 60 | users = User.objects.filter(id=id) |
| 61 | if users: |
| 62 | self.manager.save_user(users[0]) |
| 63 | |
| 64 | @event |
| 65 | def delete_user(self, kuser_id): |
| 66 | self.manager.driver.delete_user(kuser_id) |
| 67 | |
| 68 | @event |
| 69 | def save_sliver(self, id): |
| 70 | slivers = Sliver.objects.filter(id=id) |
| 71 | if slivers: |
| 72 | self.manager.save_sliver(slivers[0]) |
| 73 | |
| 74 | @event |
| 75 | def delete_sliver(self, instance_id): |
| 76 | self.manager.destroy_instance(instance_id) |
| 77 | |
| 78 | |
| 79 | |
| 80 | class EventListener: |
| 81 | |
| 82 | def __init__(self): |
| 83 | self.handler = EventHandler() |
| 84 | |
| 85 | def listen_for_event(self, event, hash): |
| 86 | url = 'http://www.feefie.com/command' |
| 87 | params = {'action': 'subscribe', |
| 88 | 'hash': hash, |
| 89 | 'htm': 1} |
| 90 | while True: |
| 91 | r = requests.get(url, params=params) |
| 92 | r_data = json.loads(r) |
| 93 | payload = r_data.get('payload') |
| 94 | self.handler.dispatch(event, **payload) |
| 95 | |
| 96 | |
| 97 | def run(self): |
| 98 | # register events |
| 99 | event_names = [{'title': name} for name in self.handler.get_events()] |
| 100 | url = 'http://www.feefie.com/command' |
| 101 | params = {'action': 'add', |
| 102 | 'u': 'pl', |
| 103 | 'events': event_names} |
| 104 | r = requests.get(url, params=params) |
| 105 | print dir(r) |
| 106 | print r |
| 107 | r_data = json.loads(r) |
| 108 | events = r_data.get('events', []) |
| 109 | # spanw a thread for each event |
| 110 | for event in events: |
| 111 | args = (event['title'], event['hash']) |
| 112 | listener_thread = threading.Thread(target=self.listen_for_event, args=args) |
| 113 | listener_tread.start() |
| 114 | |