Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/incenter/ngbclient/objSocket.java


1   
2   package org.incenter.ngbclient;
3   import org.incenter.ngbclient.*;
4   import org.incenter.gb.*;
5   
6   // Imports
7   import java.net.*;
8   import java.io.*;
9   import java.util.*;
10  import java.util.zip.*;
11  import java.lang.*;
12  
13  
14  public class objSocket extends Observable implements Runnable
15  {
16      boolean loggedIn, reading, writing;
17      Socket socket;
18      ObjectInputStream inStream;
19      ObjectOutputStream outStream;
20      GZIPInputStream GZIPin;
21      GZIPOutputStream GZIPout;
22      InputStream in;
23  
24      public static final int SERVER = 0;
25      public static final int CLIENT = 1;
26  
27      public boolean getReading() { return reading; }
28      public boolean getWriting() { return writing; }
29  
30      game_remote game;
31      public game_remote getGame() {return game;}
32      public void setGame(game_remote  v) {this.game = v;}
33  
34      login login;
35      public login getLogin() {return login;}
36      public void setLogin(login  v) {this.login = v;}
37      
38      int raceId;
39      public int getRaceId() {return raceId;}
40      public void setRaceId(int  v) {this.raceId = v;}
41      
42      String racePass;
43      public String getRacePass() {return racePass;}
44      public void setRacePass(String  v) {this.racePass = v;}
45      
46      String govPass;
47      public String getGovPass() {return govPass;}
48      public void setGovPass(String  v) {this.govPass = v;}
49      
50      int mode;
51      public int getMode() {return mode;}
52      public void setMode(int  v) {this.mode = v;}
53      
54      Thread thread;
55      public Thread getThread() {return thread;}
56      public void setThread(Thread  v) {this.thread = v;}
57      
58      int status;
59      public int getStatus() {return status;}
60      public void setStatus(int  v) {this.status = v;}
61      
62      netPacket lastPacket;
63      public netPacket getLastPacket() {return lastPacket;}
64      public void setLastPacket(netPacket  v) {this.lastPacket = v;}
65  
66      // object cache
67  
68      Hashtable cache;
69      public Hashtable getCache() {return cache;}
70      public void setCache(Hashtable  v) {this.cache = v;}
71      
72      int port;
73      public static final int DEFAULT_PORT = 2031;
74  
75      public objSocket() {
76    port = DEFAULT_PORT;
77    mode = CLIENT;
78    socket = null;
79    loggedIn = false;
80      }
81  
82      public void connect(String host, int port) throws Exception {
83        try {
84      socket = new Socket(host, port);
85      status = -1;
86      try {
87          outStream = new ObjectOutputStream(socket.getOutputStream());
88          outStream.flush();
89  
90          in = socket.getInputStream();
91          inStream = new ObjectInputStream(in);} 
92      catch(InterruptedIOException e) {;}
93  
94      status = 0;
95      lastPacket = null;
96        } catch (Exception e) {
97      socket = null;
98      status = -1;
99      throw new Exception("Object Socket Error:" + e);
100       }
101     }
102 
103     private void errorDlg(String s) {
104 
105     }
106 
107     public void run() {
108   boolean rcvd = false;
109   if(socket == null) {
110       return;
111   }
112   try {
113       while(isConnected()) {
114     try {
115         if(isReady()) {
116       rcvd = receive();
117       reading = false;
118         }
119         if(!rcvd)
120       try {thread.sleep(1000);} catch(Exception e) {;}
121         status = 0;
122         rcvd = false;
123     } catch(Exception e) {
124         status = -1;
125         break;
126     }
127       }
128   } catch(Exception e) {
129       status = -1;
130   }
131     }
132 
133     private boolean isReady() throws Exception {
134   try {
135       if(in.available() > 0)
136     return true;
137       else 
138     return false;
139   } catch(Exception e) {
140       throw new Exception("isReady::" + e);
141   }
142     }
143 
144     public void sendCommand(String id, String txt) throws Exception {
145   try {
146       netPacket np = new netPacket(new cmdId(id),
147            new objCommand(txt),
148            netPacket.CLIENT);
149       send(np);
150   } catch(Exception e) { throw new Exception("Error sending command:" + e.getMessage()); }
151     }
152 
153     public void send(netPacket np) throws Exception {
154   try {
155       writing = true;
156       outStream.writeObject(np);
157       outStream.flush();
158       writing = false;
159   } catch(Exception e) {
160       throw new Exception("objSocket.send()::" + e);
161   }
162     }
163     
164     public boolean receive() throws Exception {
165   try {
166       if(!isConnected())
167     throw new Exception("receive()::not connected");
168   } catch(Exception e) {
169       throw new Exception("objSocket.receive()::" + e);
170   }
171 
172   try {
173       reading = true;
174       Object ob = inStream.readObject();
175       if(!(ob instanceof netPacket)) {
176     return false;
177       }
178       lastPacket = (netPacket)ob;
179       if(loggedIn) {
180     if(countObservers() > 0) {
181         setChanged();
182         notifyObservers(lastPacket);
183     }
184     lastPacket = null;
185     return true;
186       } else {
187     // still waiting for the game object
188     if(lastPacket.getObjects().size() == 1) {
189         ob = lastPacket.getObjects().elementAt(0);
190         if((ob instanceof game_remote)) {
191       game = (game_remote)ob;
192       netPacket np = new netPacket(new cmdId("Login"),
193                  login,
194                  netPacket.CLIENT);
195       send(np);
196       loggedIn = true;
197       setChanged();
198       notifyObservers("Waiting for login..");
199       return true;
200         }
201     } else {
202         return false;
203     }
204       }
205       return true;
206 
207   } catch(Exception e) {
208       lastPacket = null;
209       throw new Exception("objSocket.receive()::" + e);
210   }
211     }
212     
213     public void closeConnections() throws Exception  {
214   try {
215       if(socket == null) return;
216       socket.close();
217       socket = null;
218       setChanged();
219       notifyObservers(new String("objConnectionClosed"));
220   }
221   catch (Exception e) {
222       socket = null;
223       status = -1;
224       throw new Exception("objSocket.closeConnections()::" + e);
225   }
226     }
227     
228     public boolean isConnected() throws Exception {
229   try {
230       if(socket == null) {
231     inStream = null;
232     outStream = null;
233     return false;
234       }
235       if(inStream == null || outStream == null) {
236     throw new Exception("objSocket.isConnected():No streams");
237       }
238       return true;
239   } catch(Exception e) {
240       throw new Exception("objSocket.receive()::" + e);
241   }
242     }
243 
244     protected void finalize () {
245   if (socket != null) {
246       try {
247     socket.close();
248       }
249       catch (Exception e) {
250     status = -1;
251       }
252       socket = null;
253   }
254     }
255 
256 }