~ubuntu-branches/ubuntu/oneiric/libjboss-remoting-java/oneiric

« back to all changes in this revision

Viewing changes to src/main/org/jboss/remoting/samples/chat/client/RemoteStrategyRemoting.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: package-import@ubuntu.com-20110909140103-hqokx61534tas9rg
Tags: 2.5.3.SP1-1
* Newer but not newest upstream release. Do not build samples.
* Change debian/watch to upstream's svn repo.
* Add patch to fix compile error caused by tomcat update.
  (Closes: #628303)
* Switch to source format 3.0.
* Switch to debhelper level 7.
* Remove useless Depends.
* Update Standards-Version: 3.9.2.
* Update README.source.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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.
 
6
*
 
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.
 
11
*
 
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.
 
16
*
 
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.
 
21
*/
 
22
 
 
23
/*
 
24
 * Created on Feb 21, 2006
 
25
 */
 
26
package org.jboss.remoting.samples.chat.client;
 
27
 
 
28
import java.net.MalformedURLException;
 
29
import java.util.ArrayList;
 
30
 
 
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;
 
43
 
 
44
 
 
45
/**
 
46
 * A RemoteStrategyRemoting.
 
47
 
 
48
 * @author <a href="mailto:r.sigal@computer.org">Ron Sigal</a>
 
49
 * @version $Revision: 735 $
 
50
 * <p>
 
51
 * Copyright (c) 2005
 
52
 * </p>
 
53
 */
 
54
 
 
55
public class RemoteStrategyRemoting implements RemoteStrategy
 
56
{
 
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";
 
60
   
 
61
   private Client managerClient;
 
62
   private Connector receiverConnector;
 
63
   private boolean locallyShuttingDown = false;
 
64
   private boolean remotelyShuttingDown = false;
 
65
   
 
66
   
 
67
   public RemoteStrategyRemoting() throws Exception
 
68
   {
 
69
      String chatManagerUriString = Parameters.getParameter("chatManagerUri");
 
70
      
 
71
      if (chatManagerUriString == null)
 
72
         chatManagerUriString = chatManagerLocatorDefault;
 
73
 
 
74
      InvokerLocator chatManagerLocator = new InvokerLocator(chatManagerUriString);
 
75
      managerClient = new Client(chatManagerLocator);
 
76
      managerClient.connect();
 
77
   }
 
78
 
 
79
   
 
80
   public ArrayList list() throws RemoteConnectionException, ShuttingDownException
 
81
   {
 
82
      RemoteInvocation invocation = new RemoteInvocation("list", null);
 
83
      
 
84
      try
 
85
      {
 
86
         return (ArrayList) managerClient.invoke(invocation);
 
87
      }
 
88
      catch (Throwable t)
 
89
      {
 
90
         log.error(t);
 
91
         t.printStackTrace();
 
92
         throw new RemoteConnectionException();
 
93
      }
 
94
   }
 
95
 
 
96
 
 
97
   public ChatServer createChat(String description,
 
98
                                ChatMember owner,
 
99
                                TalkFrame talkFrame,
 
100
                                ReadWriteArrayList outgoingLines)
 
101
   throws NameInUseException, RemoteConnectionException, ShuttingDownException
 
102
   {
 
103
      Client serverClient = null;
 
104
      
 
105
      // Create new chat room and create Client for new chat room server.
 
106
      try
 
107
      {
 
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();
 
113
      }
 
114
      catch (Throwable e)
 
115
      {
 
116
         log.error(e);
 
117
         e.printStackTrace();
 
118
         throw new RemoteConnectionException();
 
119
      }
 
120
         
 
121
      // Create callback handler to process incoming messages.
 
122
      try
 
123
      {
 
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);
 
134
      }
 
135
      catch (MalformedURLException e)
 
136
      {
 
137
         log.error(e);
 
138
         e.printStackTrace();
 
139
         throw new RemoteConnectionException();
 
140
      }
 
141
      catch (Throwable e)
 
142
      {
 
143
         log.error(e);
 
144
         e.printStackTrace();
 
145
         throw new RemoteConnectionException();
 
146
      }
 
147
      
 
148
      // Create stub for new chat room server.
 
149
      ChatServer chatServer = new ChatServerStub(serverClient);
 
150
      
 
151
      // Create thread to send new outgoing messages to chat room server.
 
152
      SendThread sendThread = new SendThread(chatServer, outgoingLines);
 
153
      sendThread.start();
 
154
      
 
155
      return chatServer;
 
156
   }
 
157
   
 
158
   
 
159
   public ChatServer join(String key, ChatMember newMember, TalkFrame talkFrame, ReadWriteArrayList outgoingLines)
 
160
         throws NameInUseException, RemoteConnectionException, ShuttingDownException
 
161
   {
 
162
      Client serverClient = null;
 
163
      
 
164
      // Join chat room and create Client for chat room server.
 
165
      try
 
166
      {
 
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();
 
173
      }
 
174
      catch (Throwable e)
 
175
      {
 
176
         log.error(e);
 
177
         e.printStackTrace();
 
178
         throw new RemoteConnectionException();
 
179
      }
 
180
         
 
181
      // Create callback handler to process incoming messages.
 
182
      try
 
183
      {
 
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);
 
193
      }
 
194
      catch (MalformedURLException e)
 
195
      {
 
196
         log.error(e);
 
197
         e.printStackTrace();
 
198
         throw new RemoteConnectionException();
 
199
      }
 
200
      catch (Throwable e)
 
201
      {
 
202
         log.error(e);
 
203
         e.printStackTrace();
 
204
         throw new RemoteConnectionException();
 
205
      }
 
206
      
 
207
      
 
208
      // Create stub for new chat room server.
 
209
      ChatServer chatServer = new ChatServerStub(serverClient);
 
210
      
 
211
      // Create thread to send new outgoing messages to chat room server.
 
212
      SendThread sendThread = new SendThread(chatServer, outgoingLines);
 
213
      sendThread.start();
 
214
  
 
215
      return chatServer;
 
216
   }
 
217
 
 
218
 
 
219
   public void setShuttingDown()
 
220
   {
 
221
      locallyShuttingDown = true;
 
222
   }
 
223
   
 
224
   
 
225
   class SendThread extends Thread
 
226
   {
 
227
      private ChatServer chatServer;
 
228
      private ReadWriteArrayList outgoingLines;
 
229
      
 
230
      SendThread(ChatServer chatServer, ReadWriteArrayList outgoingLines)
 
231
      {
 
232
         this.chatServer = chatServer;
 
233
         this.outgoingLines = outgoingLines;
 
234
      }
 
235
      
 
236
      public void run() {
 
237
         while (!locallyShuttingDown && !remotelyShuttingDown)
 
238
         {
 
239
           try {
 
240
             chatServer.send( (ChatMessage) outgoingLines.firstElement());
 
241
             outgoingLines.remove(0);
 
242
           } catch (RemoteConnectionException re)
 
243
           {
 
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;
 
249
           }
 
250
         }
 
251
       }
 
252
     }
 
253
   
 
254
   
 
255
   class ChatServerStub implements ChatServer
 
256
   {
 
257
      private Client serverClient;
 
258
    
 
259
      public ChatServerStub(Client serverClient)
 
260
      {
 
261
         this.serverClient = serverClient;
 
262
      }
 
263
     
 
264
      public ArrayList getBackChat() throws RemoteConnectionException, ShuttingDownException
 
265
      {
 
266
         RemoteInvocation invocation = new RemoteInvocation("getBackChat", null);
 
267
         
 
268
         try
 
269
         {
 
270
            return (ArrayList) serverClient.invoke(invocation);
 
271
         }
 
272
         catch (Throwable e)
 
273
         {
 
274
            log.error(e);
 
275
            throw new RemoteConnectionException();
 
276
         }
 
277
      }
 
278
      
 
279
      public ChatInfo getChatInfo() throws RemoteConnectionException, ShuttingDownException
 
280
      {
 
281
         RemoteInvocation invocation = new RemoteInvocation("getChatInfo", null);
 
282
         
 
283
         try
 
284
         {
 
285
            return (ChatInfo) serverClient.invoke(invocation);
 
286
         }
 
287
         catch (Throwable e)
 
288
         {
 
289
            log.error(e);
 
290
            throw new RemoteConnectionException();
 
291
         }
 
292
      }
 
293
      
 
294
      public void leave(ChatMember member) throws RemoteConnectionException, ShuttingDownException
 
295
      {
 
296
         RemoteInvocation invocation = new RemoteInvocation("leave", new Object[] {member});
 
297
         
 
298
         try
 
299
         {
 
300
            serverClient.invoke(invocation);
 
301
         }
 
302
         catch (Throwable e)
 
303
         {
 
304
            log.error(e);
 
305
            throw new RemoteConnectionException();
 
306
         }
 
307
      }
 
308
      
 
309
      public void send(ChatMessage mesg) throws RemoteConnectionException, ShuttingDownException
 
310
      {
 
311
         RemoteInvocation invocation = new RemoteInvocation("send", new Object[] {mesg});
 
312
         
 
313
         try
 
314
         {
 
315
            serverClient.invoke(invocation);
 
316
         }
 
317
         catch (Throwable e)
 
318
         {
 
319
            log.error(e);
 
320
            throw new RemoteConnectionException();
 
321
         }
 
322
      }
 
323
   }
 
324
}
 
 
b'\\ No newline at end of file'