full OpenSRF support for JS
authormiker <miker@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Fri, 30 Jun 2006 20:33:37 +0000 (20:33 +0000)
committermiker <miker@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Fri, 30 Jun 2006 20:33:37 +0000 (20:33 +0000)
git-svn-id: svn://svn.open-ils.org/ILS/trunk@4874 dcc99617-32d9-48b4-a31d-7c20da2025e4

Open-ILS/src/javascript/backend/libs/jsonopensrfrequest.js
Open-ILS/src/perlmods/OpenILS/Utils/ScriptRunner.pm

index a37a095..fd9a8d1 100644 (file)
@@ -12,18 +12,85 @@ function JSONOpenSRFRequest () {
         __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");
        }
index aabf9f8..36b7e20 100644 (file)
@@ -54,11 +54,23 @@ sub init {
        $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(
@@ -355,6 +367,53 @@ sub _xmlhttprequest_send {
                
 }
 
+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;
@@ -370,14 +429,15 @@ sub _jsonopensrfrequest_send {
        # 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');