Orbjson 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +27 -4
- data/bin/orbjson +8 -2
- data/docs/getting_started_with_orbjson_short_version.pdf +0 -0
- data/docs/tutorial_code.tgz +0 -0
- data/examples/cgi/script/jsonrpc.js +1 -1
- data/examples/cgi/script/jsonrpc_async.js +260 -0
- data/examples/webrick/index.html +1 -1
- data/examples/webrick/{index.html,bak → index_async.html} +77 -69
- data/examples/webrick/script/jsonrpc.js +1 -1
- data/examples/webrick/script/jsonrpc_async.js +262 -0
- data/examples/webrick/server.rb +1 -1
- data/lib/orbjson.rb +72 -43
- data/lib/skeleton/script/jsonrpc.js +1 -1
- data/lib/skeleton/script/jsonrpc_async.js +261 -0
- data/lib/skeleton/webrick/config.yml +3 -2
- data/lib/utils.rb +29 -0
- metadata +11 -26
- data/dep/handy/script/jsonrpc.js +0 -187
- data/dep/handy/script/jsonrpc.js,bak +0 -187
- data/dep/required/ruby-json/ruby-json-1.1.1.gem +0 -0
- data/docs/todo.txt +0 -0
- data/examples/cgi/Orbjson.log +0 -66
- data/examples/cgi/index.html,bak +0 -71
- data/examples/cgi/json-rpc.rb,bak +0 -34
- data/examples/cgi/orbjson_cgi.log +0 -35
- data/examples/cgi/script/jsonrpc.js,bak +0 -187
- data/examples/cgi/services/sample.rb,bak +0 -21
- data/examples/webrick/Orbjson.log +0 -49
- data/examples/webrick/script/jsonrpc.js,bak +0 -187
- data/examples/webrick/server.rb,bak +0 -28
- data/examples/webrick/services/sample.rb,bak +0 -21
- data/lib/skeleton/cgi/json-rpc.rb,bak +0 -60
- data/lib/skeleton/script/jsonrpc.js,bak +0 -187
- data/lib/skeleton/webrick/server.rb,bak +0 -57
@@ -1,21 +0,0 @@
|
|
1
|
-
class Details
|
2
|
-
|
3
|
-
def initialize( )
|
4
|
-
@detail_set = Hash.new( "Nothing is known about your selection" )
|
5
|
-
data = { 'JSON' => "A serialization format for transmitting native JavaScript objects as plain text.",
|
6
|
-
'JSON-RPC' => "Like XML-RPC, but use JSON as the wire format.",
|
7
|
-
'Ruby' => "It's got soul, and it's super bad!" }
|
8
|
-
|
9
|
-
@detail_set.merge!( data )
|
10
|
-
end
|
11
|
-
|
12
|
-
|
13
|
-
def fetch item
|
14
|
-
@detail_set[ item ]
|
15
|
-
end
|
16
|
-
|
17
|
-
|
18
|
-
end
|
19
|
-
|
20
|
-
|
21
|
-
|
@@ -1,49 +0,0 @@
|
|
1
|
-
# Logfile created on Mon Feb 28 22:59:12 US Mountain Standard Time 2005 by logger.rb/1.5.2.4
|
2
|
-
D, [2005-02-28T22:59:12.571000 #1752] DEBUG -- : Regist now has
|
3
|
-
[:System, :pipeline_elements, :logging_interceptor, :logs, :system, :definition_context_factory, :log_for, :namespace_impl_factory, :service_models, :Details, :interceptor_impl_factory]
|
4
|
-
D, [2005-02-28T22:59:53.530000 #1752] DEBUG -- : klass, method = system, listMethods
|
5
|
-
D, [2005-02-28T22:59:53.530000 #1752] DEBUG -- : 146
|
6
|
-
E, [2005-02-28T22:59:53.590000 #1752] ERROR -- : System#list_methods error (eval):1:in `list_methods': uninitialized constant Orbjson::System::Pipeline_elements
|
7
|
-
E, [2005-02-28T22:59:53.630000 #1752] ERROR -- : System#list_methods error (eval):1:in `list_methods': uninitialized constant Orbjson::System::Logging_interceptor
|
8
|
-
E, [2005-02-28T22:59:53.660000 #1752] ERROR -- : System#list_methods error (eval):1:in `list_methods': uninitialized constant Orbjson::System::Logs
|
9
|
-
E, [2005-02-28T22:59:53.700000 #1752] ERROR -- : System#list_methods error (eval):1:in `list_methods': uninitialized constant Orbjson::System::Definition_context_factory
|
10
|
-
E, [2005-02-28T22:59:53.700000 #1752] ERROR -- : System#list_methods error (eval):1:in `list_methods': uninitialized constant Orbjson::System::Log_for
|
11
|
-
E, [2005-02-28T22:59:53.730000 #1752] ERROR -- : System#list_methods error (eval):1:in `list_methods': uninitialized constant Orbjson::System::Namespace_impl_factory
|
12
|
-
E, [2005-02-28T22:59:53.750000 #1752] ERROR -- : System#list_methods error (eval):1:in `list_methods': uninitialized constant Orbjson::System::Service_models
|
13
|
-
E, [2005-02-28T22:59:53.770000 #1752] ERROR -- : System#list_methods error (eval):1:in `list_methods': uninitialized constant Orbjson::System::Interceptor_impl_factory
|
14
|
-
D, [2005-02-28T23:00:00.460000 #1752] DEBUG -- : klass, method = details, fetch
|
15
|
-
D, [2005-02-28T23:00:00.470000 #1752] DEBUG -- : 137
|
16
|
-
D, [2005-02-28T23:00:00.490000 #1752] DEBUG -- : 142
|
17
|
-
D, [2005-02-28T23:00:03.244000 #1752] DEBUG -- : klass, method = details, fetch
|
18
|
-
D, [2005-02-28T23:00:03.244000 #1752] DEBUG -- : 137
|
19
|
-
D, [2005-02-28T23:00:03.264000 #1752] DEBUG -- : 142
|
20
|
-
D, [2005-02-28T23:00:04.736000 #1752] DEBUG -- : klass, method = details, fetch
|
21
|
-
D, [2005-02-28T23:00:04.736000 #1752] DEBUG -- : 137
|
22
|
-
D, [2005-02-28T23:00:04.756000 #1752] DEBUG -- : 142
|
23
|
-
D, [2005-02-28T23:00:06.058000 #1752] DEBUG -- : klass, method = details, fetch
|
24
|
-
D, [2005-02-28T23:00:06.058000 #1752] DEBUG -- : 137
|
25
|
-
D, [2005-02-28T23:00:06.078000 #1752] DEBUG -- : 142
|
26
|
-
D, [2005-02-28T23:00:07.530000 #1752] DEBUG -- : klass, method = details, fetch
|
27
|
-
D, [2005-02-28T23:00:07.540000 #1752] DEBUG -- : 137
|
28
|
-
D, [2005-02-28T23:00:07.560000 #1752] DEBUG -- : 142
|
29
|
-
D, [2005-02-28T23:00:08.882000 #1752] DEBUG -- : klass, method = details, fetch
|
30
|
-
D, [2005-02-28T23:00:08.892000 #1752] DEBUG -- : 137
|
31
|
-
D, [2005-02-28T23:00:08.912000 #1752] DEBUG -- : 142
|
32
|
-
D, [2005-02-28T23:00:10.714000 #1752] DEBUG -- : klass, method = details, fetch
|
33
|
-
D, [2005-02-28T23:00:10.714000 #1752] DEBUG -- : 137
|
34
|
-
D, [2005-02-28T23:00:10.734000 #1752] DEBUG -- : 142
|
35
|
-
D, [2005-02-28T23:00:11.766000 #1752] DEBUG -- : klass, method = details, fetch
|
36
|
-
D, [2005-02-28T23:00:11.766000 #1752] DEBUG -- : 137
|
37
|
-
D, [2005-02-28T23:00:11.766000 #1752] DEBUG -- : 142
|
38
|
-
D, [2005-02-28T23:00:12.447000 #1752] DEBUG -- : klass, method = details, fetch
|
39
|
-
D, [2005-02-28T23:00:12.457000 #1752] DEBUG -- : 137
|
40
|
-
D, [2005-02-28T23:00:12.477000 #1752] DEBUG -- : 142
|
41
|
-
D, [2005-02-28T23:00:15.231000 #1752] DEBUG -- : klass, method = details, fetch
|
42
|
-
D, [2005-02-28T23:00:15.231000 #1752] DEBUG -- : 137
|
43
|
-
D, [2005-02-28T23:00:15.261000 #1752] DEBUG -- : 142
|
44
|
-
D, [2005-02-28T23:00:19.267000 #1752] DEBUG -- : klass, method = details, fetch
|
45
|
-
D, [2005-02-28T23:00:19.277000 #1752] DEBUG -- : 137
|
46
|
-
D, [2005-02-28T23:00:19.297000 #1752] DEBUG -- : 142
|
47
|
-
D, [2005-02-28T23:00:20.258000 #1752] DEBUG -- : klass, method = details, fetch
|
48
|
-
D, [2005-02-28T23:00:20.268000 #1752] DEBUG -- : 137
|
49
|
-
D, [2005-02-28T23:00:20.288000 #1752] DEBUG -- : 142
|
@@ -1,187 +0,0 @@
|
|
1
|
-
/*
|
2
|
-
* JSON-RPC JavaScript client
|
3
|
-
*
|
4
|
-
* $Id: jsonrpc.js,v 1.1.1.1 2005/02/28 03:06:29 jamesgbritt Exp $
|
5
|
-
*
|
6
|
-
* Copyright (c) 2003-2004 Jan-Klaas Kollhof
|
7
|
-
* Copyright (c) 2005 Michael Clark, Metaparadigm Pte Ltd
|
8
|
-
*
|
9
|
-
* This code is based on Jan-Klaas' JavaScript o lait library (jsolait).
|
10
|
-
*
|
11
|
-
* This library is free software; you can redistribute it and/or
|
12
|
-
* modify it under the terms of the GNU Lesser General Public (LGPL)
|
13
|
-
* License as published by the Free Software Foundation; either
|
14
|
-
* version 2.1 of the License, or (at your option) any later version.
|
15
|
-
*
|
16
|
-
* This library is distributed in the hope that it will be useful,
|
17
|
-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
18
|
-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
19
|
-
* Lesser General Public License for more details: http://www.gnu.org/
|
20
|
-
*
|
21
|
-
*/
|
22
|
-
|
23
|
-
// TODO: Add async
|
24
|
-
// TODO: Add HTTP auth (user, password)
|
25
|
-
|
26
|
-
Object.prototype.toJSON = function Object_toJSON() {
|
27
|
-
var v = [];
|
28
|
-
for(attr in this) {
|
29
|
-
if(this[attr] == null) v.push("\"" + attr + "\": null");
|
30
|
-
else if(typeof this[attr] == "function"); // skip
|
31
|
-
else v.push("\"" + attr + "\": " + this[attr].toJSON());
|
32
|
-
}
|
33
|
-
return "{" + v.join(", ") + "}";
|
34
|
-
}
|
35
|
-
|
36
|
-
String.prototype.toJSON = function String_toJSON() {
|
37
|
-
return "\"" + this.replace(/([\"\\])/g, "\\$1") + "\"";
|
38
|
-
}
|
39
|
-
|
40
|
-
Number.prototype.toJSON = function Number_toJSON() {
|
41
|
-
return this.toString();
|
42
|
-
}
|
43
|
-
|
44
|
-
Boolean.prototype.toJSON = function Boolean_toJSON() {
|
45
|
-
return this.toString();
|
46
|
-
}
|
47
|
-
|
48
|
-
Date.prototype.toJSON = function Date_toJSON() {
|
49
|
-
this.valueOf().toString();
|
50
|
-
}
|
51
|
-
|
52
|
-
Array.prototype.toJSON = function Array_toJSON() {
|
53
|
-
var v = [];
|
54
|
-
for(var i=0; i<this.length; i++) {
|
55
|
-
if(this[i] == null) v.push("null");
|
56
|
-
else v.push(this[i].toJSON());
|
57
|
-
}
|
58
|
-
return "[" + v.join(", ") + "]";
|
59
|
-
}
|
60
|
-
|
61
|
-
JSONRpcClient = function JSONRpcClient_ctor(serverURL, objectID) {
|
62
|
-
this.serverURL = serverURL;
|
63
|
-
this.objectID = objectID;
|
64
|
-
|
65
|
-
// Lazy initialization of the XMLHttpRequest singleton
|
66
|
-
if(!JSONRpcClient.http)
|
67
|
-
JSONRpcClient.http = JSONRpcClient.getHTTPRequest();
|
68
|
-
|
69
|
-
// Add standard methods
|
70
|
-
this.addMethods(["system.listMethods"]);
|
71
|
-
|
72
|
-
// Query the methods on the server and add them to this object
|
73
|
-
var m = this.sendRequest("system.listMethods", []);
|
74
|
-
this.addMethods(m);
|
75
|
-
}
|
76
|
-
|
77
|
-
JSONRpcClient.Exception = function JSONRpcClient_Exception_ctor(code, msg) {
|
78
|
-
this.code = code;
|
79
|
-
this.msg = msg;
|
80
|
-
}
|
81
|
-
|
82
|
-
JSONRpcClient.Exception.prototype.toString =
|
83
|
-
function JSONRpcClient_Exception_toString(code, msg) {
|
84
|
-
return "JSONRpcClientException: " + this.msg;
|
85
|
-
}
|
86
|
-
|
87
|
-
JSONRpcClient.Method =
|
88
|
-
function JSONRpcClient_Method_ctor(client, methodName) {
|
89
|
-
var fn=function() {
|
90
|
-
var args = [];
|
91
|
-
for(var i=0;i<arguments.length;i++) {
|
92
|
-
args.push(arguments[i]);
|
93
|
-
}
|
94
|
-
return fn.client.sendRequest.call(fn.client, fn.methodName, args);
|
95
|
-
}
|
96
|
-
fn.client = client;
|
97
|
-
fn.methodName = methodName;
|
98
|
-
return fn;
|
99
|
-
}
|
100
|
-
|
101
|
-
JSONRpcClient.prototype.addMethods =
|
102
|
-
function JSONRpcClient_addMethods(methodNames) {
|
103
|
-
for(var i=0; i<methodNames.length; i++) {
|
104
|
-
var obj = this;
|
105
|
-
var names = methodNames[i].split(".");
|
106
|
-
for(var n=0; n<names.length-1; n++){
|
107
|
-
var name = names[n];
|
108
|
-
if(obj[name]){
|
109
|
-
obj = obj[name];
|
110
|
-
}
|
111
|
-
else {
|
112
|
-
obj[name] = new Object();
|
113
|
-
obj = obj[name];
|
114
|
-
}
|
115
|
-
}
|
116
|
-
var name = names[names.length-1];
|
117
|
-
if(!obj[name]){
|
118
|
-
var method = new JSONRpcClient.Method(this, methodNames[i]);
|
119
|
-
obj[name] = method;
|
120
|
-
}
|
121
|
-
}
|
122
|
-
}
|
123
|
-
|
124
|
-
JSONRpcClient.prototype.sendRequest =
|
125
|
-
function JSONRpcClient_sendRequest(methodName, args) {
|
126
|
-
// Make request object
|
127
|
-
var obj = {"method" : methodName, "params" : args};
|
128
|
-
if (this.objectID) obj.objectID = this.objectID;
|
129
|
-
|
130
|
-
// Marshall the request object to a JSON string
|
131
|
-
var req_data = obj.toJSON();
|
132
|
-
|
133
|
-
// Send the request
|
134
|
-
JSONRpcClient.http.open("POST", this.serverURL, false); // no async
|
135
|
-
// setRequestHeader is missing in Opera 8 Beta
|
136
|
-
try {
|
137
|
-
JSONRpcClient.http.setRequestHeader("Content-type", "text/plain");
|
138
|
-
}
|
139
|
-
catch(e) {}
|
140
|
-
JSONRpcClient.http.send(req_data);
|
141
|
-
|
142
|
-
// Unmarshall the response
|
143
|
-
// DEBUG
|
144
|
-
try {
|
145
|
-
eval("var obj = " + JSONRpcClient.http.responseText);
|
146
|
-
}
|
147
|
-
catch(e) {
|
148
|
-
alert( e )
|
149
|
-
alert( JSONRpcClient.http.responseText )
|
150
|
-
obj.err = e.toString()
|
151
|
-
}
|
152
|
-
if( obj.error) {
|
153
|
-
alert( JSONRpcClient.http.responseText )
|
154
|
-
throw new JSONRpcClient.Exception(obj.error.code, obj.error.msg);
|
155
|
-
}
|
156
|
-
var res = obj.result;
|
157
|
-
|
158
|
-
// Handle CallableProxy
|
159
|
-
if(res && res.objectID && res.JSONRPCType == "CallableReference")
|
160
|
-
return new JSONRpcClient(this.serverURL, res.objectID);
|
161
|
-
|
162
|
-
return res;
|
163
|
-
}
|
164
|
-
|
165
|
-
JSONRpcClient.getHTTPRequest = function JSONRpcClient_getHTTPRequest() {
|
166
|
-
try { // to get the mozilla httprequest object
|
167
|
-
return new XMLHttpRequest();
|
168
|
-
}
|
169
|
-
catch(e) {}
|
170
|
-
|
171
|
-
try { // to get MS HTTP request object
|
172
|
-
return new ActiveXObject("Msxml2.XMLHTTP.4.0");
|
173
|
-
}
|
174
|
-
catch(e) {}
|
175
|
-
|
176
|
-
try { // to get MS HTTP request object
|
177
|
-
return new ActiveXObject("Msxml2.XMLHTTP");
|
178
|
-
}
|
179
|
-
catch(e) {}
|
180
|
-
|
181
|
-
try {// to get the old MS HTTP request object
|
182
|
-
return new ActiveXObject("microsoft.XMLHTTP");
|
183
|
-
}
|
184
|
-
catch(e) {}
|
185
|
-
|
186
|
-
throw new JSONRpcClient.Exception(0, "Can't create XMLHttpRequest object");
|
187
|
-
}
|
@@ -1,28 +0,0 @@
|
|
1
|
-
#!/usr/local/bin/ruby
|
2
|
-
require 'webrick'
|
3
|
-
|
4
|
-
require "orbjson"
|
5
|
-
include WEBrick
|
6
|
-
|
7
|
-
|
8
|
-
Socket.do_not_reverse_lookup = true
|
9
|
-
|
10
|
-
$logger = Logger.new( "orbjson.log" )
|
11
|
-
$logger.level = Logger::WARN
|
12
|
-
|
13
|
-
s = HTTPServer.new( :Port => 2222,
|
14
|
-
:DocumentRoot => File.dirname( __FILE__ ) )
|
15
|
-
|
16
|
-
s.mount("/json-rpc", Orbjson::WEBrick_JSON_RPC )
|
17
|
-
|
18
|
-
|
19
|
-
s.mount_proc( "/exit" ){|req, res| s.shutdown; exit; }
|
20
|
-
s.mount_proc( "/quit" ){|req, res| s.shutdown; exit; }
|
21
|
-
|
22
|
-
Orbjson::System.init( "file://config.yml" )
|
23
|
-
|
24
|
-
|
25
|
-
trap( "INT" ){ s.shutdown }
|
26
|
-
trap( 1 ){ s.shutdown }
|
27
|
-
s.start
|
28
|
-
|
@@ -1,21 +0,0 @@
|
|
1
|
-
class Details
|
2
|
-
|
3
|
-
def initialize( )
|
4
|
-
@detail_set = Hash.new( "Nothing is known about your selection" )
|
5
|
-
data = { 'JSON' => "A serialization format for transmitting native JavaScript objects as plain text.",
|
6
|
-
'JSON-RPC' => "Like XML-RPC, but use JSON as the wire format.",
|
7
|
-
'Ruby' => "It's got soul, and it's super bad!" }
|
8
|
-
|
9
|
-
@detail_set.merge!( data )
|
10
|
-
end
|
11
|
-
|
12
|
-
|
13
|
-
def fetch item
|
14
|
-
@detail_set[ item ]
|
15
|
-
end
|
16
|
-
|
17
|
-
|
18
|
-
end
|
19
|
-
|
20
|
-
|
21
|
-
|
@@ -1,60 +0,0 @@
|
|
1
|
-
#!/usr/local/bin/ruby
|
2
|
-
|
3
|
-
|
4
|
-
require 'cgi'
|
5
|
-
require 'rubygems'
|
6
|
-
require "orbjson"
|
7
|
-
require 'logger'
|
8
|
-
|
9
|
-
include Orbjson
|
10
|
-
|
11
|
-
|
12
|
-
$logger = Logger.new( "orbjson_cgi.log" )
|
13
|
-
|
14
|
-
# Change this to ERROR or something when your code does
|
15
|
-
# all it should, and does it right:
|
16
|
-
$logger.level = Logger::DEBUG
|
17
|
-
|
18
|
-
|
19
|
-
$logger.debug( "Have a request!" )
|
20
|
-
|
21
|
-
# You can configure the Orbjsn services list in a few ways.
|
22
|
-
# Well, three, really:
|
23
|
-
# 1. Pass in a path to a local YAML file; the path must begin with
|
24
|
-
# 'file://'
|
25
|
-
# For example: cfg = 'file://config.yaml'
|
26
|
-
# json_rpc = CGI_JSON_RPC.new( cfg )
|
27
|
-
#
|
28
|
-
# 2. Pass in some YAML:
|
29
|
-
# cfg = 'services/sample:
|
30
|
-
# - Details'
|
31
|
-
# json_rpc = CGI_JSON_RPC.new( cfg )
|
32
|
-
#
|
33
|
-
# 3. Pass in some an atual Hash objectL:
|
34
|
-
# cfg = { 'services/sample' => ['Details'] }
|
35
|
-
# json_rpc = CGI_JSON_RPC.new( cfg )
|
36
|
-
#
|
37
|
-
# The hash (however you express it) consists of 'require' paths mapped
|
38
|
-
# to arrays of classes to instantiate.
|
39
|
-
|
40
|
-
|
41
|
-
# Change this to suit your actual situation:
|
42
|
-
cfg = 'services/sample:
|
43
|
-
- Details'
|
44
|
-
json_rpc = CGI_JSON_RPC.new( cfg )
|
45
|
-
|
46
|
-
print "content-type: text/javascript\n\n"
|
47
|
-
res = json_rpc.process_request
|
48
|
-
$logger.debug( "Have a response:\n#{res}" )
|
49
|
-
# Logging may make an already slow CGI app go
|
50
|
-
# even slower, so consider dropping this suf when things are good.
|
51
|
-
|
52
|
-
print res
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
@@ -1,187 +0,0 @@
|
|
1
|
-
/*
|
2
|
-
* JSON-RPC JavaScript client
|
3
|
-
*
|
4
|
-
* $Id: jsonrpc.js,v 1.1.1.1 2005/02/28 03:06:28 jamesgbritt Exp $
|
5
|
-
*
|
6
|
-
* Copyright (c) 2003-2004 Jan-Klaas Kollhof
|
7
|
-
* Copyright (c) 2005 Michael Clark, Metaparadigm Pte Ltd
|
8
|
-
*
|
9
|
-
* This code is based on Jan-Klaas' JavaScript o lait library (jsolait).
|
10
|
-
*
|
11
|
-
* This library is free software; you can redistribute it and/or
|
12
|
-
* modify it under the terms of the GNU Lesser General Public (LGPL)
|
13
|
-
* License as published by the Free Software Foundation; either
|
14
|
-
* version 2.1 of the License, or (at your option) any later version.
|
15
|
-
*
|
16
|
-
* This library is distributed in the hope that it will be useful,
|
17
|
-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
18
|
-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
19
|
-
* Lesser General Public License for more details: http://www.gnu.org/
|
20
|
-
*
|
21
|
-
*/
|
22
|
-
|
23
|
-
// TODO: Add async
|
24
|
-
// TODO: Add HTTP auth (user, password)
|
25
|
-
|
26
|
-
Object.prototype.toJSON = function Object_toJSON() {
|
27
|
-
var v = [];
|
28
|
-
for(attr in this) {
|
29
|
-
if(this[attr] == null) v.push("\"" + attr + "\": null");
|
30
|
-
else if(typeof this[attr] == "function"); // skip
|
31
|
-
else v.push("\"" + attr + "\": " + this[attr].toJSON());
|
32
|
-
}
|
33
|
-
return "{" + v.join(", ") + "}";
|
34
|
-
}
|
35
|
-
|
36
|
-
String.prototype.toJSON = function String_toJSON() {
|
37
|
-
return "\"" + this.replace(/([\"\\])/g, "\\$1") + "\"";
|
38
|
-
}
|
39
|
-
|
40
|
-
Number.prototype.toJSON = function Number_toJSON() {
|
41
|
-
return this.toString();
|
42
|
-
}
|
43
|
-
|
44
|
-
Boolean.prototype.toJSON = function Boolean_toJSON() {
|
45
|
-
return this.toString();
|
46
|
-
}
|
47
|
-
|
48
|
-
Date.prototype.toJSON = function Date_toJSON() {
|
49
|
-
this.valueOf().toString();
|
50
|
-
}
|
51
|
-
|
52
|
-
Array.prototype.toJSON = function Array_toJSON() {
|
53
|
-
var v = [];
|
54
|
-
for(var i=0; i<this.length; i++) {
|
55
|
-
if(this[i] == null) v.push("null");
|
56
|
-
else v.push(this[i].toJSON());
|
57
|
-
}
|
58
|
-
return "[" + v.join(", ") + "]";
|
59
|
-
}
|
60
|
-
|
61
|
-
JSONRpcClient = function JSONRpcClient_ctor(serverURL, objectID) {
|
62
|
-
this.serverURL = serverURL;
|
63
|
-
this.objectID = objectID;
|
64
|
-
|
65
|
-
// Lazy initialization of the XMLHttpRequest singleton
|
66
|
-
if(!JSONRpcClient.http)
|
67
|
-
JSONRpcClient.http = JSONRpcClient.getHTTPRequest();
|
68
|
-
|
69
|
-
// Add standard methods
|
70
|
-
this.addMethods(["system.listMethods"]);
|
71
|
-
|
72
|
-
// Query the methods on the server and add them to this object
|
73
|
-
var m = this.sendRequest("system.listMethods", []);
|
74
|
-
this.addMethods(m);
|
75
|
-
}
|
76
|
-
|
77
|
-
JSONRpcClient.Exception = function JSONRpcClient_Exception_ctor(code, msg) {
|
78
|
-
this.code = code;
|
79
|
-
this.msg = msg;
|
80
|
-
}
|
81
|
-
|
82
|
-
JSONRpcClient.Exception.prototype.toString =
|
83
|
-
function JSONRpcClient_Exception_toString(code, msg) {
|
84
|
-
return "JSONRpcClientException: " + this.msg;
|
85
|
-
}
|
86
|
-
|
87
|
-
JSONRpcClient.Method =
|
88
|
-
function JSONRpcClient_Method_ctor(client, methodName) {
|
89
|
-
var fn=function() {
|
90
|
-
var args = [];
|
91
|
-
for(var i=0;i<arguments.length;i++) {
|
92
|
-
args.push(arguments[i]);
|
93
|
-
}
|
94
|
-
return fn.client.sendRequest.call(fn.client, fn.methodName, args);
|
95
|
-
}
|
96
|
-
fn.client = client;
|
97
|
-
fn.methodName = methodName;
|
98
|
-
return fn;
|
99
|
-
}
|
100
|
-
|
101
|
-
JSONRpcClient.prototype.addMethods =
|
102
|
-
function JSONRpcClient_addMethods(methodNames) {
|
103
|
-
for(var i=0; i<methodNames.length; i++) {
|
104
|
-
var obj = this;
|
105
|
-
var names = methodNames[i].split(".");
|
106
|
-
for(var n=0; n<names.length-1; n++){
|
107
|
-
var name = names[n];
|
108
|
-
if(obj[name]){
|
109
|
-
obj = obj[name];
|
110
|
-
}
|
111
|
-
else {
|
112
|
-
obj[name] = new Object();
|
113
|
-
obj = obj[name];
|
114
|
-
}
|
115
|
-
}
|
116
|
-
var name = names[names.length-1];
|
117
|
-
if(!obj[name]){
|
118
|
-
var method = new JSONRpcClient.Method(this, methodNames[i]);
|
119
|
-
obj[name] = method;
|
120
|
-
}
|
121
|
-
}
|
122
|
-
}
|
123
|
-
|
124
|
-
JSONRpcClient.prototype.sendRequest =
|
125
|
-
function JSONRpcClient_sendRequest(methodName, args) {
|
126
|
-
// Make request object
|
127
|
-
var obj = {"method" : methodName, "params" : args};
|
128
|
-
if (this.objectID) obj.objectID = this.objectID;
|
129
|
-
|
130
|
-
// Marshall the request object to a JSON string
|
131
|
-
var req_data = obj.toJSON();
|
132
|
-
|
133
|
-
// Send the request
|
134
|
-
JSONRpcClient.http.open("POST", this.serverURL, false); // no async
|
135
|
-
// setRequestHeader is missing in Opera 8 Beta
|
136
|
-
try {
|
137
|
-
JSONRpcClient.http.setRequestHeader("Content-type", "text/plain");
|
138
|
-
}
|
139
|
-
catch(e) {}
|
140
|
-
JSONRpcClient.http.send(req_data);
|
141
|
-
|
142
|
-
// Unmarshall the response
|
143
|
-
// DEBUG
|
144
|
-
try {
|
145
|
-
eval("var obj = " + JSONRpcClient.http.responseText);
|
146
|
-
}
|
147
|
-
catch(e) {
|
148
|
-
alert( e )
|
149
|
-
alert( JSONRpcClient.http.responseText )
|
150
|
-
obj.err = e.toString()
|
151
|
-
}
|
152
|
-
if( obj.error) {
|
153
|
-
alert( JSONRpcClient.http.responseText )
|
154
|
-
throw new JSONRpcClient.Exception(obj.error.code, obj.error.msg);
|
155
|
-
}
|
156
|
-
var res = obj.result;
|
157
|
-
|
158
|
-
// Handle CallableProxy
|
159
|
-
if(res && res.objectID && res.JSONRPCType == "CallableReference")
|
160
|
-
return new JSONRpcClient(this.serverURL, res.objectID);
|
161
|
-
|
162
|
-
return res;
|
163
|
-
}
|
164
|
-
|
165
|
-
JSONRpcClient.getHTTPRequest = function JSONRpcClient_getHTTPRequest() {
|
166
|
-
try { // to get the mozilla httprequest object
|
167
|
-
return new XMLHttpRequest();
|
168
|
-
}
|
169
|
-
catch(e) {}
|
170
|
-
|
171
|
-
try { // to get MS HTTP request object
|
172
|
-
return new ActiveXObject("Msxml2.XMLHTTP.4.0");
|
173
|
-
}
|
174
|
-
catch(e) {}
|
175
|
-
|
176
|
-
try { // to get MS HTTP request object
|
177
|
-
return new ActiveXObject("Msxml2.XMLHTTP");
|
178
|
-
}
|
179
|
-
catch(e) {}
|
180
|
-
|
181
|
-
try {// to get the old MS HTTP request object
|
182
|
-
return new ActiveXObject("microsoft.XMLHTTP");
|
183
|
-
}
|
184
|
-
catch(e) {}
|
185
|
-
|
186
|
-
throw new JSONRpcClient.Exception(0, "Can't create XMLHttpRequest object");
|
187
|
-
}
|