__jsonopensrfreqid++;
}
+JSONOpenSRFRequest.method('create',function (service) {
+ this._service = service;
+ __jsonopensrfreq_hash['id' + this._hash_id] = {};
+});
+
JSONOpenSRFRequest.method('open',function (service, method, async) {
this._service = service;
- this._service = method;
+ this._method = method;
+ this._async = (async ? 1 : 0);
+ __jsonopensrfreq_hash['id' + this._hash_id] = {};
+});
+
+JSONOpenSRFRequest.method('close',function () {
+ this._service = null;
+ this._method = null;
+ this._async = null;
+ __jsonopensrfreq_hash['id' + this._hash_id] = {};
+});
+
+JSONOpenSRFRequest.method('call',function (method, async) {
+ this._method = method;
this._async = (async ? 1 : 0);
});
+JSONOpenSRFRequest.method('connect', function (service) {
+
+ if (service) this._service = service;
+
+ if (!this._service)
+ throw "call .open with a service before calling .connect";
+ try {
+ _OILS_FUNC_jsonopensrfrequest_connect(this._hash_id,this._service);
+ } catch (e) {
+ alert("Sorry, no JSONOpenSRFRequest support");
+ }
+
+ this.connected = __jsonopensrfreq_hash['id' + this._hash_id].connected;
+});
+
+JSONOpenSRFRequest.method('disconnect', function () {
+
+ if (!this._service)
+ throw "call .connect before calling .disconnect";
+ try {
+ _OILS_FUNC_jsonopensrfrequest_disconnect(this._hash_id);
+ } catch (e) {
+ alert("Sorry, no JSONOpenSRFRequest support");
+ }
+
+ this.connected = __jsonopensrfreq_hash['id' + this._hash_id].connected;
+});
+
+JSONOpenSRFRequest.method('finish', function () {
+
+ if (!this._service)
+ throw "call .connect before calling .finish";
+ try {
+ _OILS_FUNC_jsonopensrfrequest_disconnect(this._hash_id);
+ _OILS_FUNC_jsonopensrfrequest_finish(this._hash_id);
+ } catch (e) {
+ alert("Sorry, no JSONOpenSRFRequest support");
+ }
+
+ this.connected = __jsonopensrfreq_hash['id' + this._hash_id].connected;
+});
+
JSONOpenSRFRequest.method('send', function () {
- __jsonopensrfreq_hash['id' + this._hash_id] = {};
+ if (!this._service)
+ throw "call .open with a service and a method before calling .send";
+
+ var data = [];
+ for (var i = 0; i < arguments.length; i++) {
+ data[i] = arguments[i];
+ }
try {
- _OILS_FUNC_jsonopensrfrequest_send(this._hash_id,this._service,this._method,this._async,js2JSON(arguments));
+ //log_debug( this._hash_id + " -> " + this._service + " -> " + this._method + " -> " + this._async + " -> " + js2JSON(data));
+ _OILS_FUNC_jsonopensrfrequest_send(this._hash_id,this._service,this._method,this._async,js2JSON(data));
} catch (e) {
alert("Sorry, no JSONOpenSRFRequest support");
}
$self->insert(alert => sub { $logger->warn("script_runner: @_"); return 1;} );
$self->insert(load_lib => sub { $self->load_lib(@_); });
- # OpenSRF support function
+ # OpenSRF support functions
$self->insert(
_OILS_FUNC_jsonopensrfrequest_send =>
sub { $self->_jsonopensrfrequest_send(@_); }
);
+ $self->insert(
+ _OILS_FUNC_jsonopensrfrequest_connect =>
+ sub { $self->_jsonopensrfrequest_connect(@_); }
+ );
+ $self->insert(
+ _OILS_FUNC_jsonopensrfrequest_disconnect =>
+ sub { $self->_jsonopensrfrequest_disconnect(@_); }
+ );
+ $self->insert(
+ _OILS_FUNC_jsonopensrfrequest_finish =>
+ sub { $self->_jsonopensrfrequest_finish(@_); }
+ );
# XML support functions
$self->insert(
}
+our %_jsonopensrfrequest_cache = ();
+
+sub _jsonopensrfrequest_connect {
+ my $self = shift;
+ my $id = shift;
+ my $service = shift;
+
+ my $ctx = $self->context;
+ $ctx->object_by_path('__jsonopensrfreq_hash.id'.$id);
+
+ my $ses = $_jsonopensrfrequest_cache{$id} ||
+ do { $_jsonopensrfrequest_cache{$id} = OpenSRF::AppSession->create($service) };
+
+ if($ses->connect) {
+ $ctx->property_by_path('__jsonopensrfreq_hash.id'.$id.'.connected', 1);
+ } else {
+ $ctx->property_by_path('__jsonopensrfreq_hash.id'.$id.'.connected', 0);
+ }
+}
+
+sub _jsonopensrfrequest_disconnect {
+ my $self = shift;
+ my $id = shift;
+
+ my $ctx = $self->context;
+ $ctx->object_by_path('__jsonopensrfreq_hash.id'.$id);
+
+ my $ses = $_jsonopensrfrequest_cache{$id};
+ return unless $ses;
+
+ $ses->disconnect;
+}
+
+sub _jsonopensrfrequest_finish {
+ my $self = shift;
+ my $id = shift;
+
+ my $ctx = $self->context;
+ $ctx->object_by_path('__jsonopensrfreq_hash.id'.$id);
+
+ my $ses = $_jsonopensrfrequest_cache{$id};
+ return unless $ses;
+
+ $ses->finish;
+ delete $_jsonopensrfrequest_cache{$id};
+}
+
sub _jsonopensrfrequest_send {
my $self = shift;
my $id = shift;
# just so perl has access to it...
$ctx->object_by_path('__jsonopensrfreq_hash.id'.$id);
- my $ses = OpenSRF::AppSession->create($service);
+ my $ses = $_jsonopensrfrequest_cache{$id} ||
+ do { $_jsonopensrfrequest_cache{$id} = OpenSRF::AppSession->create($service) };
my $req = $ses->request($method,@p);
$req->wait_complete;
if (!$req->failed) {
- my $res = $req->content;
+ my $res = $req->recv->content;
- $ctx->property_by_path('__jsonopensrfreq_hash.id'.$id.'.responseText', $res);
+ $ctx->property_by_path('__jsonopensrfreq_hash.id'.$id.'.responseText', JSON->perl2JSON($res));
$ctx->property_by_path('__jsonopensrfreq_hash.id'.$id.'.readyState', 4);
$ctx->property_by_path('__jsonopensrfreq_hash.id'.$id.'.statusText', 'OK');
$ctx->property_by_path('__jsonopensrfreq_hash.id'.$id.'.status', '200');