KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > datashare > client > BroadCastClient


1 /* ----- BEGIN LICENSE BLOCK -----
2  * Version: MPL 1.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is the DataShareServer.
15  *
16  * The Initial Developer of the Original Code is
17  * Ball Aerospace & Technologies Corp, Fairborn, Ohio
18  * Portions created by the Initial Developer are Copyright (C) 2001
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s): Charles Wood <cwood@ball.com>
22  *
23  * ----- END LICENSE BLOCK ----- */

24 /* RCS $Id: BroadCastClient.java,v 1.2 2002/01/20 23:30:24 lizellaman Exp $
25  * $Log: BroadCastClient.java,v $
26  * Revision 1.2 2002/01/20 23:30:24 lizellaman
27  * javadoc updates
28  *
29  * Revision 1.1 2002/01/03 03:21:36 lizellaman
30  * existing file, moved to client package
31  *
32  *
33  */

34 package org.datashare.client;
35
36 import org.datashare.objects.DataShareObject;
37 import org.datashare.objects.UpdateAvailableMsg;
38 import org.datashare.objects.ChannelDescription;
39 import org.datashare.client.DataShareClient;
40 import org.datashare.client.DataShareClientInterface;
41 import org.datashare.client.DataShareConnection;
42 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
43 import java.net.InetAddress JavaDoc;
44
45 /**
46  * This class will provide the methods necessary to establish command/status and
47  * data connections over a network to a DataShareServer, and provide the input and
48  * output methods needed to share data with other BroadCast clients.
49  * BroadCastClients do not care to know about other clients that they are sending data to
50  * (they have no way to find out about other clients, if you need to know about other clients,
51  * use the more general DataShareClient class). BroadCastClients always have their data channels
52  * in the session named 'BroadCastClient'. BroadCastClient always uses a TCP connection for it's data.
53  *
54  * @version 1.1
55  */

56 public class BroadCastClient implements DataShareClientInterface
57    {
58    /**
59     * if true, will cause the newDataReceived method of the
60     * BroadCastClientInterface to be executed asynchronously on the AWT event
61     * dispatching thread. This will happen after all pending AWT events have
62     * been processed. Should be set to true when received data (via the
63     * newDataReceived method of the BroadCastClientInterface) will be used to
64     * update the GUI. This is implemented via a SwingUtilities.invokeLater()
65     * call. Additional documentation and examples for this method can be found
66     * at <a HREF="http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html#invokeLater">How to Use Threads</a>, in The Java Tutorial.
67     */

68    private boolean useThreads = false;
69
70    /**
71     * for reference only, will be set by the corresponding value in the call to initialize.
72     */

73    private String JavaDoc clientName;
74
75    /**
76     * for reference only, will be set by the corresponding value in the call to initialize, after it has been made unique by the DataShareServer: will be how this client is known on the DataShareServer. No two clients can have the same name, but all can ask for the same name via the clientName parameter in the initialize call.
77     */

78    private String JavaDoc clientUniqueName;
79
80    // these have to do with the connection to the server's command status channel
81

82    /**
83     * for reference only, will be set by the corresponding value in the call to initialize.
84     */

85    private InetAddress JavaDoc serverInetAddress;
86
87    /**
88     * for reference only, will be set by the corresponding value in the call to initialize.
89     */

90    private int serverCommandStatusPort;
91
92    /**
93     * for reference only, will be set by the corresponding value in the call to initialize.
94     */

95    private String JavaDoc channelName;
96
97    /**
98     * will be set by the corresponding value in the call to initialize, should be the set to the class that is
99     * using/supplying our data (note that the class must implement BroadCastClientInterface).
100     */

101    private BroadCastInterface bci;
102
103    /**
104     * provides most of the functionality of connecting to the DataShareServer
105     */

106    private DataShareClient myClient = new DataShareClient();
107
108    /**
109     * Constructor, note that initialize must be called for this class to do anything useful.
110     */

111    public BroadCastClient()
112       {}
113
114    /**
115     * causes connections to DataShareServer to be established, must be called
116     * before any data
117     * can be shared.
118     *
119     * @param serverInetAddress must be set to the IP address, or name, of the
120     * DataShareServer machine
121     * @param serverCommandStatusPort must be set to the port that the
122     * DataShareServer is using for command/status connections
123     * @param clientName the name that this client would like to be know
124     * by, will be made unique by the DataShareServer and set into the
125     * clientUniqueName field
126     * @param bci must be set to the class that implements the
127     * BroadCastInterface, used to route any received data and to notify
128     * in case of network problems
129     * @param channelName the name of the channel that this client should
130     * use for data. Only clients in the same channel will be able to
131     * share data.
132     */

133    public void
134    initialize(InetAddress JavaDoc serverInetAddress,
135               int serverCommandStatusPort,
136               String JavaDoc clientName,
137               BroadCastInterface bci,
138               String JavaDoc channelName)
139       {
140       myClient.initialize(this,
141                           serverInetAddress,
142                           serverCommandStatusPort,
143                           "BroadCastClient",
144                           "used by all BroadCastClients",
145                           clientName,
146                           "",
147                           "",
148                           "",
149                           channelName,
150                           "shared by all clients for this workflow",
151                           ChannelDescription.TCP);
152       this.serverInetAddress = serverInetAddress;
153       this.serverCommandStatusPort = serverCommandStatusPort;
154       // this.clientName = myClient.clientUniqueName; must wait until data channel ready so we know name is unique
155
this.channelName = channelName;
156       this.bci = bci;
157       }
158
159    /**
160     * call this when you want the command/status and data connections to be closed
161     * gracefully, like when preparing to exit.
162     */

163    public void
164    shutDown()
165       {
166       myClient.shutDown();
167       }
168
169    /**
170     * Use this method to send an object to everybody in a Channel (including our instance)
171     *
172     * @param object the data to share, probably should be serializable
173     */

174    public void
175    sendDataToAll(Object JavaDoc object)
176       {
177       myClient.sendDataToAll(object);
178       }
179
180    /**
181     * Use this method to send an object to everybody in a Channel (excluding our instance)
182     *
183     * @param object the data to share, probably should be serializable
184     */

185    public void
186    sendDataToOthers(Object JavaDoc object)
187       {
188       myClient.sendDataToOthers(object);
189       }
190
191    /**
192     * This method should not be overidden and is called automatically when data
193     * is received. This method can ensure that any objects we receive
194     * are sent
195     * to the BroadCastClientInterface.newDataReceived method in a thread safe manner.
196     * dataShareObjects that may cause a GUI update should be made thread safe by
197     * setting the 'useThreads' field to true.
198     *
199     * @param dataShareObject the object received from the data channel
200     */

201    public void
202    dataReceived(DataShareObject dataShareObject)
203       {
204       if(useThreads)
205          {
206          final DataShareObject thisObject = dataShareObject;
207          final Runnable JavaDoc sendTheObject = new Runnable JavaDoc()
208             {
209             public void run()
210                {
211                bci.newDataReceived(thisObject);
212                }
213             };
214          javax.swing.SwingUtilities.invokeLater(sendTheObject);
215          }
216       else
217          {
218          bci.newDataReceived(dataShareObject);
219          }
220       }
221
222    /**
223     * do not call this method. It is called automatically when the data connection is setup and available
224     * for use. The
225     * data channel must not be used before this gets called. The
226     * BroadCastInterface will be
227     * notified via by calling its dataChannelIsReady method call.
228     *
229     * @param success true if the data channel is ready for use, false if we
230     * failed to get it established.
231     */

232    public void
233    dataChannelIsReady(boolean success)
234       {
235       clientUniqueName = myClient.clientUniqueName; // now we know our unique name has been determined
236
bci.dataChannelIsReady(success);
237       }
238
239    /**
240     * do not call this method. It is called automatically when the command/status connection has an error.
241     * The BroadCastInterface will be notified via its commandStatusConnectionLost() method call.
242     *
243     * @param fatalError set to true for non-recoverable errors (none are
244     * recoverable at this time)
245     * @param errorMsg describes the nature of the error
246     */

247    public void
248    commandStatusConnectionError(boolean fatalError, String JavaDoc errorMsg)
249       {
250       System.out.println("BroadCastClient lost commandStatusConnection: " + errorMsg);
251       if(fatalError)
252          bci.commandStatusConnectionLost();
253       }
254
255    /**
256     * do not call this method. It is called automatically when the data connection has been lost.
257     * The BroadCastInterface will be notified via its dataConnectionLost() method call.
258     *
259     * @param dsc describes the connection that we lost
260     */

261    public void
262    connectionLost(DataShareConnection dsc)
263       {
264       System.out.println("BroadCastClient lost data connection for channel: " + dsc.dscd.channelDescription.channelName);
265       bci.dataConnectionLost();
266       }
267
268    /**
269     * called when a change has occured with the session/client tree, not used. (required by the interface we implement)
270     *
271     * @param msg describes the update (if we has ever asked for them, which we
272     * don't)
273     */

274    public void
275    updateReceived(UpdateAvailableMsg msg)
276       {}
277
278    /**
279     * called when a new tree has been received, not used. (required by the interface we implement)
280     *
281     * @param newTreeNode describes the tree (if we has ever asked for them,
282     * which we don't)
283     */

284    public void
285    treeReceived(DefaultMutableTreeNode JavaDoc newTreeNode)
286       {}
287
288
289    }
290
Popular Tags