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

« back to all changes in this revision

Viewing changes to src/main/org/jboss/remoting/samples/chat/client/LocalStrategy.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
package org.jboss.remoting.samples.chat.client;
 
2
 
 
3
import java.awt.Component;
 
4
import java.util.ArrayList;
 
5
 
 
6
import javax.swing.SwingUtilities;
 
7
 
 
8
import org.jboss.logging.Logger;
 
9
import org.jboss.remoting.samples.chat.exceptions.ConnectionException;
 
10
import org.jboss.remoting.samples.chat.exceptions.CreateConnectionException;
 
11
import org.jboss.remoting.samples.chat.exceptions.JoinConnectionException;
 
12
import org.jboss.remoting.samples.chat.exceptions.ListConnectionException;
 
13
import org.jboss.remoting.samples.chat.exceptions.NameInUseException;
 
14
import org.jboss.remoting.samples.chat.exceptions.RemoteConnectionException;
 
15
import org.jboss.remoting.samples.chat.exceptions.ShuttingDownException;
 
16
import org.jboss.remoting.samples.chat.exceptions.TalkConnectionException;
 
17
import org.jboss.remoting.samples.chat.server.ChatServer;
 
18
import org.jboss.remoting.samples.chat.utility.ReadWriteArrayList;
 
19
 
 
20
class Wrapper
 
21
{
 
22
   private Object obj;
 
23
 
 
24
   public Wrapper()
 
25
   {
 
26
   }
 
27
 
 
28
   public Wrapper(Object o)
 
29
   {
 
30
      obj = o;
 
31
   }
 
32
 
 
33
   public void set(Object o)
 
34
   {
 
35
      obj = o;
 
36
   }
 
37
 
 
38
   public Object get()
 
39
   {
 
40
      return obj;
 
41
   }
 
42
}
 
43
 
 
44
// ****************************************************************
 
45
 
 
46
public class LocalStrategy
 
47
implements ConnectionStrategy, CreateConnectionStrategy, InfoConnectionStrategy, JoinConnectionStrategy, ListConnectionStrategy
 
48
{
 
49
   protected static final Logger log = Logger.getLogger(LocalStrategy.class);
 
50
   
 
51
   private RemoteStrategy remoteStrategy;
 
52
   private CloseableFrame parent;
 
53
   private CreateFrame createFrame;
 
54
   private InfoFrame infoFrame;
 
55
   private JoinFrame joinFrame;
 
56
   private ListFrame listFrame;
 
57
   private TalkFrame talkFrame;
 
58
   private Thread readThread;
 
59
   private boolean isChatting;
 
60
   private ShutDownDialog shuttingDownDialog = new ShutDownDialog();
 
61
   
 
62
   
 
63
   public LocalStrategy(CloseableFrame parent, RemoteStrategy remoteStrategy)
 
64
   {
 
65
      this.parent = parent;
 
66
      this.remoteStrategy = remoteStrategy;
 
67
   }
 
68
 
 
69
   
 
70
   // ****************************************************************
 
71
   // ConnectionStrategy
 
72
   public void list()
 
73
   {
 
74
      final Wrapper chatRoomDescriptions = new Wrapper();
 
75
 
 
76
      Thread t = new Thread()
 
77
      {
 
78
         public void run()
 
79
         {
 
80
            try
 
81
            {
 
82
               chatRoomDescriptions.set(remoteStrategy.list());
 
83
            }
 
84
            catch (ShuttingDownException e)
 
85
            {
 
86
               SwingUtilities.invokeLater(shuttingDownDialogRunnable);
 
87
            }
 
88
            catch (RemoteConnectionException e)
 
89
            {
 
90
               log.error(e);
 
91
            }
 
92
 
 
93
            Runnable r = new Runnable()
 
94
            {
 
95
               public void run()
 
96
               {
 
97
                  new ListFrame(LocalStrategy.this, (ArrayList) chatRoomDescriptions.get(), parent).show();
 
98
               }
 
99
            };
 
100
 
 
101
            SwingUtilities.invokeLater(r);
 
102
         }
 
103
      };
 
104
 
 
105
      t.start();
 
106
   }
 
107
 
 
108
   
 
109
   public void create() throws ConnectionException
 
110
   {
 
111
      createFrame = new CreateFrame(this, parent);
 
112
      createFrame.show();
 
113
   }
 
114
 
 
115
   
 
116
   // ****************************************************************
 
117
   // CreateConnectionStrategy
 
118
   public void createChat(final String description, final ChatMember owner) throws CreateConnectionException
 
119
   {
 
120
      Thread t = new Thread()
 
121
      {
 
122
         TalkFrame talkFrame = new TalkFrame(description, owner.get_name(), parent);
 
123
         ReadWriteArrayList outgoingLines = new ReadWriteArrayList();
 
124
         ChatServer chatServer = null;
 
125
         
 
126
         public void run()
 
127
         {
 
128
            try
 
129
            {
 
130
               chatServer = remoteStrategy.createChat(description, owner, talkFrame, outgoingLines);
 
131
            }
 
132
            catch (ShuttingDownException sde)
 
133
            {
 
134
               SwingUtilities.invokeLater(shuttingDownDialogRunnable);
 
135
            }
 
136
            catch (RemoteConnectionException e)
 
137
            {
 
138
               log.error("Cannot create chat room: " + description);
 
139
               log.error(e);
 
140
            }
 
141
            catch (NameInUseException e)
 
142
            {
 
143
               System.out.println("Pick a new name");
 
144
            }
 
145
 
 
146
            Runnable r = new Runnable()
 
147
            {
 
148
               public void run()
 
149
               {
 
150
                  talkFrame.registerStrategy(new TalkConnectionStrategyImpl(owner, chatServer, outgoingLines));
 
151
                  talkFrame.show();
 
152
               }
 
153
            };
 
154
 
 
155
            SwingUtilities.invokeLater(r);
 
156
         }
 
157
      };
 
158
 
 
159
      t.start();
 
160
   }
 
161
 
 
162
   
 
163
   // ****************************************************************
 
164
   // JoinConnectionStrategy
 
165
   public void join(final ChatInfo chatInfo, final ChatMember newMember) throws JoinConnectionException
 
166
   {
 
167
      Thread t = new Thread()
 
168
      {
 
169
         TalkFrame talkFrame = new TalkFrame(chatInfo.get_description(), newMember.get_name(), parent);
 
170
         ReadWriteArrayList outgoingLines = new ReadWriteArrayList();
 
171
         Wrapper remoteChatServerWrapper = new Wrapper();
 
172
         ChatServer chatServer = null;
 
173
         
 
174
         public void run()
 
175
         {
 
176
            try
 
177
            {
 
178
               chatServer = remoteStrategy.join(chatInfo.get_key(), newMember, talkFrame, outgoingLines);
 
179
            }
 
180
            catch (ShuttingDownException sde)
 
181
            {
 
182
               SwingUtilities.invokeLater(shuttingDownDialogRunnable);
 
183
            }
 
184
            catch (RemoteConnectionException e)
 
185
            {
 
186
               log.error("Cannot join chat room: " + chatInfo.get_description());
 
187
               log.error(e);
 
188
            }
 
189
            catch (NameInUseException niue)
 
190
            {
 
191
               System.out.println("Pick a new name");
 
192
            }
 
193
 
 
194
            Runnable r = new Runnable()
 
195
            {
 
196
               public void run()
 
197
               {
 
198
                  talkFrame.registerStrategy(new TalkConnectionStrategyImpl(newMember, chatServer, outgoingLines));
 
199
                  talkFrame.registerChatKey(chatInfo.get_key());
 
200
                  talkFrame.show();
 
201
               }
 
202
            };
 
203
 
 
204
            SwingUtilities.invokeLater(r);
 
205
         }
 
206
      };
 
207
 
 
208
      t.start();
 
209
   }
 
210
 
 
211
   
 
212
   // ****************************************************************
 
213
   // InfoConnectionStrategy + ListConnectionStrategy
 
214
   public void getId(ChatInfo chatInfo)
 
215
   {
 
216
      joinFrame = new JoinFrame(chatInfo, this, parent);
 
217
      joinFrame.show();
 
218
   }
 
219
 
 
220
   public void getInfo(ArrayList chatInfoList, int key) throws ListConnectionException
 
221
   {
 
222
      infoFrame = new InfoFrame(this, (ChatInfo) chatInfoList.get(key), parent);
 
223
      infoFrame.show();
 
224
   }
 
225
 
 
226
   
 
227
   // ****************************************************************
 
228
   // TalkConnectionStrategy
 
229
   class TalkConnectionStrategyImpl implements TalkConnectionStrategy
 
230
   {
 
231
      private ChatServer chatServer;
 
232
      private ChatMember member;
 
233
      private ReadWriteArrayList outgoingLines;
 
234
 
 
235
      public TalkConnectionStrategyImpl(ChatMember member, ChatServer cs, ReadWriteArrayList outgoingLines)
 
236
      {
 
237
         chatServer = cs;
 
238
         this.member = member;
 
239
         this.outgoingLines = outgoingLines;
 
240
      }
 
241
 
 
242
      // getBackChat() is not currently used, since the ChatReceiverThread created by ChatManager_Impl
 
243
      // sends all existing lines in the chat room, starting with line 0.
 
244
      //      public void getBackChat(final TalkFrame talkFrame)
 
245
      //      {
 
246
      //        Thread t = new Thread() {
 
247
      //          public void run() {
 
248
      //            try {
 
249
      //              String[] backChat =  remoteChatServer.getBackChat();
 
250
      //              if (backChat == null)
 
251
      //                backChat = new String[0];
 
252
      //              talkFrame.setBackChat(backChat);
 
253
      //            }
 
254
      //            catch (ShuttingDownException sde) {
 
255
      //              SwingUtilities.invokeLater(shuttingDownDialogRunnable);
 
256
      //            }
 
257
      //            catch (RemoteConnectionException rce) {
 
258
      //              System.out.println("TalkConnectionStrategy: unable to get back chat");
 
259
      //              System.out.println(rce);
 
260
      //            }
 
261
      //          }
 
262
      //        };
 
263
      //
 
264
      //        t.start();
 
265
      //      }
 
266
 
 
267
      public void send(ChatMessage message) throws TalkConnectionException
 
268
      {
 
269
         outgoingLines.add(message);
 
270
      }
 
271
 
 
272
      public void leave()
 
273
      {
 
274
         Thread t = new Thread()
 
275
         {
 
276
            public void run()
 
277
            {
 
278
               try
 
279
               {
 
280
                  chatServer.leave(member);
 
281
               }
 
282
               catch (ShuttingDownException sde)
 
283
               {
 
284
                  SwingUtilities.invokeLater(shuttingDownDialogRunnable);
 
285
               }
 
286
               catch (RemoteConnectionException e)
 
287
               {
 
288
                  log.error("TalkConnectionStrategy.leave(): unable to leave chat room");
 
289
                  log.error(e);
 
290
               }
 
291
 
 
292
               chatServer = null;
 
293
            }
 
294
         };
 
295
 
 
296
         t.start();
 
297
      }
 
298
   }
 
299
 
 
300
   // ****************************************************************
 
301
   public void notifyOnClose(Component c)
 
302
   {
 
303
      c.setVisible(false);
 
304
   }
 
305
 
 
306
   protected boolean isChatting()
 
307
   {
 
308
      return isChatting;
 
309
   }
 
310
 
 
311
   protected void setIsChatting(boolean b)
 
312
   {
 
313
      isChatting = b;
 
314
   }
 
315
 
 
316
   // ****************************************************************
 
317
 
 
318
   private Runnable shuttingDownDialogRunnable = new Runnable()
 
319
   {
 
320
      public void run()
 
321
      {
 
322
         shuttingDownDialog.show();
 
323
      }
 
324
   };
 
325
}
 
 
b'\\ No newline at end of file'