Revision bec1a51b

b/flowspec/views.py
269 269
    
270 270
@never_cache
271 271
def load_jscript(request, file):
272
    return render_to_response('%s.js' % file, context_instance=RequestContext(request), mimetype="text/javascript")
272
    long_polling_timeout = int(settings.POLL_SESSION_UPDATE)*1000 + 10000
273
    return render_to_response('%s.js' % file, {'timeout': long_polling_timeout}, context_instance=RequestContext(request), mimetype="text/javascript")
b/poller/views.py
40 40
    cache_size = 500
41 41

  
42 42
    def __init__(self):
43
        logger.info("initializing")
43 44
        self.user = None
44 45
        self.user_cache = {}
45 46
        self.user_cursor = {}
......
63 64
                assert(self.new_message_user_event[user])
64 65
            except:
65 66
                self.new_message_user_event[user] = Event()
66
    #        self.new_message_user_event[user] = Event()
67 67
            try:
68 68
                if self.user_cache[user]:
69 69
                    self.user_cursor[user] = self.user_cache[user][-1]['id']
......
88 88
            self.user_cursor[user] = self.user_cache[user][-1]['id']
89 89
        else:
90 90
            self.user_cursor[user] = self.user_cache[user][-2]['id']
91
#        self.cache.append(msg)
92 91
        if len(self.user_cache[user]) > self.cache_size:
93 92
            self.user_cache[user] = self.user_cache[user][-self.cache_size:]
94 93
        self.new_message_user_event[user].set()
......
99 98
        if request.is_ajax():
100 99
            cursor = {}
101 100
            try:
102
    #            user = request.user.username
103 101
                user = request.user.get_profile().peer.domain_name
104 102
            except:
105 103
                user = None
......
112 110
            except:
113 111
                self.user_cache[user] = []
114 112
            if not self.user_cache[user] or cursor[user] == self.user_cache[user][-1]['id']:
115
                self.new_message_user_event[user].wait()
116
    #            self.new_message_event.wait()
117
    #        assert cursor[user] != self.user_cache[user][-1]['id'], cursor[user]
113
                self.new_message_user_event[user].wait(settings.POLL_SESSION_UPDATE)
118 114
            try:
119 115
                for index, m in enumerate(self.user_cache[user]):
120 116
                    if m['id'] == cursor[user]:
......
124 120
                if self.user_cache[user]:
125 121
                    self.user_cursor[user] = self.user_cache[user][-1]['id']
126 122
        return HttpResponseRedirect(reverse('group-routes'))
127
    #            else:
128
    #                request.session.pop('cursor', None)
129 123

  
130 124
    def monitor_polls(self, polls=None):
131 125
        b = beanstalkc.Connection()
......
144 138
            p.spawn(self.monitor_polls)
145 139
            
146 140
msgs = Msgs()
147

  
148 141
main = msgs.main
149 142

  
150 143
message_new = msgs.message_new
......
154 147
poll = msgs.start_polling
155 148
poll()
156 149

  
157

  
158

  
159

  
160

  
161

  
162

  
163

  
164

  
165

  
b/templates/base.html
11 11
<link rel="stylesheet" type="text/css" href="/static/css/smoothness/jquery-ui-1.8.13.custom.css">
12 12
<script type="text/javascript" src="/static/js/jquery-ui-1.8.12.custom.min.js"></script>
13 13
<script type="text/javascript" src="/static/js/jquery.tooltip.min.js"></script>
14
{% if user.is_authenticated %}
15
<script type="text/javascript" src="{% url load-js 'poller' %}"></script>
16
{% endif %}
17 14
<script type="text/javascript">
18 15

  
19 16
  	function setlang(lang){
b/templates/poller.js
29 29
    $("#message").select();
30 30
    {% if user.is_authenticated %}
31 31
    updater.start();
32
    updater.keepalive();
32
    updater.poll();
33 33
    
34 34
    {% endif %}
35 35
});
......
57 57
}
58 58

  
59 59
jQuery.postJSON = function(url, args, callback) {
60
    $.ajax({url: url, dataType: "text", type: "POST",
60
    $.ajax({url: url, dataType: "json", type: "POST",
61 61
	    success: function(response) {
62
	if (callback) callback(eval("(" + response + ")"));
62
	if (callback) callback(response);
63 63
    }, error: function(response) {
64
	console.log("ERROR:", response)
64
	console.log("ERROR:", response);
65 65
    }});
66 66
};
67 67

  
......
92 92
var updater = {
93 93
    errorSleepTime: 500,
94 94
    cursor: null,
95
    xhrlp: null,
96
    keepalivetime:  120000,
97
    keepalive: function (){
98
	try {
99
		updater.xhrlp.abort();
100
	}
101
	catch (e) {	 
102
	}
103
	updater.poll();
104
	if (updater.errorSleepTime == 500){
105
		window.setTimeout(updater.keepalive, updater.keepalivetime);
106
	}
107
	else{
108
		window.setTimeout(updater.keepalive, updater.keepalivetime+updater.errorSleepTime);
109
	}
110
	},
111
    
112 95
    start: function() {
113
		$.ajax({url: "{% url fetch-existing %}", type: "POST", dataType: "text",
96
		$.ajax({url: "{% url fetch-existing %}", type: "POST", dataType: "json",
114 97
    		success: updater.onFetchExisting,
115 98
    		error: updater.onError});
116 99
        },
117 100
    
118 101
    poll: function() {
119 102
    	{% if user.is_authenticated %}
120
    	if (updater.errorSleepTime > 60000){
121
    		window.setTimeout('location.reload()', 1000);
122
    		}
123
    	updater.xhrlp=$.ajax({url: "{% url fetch-updates %}", type: "POST", dataType: "text",
103
    	if (errorSleepTime > 128000){
104
    		window.setTimeout('location.reload()', 500);
105
    	}
106
    	timeout = {{timeout}};
107
    	$.ajax({url: "{% url fetch-updates %}", type: "POST", dataType: "json",
124 108
    		success: updater.onSuccess,
109
    		timeout: timeout,
125 110
    		error: updater.onError});
126 111
    	{% endif %}
127 112
    },
128 113
    onSuccess: function(response) {
129 114
	try {
130
	    updater.newMessages(eval("(" + response + ")"));
115
	    updater.newMessages(response);
131 116
	} catch (e) {
132 117
	    updater.onError();
133 118
	    return;
......
138 123

  
139 124
    onFetchExisting: function(response) {
140 125
    	try {
141
    	    updater.existingMessages(eval("(" + response + ")"));
126
    	    updater.existingMessages(response);
127

  
142 128
    	} catch (e) {
143
//    	    updater.onError();
129
    	    updater.onError();
144 130
    	    return;
145 131
    	}
146 132
        },
147 133
     
148 134
    onError: function(response, text) {
149
        	if (text != 'abort'){
150
				updater.errorSleepTime *= 2;
151
				console.log("Poll error; sleeping for", updater.errorSleepTime, "ms");
152
				window.setTimeout(updater.keepalive, updater.errorSleepTime);
135
        	if (text == 'timeout'){
136
        		window.setTimeout('location.reload()', 3000);
153 137
        	}
138
        	updater.errorSleepTime *= 2;
139
			console.log("Poll error; sleeping for", updater.errorSleepTime, "ms");
140
			window.setTimeout(updater.poll, updater.errorSleepTime);
141
        	
154 142
    },
155 143

  
156 144
    newMessages: function(response) {
157 145
	if (!response.messages) return;
146
	if (response.messages.length == 0){
147
		return true;
148
	}
158 149
	updater.cursor = response.cursor;
159 150
	var messages = response.messages;
160 151
	updater.cursor = messages[messages.length - 1].id;
161
//	console.log(messages.length, "new messages, cursor:", updater.cursor);
152
	console.log(messages.length, "new messages, cursor:", updater.cursor);
162 153
	
163 154
	for (var i = 0; i < messages.length; i++) {
164 155
	    updater.showMessage(messages[i]);
......
172 163

  
173 164
    existingMessages: function(response) {
174 165
    	if (!response.messages) return;
166
    	if (response.messages.length == 0){
167
    		return true;
168
    	}
175 169
    	updater.cursor = response.cursor;
176 170
    	var messages = response.messages;
177 171
    	updater.cursor = messages[messages.length - 1].id;
b/templates/user_routes.html
1 1
{% extends "base.html" %}
2 2
{% load i18n %}
3 3
{% block extrahead %}
4
{% if user.is_authenticated %}
5
<script type="text/javascript" src="{% url load-js 'poller' %}"></script>
6
{% endif %}
4 7
<script type="text/javascript" src="/static/js/jquery.dataTables.js"></script>
5 8
<script type="text/javascript">
6 9
	$(document).ready( function(){

Also available in: Unified diff