1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is Mozilla JavaScript Shell project.
17 * The Initial Developer of the Original Code is
19 * Portions created by the Initial Developer are Copyright (C) 2003
20 * the Initial Developer. All Rights Reserved.
23 * Alex Fritze <alex@croczilla.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "nsJSShServer.h"
41 #include "nsISocketTransport.h"
42 #include "nsIComponentManager.h"
43 #include "nsIInputStream.h"
44 #include "nsIOutputStream.h"
45 #include "nsThreadUtils.h"
47 #include "nsComponentManagerUtils.h"
49 //**********************************************************************
50 // ConnectionListener helper class
52 class ConnectionListener
: public nsIServerSocketListener
55 ConnectionListener(const nsACString
&startupURI
);
56 ~ConnectionListener();
58 NS_DECL_NSISERVERSOCKETLISTENER
61 nsCString mStartupURI
;
64 //----------------------------------------------------------------------
67 ConnectionListener::ConnectionListener(const nsACString
&startupURI
)
68 : mStartupURI(startupURI
)
72 ConnectionListener::~ConnectionListener()
77 already_AddRefed
<nsIServerSocketListener
> CreateConnectionListener(const nsACString
&startupURI
)
79 nsIServerSocketListener
* obj
= new ConnectionListener(startupURI
);
84 //----------------------------------------------------------------------
85 // nsISupports methods:
87 NS_IMPL_THREADSAFE_ADDREF(ConnectionListener
)
88 NS_IMPL_THREADSAFE_RELEASE(ConnectionListener
)
90 NS_INTERFACE_MAP_BEGIN(ConnectionListener
)
91 NS_INTERFACE_MAP_ENTRY(nsISupports
)
92 NS_INTERFACE_MAP_ENTRY(nsIServerSocketListener
)
95 //----------------------------------------------------------------------
96 // nsIServerSocketListener methods:
98 /* void onSocketAccepted (in nsIServerSocket aServ, in nsISocketTransport aTransport); */
99 NS_IMETHODIMP
ConnectionListener::OnSocketAccepted(nsIServerSocket
*aServ
, nsISocketTransport
*aTransport
)
101 nsCOMPtr
<nsIInputStream
> input
;
102 nsCOMPtr
<nsIOutputStream
> output
;
103 aTransport
->OpenInputStream(nsITransport::OPEN_BLOCKING
, 0, 0, getter_AddRefs(input
));
104 aTransport
->OpenOutputStream(nsITransport::OPEN_BLOCKING
, 0, 0, getter_AddRefs(output
));
106 LOG(("JSSh server: new connection!\n"));
108 nsCOMPtr
<nsIRunnable
> shell
= CreateJSSh(input
, output
, mStartupURI
);
110 nsCOMPtr
<nsIThread
> thread
;
111 NS_NewThread(getter_AddRefs(thread
), shell
);
115 /* void onStopListening (in nsIServerSocket aServ, in nsresult aStatus); */
116 NS_IMETHODIMP
ConnectionListener::OnStopListening(nsIServerSocket
*aServ
, nsresult aStatus
)
118 LOG(("JSSh server: stopped listening!\n"));
122 //**********************************************************************
123 // nsJSShServer implementation
125 nsJSShServer::nsJSShServer()
127 LOG(("nsJSShServer ctor\n"));
130 nsJSShServer::~nsJSShServer()
132 LOG(("nsJSShServer dtor\n"));
133 // XXX should we stop listening or not??
137 //----------------------------------------------------------------------
138 // nsISupports methods:
140 NS_IMPL_ADDREF(nsJSShServer
)
141 NS_IMPL_RELEASE(nsJSShServer
)
143 NS_INTERFACE_MAP_BEGIN(nsJSShServer
)
144 NS_INTERFACE_MAP_ENTRY(nsISupports
)
145 NS_INTERFACE_MAP_ENTRY(nsIJSShServer
)
148 //----------------------------------------------------------------------
149 // nsIJSShServer methods:
151 /* void startServerSocket (in unsigned long port, in AUTF8String startupURI,
152 in boolean loopbackOnly); */
154 nsJSShServer::StartServerSocket(PRUint32 port
, const nsACString
& startupURI
,
158 NS_ERROR("server socket already running");
159 return NS_ERROR_FAILURE
;
162 mServerSocket
= do_CreateInstance(NS_SERVERSOCKET_CONTRACTID
);
163 if (!mServerSocket
) {
164 NS_ERROR("could not create server socket");
165 return NS_ERROR_FAILURE
;
168 if (NS_FAILED(mServerSocket
->Init(port
, loopbackOnly
, -1))) {
169 mServerSocket
= nsnull
;
170 NS_ERROR("server socket initialization error");
171 return NS_ERROR_FAILURE
;
174 nsCOMPtr
<nsIServerSocketListener
> listener
= CreateConnectionListener(startupURI
);
175 mServerSocket
->AsyncListen(listener
);
178 mServerStartupURI
= startupURI
;
179 mServerLoopbackOnly
= loopbackOnly
;
184 /* void stopServerSocket (); */
185 NS_IMETHODIMP
nsJSShServer::StopServerSocket()
188 mServerSocket
->Close();
189 mServerSocket
= nsnull
;
191 mServerStartupURI
.Truncate();
192 mServerLoopbackOnly
= PR_FALSE
;
196 /* void runShell (in nsIInputStream input, in nsIOutputStream output,
197 in string startupURI, in boolean blocking); */
199 nsJSShServer::RunShell(nsIInputStream
*input
, nsIOutputStream
*output
,
200 const char *startupURI
, PRBool blocking
)
202 nsCOMPtr
<nsIRunnable
> shell
= CreateJSSh(input
, output
, nsCString(startupURI
));
204 nsCOMPtr
<nsIThread
> thread
;
205 NS_NewThread(getter_AddRefs(thread
), shell
);
213 /* readonly attribute boolean serverListening; */
215 nsJSShServer::GetServerListening(PRBool
*aServerListening
)
218 *aServerListening
= PR_TRUE
;
220 *aServerListening
= PR_FALSE
;
225 /* readonly attribute unsigned long serverPort; */
227 nsJSShServer::GetServerPort(PRUint32
*aServerPort
)
229 *aServerPort
= mServerPort
;
233 /* readonly attribute AUTF8String serverStartupURI; */
235 nsJSShServer::GetServerStartupURI(nsACString
& aServerStartupURI
)
237 aServerStartupURI
= mServerStartupURI
;
241 /* readonly attribute boolean serverLoopbackOnly; */
243 nsJSShServer::GetServerLoopbackOnly(PRBool
*aServerLoopbackOnly
)
245 *aServerLoopbackOnly
= mServerLoopbackOnly
;