home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 97 / CD-ROM 97 / CD-ROM 97.iso / internet / ghostzilla / ghsetup.exe / chrome / chatzilla.jar / content / chatzilla / lib / js / connection-xpcom.js < prev    next >
Encoding:
Text File  |  2002-04-09  |  8.5 KB  |  324 lines

  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  *
  3.  * The contents of this file are subject to the Mozilla Public
  4.  * License Version 1.1 (the "License"); you may not use this file
  5.  * except in compliance with the License. You may obtain a copy of
  6.  * the License at http://www.mozilla.org/MPL/
  7.  *
  8.  * Software distributed under the License is distributed on an "AS
  9.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10.  * implied. See the License for the specific language governing
  11.  * rights and limitations under the License.
  12.  *
  13.  * The Original Code is JSIRC Library
  14.  *
  15.  * The Initial Developer of the Original Code is New Dimensions Consulting,
  16.  * Inc. Portions created by New Dimensions Consulting, Inc. are
  17.  * Copyright (C) 1999 New Dimenstions Consulting, Inc. All
  18.  * Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  *  Robert Ginda, rginda@ndcico.com, original author
  22.  *  Peter Van der Beken, peter.vanderbeken@pandora.be, necko-only version
  23.  *
  24.  * depends on utils.js, and XPCOM/XPConnect in the JS environment
  25.  *
  26.  */
  27.  
  28. const NS_ERROR_MODULE_NETWORK = 2152398848;
  29.  
  30. const NS_ERROR_UNKNOWN_HOST = NS_ERROR_MODULE_NETWORK + 30;
  31. const NS_ERROR_CONNECTION_REFUSED = NS_ERROR_MODULE_NETWORK + 13;
  32. const NS_ERROR_NET_TIMEOUT = NS_ERROR_MODULE_NETWORK + 14;
  33.  
  34. const NS_NET_STATUS_RESOLVING_HOST = NS_ERROR_MODULE_NETWORK + 3;
  35. const NS_NET_STATUS_CONNECTED_TO = NS_ERROR_MODULE_NETWORK + 4;
  36. const NS_NET_STATUS_SENDING_TO = NS_ERROR_MODULE_NETWORK + 5;
  37. const NS_NET_STATUS_RECEIVING_FROM = NS_ERROR_MODULE_NETWORK + 6;
  38. const NS_NET_STATUS_CONNECTING_TO = NS_ERROR_MODULE_NETWORK + 7;
  39.  
  40. function toScriptableInputStream (i)
  41. {
  42.     var si = Components.classes["@mozilla.org/scriptableinputstream;1"];
  43.     
  44.     si = si.createInstance();
  45.     si = si.QueryInterface(Components.interfaces.nsIScriptableInputStream);
  46.     si.init(i);
  47.  
  48.     return si;
  49.     
  50. }
  51.  
  52. function CBSConnection ()
  53. {
  54.     var sockServiceClass =
  55.         Components.classesByID["{c07e81e0-ef12-11d2-92b6-00105a1b0d64}"];
  56.     
  57.     if (!sockServiceClass)
  58.         throw ("Couldn't get socket service class.");
  59.     
  60.     var sockService = sockServiceClass.getService();
  61.     if (!sockService)
  62.         throw ("Couldn't get socket service.");
  63.  
  64.     this._sockService = sockService.QueryInterface
  65.         (Components.interfaces.nsISocketTransportService);
  66.  
  67.     this.wrappedJSObject = this;
  68.  
  69. }
  70.  
  71. CBSConnection.prototype.connect =
  72. function bc_connect(host, port, bind, tcp_flag, observer)
  73. {
  74.     if (typeof tcp_flag == "undefined")
  75.         tcp_flag = false;
  76.     
  77.     this.host = host.toLowerCase();
  78.     this.port = port;
  79.     this.bind = bind;
  80.     this.tcp_flag = tcp_flag;
  81.  
  82.     // Lets get a transportInfo for this
  83.     var pps = Components.classes["@mozilla.org/network/protocol-proxy-service;1"].
  84.         getService().
  85.         QueryInterface(Components.interfaces.nsIProtocolProxyService);
  86.  
  87.     if (!pps)
  88.         throw ("Couldn't get protocol proxy service");
  89.  
  90.     var ios = Components.classes["@mozilla.org/network/io-service;1"].
  91.       getService(Components.interfaces.nsIIOService);
  92.     var spec = "irc://" + host + ':' + port;
  93.     var uri = ios.newURI(spec,null,null);
  94.     var info = pps.examineForProxy(uri);
  95.  
  96.     this._transport = this._sockService.createTransport (host, port, info,
  97.                                                          0, 0);
  98.     if (!this._transport)
  99.         throw ("Error creating transport.");
  100.  
  101.     if (jsenv.HAS_NSPR_EVENTQ) 
  102.     {   /* we've got an event queue, so start up an async write */
  103.         this._streamProvider = new StreamProvider (observer);
  104.         this._write_req =
  105.             this._transport.asyncWrite (this._streamProvider, this,
  106.                                         0, -1, 0);
  107.     }
  108.     else
  109.     {   /* no nspr event queues in this environment, we can't use async calls,
  110.          * so set up the streams. */
  111.         this._outputStream = this._transport.openOutputStream(0, -1, 0);
  112.         if (!this._outputStream)
  113.             throw "Error getting output stream.";
  114.         this._inputStream =
  115.             toScriptableInputStream(this._transport.openInputStream (0, -1, 0));
  116.         if (!this._inputStream)
  117.             throw "Error getting input stream.";
  118.     }    
  119.  
  120.     this.connectDate = new Date();
  121.     this.isConnected = true;
  122.  
  123.     return this.isConnected;
  124.   
  125. }
  126.  
  127. CBSConnection.prototype.disconnect =
  128. function bc_disconnect()
  129. {
  130.     if ("_inputStream" in this && this._inputStream)
  131.         this._inputStream.close();
  132.     /*
  133.     this._streamProvider.close();
  134.     if (this._streamProvider.isBlocked)
  135.       this._write_req.resume();
  136.     */
  137. }
  138.  
  139. CBSConnection.prototype.sendData =
  140. function bc_senddata(str)
  141. {
  142.     if (!this.isConnected)
  143.         throw "Not Connected.";
  144.  
  145.     if (jsenv.HAS_NSPR_EVENTQ)
  146.         this.asyncWrite (str);
  147.     else
  148.         this.sendDataNow (str);
  149. }
  150.     
  151. CBSConnection.prototype.readData =
  152. function bc_readdata(timeout, count)
  153. {
  154.     if (!this.isConnected)
  155.         throw "Not Connected.";
  156.  
  157.     var rv;
  158.  
  159.     try
  160.     {
  161.         rv = this._inputStream.read (count);
  162.     }
  163.     catch (ex)
  164.     {
  165.         dd ("*** Caught " + ex + " while reading.")
  166.         this.isConnected = false;
  167.         throw (ex);
  168.     }
  169.     
  170.     return rv;
  171. }
  172.  
  173. CBSConnection.prototype.startAsyncRead =
  174. function bc_saread (observer)
  175. {
  176.     this._transport.asyncRead (new StreamListener (observer), this, 0, -1, 0);
  177. }
  178.  
  179. CBSConnection.prototype.asyncWrite =
  180. function bc_awrite (str)
  181. {
  182.     this._streamProvider.pendingData += str;
  183.     if (this._streamProvider.isBlocked)
  184.     {
  185.         this._write_req.resume();
  186.         this._streamProvider.isBlocked = false;
  187.     }
  188. }
  189.  
  190. CBSConnection.prototype.hasPendingWrite =
  191. function bc_haspwrite ()
  192. {
  193.     return (this._streamProvider.pendingData != "");
  194. }
  195.  
  196. CBSConnection.prototype.sendDataNow =
  197. function bc_senddatanow(str)
  198. {
  199.     var rv = false;
  200.     
  201.     try
  202.     {
  203.         this._outputStream.write(str, str.length);
  204.         rv = true;
  205.     }
  206.     catch (ex)
  207.     {
  208.         dd ("*** Caught " + ex + " while sending.")
  209.         this.isConnected = false;
  210.         throw (ex);
  211.     }
  212.     
  213.     return rv;
  214. }
  215.  
  216. function _notimpl ()
  217. {
  218.     throw "Not Implemented.";
  219. }
  220.  
  221. if (!jsenv.HAS_NSPR_EVENTQ)
  222. {
  223.     CBSConnection.prototype.startAsyncRead = _notimpl;
  224.     CBSConnection.prototype.asyncWrite = _notimpl;
  225. }
  226. else
  227. {
  228.     CBSConnection.prototype.sendDataNow = _notimpl;
  229. }
  230.  
  231. delete _notimpl;
  232.     
  233. function StreamProvider(observer)
  234. {
  235.     this._observer = observer;
  236. }
  237.  
  238. StreamProvider.prototype.pendingData = "";
  239. StreamProvider.prototype.isBlocked = true;
  240.  
  241. StreamProvider.prototype.close =
  242. function sp_close ()
  243. {
  244.     this.isClosed = true;
  245. }
  246.     
  247. StreamProvider.prototype.onDataWritable =
  248. function sp_datawrite (request, ctxt, ostream, offset, count)
  249. {
  250.     //dd ("StreamProvider.prototype.onDataWritable");
  251.  
  252.     if (this.isClosed)
  253.         throw Components.results.NS_BASE_STREAM_CLOSED;
  254.     
  255.     if (!this.pendingData)
  256.     {
  257.         this.isBlocked = true;
  258.  
  259.         /* this is here to support pre-XPCDOM builds (0.9.0 era), which
  260.          * don't have this result code mapped. */
  261.         if (!Components.results.NS_BASE_STREAM_WOULD_BLOCK)
  262.             throw 2152136711;
  263.         
  264.         throw Components.results.NS_BASE_STREAM_WOULD_BLOCK;
  265.     }
  266.     
  267.     var len = ostream.write (this.pendingData, this.pendingData.length);
  268.     this.pendingData = this.pendingData.substr (len);
  269. }
  270.  
  271. StreamProvider.prototype.onStartRequest =
  272. function sp_startreq (request, ctxt)
  273. {
  274.     //dd ("StreamProvider::onStartRequest: " + request + ", " + ctxt);
  275. }
  276.  
  277.  
  278. StreamProvider.prototype.onStopRequest =
  279. function sp_stopreq (request, ctxt, status)
  280. {
  281.     //dd ("StreamProvider::onStopRequest: " + request + ", " + ctxt + ", " +
  282.     //    status);
  283.     if (this._observer)
  284.         this._observer.onStreamClose(status);
  285. }
  286.  
  287. function StreamListener(observer)
  288. {
  289.     this._observer = observer;
  290. }
  291.  
  292. StreamListener.prototype.onStartRequest =
  293. function sl_startreq (request, ctxt)
  294. {
  295.     //dd ("StreamListener::onStartRequest: " + request + ", " + ctxt);
  296. }
  297.  
  298. StreamListener.prototype.onStopRequest =
  299. function sl_stopreq (request, ctxt, status)
  300. {
  301.     //dd ("StreamListener::onStopRequest: " + request + ", " + ctxt + ", " +
  302.     //status);
  303.     if (this._observer)
  304.         this._observer.onStreamClose(status);
  305. }
  306.  
  307. StreamListener.prototype.onDataAvailable =
  308. function sl_dataavail (request, ctxt, inStr, sourceOffset, count)
  309. {
  310.     ctxt = ctxt.wrappedJSObject;
  311.     if (!ctxt)
  312.     {
  313.         dd ("*** Can't get wrappedJSObject from ctxt in " +
  314.             "StreamListener.onDataAvailable ***");
  315.         return;
  316.     }
  317.     if (!ctxt._inputStream)
  318.         ctxt._inputStream = toScriptableInputStream (inStr);
  319.  
  320.     if (this._observer)
  321.         this._observer.onStreamDataAvailable(request, inStr, sourceOffset,
  322.                                              count);
  323. }
  324.