]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.webapp/src/main/webapp/argeo-ria-src/class/org/argeo/ria/remote/AmqClient.js
4952561d5c18c3b600c054dff93ee4c8aaa4782d
[gpl/argeo-slc.git] / org.argeo.slc.webapp / src / main / webapp / argeo-ria-src / class / org / argeo / ria / remote / AmqClient.js
1 qx.Class.define("org.argeo.ria.remote.AmqClient", {
2
3 extend : qx.core.Object,
4 construct : function(){
5 this.base(arguments);
6 },
7 members : {
8 // The URI of the MessageListenerServlet
9 uri : '/amq',
10
11 // Polling. Set to true (default) if waiting poll for messages is needed
12 poll : true,
13
14 // Poll delay. if set to positive integer, this is the time to wait in ms before
15 // sending the next poll after the last completes.
16 _pollDelay : 0,
17
18 _first : true,
19 _pollEvent : function(first) {
20 },
21 _handlers : new Array(),
22
23 _messages : 0,
24 _messageQueue : '',
25 _queueMessages : 0,
26
27 _messageHandler : function(request) {
28 try {
29 if (request.status == 200) {
30 var response = request.responseXML
31 .getElementsByTagName("ajax-response");
32 if (response != null && response.length == 1) {
33 for (var i = 0; i < response[0].childNodes.length; i++) {
34 var responseElement = response[0].childNodes[i];
35
36 // only process nodes of type element.....
37 if (responseElement.nodeType != 1)
38 continue;
39
40 var id = responseElement.getAttribute('id');
41
42 var handler = this._handlers[id];
43 if (handler != null) {
44 for (var j = 0; j < responseElement.childNodes.length; j++) {
45 handler(responseElement.childNodes[j]);
46 }
47 }
48 }
49 }
50 }
51 } catch (e) {
52 alert(e);
53 }
54 },
55
56 startBatch : function() {
57 this._queueMessages++;
58 },
59
60 endBatch : function() {
61 this._queueMessages--;
62 if (this._queueMessages == 0 && this._messages > 0) {
63 var body = this._messageQueue;
64 this._messageQueue = '';
65 this._messages = 0;
66 this._queueMessages++;
67 var request = new qx.io.remote.Request(this.uri, "post", "text/plain");
68 request.addListener("completed", this.endBatch, this);
69 request.send();
70 }
71 },
72
73 _pollHandler : function(request) {
74 this.startBatch();
75 try {
76 this._messageHandler(request);
77 this._pollEvent(this._first);
78 this._first = false;
79 } catch (e) {
80 alert(e);
81 }
82 this.endBatch();
83
84 if (this._pollDelay > 0)
85 qx.event.Timer.once(this._sendPoll, this, this._pollDelay);
86 else
87 this._sendPoll();
88 },
89
90 _sendPoll : function(request) {
91 var request = new qx.io.remote.Request(this.uri, "get", "application/xml");
92 request.addListener("completed", this._pollHandler, this);
93 request.send();
94 },
95
96 // Add a function that gets called on every poll response, after all received
97 // messages have been handled. The poll handler is past a boolean that indicates
98 // if this is the first poll for the page.
99 addPollHandler : function(func) {
100 var old = this._pollEvent;
101 this._pollEvent = function(first) {
102 old(first);
103 func(first);
104 }
105 },
106
107 // Send a JMS message to a destination (eg topic://MY.TOPIC). Message should be xml or encoded
108 // xml content.
109 sendMessage : function(destination, message) {
110 this._sendMessage(destination, message, 'send');
111 },
112
113 // Listen on a channel or topic. handler must be a function taking a message arguement
114 addListener : function(id, destination, handler) {
115 this._handlers[id] = handler;
116 this._sendMessage(destination, id, 'listen');
117 },
118
119 // remove Listener from channel or topic.
120 removeListener : function(id, destination) {
121 this._handlers[id] = null;
122 this._sendMessage(destination, id, 'unlisten');
123 },
124
125 _sendMessage : function(destination, message, type) {
126 if (this._queueMessages > 0) {
127 if (this._messages == 0) {
128 this._messageQueue = 'destination=' + destination
129 + '&message=' + message + '&type=' + type;
130 } else {
131 this._messageQueue += '&d' + this._messages + '='
132 + destination + '&m' + this._messages + '='
133 + message + '&t' + this._messages + '=' + type;
134 }
135 this._messages++;
136 } else {
137 this.startBatch();
138 var req = new qx.io.remote.Request(this.uri, "post", "application/xml");
139 req.setParameter("destination", destination);
140 req.setParameter("message", message);
141 req.setParameter("type", type);
142 req.addListener("completed", this.endBatch, this);
143 req.send();
144 }
145 },
146
147 _startPolling : function() {
148 if (this.poll){
149 var req = new qx.io.remote.Request(this.uri, "get", "application/xml");
150 req.setParameter("timeout", "10");
151 req.addListener("completed", this._pollHandler, this);
152 req.send();
153 }
154 }
155 }
156 });