summaryrefslogtreecommitdiff
path: root/js/codeq/comms.js
blob: 8b89cdea8153999461a06cbcfba10eef813eff3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
(function () {

    var send = function (service, json) {
        if (json instanceof Object) json = codeq.jsonize(json);
        return Q.Promise(function (resolve, reject, notify) {
            $.ajax({
                'type': 'POST',
                'url': codeq.urlPrefix + service,
                'accepts': 'application/json',
                'contentType': 'application/json; charset=UTF-8', // type of our request
                'data': json,
                'processData': false, // don't process outgoing data
                'dataType': 'json', // expected type of the response
                'timeout': 60000, // one minute
                'error': function sendErrorHandler(jqXHR, textStatus, errorThrown) {
                    reject(new Error(errorThrown || textStatus));
                },
                'success': function sendSuccessHandler(data, textStatus, jqXHR) {
                    resolve(data);
                }
            });
        });
    };

    var activities = {}, // keyed by problem_id: activity info waiting to be sent to the server
        activityKeys = [], // the keys to activities dictionary
        resolveActivity = function (promiseResolvers) {
            var i;
            // signal everyone that the queue is flushed
            for (i = 0; i < promiseResolvers.length; i++) {
                try {
                    promiseResolvers[i][0]();
                }
                catch (e) {}
            }
            promiseResolvers.length = 0;
        },
        sendActivityInternal = function () {
            var activityKey, activityDescriptor, trace, sendCount;
            // send max. 100 activities, do not be excessive
            if (activityKeys.length == 0) return;
            activityKey = activityKeys[0];
            activityDescriptor = activities[activityKey];
            trace = activityDescriptor.trace;
            if (trace.length > 100) {
                sendCount = 100;
                trace = trace.slice(0, 100);
            }
            else {
                sendCount = trace.length;
            }
            send('activity', {'trace': trace, 'solution': activityDescriptor.solution, 'problem_id': activityDescriptor.problem_id, 'sid': codeq.sid}).then(
                function sendActivitySuccess() {
                    activityDescriptor.trace.splice(0, sendCount);
                    if (activityDescriptor.trace.length > 0) sendActivityInternal();
                    else {
                        delete activities[activityKey];
                        activityKeys.shift();
                        resolveActivity(activityDescriptor.promiseResolvers);
                    }
                },
                function sendActivityFailure() {
                    Q.delay(500).then(sendActivityInternal).done();
                }
            ).done();
        };

    codeq.comms = {
        sendActivity: function commsSendActivity (trace, solution, problem_id) {
            return Q.Promise(function (resolve, reject, notify) {
                var triggerSending = activityKeys.length == 0,
                    activityDescriptor = activities[problem_id],
                    problemTrace, promiseResolvers, i;
                if (!activityDescriptor) {
                    problemTrace = [];
                    promiseResolvers = [];
                    activityDescriptor = {
                        'problem_id': problem_id,
                        'trace': problemTrace,
                        'solution': solution,
                        'promiseResolvers': promiseResolvers
                    };
                    activities[problem_id] = activityDescriptor;
                    activityKeys.push(problem_id);
                }
                else {
                    problemTrace = activityDescriptor.trace;
                    promiseResolvers = activityDescriptor.promiseResolvers;
                    if (solution) activityDescriptor.solution = solution;
                }
                if (trace instanceof Array) {
                    for (i = 0; i < trace.length; i++) {
                        problemTrace.push(trace[i]);
                    }
                }
                else {
                    problemTrace.push(trace);
                }
                promiseResolvers.push([resolve, reject]);
                if (triggerSending) {
                    setTimeout(sendActivityInternal, 0); // async trigger: see if you can collect some more payload
                }
            });
        },

        sendQuery: function commsSendQuery (query, problem_id) {
            var existingActivity = activities[problem_id],
                tmp;
            query['sid'] = codeq.sid;
            if (existingActivity) {
                tmp = query['trace'];
                if (tmp) {
                    // the trace will be sent separately, so it will be in the proper order
                    tmp.splice(0, 0, existingActivity.trace.length, 0); // add two parameters in front, they will form the first two parameters to the following splice() invocation
                    existingActivity.trace.splice.apply(existingActivity.trace, tmp);
                    delete query['trace'];
                }
                tmp = query['program'];
                if (tmp) existingActivity.solution = tmp;
            }
            return send('query', query);
        },

        sendHint: function commsSendHint (json) {
            json['sid'] = codeq.sid;
            return send('hint', json);
        },

        sendTest: function commsSendTest (json) {
            json['sid'] = codeq.sid;
            return send('test', json);
        },

        getProblem: function commsGetProblem (language, problem_group, problem) {
            return send('get_problem', {
                'sid': codeq.sid,
                'language': language,
                'problem_group': problem_group,
                'problem': problem
            });
        }
    };
})();