]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/util/socket.js
LP 2061136 follow-up: ng lint --fix
[working/Evergreen.git] / Open-ILS / xul / staff_client / chrome / content / util / socket.js
1 dump('entering util/socket.js\n');
2 // vim:noet:sw=4:ts=4:
3
4 /*
5     Usage example:
6
7     Install netcat on a server and as root do:  nc -l -p 5000
8
9     Then, in the staff client, load Admin -> For Developers -> Javascript Shell
10
11     Enter:
12
13     JSAN.use('util.socket');
14     var s = new util.socket('server hostname or IP address here', 5000);
15     s.write('hello\n');
16
17     On the server, reply with world<enter>
18
19     Back in the javascript shell, use
20
21     s.read();
22
23 */
24
25 if (typeof util == 'undefined') util = {};
26 util.socket = function (host,port,listener) {
27
28     try {
29         if (!host && !port) {
30             throw('host = ' + host + '  port = ' + port);
31         }
32
33         this.host = host;
34         this.port = port;
35         if (listener) {
36             this.listener = listener;
37         } else {
38             this._create_listener = true;
39         }
40
41         this.init();
42
43     } catch(E) {
44         alert('error in util.socket constructor: ' + E);
45         throw(E);
46     }
47
48     return this;
49 };
50
51 util.socket.prototype = {
52     '_data' : '',
53     '_onStartRequest' : null,
54     'onStartRequest' : function(callback) {
55         this._onStartRequest = callback;
56     },
57     '_onDataAvailable' : null,
58     'onDataAvailable' : function(callback) {
59         this._onDataAvailable = callback;
60     },
61     '_onStopRequest' : null,
62     'onStopRequest' : function(callback) {
63         this._onStopRequest = callback;
64     },
65     '_reconnectOnStop' : false,
66     'dataCallback' : function(callback) {
67         this._dataCallback = callback;
68     },
69     'init' : function() {
70         const Cc = Components.classes;
71         const Ci = Components.interfaces;
72         const socket_Cc = "@mozilla.org/network/socket-transport-service;1";
73         var transportService = Cc[socket_Cc].getService(
74             Ci.nsISocketTransportService
75         );
76         this.socket = transportService.createTransport(
77             null,0,this.host,this.port,null);
78         this.outputStream = this.socket.openOutputStream(0,0,0);
79         this.rawInputStream = this.socket.openInputStream(0,0,0);
80         const istream_Cc = "@mozilla.org/scriptableinputstream;1";
81         this.inputStream = Cc[istream_Cc].createInstance(
82             Ci.nsIScriptableInputStream
83         ).init(
84             this.rawInputStream
85         );
86
87         if (this._create_listener) {
88             this.listener = this.generate_listener();
89         }
90         const pump_Cc = "@mozilla.org/network/input-stream-pump;1";
91         this.pump = Cc[pump_Cc].createInstance(Ci.nsIInputStreamPump);
92         this.pump.init(this.rawInputStream,-1,-1,0,0,true);
93         this.pump.asyncRead(this.listener,null);
94     },
95     'close' : function() {
96         dump('util.socket.close() on page ' + location.href + '\n');
97         try {
98             this.pump.cancel(true);
99             this.socket.close(true);
100         } catch(E) {
101             dump('Error in util.socket.close(): ' + E + '\n');
102         }
103     },
104     'generate_listener' : function() {
105         var obj = this;
106         Components.utils.import("resource://gre/modules/NetUtil.jsm");
107         return {
108             onStartRequest : function(request,context) {
109                 dump('util.socket.pump.onStartRequest on page ' + location.href + '\n');
110                 if (obj._onStartRequest) {
111                     obj._onStartRequest(request,context);
112                 }
113             },
114             onDataAvailable : function(request,context,stream,offset,count) {
115                 dump('util.socket.pump.onDataAvailable on page ' + location.href + '\n');
116                 if (obj._onDataAvailable) {
117                     dump('util.socket.pump.onDataAvailable using _onDataAvailable\n');
118                     obj._onDataAvailable(request,context,stream,offset,count);
119                 }
120                 var data = NetUtil.readInputStreamToString(stream,count);
121                 dump(data + '\n');
122                 if (obj._dataCallback) {
123                     dump('util.socket.pump.onDataAvailable using _dataCallback\n');
124                     obj._dataCallback(data);
125                 } else {
126                     dump('util.socket.pump.onDataAvailable no _dataCallback to use\n');
127                     obj._data += data;
128                 }
129             },
130             onStopRequest : function(request,context,result) {
131                 dump('util.socket.pump.onStopRequest on page ' + location.href + '\n');
132                 if (!Components.isSuccessCode(result)) {
133                     dump(result + '\n');
134                 }
135                 if (obj._onStopRequest) {
136                     obj._onStopRequest(request,context,result);
137                 } else if (obj._reconnectOnStop) {
138                     // FIXME - cannot get this to work, but you
139                     // can overwrite the socket with a new socket
140                     // within  _onStopRequest
141                 }
142             }
143         };
144     },
145     'write' : function(s) {
146         this.outputStream.write(s,s.length);
147     },
148     'read' : function(peek) {
149         var data = this._data;
150         if (!peek) {
151             this._data = '';
152         }
153         return data;
154     }
155 }
156
157 dump('exiting util/socket.js\n');