source: gs3-extensions/meandre/trunk/src/web/meandre-controller.js@ 28547

Last change on this file since 28547 was 28547, checked in by davidb, 10 years ago

Javascript file used by runtime system

  • Property svn:executable set to *
File size: 8.2 KB
Line 
1
2// Examples of how to control a Meandre Infrastructure (running on default port of 1714)
3
4// http://localhost:1714/services/execute/flow.txt?statistics=true&uri=http://greenstone.org/meandre/flows/chromagram-v3/&token=abc
5// => Runs the specified flow,
6// => gives the Infrastructure a token to identify the given running instance
7
8// http://localhost:1714/services/execute/uri_flow.txt?statistics=true&token=abc
9// => Uses the 'token' to glean values of the running instance, such as which port it is using
10// => Reponse is plain text in value-pair form of properties
11
12
13// http://localhost:1718/http://greenstone.org/meandre/flows/chromagram-v3/instance/greenstone-doc-id-service-to-signal/0?cmd=generate-audio-signal&docOID=abc
14// => Contacts the specified component in the running workflow
15// => Note: the port used (1718) is the value returned from using 'uri_flow.txt' above
16// => The URI given to the server on this port is the full URI 'name' shown in the Meandre Workbench for this components
17
18
19
20var MeandreController = function(webDomain, infrastructurePort, workbenchPort)
21{
22 this.proxyURL = "/greenstone3/cgi-bin/meandre-proxy.pl";
23
24 this.webDomain = webDomain || "http://localhost";
25
26 this.infrastructurePort = (infrastructurePort || "1714");
27 this.workbenchPort = (workbenchPort || "1712");
28 this.infrastructureBaseURL = this.webDomain + ":" + this.infrastructurePort;
29
30 this.workbenchURL = this.webDomain + ":" + this.workbenchPort + "/Workbench.html";
31
32 this.initiateFlowServlet = "/services/execute/flow.txt";
33 this.runningFlowServlet = "/services/execute/uri_flow.txt";
34 this.abortFlowServlet = "/admin/abort.txt";
35
36};
37
38
39MeandreController.prototype.runFlow = function(flowURL) {
40
41 console.log("**** using hardwired username of 'admin' for now");
42 var username = "admin";
43
44 var timestamp = new Date().getTime();
45 var token = username + "_" + timestamp;
46
47 var url = this.infrastructureBaseURL + this.initiateFlowServlet + "?statistics=true&uri=" + flowURL + "&token=" + token;
48 var proxiedURL = this.proxyURL + "?" + encodeURIComponent(url);
49
50 // Callback called when flow naturally finishes, or is stopped through an abort command
51 var request = $.ajax({
52 url: proxiedURL,
53 cache: false
54 });
55
56 request.done(function(text) {
57 console.info( "Meandre Flow completed for URL: " + url);
58 });
59
60 request.fail(function(jqXHR, textStatus) {
61 alert( "Request for URL:\n\n " + url + "\n\nfailed: " + textStatus + "\n--------\n" + jqXHR.statusText );
62 });
63
64 return token;
65};
66
67
68
69MeandreController.prototype.optionsToArgs = function(options) {
70
71 var args = null;
72
73 // nice if this could be done as a one-liner with 'join()'
74 for (var k in options){
75 if (args==null) {
76 args = "";
77 }
78 else {
79 args += "&";
80 }
81
82 args += k + "=" + options[k];
83 }
84
85 return args;
86}
87
88MeandreController.prototype.webComponentToURL = function(webDomain,port,webComp,options) {
89
90 var args = this.optionsToArgs(options);
91
92 var url = webDomain + ":" + port;
93
94 if (webComp.indexOf("/") != 0) {
95 url += "/";
96 }
97 url += webComp + "?" + args;
98
99 return url;
100}
101
102function valuePairsTextToPropertyTable(text) {
103
104 var lines = text.split("\n");
105
106 for (var i=0; i<lines.length; i++) {
107 var line = lines[i];
108
109 var pair = line.match(/\s*(\w+)\s*=\s*(.*?)\s*$/);
110
111 if ((pair!=null) && (pair.length==3)) {
112 var metaname = pair[1];
113 var metaval = pair[2];
114
115 // console.log("**** storing '"+metaname+"' = '" + metaval +"'");
116
117 this[metaname] = metaval;
118 }
119 }
120
121 //return props;
122}
123
124MeandreController.prototype.webComponentProperties = function(port,webComp,options) {
125
126/*
127 var args = this.optionsToArgs(options);
128
129 var url = this.webDomain + ":" + port;
130
131 if (webComp..indexOf("/") != 0) {
132 url = + "/";
133 }
134 url += webComp + "?" + args;
135*/
136
137 var url = this.webComponentToURL(this.webDomain,port,webComp,options);
138
139 // make *synchronous* call, and parse the returned text file as value-pairs
140
141 var props = {};
142
143 var proxiedURL = this.proxyURL + "?" + encodeURIComponent(url);
144
145 var request = $.ajax({
146 url: proxiedURL,
147 context: props,
148 async: false,
149 cache: false
150 });
151
152 request.done(valuePairsTextToPropertyTable);
153
154/*
155function (text) {
156
157 var lines = text.split("\n");
158 for (var i=0; i<lines.length; i++) {
159 var line = lines[i];
160
161 var pair = line.match(/\s*(\w+)\s*=\s*(.*?)\s*$/);
162
163 if ((pair!=null) && (pair.length==3)) {
164 var metaname = pair[1];
165 var metaval = pair[2];
166
167 // console.log("**** storing '"+metaname+"' = '" + metaval +"'");
168
169 props[metaname] = metaval;
170 }
171 }
172 });
173*/
174
175 request.fail(function(jqXHR, textStatus) {
176 console.error( "Request for URL:\n\n " + url + "\n\nfailed: " + + textStatus + "\n--------\n" + jqXHR.statusText );
177 });
178
179 return props;
180};
181
182
183MeandreController.prototype.webComponent = function(port,webComp,options) {
184
185 var payload = null;
186
187 var url = this.webComponentToURL(this.webDomain,port,webComp,options);
188
189 var proxiedURL = this.proxyURL + "?" + encodeURIComponent(url);
190
191// console.log("**** proxiedURL = " + proxiedURL);
192
193 // Synchronous call to return returned output from URL
194 var request = $.ajax({
195 url: proxiedURL,
196 async: false,
197 cache: false
198 });
199
200 request.done(function(output) {
201 console.info( "Response of " + output.length + " bytes from URL: " + url + " received");
202 payload = output;
203 });
204
205 request.fail(function(jqXHR, textStatus) {
206 alert( "Request for URL:\n\n " + url + "\n\nfailed: " + textStatus + "\n--------\n" + jqXHR.statusText );
207 });
208
209 return payload;
210};
211
212
213
214MeandreController.prototype.binaryWebComponent = function(port,webComp,options, callback, context) {
215
216 var payload = { data: null };
217/*
218 var args = optionsToArgs(options);
219
220 var url = this.webDomain + ":" + port + "/" + webComp + "?" + args;
221*/
222
223 var url = this.webComponentToURL(this.webDomain,port,webComp,options);
224
225 //console.log("binaryWebComponent: url = " + url);
226
227 var timestamp = new Date().getTime();
228 var proxiedURL = this.proxyURL + "?" + encodeURIComponent(url) + "&_=" + timestamp;
229
230 // See if possible to do the same with jquery's ajax method ?? // ****
231
232 var oReq = new XMLHttpRequest();
233 oReq.open("GET",proxiedURL, true);
234 oReq.responseType = "arraybuffer";
235
236 oReq.onload = function (oEvent) { callback.call(context,oEvent,oReq) };
237
238/*
239 oReq.onload = function (oEvent) {
240
241 var arrayBuffer = oReq.response; // Note: not oReq.responseText
242
243 if (arrayBuffer.byteLength) {
244 console.log("*** Recieved binary data: raw byte array buffer len = " + arrayBuffer.byteLength);
245
246 var float32Array = new Float32Array(arrayBuffer);
247
248 payload.data = float32Array;
249 }
250 };
251*/
252
253 oReq.send(null);
254
255 return payload;
256
257};
258
259
260MeandreController.prototype.runningFlowProperties = function(token) {
261
262 var port = this.infrastructurePort;
263 var webComp = this.runningFlowServlet;
264 var options = { statistics: true, token: token };
265
266 var props = this.webComponentProperties(port,webComp,options);
267
268 return props;
269};
270
271
272MeandreController.prototype.abortFlow = function(port) {
273
274 var url = this.webDomain + ":" + port + this.abortFlowServlet;
275
276 var proxiedURL = this.proxyURL + "?" + encodeURIComponent(url);
277
278 // Asynchronous call to stop the given flow
279 var request = $.ajax({
280 url: proxiedURL
281 });
282
283 request.done(function(text) {
284 console.info( "Request to abort Meandre Flow for URL: " + url + " issued");
285 });
286
287 request.fail(function(jqXHR, textStatus) {
288 alert( "Request for URL:\n\n " + url + "\n\nfailed: " + textStatus + "\n--------\n" + jqXHR.statusText );
289 });
290
291};
292
293MeandreController.prototype.loadWorkbench = function(frameId,workflowPos) {
294
295 var url = this.workbenchURL + "?" + "displayMode=compact&workflowPos=" + workflowPos;
296 //var proxiedURL = this.proxyURL + "?" + encodeURIComponent(url);
297
298 $('#'+frameId).attr("src",url);
299
300 //$('#'+frameId).attr("src",proxiedURL);
301
302}
303
Note: See TracBrowser for help on using the repository browser.