home *** CD-ROM | disk | FTP | other *** search
/ ftp.swcp.com / ftp.swcp.com.zip / ftp.swcp.com / mac / mozilla-mac-0.9.sea.hqx / mozilla-mac-0.9 / Chrome / chatzilla.jar / content / chatzilla / lib / js / connection-xpcom.js < prev    next >
Text File  |  2001-05-05  |  6KB  |  226 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, XPCOM, and the XPCOM component
  25.  *  component://misc/bs/connection
  26.  *
  27.  * sane wrapper around the insane bsIConnection component.  This
  28.  * component needs to be replaced, or at least fixed, so this wrapper
  29.  * will hopefully make it easy to do this in the future.
  30.  *
  31.  */
  32.  
  33. function toScriptableInputStream (i)
  34. {
  35.     var si = Components.classes["@mozilla.org/scriptableinputstream;1"];
  36.     
  37.     si = si.createInstance();
  38.     si = si.QueryInterface(Components.interfaces.nsIScriptableInputStream);
  39.     si.init(i);
  40.  
  41.     return si;
  42.     
  43. }
  44.  
  45. function CBSConnection ()
  46. {
  47.     var sockServiceClass =
  48.         Components.classesByID["{c07e81e0-ef12-11d2-92b6-00105a1b0d64}"];
  49.     
  50.     if (!sockServiceClass)
  51.         throw ("Couldn't get socket service class.");
  52.     
  53.     var sockService = sockServiceClass.getService();
  54.     if (!sockService)
  55.         throw ("Couldn't get socket service.");
  56.  
  57.     this._sockService = sockService.QueryInterface
  58.         (Components.interfaces.nsISocketTransportService);
  59.  
  60.     this.wrappedJSObject = this;
  61.  
  62. }
  63.  
  64. CBSConnection.prototype.connect = function(host, port, bind, tcp_flag)
  65. {
  66.     if (typeof tcp_flag == "undefined")
  67.         tcp_flag = false;
  68.     
  69.     this.host = host.toLowerCase();
  70.     this.port = port;
  71.     this.bind = bind;
  72.     this.tcp_flag = tcp_flag;
  73.  
  74.     this._channel = this._sockService.createTransport (host, port, null, -1,
  75.                                                        0, 0);
  76.     if (!this._channel)
  77.         throw ("Error opening channel.");
  78.  
  79.     this._outputStream = this._channel.openOutputStream(0, -1, 0);
  80.     if (!this._outputStream)
  81.         throw ("Error getting output stream.");
  82.  
  83.     this.connectDate = new Date();
  84.     this.isConnected = true;
  85.  
  86.     return this.isConnected;
  87.   
  88. }
  89.  
  90. CBSConnection.prototype.disconnect = function()
  91. {
  92.     
  93.     if (this.isConnected) {
  94.         this.isConnected = false;
  95.         this._inputStream.close();
  96.         /* .close() not implemented for output streams
  97.           this._outputStream.close();
  98.         */
  99.     }
  100.  
  101. }
  102.  
  103. CBSConnection.prototype.sendData = function(str)
  104. {
  105.     if (!this.isConnected)
  106.         throw "Not Connected.";
  107.  
  108.     var rv = false;
  109.     
  110.     try
  111.     {
  112.         this._outputStream.write(str, str.length);
  113.         rv = true;
  114.     }
  115.     catch (ex)
  116.     {
  117.         if (typeof ex != "undefined")
  118.         {
  119.             this.isConnected = false;
  120.             throw (ex);
  121.         }
  122.         else
  123.             rv = false;
  124.     }
  125.     
  126.     return rv;
  127. }
  128.  
  129. CBSConnection.prototype.readData = function(timeout)
  130. {
  131.     if (!this.isConnected)
  132.         throw "Not Connected.";
  133.  
  134.     if (!this._inputStream)
  135.     {
  136.         this._inputStream =
  137.             toScriptableInputStream(this._channel.openInputStream (0, -1, 0));
  138.         if (!this._inputStream)
  139.             throw ("Error getting input stream.");
  140.     }
  141.     
  142.     var rv, av;
  143.  
  144.     try
  145.     {
  146.         av = this._inputStream.available();
  147.         if (av)
  148.             rv = this._inputStream.read (av);
  149.         else
  150.             rv = "";
  151.     }
  152.     catch (ex)
  153.     {
  154.         dd ("*** Caught " + ex + " while reading.")
  155.         if (typeof ex != "undefined") {
  156.             this.isConnected = false;
  157.             throw (ex);
  158.         } else {
  159.             rv = "";
  160.         }
  161.     }
  162.     
  163.     return rv;
  164. }
  165.  
  166. if (jsenv.HAS_DOCUMENT)
  167. {
  168.     CBSConnection.prototype.startAsyncRead =
  169.     function (server)
  170.     {
  171.         this._channel.asyncRead (new StreamListener (server), this, 0, -1, 0);
  172.     }
  173. }
  174.  
  175. function StreamListener(server)
  176. {
  177.     this.server = server;
  178. }
  179.  
  180. StreamListener.prototype.onStartRequest =
  181. function (request, ctxt)
  182. {
  183.     dd ("onStartRequest: " + request + ", " + ctxt);
  184. }
  185.  
  186. StreamListener.prototype.onStopRequest =
  187. function (request, ctxt, status)
  188. {
  189.     ctxt = ctxt.wrappedJSObject;
  190.     if (!ctxt)
  191.     {
  192.         dd ("*** Can't get wrappedJSObject from ctxt in " +
  193.             "StreamListener.onDataAvailable ***");
  194.         return;
  195.     }
  196.  
  197.     ctxt.isConnected = false;
  198.  
  199.     dd ("onStopRequest: " + request + ", " + ctxt + ", " + status);
  200.  
  201.     var ev = new CEvent ("server", "disconnect", this.server,
  202.                          "onDisconnect");
  203.     this.server.parent.eventPump.addEvent (ev);
  204.  
  205. }
  206.  
  207. StreamListener.prototype.onDataAvailable =
  208. function (request, ctxt, inStr, sourceOffset, count)
  209. {
  210.     ctxt = ctxt.wrappedJSObject;
  211.     if (!ctxt)
  212.     {
  213.         dd ("*** Can't get wrappedJSObject from ctxt in " +
  214.             "StreamListener.onDataAvailable ***");
  215.         return;
  216.     }
  217.     
  218.     if (!ctxt._inputStream)
  219.         ctxt._inputStream = toScriptableInputStream (inStr);
  220.  
  221.     var ev = new CEvent ("server", "data-available", this.server,
  222.                          "onDataAvailable");
  223.     ev.line = ctxt.readData(0);
  224.     this.server.parent.eventPump.addEvent (ev);
  225. }
  226.