Commit f324d13e authored by Denis Bilenko's avatar Denis Bilenko

webchat: use class instead of global variables

parent 18754f7e
...@@ -6,50 +6,56 @@ from django.http import HttpResponse ...@@ -6,50 +6,56 @@ from django.http import HttpResponse
from gevent.event import Event from gevent.event import Event
from webchat import settings from webchat import settings
messages = []
MESSAGES_SIZE = 200
new_message_event = Event()
class ChatRoom(object):
def new_message(from_, body): cache_size = 200
def __init__(self):
self.cache = []
self.new_message_event = Event()
def main(self, request):
if self.cache:
request.session['cursor'] = self.cache[-1]['id']
return render_to_response('index.html', {'MEDIA_URL': settings.MEDIA_URL, 'messages': self.cache})
def message_new(self, request):
name = request.META.get('REMOTE_ADDR') or 'Anonymous'
msg = create_message(name, request.POST['body'])
self.cache.append(msg)
if len(self.cache) > self.cache_size:
self.cache = self.cache[-self.cache_size:]
self.new_message_event.set()
self.new_message_event.clear()
return json_response(msg)
def message_updates(self, request):
cursor = request.session.get('cursor')
if not self.cache or cursor == self.cache[-1]['id']:
self.new_message_event.wait()
assert cursor != self.cache[-1]['id'], cursor
try:
for index, m in enumerate(self.cache):
if m['id'] == cursor:
return json_response({'messages': self.cache[index+1:]})
return json_response({'messages': self.cache})
finally:
if self.cache:
request.session['cursor'] = self.cache[-1]['id']
else:
request.session.pop('cursor', None)
room = ChatRoom()
main = room.main
message_new = room.message_new
message_updates = room.message_updates
def create_message(from_, body):
data = {'id': str(uuid.uuid4()), 'from': from_, 'body': body} data = {'id': str(uuid.uuid4()), 'from': from_, 'body': body}
data['html'] = render_to_string('message.html', dictionary={'message': data}) data['html'] = render_to_string('message.html', dictionary={'message': data})
return data return data
def last_message_id():
if messages:
return messages[-1]['id']
def main(request):
if messages:
request.session['cursor'] = messages[-1]['id']
return render_to_response('index.html', {'MEDIA_URL': settings.MEDIA_URL, 'messages': messages})
def message_new(request):
name = request.META.get('REMOTE_ADDR') or 'Anonymous'
msg = new_message(name, request.POST['body'])
messages.append(msg)
if len(messages) > MESSAGES_SIZE:
del messages[0]
new_message_event.set()
new_message_event.clear()
return json_response(msg)
def message_updates(request):
cursor = request.session.get('cursor')
if cursor == last_message_id():
new_message_event.wait()
assert cursor != last_message_id(), cursor
try:
for index, m in enumerate(messages):
if m['id'] == cursor:
return json_response({'messages': messages[index+1:]})
return json_response({'messages': messages})
finally:
if messages:
request.session['cursor'] = messages[-1]['id']
else:
request.session.pop('cursor', None)
def json_response(value, **kwargs): def json_response(value, **kwargs):
kwargs.setdefault('content_type', 'text/javascript; charset=UTF-8') kwargs.setdefault('content_type', 'text/javascript; charset=UTF-8')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment