private static void Version1(JsonRpcServiceClass service, Uri url, IndentedTextWriter writer) { Debug.Assert(service != null); Debug.Assert(url!= null); Debug.Assert(!url.IsFile); Debug.Assert(writer != null); writer.WriteLine("// Proxy version 1.0"); writer.WriteLine(); writer.Write("function "); writer.Write(service.Name); writer.WriteLine("(url)"); writer.WriteLine("{"); writer.Indent++; JsonRpcMethod[] methods = service.GetMethods(); string[] methodNames = new string[methods.Length]; for (int i = 0; i < methods.Length; i++) { JsonRpcMethod method = methods[i]; methodNames[i] = method.Name; if (method.Description.Length > 0) { // TODO: What to do if /* and */ appear in the summary? writer.Write("/* "); writer.Write(method.Description); writer.WriteLine(" */"); writer.WriteLine(); } writer.Write("this[\""); writer.Write(method.Name); writer.Write("\"] = function("); JsonRpcParameter[] parameters = method.GetParameters(); foreach (JsonRpcParameter parameter in parameters) { writer.Write(parameter.Name); writer.Write(", "); } writer.WriteLine("callback)"); writer.WriteLine("{"); writer.Indent++; writer.Write("return call(\""); writer.Write(method.Name); writer.Write("\", ["); foreach (JsonRpcParameter parameter in parameters) { if (parameter.Position > 0) writer.Write(','); writer.Write(' '); writer.Write(parameter.Name); } writer.WriteLine(" ], callback);"); writer.Indent--; writer.WriteLine("}"); writer.WriteLine(); } writer.Write("var url = typeof(url) === 'string' ? url : '"); writer.Write(url); writer.WriteLine("';"); writer.WriteLine(@"var self = this; var nextId = 0; function call(method, params, callback) { var request = { id : nextId++, method : method, params : params }; return callback == null ? callSync(method, request) : callAsync(method, request, callback); } function callSync(method, request) { var http = newHTTP(); http.open('POST', url, false, self.httpUserName, self.httpPassword); setupHeaders(http, method); http.send(JSON.stringify(request)); if (http.status != 200) throw { message : http.status + ' ' + http.statusText, toString : function() { return message; } }; var response = JSON.eval(http.responseText); if (response.error != null) throw response.error; return response.result; } function callAsync(method, request, callback) { var http = newHTTP(); http.open('POST', url, true, self.httpUserName, self.httpPassword); setupHeaders(http, method); http.onreadystatechange = function() { http_onreadystatechange(http, callback); } http.send(JSON.stringify(request)); return request.id; } function setupHeaders(http, method) { http.setRequestHeader('Content-Type', 'text/plain; charset=utf-8'); http.setRequestHeader('X-JSON-RPC', method); } function http_onreadystatechange(sender, callback) { if (sender.readyState == /* complete */ 4) { var response = sender.status == 200 ? JSON.eval(sender.responseText) : {}; response.xmlHTTP = sender; callback(response); } } function newHTTP() { return typeof(ActiveXObject) === 'function' ? new ActiveXObject('Microsoft.XMLHTTP') : /* IE 5 */ new XMLHttpRequest(); /* Safari 1.2, Mozilla 1.0/Firefox, and Netscape 7 */ }"); writer.Indent--; writer.WriteLine("}"); writer.WriteLine(); writer.Write(service.Name); writer.Write(".rpcMethods = "); JsonTextWriter jsonWriter = new JsonTextWriter(writer); jsonWriter.WriteArray(methodNames); writer.WriteLine(";"); }
private void Version2(JsonRpcServiceClass service, Uri url, IndentedTextWriter writer) { Debug.Assert(service != null); Debug.Assert(url!= null); Debug.Assert(!url.IsFile); Debug.Assert(writer != null); writer.WriteLine("// Proxy version 2.0"); writer.WriteLine(); writer.Write("var "); writer.Write(service.Name); writer.Write(@" = function() { var nextId = 0; var proxy = { url : """); writer.Write(url); writer.Write(@""", rpc : {"); writer.WriteLine(); writer.Indent += 3; JsonRpcMethod[] methods = service.GetMethods(); string[] methodNames = new string[methods.Length]; for (int i = 0; i < methods.Length; i++) methodNames[i] = methods[i].Name; Array.Sort(methodNames, methods); for (int i = 0; i < methods.Length; i++) { JsonRpcMethod method = methods[i]; writer.WriteLine(); if (method.Description.Length > 0) { // TODO: What to do if /* and */ appear in the summary? writer.Write("/* "); writer.Write(method.Description); writer.WriteLine(" */"); writer.WriteLine(); } writer.Write('\"'); writer.Write(method.Name); writer.Write("\" : function("); JsonRpcParameter[] parameters = method.GetParameters(); foreach (JsonRpcParameter parameter in parameters) { writer.Write(parameter.Name); writer.Write(", "); } writer.WriteLine("callback) {"); writer.Indent++; writer.Write("return new Call(\""); writer.Write(method.Name); writer.Write("\", ["); foreach (JsonRpcParameter parameter in parameters) { if (parameter.Position > 0) writer.Write(','); writer.Write(' '); writer.Write(parameter.Name); } writer.WriteLine(" ], callback);"); writer.Indent--; writer.Write("}"); if (i < (methods.Length - 1)) writer.Write(','); writer.WriteLine(); } writer.Indent--; writer.WriteLine(@"} } function Call(method, params, callback) { this.url = proxy.url; this.callback = callback; this.request = { id : ++nextId, method : method, params : params }; } Call.prototype.call = function(channel) { return channel(this); } return proxy; }();"); writer.Indent--; }