Statistics
| Branch: | Tag: | Revision:

root / poller / views.py @ 0f03076d

History | View | Annotate | Download (5 kB)

1 3e99e2d1 Leonidas Poulopoulos
from gevent import monkey
2 3e99e2d1 Leonidas Poulopoulos
monkey.patch_all()
3 3e99e2d1 Leonidas Poulopoulos
from gevent.pool import Pool
4 25d08a62 Leonidas Poulopoulos
import json
5 3e99e2d1 Leonidas Poulopoulos
6 3e99e2d1 Leonidas Poulopoulos
import uuid
7 3e99e2d1 Leonidas Poulopoulos
import datetime
8 3e99e2d1 Leonidas Poulopoulos
from django.shortcuts import render_to_response
9 3e99e2d1 Leonidas Poulopoulos
from django.template.loader import render_to_string
10 3e99e2d1 Leonidas Poulopoulos
from django.http import HttpResponse
11 3e99e2d1 Leonidas Poulopoulos
from gevent.event import Event
12 3e99e2d1 Leonidas Poulopoulos
from django.conf import settings
13 94fb8123 Leonidas Poulopoulos
#from django.views.decorators.csrf import csrf_exempt
14 97e42c7d Leonidas Poulopoulos
from django.http import HttpResponseRedirect
15 97e42c7d Leonidas Poulopoulos
from django.core.urlresolvers import reverse
16 97e42c7d Leonidas Poulopoulos
17 3e99e2d1 Leonidas Poulopoulos
18 f57f6e68 Leonidas Poulopoulos
import beanstalkc
19 3e99e2d1 Leonidas Poulopoulos
20 3e99e2d1 Leonidas Poulopoulos
import logging
21 3e99e2d1 Leonidas Poulopoulos
22 3e99e2d1 Leonidas Poulopoulos
FORMAT = '%(asctime)s %(levelname)s: %(message)s'
23 3e99e2d1 Leonidas Poulopoulos
logging.basicConfig(format=FORMAT)
24 3e99e2d1 Leonidas Poulopoulos
logger = logging.getLogger(__name__)
25 3e99e2d1 Leonidas Poulopoulos
logger.setLevel(logging.DEBUG)
26 3e99e2d1 Leonidas Poulopoulos
27 3e99e2d1 Leonidas Poulopoulos
28 25d08a62 Leonidas Poulopoulos
def create_message(body, user):
29 25d08a62 Leonidas Poulopoulos
    data = {'id': str(uuid.uuid4()), 'body': body, 'user':user}
30 3e99e2d1 Leonidas Poulopoulos
    data['html'] = render_to_string('poll_message.html', dictionary={'message': data})
31 3e99e2d1 Leonidas Poulopoulos
    return data
32 3e99e2d1 Leonidas Poulopoulos
33 3e99e2d1 Leonidas Poulopoulos
34 3e99e2d1 Leonidas Poulopoulos
def json_response(value, **kwargs):
35 3e99e2d1 Leonidas Poulopoulos
    kwargs.setdefault('content_type', 'text/javascript; charset=UTF-8')
36 c00eba1c Leonidas Poulopoulos
    return HttpResponse(json.dumps(value), **kwargs)
37 3e99e2d1 Leonidas Poulopoulos
38 3e99e2d1 Leonidas Poulopoulos
class Msgs(object):
39 94fb8123 Leonidas Poulopoulos
    cache_size = 500
40 3e99e2d1 Leonidas Poulopoulos
41 3e99e2d1 Leonidas Poulopoulos
    def __init__(self):
42 bec1a51b Leonidas Poulopoulos
        logger.info("initializing")
43 97e42c7d Leonidas Poulopoulos
        self.user = None
44 25d08a62 Leonidas Poulopoulos
        self.user_cache = {}
45 25d08a62 Leonidas Poulopoulos
        self.user_cursor = {}
46 3e99e2d1 Leonidas Poulopoulos
        self.cache = []
47 25d08a62 Leonidas Poulopoulos
        self.new_message_event = None
48 25d08a62 Leonidas Poulopoulos
        self.new_message_user_event = {}
49 3e99e2d1 Leonidas Poulopoulos
50 3e99e2d1 Leonidas Poulopoulos
    def main(self, request):
51 25d08a62 Leonidas Poulopoulos
        if self.user_cache:
52 25d08a62 Leonidas Poulopoulos
            request.session['cursor'] = self.user_cache[-1]['id']
53 25d08a62 Leonidas Poulopoulos
        return render_to_response('poll.html', {'messages': self.user_cache})
54 97e42c7d Leonidas Poulopoulos
55 3e99e2d1 Leonidas Poulopoulos
    def message_existing(self, request):
56 97e42c7d Leonidas Poulopoulos
        if request.is_ajax():
57 97e42c7d Leonidas Poulopoulos
            try:
58 97e42c7d Leonidas Poulopoulos
                user = request.user.get_profile().peer.domain_name
59 97e42c7d Leonidas Poulopoulos
            except:
60 97e42c7d Leonidas Poulopoulos
                user = None
61 97e42c7d Leonidas Poulopoulos
                return False
62 97e42c7d Leonidas Poulopoulos
            try:
63 97e42c7d Leonidas Poulopoulos
                assert(self.new_message_user_event[user])
64 97e42c7d Leonidas Poulopoulos
            except:
65 97e42c7d Leonidas Poulopoulos
                self.new_message_user_event[user] = Event()
66 97e42c7d Leonidas Poulopoulos
            try:
67 97e42c7d Leonidas Poulopoulos
                if self.user_cache[user]:
68 97e42c7d Leonidas Poulopoulos
                    self.user_cursor[user] = self.user_cache[user][-1]['id']
69 97e42c7d Leonidas Poulopoulos
            except:
70 97e42c7d Leonidas Poulopoulos
                self.user_cache[user] = []
71 97e42c7d Leonidas Poulopoulos
                self.user_cursor[user] = ''
72 97e42c7d Leonidas Poulopoulos
            return json_response({'messages': self.user_cache[user]})
73 94fb8123 Leonidas Poulopoulos
        return HttpResponseRedirect(reverse('group-routes'))
74 3e99e2d1 Leonidas Poulopoulos
    
75 25d08a62 Leonidas Poulopoulos
    def message_new(self, mesg=None):
76 3e99e2d1 Leonidas Poulopoulos
        if mesg:
77 25d08a62 Leonidas Poulopoulos
            message = mesg['message']
78 25d08a62 Leonidas Poulopoulos
            user = mesg['username']
79 3e99e2d1 Leonidas Poulopoulos
            now = datetime.datetime.now()
80 25d08a62 Leonidas Poulopoulos
            msg = create_message("[%s]: %s"%(now.strftime("%Y-%m-%d %H:%M:%S"),message), user)
81 25d08a62 Leonidas Poulopoulos
        try:
82 25d08a62 Leonidas Poulopoulos
            isinstance(self.user_cache[user], list)
83 25d08a62 Leonidas Poulopoulos
        except:
84 25d08a62 Leonidas Poulopoulos
            self.user_cache[user] = []
85 25d08a62 Leonidas Poulopoulos
        self.user_cache[user].append(msg)
86 25d08a62 Leonidas Poulopoulos
        if self.user_cache[user][-1] == self.user_cache[user][0]: 
87 25d08a62 Leonidas Poulopoulos
            self.user_cursor[user] = self.user_cache[user][-1]['id']
88 25d08a62 Leonidas Poulopoulos
        else:
89 25d08a62 Leonidas Poulopoulos
            self.user_cursor[user] = self.user_cache[user][-2]['id']
90 25d08a62 Leonidas Poulopoulos
        if len(self.user_cache[user]) > self.cache_size:
91 25d08a62 Leonidas Poulopoulos
            self.user_cache[user] = self.user_cache[user][-self.cache_size:]
92 25d08a62 Leonidas Poulopoulos
        self.new_message_user_event[user].set()
93 25d08a62 Leonidas Poulopoulos
        self.new_message_user_event[user].clear()
94 3e99e2d1 Leonidas Poulopoulos
        return json_response(msg)
95 3e99e2d1 Leonidas Poulopoulos
    
96 3e99e2d1 Leonidas Poulopoulos
    def message_updates(self, request):
97 97e42c7d Leonidas Poulopoulos
        if request.is_ajax():
98 97e42c7d Leonidas Poulopoulos
            cursor = {}
99 97e42c7d Leonidas Poulopoulos
            try:
100 97e42c7d Leonidas Poulopoulos
                user = request.user.get_profile().peer.domain_name
101 97e42c7d Leonidas Poulopoulos
            except:
102 97e42c7d Leonidas Poulopoulos
                user = None
103 97e42c7d Leonidas Poulopoulos
                return False
104 a61c1448 Leonidas Poulopoulos
            try:
105 a61c1448 Leonidas Poulopoulos
                cursor[user] = self.user_cursor[user]
106 a61c1448 Leonidas Poulopoulos
            except:
107 a61c1448 Leonidas Poulopoulos
                return HttpResponse(content='', mimetype=None, status=400)
108 97e42c7d Leonidas Poulopoulos
                
109 97e42c7d Leonidas Poulopoulos
            try:
110 97e42c7d Leonidas Poulopoulos
                if not isinstance(self.user_cache[user], list):
111 97e42c7d Leonidas Poulopoulos
                    self.user_cache[user] = []
112 97e42c7d Leonidas Poulopoulos
            except:
113 25d08a62 Leonidas Poulopoulos
                self.user_cache[user] = []
114 97e42c7d Leonidas Poulopoulos
            if not self.user_cache[user] or cursor[user] == self.user_cache[user][-1]['id']:
115 bec1a51b Leonidas Poulopoulos
                self.new_message_user_event[user].wait(settings.POLL_SESSION_UPDATE)
116 97e42c7d Leonidas Poulopoulos
            try:
117 97e42c7d Leonidas Poulopoulos
                for index, m in enumerate(self.user_cache[user]):
118 97e42c7d Leonidas Poulopoulos
                    if m['id'] == cursor[user]:
119 97e42c7d Leonidas Poulopoulos
                        return json_response({'messages': self.user_cache[user][index + 1:]})
120 97e42c7d Leonidas Poulopoulos
                return json_response({'messages': self.user_cache[user]})
121 97e42c7d Leonidas Poulopoulos
            finally:
122 97e42c7d Leonidas Poulopoulos
                if self.user_cache[user]:
123 97e42c7d Leonidas Poulopoulos
                    self.user_cursor[user] = self.user_cache[user][-1]['id']
124 94fb8123 Leonidas Poulopoulos
        return HttpResponseRedirect(reverse('group-routes'))
125 3e99e2d1 Leonidas Poulopoulos
126 3e99e2d1 Leonidas Poulopoulos
    def monitor_polls(self, polls=None):
127 3e99e2d1 Leonidas Poulopoulos
        b = beanstalkc.Connection()
128 3e99e2d1 Leonidas Poulopoulos
        b.watch(settings.POLLS_TUBE)
129 3e99e2d1 Leonidas Poulopoulos
        while True:
130 3e99e2d1 Leonidas Poulopoulos
            job = b.reserve()
131 25d08a62 Leonidas Poulopoulos
            msg = json.loads(job.body)
132 3e99e2d1 Leonidas Poulopoulos
            job.bury()
133 25d08a62 Leonidas Poulopoulos
            self.message_new(msg)
134 3e99e2d1 Leonidas Poulopoulos
            
135 3e99e2d1 Leonidas Poulopoulos
    
136 3e99e2d1 Leonidas Poulopoulos
    def start_polling(self):
137 3e99e2d1 Leonidas Poulopoulos
        logger.info("Start Polling")
138 3e99e2d1 Leonidas Poulopoulos
        p = Pool(10)
139 3e99e2d1 Leonidas Poulopoulos
        while True:
140 3e99e2d1 Leonidas Poulopoulos
            p.spawn(self.monitor_polls)
141 3e99e2d1 Leonidas Poulopoulos
            
142 3e99e2d1 Leonidas Poulopoulos
msgs = Msgs()
143 3e99e2d1 Leonidas Poulopoulos
main = msgs.main
144 3e99e2d1 Leonidas Poulopoulos
145 3e99e2d1 Leonidas Poulopoulos
message_new = msgs.message_new
146 3e99e2d1 Leonidas Poulopoulos
message_updates = msgs.message_updates
147 3e99e2d1 Leonidas Poulopoulos
message_existing = msgs.message_existing
148 3e99e2d1 Leonidas Poulopoulos
149 3e99e2d1 Leonidas Poulopoulos
poll = msgs.start_polling
150 3e99e2d1 Leonidas Poulopoulos
poll()