2
* JBoss, Home of Professional Open Source
3
* Copyright 2005, JBoss Inc., and individual contributors as indicated
4
* by the @authors tag. See the copyright.txt in the distribution for a
5
* full listing of individual contributors.
7
* This is free software; you can redistribute it and/or modify it
8
* under the terms of the GNU Lesser General Public License as
9
* published by the Free Software Foundation; either version 2.1 of
10
* the License, or (at your option) any later version.
12
* This software is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this software; if not, write to the Free
19
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
24
* Created on Feb 21, 2006
26
package org.jboss.remoting.samples.chat.client;
28
import java.net.MalformedURLException;
29
import java.util.ArrayList;
31
import org.jboss.logging.Logger;
32
import org.jboss.remoting.Client;
33
import org.jboss.remoting.InvokerLocator;
34
import org.jboss.remoting.callback.InvokerCallbackHandler;
35
import org.jboss.remoting.invocation.RemoteInvocation;
36
import org.jboss.remoting.samples.chat.exceptions.NameInUseException;
37
import org.jboss.remoting.samples.chat.exceptions.RemoteConnectionException;
38
import org.jboss.remoting.samples.chat.exceptions.ShuttingDownException;
39
import org.jboss.remoting.samples.chat.server.ChatServer;
40
import org.jboss.remoting.samples.chat.utility.Parameters;
41
import org.jboss.remoting.samples.chat.utility.ReadWriteArrayList;
42
import org.jboss.remoting.transport.Connector;
46
* A RemoteStrategyRemoting.
48
* @author <a href="mailto:r.sigal@computer.org">Ron Sigal</a>
49
* @version $Revision: 735 $
55
public class RemoteStrategyRemoting implements RemoteStrategy
57
protected static final Logger log = Logger.getLogger(RemoteStrategyRemoting.class);
58
private static String chatClientLocatorDefault = "socket://localhost:0";
59
private static String chatManagerLocatorDefault = "socket://localhost:1969";
61
private Client managerClient;
62
private Connector receiverConnector;
63
private boolean locallyShuttingDown = false;
64
private boolean remotelyShuttingDown = false;
67
public RemoteStrategyRemoting() throws Exception
69
String chatManagerUriString = Parameters.getParameter("chatManagerUri");
71
if (chatManagerUriString == null)
72
chatManagerUriString = chatManagerLocatorDefault;
74
InvokerLocator chatManagerLocator = new InvokerLocator(chatManagerUriString);
75
managerClient = new Client(chatManagerLocator);
76
managerClient.connect();
80
public ArrayList list() throws RemoteConnectionException, ShuttingDownException
82
RemoteInvocation invocation = new RemoteInvocation("list", null);
86
return (ArrayList) managerClient.invoke(invocation);
92
throw new RemoteConnectionException();
97
public ChatServer createChat(String description,
100
ReadWriteArrayList outgoingLines)
101
throws NameInUseException, RemoteConnectionException, ShuttingDownException
103
Client serverClient = null;
105
// Create new chat room and create Client for new chat room server.
108
Object[] args = new Object[] {description, owner};
109
RemoteInvocation invocation = new RemoteInvocation("createChat", args);
110
InvokerLocator serverLocator = (InvokerLocator) managerClient.invoke(invocation);
111
serverClient = new Client(serverLocator);
112
serverClient.connect();
118
throw new RemoteConnectionException();
121
// Create callback handler to process incoming messages.
124
receiverConnector = new Connector();
125
String receiverLocatorString = Parameters.getParameter("clientUriString", chatClientLocatorDefault);
126
InvokerLocator receiverLocator = new InvokerLocator(receiverLocatorString);
127
receiverConnector.setInvokerLocator(receiverLocator.getLocatorURI());
128
log.info(receiverLocator.getLocatorURI());
129
receiverConnector.start();
130
receiverLocator = receiverConnector.getLocator();
131
log.info(receiverConnector.getInvokerLocator());
132
InvokerCallbackHandler receiverHandler = new ChatReceiverHandler(talkFrame);
133
serverClient.addListener(receiverHandler, receiverLocator, owner);
135
catch (MalformedURLException e)
139
throw new RemoteConnectionException();
145
throw new RemoteConnectionException();
148
// Create stub for new chat room server.
149
ChatServer chatServer = new ChatServerStub(serverClient);
151
// Create thread to send new outgoing messages to chat room server.
152
SendThread sendThread = new SendThread(chatServer, outgoingLines);
159
public ChatServer join(String key, ChatMember newMember, TalkFrame talkFrame, ReadWriteArrayList outgoingLines)
160
throws NameInUseException, RemoteConnectionException, ShuttingDownException
162
Client serverClient = null;
164
// Join chat room and create Client for chat room server.
167
Object[] args = new Object[] {key, newMember};
168
RemoteInvocation invocation = new RemoteInvocation("join", args);
169
managerClient.invoke(invocation);
170
InvokerLocator serverLocator = new InvokerLocator(key);
171
serverClient = new Client(serverLocator);
172
serverClient.connect();
178
throw new RemoteConnectionException();
181
// Create callback handler to process incoming messages.
184
receiverConnector = new Connector();
185
String receiverLocatorString = Parameters.getParameter("clientUriString", chatClientLocatorDefault);
186
InvokerLocator receiverLocator = new InvokerLocator(receiverLocatorString);
187
receiverConnector.setInvokerLocator(receiverLocator.getLocatorURI());
188
log.info(receiverLocator.getLocatorURI());
189
receiverConnector.start();
190
receiverLocator = receiverConnector.getLocator();
191
InvokerCallbackHandler receiverHandler = new ChatReceiverHandler(talkFrame);
192
serverClient.addListener(receiverHandler, receiverLocator, newMember);
194
catch (MalformedURLException e)
198
throw new RemoteConnectionException();
204
throw new RemoteConnectionException();
208
// Create stub for new chat room server.
209
ChatServer chatServer = new ChatServerStub(serverClient);
211
// Create thread to send new outgoing messages to chat room server.
212
SendThread sendThread = new SendThread(chatServer, outgoingLines);
219
public void setShuttingDown()
221
locallyShuttingDown = true;
225
class SendThread extends Thread
227
private ChatServer chatServer;
228
private ReadWriteArrayList outgoingLines;
230
SendThread(ChatServer chatServer, ReadWriteArrayList outgoingLines)
232
this.chatServer = chatServer;
233
this.outgoingLines = outgoingLines;
237
while (!locallyShuttingDown && !remotelyShuttingDown)
240
chatServer.send( (ChatMessage) outgoingLines.firstElement());
241
outgoingLines.remove(0);
242
} catch (RemoteConnectionException re)
244
System.out.println("RemoteStrategyImpl.createChat(): unable to send next line:");
245
// System.out.println(" " + (String) outgoingLines.firstElement());
246
} catch (ShuttingDownException sde) {
247
System.out.println("RemoteStrategyImpl.createChat(): ChatServer is shutting down");
248
remotelyShuttingDown = true;
255
class ChatServerStub implements ChatServer
257
private Client serverClient;
259
public ChatServerStub(Client serverClient)
261
this.serverClient = serverClient;
264
public ArrayList getBackChat() throws RemoteConnectionException, ShuttingDownException
266
RemoteInvocation invocation = new RemoteInvocation("getBackChat", null);
270
return (ArrayList) serverClient.invoke(invocation);
275
throw new RemoteConnectionException();
279
public ChatInfo getChatInfo() throws RemoteConnectionException, ShuttingDownException
281
RemoteInvocation invocation = new RemoteInvocation("getChatInfo", null);
285
return (ChatInfo) serverClient.invoke(invocation);
290
throw new RemoteConnectionException();
294
public void leave(ChatMember member) throws RemoteConnectionException, ShuttingDownException
296
RemoteInvocation invocation = new RemoteInvocation("leave", new Object[] {member});
300
serverClient.invoke(invocation);
305
throw new RemoteConnectionException();
309
public void send(ChatMessage mesg) throws RemoteConnectionException, ShuttingDownException
311
RemoteInvocation invocation = new RemoteInvocation("send", new Object[] {mesg});
315
serverClient.invoke(invocation);
320
throw new RemoteConnectionException();
b'\\ No newline at end of file'