001: /*
002: * Copyright (c) 2001 Silvere Martin-Michiellot All Rights Reserved.
003: *
004: * Silvere Martin-Michiellot grants you ("Licensee") a non-exclusive,
005: * royalty free, license to use, modify and redistribute this
006: * software in source and binary code form,
007: * provided that i) this copyright notice and license appear on all copies of
008: * the software; and ii) Licensee does not utilize the software in a manner
009: * which is disparaging to Silvere Martin-Michiellot.
010: *
011: * This software is provided "AS IS," without a warranty of any kind. ALL
012: * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
013: * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
014: * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. Silvere Martin-Michiellot
015: * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
016: * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
017: * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
018: * Silvere Martin-Michiellot OR ITS LICENSORS BE LIABLE
019: * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
020: * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
021: * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
022: * OR INABILITY TO USE SOFTWARE, EVEN IF Silvere Martin-Michiellot HAS BEEN
023: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
024: *
025: * This software is not designed or intended for use in on-line control of
026: * aircraft, air traffic, aircraft navigation or aircraft communications; or in
027: * the design, construction, operation or maintenance of any nuclear
028: * facility. Licensee represents and warrants that it will not use or
029: * redistribute the Software for such purposes.
030: *
031: * @Author: Silvere Martin-Michiellot
032: *
033: */
034:
035: package com.db.server;
036:
037: import com.db.net.*;
038:
039: import java.util.HashSet;
040: import java.util.Hashtable;
041: import java.net.*;
042: import java.io.*;
043:
044: /**
045: */
046:
047: public class RemoteWorldSpotsAccess {
048:
049: private InetAddress IP;
050: private int port;
051: //the HashSet contains WorldSpotBasicInformation
052: private HashSet remoteWorldSpots;
053: private Hashtable information;
054:
055: public RemoteWorldSpotsAccess(InetAddress IP, int port) {
056:
057: this .IP = IP;
058: this .port = port;
059:
060: }
061:
062: public RemoteWorldSpotsAccess(Hashtable information,
063: InetAddress IP, int port, HashSet remoteWorldSpots) {
064:
065: this .information = information;
066: this .IP = IP;
067: this .port = port;
068: this .remoteWorldSpots = remoteWorldSpots;
069:
070: }
071:
072: public final Hashtable getInformation(UniverseServer universeServer) {
073:
074: if (this .information == null) {
075: this .updateInformation(universeServer);
076: }
077: return this .information;
078:
079: }
080:
081: public final void updateInformation(UniverseServer universeServer) {
082:
083: Socket socket;
084: ObjectOutputStream objectOutputStream;
085: ObjectInputStream objectInputStream;
086:
087: ControlRequest controlRequest;
088: ControlReply controlReply;
089:
090: try {
091: //query distant server and fetch informations
092: socket = new Socket(universeServer.getIP(), universeServer
093: .getPort(), this .getIP(), this .getPort());
094:
095: objectInputStream = new ObjectInputStream(socket
096: .getInputStream());
097: objectOutputStream = new ObjectOutputStream(socket
098: .getOutputStream());
099:
100: controlRequest = new ControlRequest(
101: ControlRequest.INFORMATION);
102: // send request
103: objectOutputStream.writeObject(controlRequest);
104:
105: // read response
106: try {
107:
108: controlReply = (ControlReply) objectInputStream
109: .readObject();
110:
111: if (controlReply.getReplyKind() != ControlReply.REFUSED) {
112:
113: this .information = (Hashtable) controlReply
114: .getObjectValue();
115:
116: } else {
117:
118: this .information = new Hashtable();
119:
120: }
121:
122: } catch (ClassNotFoundException classNotFoundException) {
123: }
124:
125: objectOutputStream.flush();
126: objectInputStream.close();
127: objectOutputStream.close();
128:
129: socket.close();
130:
131: } catch (IOException iOException) {
132: }
133:
134: }
135:
136: public final InetAddress getIP() {
137:
138: return this .IP;
139:
140: }
141:
142: public final void setIP(InetAddress IP) {
143:
144: this .IP = IP;
145:
146: }
147:
148: public final int getPort() {
149:
150: return this .port;
151:
152: }
153:
154: public final void setPort(int port) {
155:
156: this .port = port;
157:
158: }
159:
160: //no actual update is made before the call (except the first time of course)
161: public final HashSet getRemoteWorldSpots(
162: UniverseServer universeServer) {
163:
164: if (this .remoteWorldSpots == null) {
165: this .updateRemoteWorldSpots(universeServer);
166: }
167: return this .remoteWorldSpots;
168:
169: }
170:
171: public final void updateRemoteWorldSpots(
172: UniverseServer universeServer) {
173:
174: Socket socket;
175: ObjectOutputStream objectOutputStream;
176: ObjectInputStream objectInputStream;
177:
178: ControlRequest controlRequest;
179: ControlReply controlReply;
180:
181: try {
182: //query distant server and fetch informations
183: socket = new Socket(universeServer.getIP(), universeServer
184: .getPort(), this .getIP(), this .getPort());
185:
186: objectInputStream = new ObjectInputStream(socket
187: .getInputStream());
188: objectOutputStream = new ObjectOutputStream(socket
189: .getOutputStream());
190:
191: controlRequest = new ControlRequest(
192: ControlRequest.LOCALWORLDSPOTS);
193: // send request
194: objectOutputStream.writeObject(controlRequest);
195:
196: // read response
197: try {
198:
199: controlReply = (ControlReply) objectInputStream
200: .readObject();
201:
202: if (controlReply.getReplyKind() != ControlReply.REFUSED) {
203:
204: this .remoteWorldSpots = (HashSet) controlReply
205: .getObjectValue();
206:
207: } else {
208:
209: this .remoteWorldSpots = new HashSet();
210:
211: }
212:
213: } catch (ClassNotFoundException classNotFoundException) {
214: }
215:
216: objectOutputStream.flush();
217: objectInputStream.close();
218: objectOutputStream.close();
219:
220: socket.close();
221:
222: } catch (IOException iOException) {
223: }
224:
225: }
226:
227: }
|