1 // 2 // The contents of this file are subject to the Mozilla Public 3 // License Version 1.1 (the "License"); you may not use this file 4 // except in compliance with the License. You may obtain a copy of 5 // the License at http://www.mozilla.org/MPL/ 6 // 7 // Software distributed under the License is distributed on an "AS 8 // IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or 9 // implied. See the License for the specific language governing 10 // rights and limitations under the License. 11 // 12 // The Original Code is State Machine Compiler (SMC). 13 // 14 // The Initial Developer of the Original Code is Charles W. Rapp. 15 // Portions created by Charles W. Rapp are 16 // Copyright (C) 2000 - 2003 Charles W. Rapp. 17 // All Rights Reserved. 18 // 19 // Contributor(s): 20 // 21 // Name 22 // client.java 23 // 24 // Description 25 // Encapsulates "TCP" client connection. 26 // 27 // RCS ID 28 // $Id: client.java,v 1.4 2005/05/28 13:51:24 cwrapp Exp $ 29 // 30 // CHANGE LOG 31 // $Log: client.java,v $ 32 // Revision 1.4 2005/05/28 13:51:24 cwrapp 33 // Update Java examples 1 - 7. 34 // 35 // Revision 1.0 2003/12/14 20:21:46 charlesr 36 // Initial revision 37 // 38 39 package smc_ex6; 40 41 import java.io.IOException; 42 import java.net.InetAddress; 43 import java.util.Random; 44 45 public final class client 46 extends Thread 47 implements TcpConnectionListener 48 { main(String[] args)49 public static void main(String[] args) 50 { 51 int port = -1; 52 String host = null; 53 54 if (args.length != 2) 55 { 56 System.err.println("client: Incorrect number of arguments."); 57 System.err.println("usage: client host port"); 58 System.exit(1); 59 } 60 else 61 { 62 host = args[0]; 63 try 64 { 65 port = Integer.parseInt(args[1]); 66 } 67 catch (NumberFormatException ex) 68 { 69 System.err.println("Invalid port number - \"" + 70 args[1] + 71 "\"."); 72 System.exit(2); 73 } 74 75 // Now try to connect to the server and start sending 76 // data. 77 try 78 { 79 InetAddress address; 80 client client; 81 82 address = InetAddress.getByName(host); 83 client = new client(); 84 85 // Create client and start running. 86 System.out.println("(Starting execution. Hit Enter to stop.)"); 87 client.run(address, port); 88 System.out.println("(Stopping execution.)"); 89 90 System.exit(0); 91 } 92 catch (Exception ex) 93 { 94 ex.printStackTrace(); 95 System.exit(5); 96 } 97 } 98 } 99 client()100 public client() 101 { 102 _client_socket = null; 103 _my_thread = null; 104 _opened = false; 105 _isRunning = false; 106 _randomizer = new Random(System.currentTimeMillis()); 107 _owner = null; 108 _errorMessage = null; 109 110 return; 111 } 112 client(TcpClient client_socket, server owner)113 public client(TcpClient client_socket, server owner) 114 { 115 _client_socket = client_socket; 116 _my_thread = null; 117 _opened = false; 118 _isRunning = false; 119 _randomizer = new Random(System.currentTimeMillis()); 120 _owner = owner; 121 _errorMessage = null; 122 123 _client_socket.setListener(this); 124 125 return; 126 } 127 run(InetAddress address, int port)128 public void run(InetAddress address, int port) 129 { 130 String port_string = Integer.toString(port); 131 132 _my_thread = Thread.currentThread(); 133 134 // If there is no client connection, create one and open 135 // it. 136 _client_socket = new TcpClient(this); 137 138 // Open the client connection. 139 System.out.print("Opening connection to " + 140 address.getHostName() + 141 ":" + 142 port_string + 143 " ... "); 144 _opened = false; 145 _client_socket.open(address, port); 146 147 // Wait for open to complete. 148 try 149 { 150 _isRunning = true; 151 while (_isRunning == true) 152 { 153 Thread.sleep(1000); 154 } 155 } 156 catch (InterruptedException interrupt) {} 157 158 if (_opened == false) 159 { 160 System.out.print("open failed"); 161 if (_errorMessage == null) 162 { 163 System.out.println("."); 164 } 165 else 166 { 167 System.out.println(" - " + _errorMessage); 168 _errorMessage = null; 169 } 170 } 171 else 172 { 173 System.out.println("open successful."); 174 run(); 175 } 176 177 return; 178 } 179 run()180 public void run() 181 { 182 long sleeptime; 183 InetAddress address = _client_socket.getAddress(); 184 int port = _client_socket.getPort(); 185 String port_string = Integer.toString(port); 186 StopThread thread = new StopThread(this); 187 int message_count = 1; 188 String message_base = "This is message #"; 189 String message; 190 byte[] data; 191 192 // Remember this thread for later. 193 if (_my_thread == null) 194 { 195 _my_thread = Thread.currentThread(); 196 } 197 198 // Create a thread to watch for a keystroke. 199 thread.start(); 200 201 // Now sit here waiting to send and receive. 202 _isRunning = true; 203 while (_isRunning == true) 204 { 205 try 206 { 207 // Decide how long before the next alarm is issued. 208 // Sleep time is in milliseconds but no less than 100. 209 sleeptime = (_randomizer.nextLong() % MAX_SLEEP_TIME); 210 if (sleeptime < MIN_SLEEP_TIME) 211 { 212 sleeptime = MIN_SLEEP_TIME; 213 } 214 215 Thread.sleep(sleeptime); 216 217 // Now send a message. 218 message = message_base + 219 Integer.toString(message_count++) + 220 "."; 221 data = message.getBytes(); 222 System.out.print("Transmitting to " + 223 address.getHostName() + 224 ":" + 225 port_string + 226 ": \"" + 227 message + 228 "\" ... "); 229 _client_socket.transmit(data, 0, data.length); 230 } 231 catch (InterruptedException interrupt) {} 232 catch (Exception jex) 233 { 234 jex.printStackTrace(); 235 _isRunning = false; 236 } 237 } 238 239 // Now that we are no longer running, close the 240 // connection. 241 System.out.print("Closing connection to " + 242 address.getHostName() + 243 ":" + 244 port_string + 245 " ... "); 246 _client_socket.close(); 247 System.out.println("closed."); 248 249 if (_owner != null) 250 { 251 _owner.clientClosed(this); 252 } 253 254 return; 255 } 256 257 // Stop the app. halt()258 public void halt() 259 { 260 _isRunning = false; 261 262 // Wake me up in case I am sleeping. 263 _my_thread.interrupt(); 264 265 return; 266 } 267 opened(TcpConnection client)268 public void opened(TcpConnection client) 269 { 270 _opened = true; 271 _my_thread.interrupt(); 272 return; 273 } 274 openFailed(String reason, TcpConnection client)275 public void openFailed(String reason, TcpConnection client) 276 { 277 _opened = false; 278 _errorMessage = reason; 279 _my_thread.interrupt(); 280 return; 281 } 282 halfClosed(TcpConnection client)283 public void halfClosed(TcpConnection client) 284 { 285 InetAddress address = _client_socket.getAddress(); 286 String port_string = Integer.toString(_client_socket.getPort()); 287 288 System.out.print("Connection from " + 289 address.getHostName() + 290 ":" + 291 port_string + 292 " has closed its side. "); 293 294 // The far end has closed its connection. Stop running 295 // since it is no longer listening. 296 _isRunning = false; 297 _my_thread.interrupt(); 298 299 return; 300 } 301 closed(String reason, TcpConnection client)302 public void closed(String reason, TcpConnection client) 303 { 304 _opened = false; 305 _isRunning = false; 306 _my_thread.interrupt(); 307 return; 308 } 309 transmitted(TcpConnection client)310 public void transmitted(TcpConnection client) 311 { 312 System.out.println("transmit successful."); 313 return; 314 } 315 transmitFailed(String reason, TcpConnection client)316 public void transmitFailed(String reason, TcpConnection client) 317 { 318 System.out.println("transmit failed - " + 319 reason + 320 "."); 321 return; 322 } 323 receive(byte[] data, TcpConnection client)324 public void receive(byte[] data, TcpConnection client) 325 { 326 String message = new String(data); 327 328 System.out.println("Received data from " + 329 ((TcpClient) client).getAddress() + 330 ":" + 331 Integer.toString(((TcpClient) client).getPort()) + 332 ": \"" + 333 message + 334 "\""); 335 return; 336 } 337 accepted(TcpClient client, TcpServer server)338 public void accepted(TcpClient client, TcpServer server) {} 339 340 // Member data 341 342 private TcpClient _client_socket; 343 private boolean _isRunning; 344 private boolean _opened; 345 private Thread _my_thread; 346 private server _owner; 347 private String _errorMessage; 348 349 // Use the following to randomly decide when to issue an 350 // alarm and what type, etc. 351 private Random _randomizer; 352 353 // Constants. 354 private static final long MIN_SLEEP_TIME = 100; 355 private static final long MAX_SLEEP_TIME = 30000; // 30 seconds.. 356 private static final int OPTION = 0; 357 private static final int PORT = 1; 358 private static final int HOST = 2; 359 360 // Inner classes. 361 362 private final class StopThread 363 extends Thread 364 { StopThread(client client)365 private StopThread(client client) 366 { 367 _client = client; 368 } 369 run()370 public void run() 371 { 372 // As soon as any key is hit, stop. 373 try 374 { 375 System.in.read(); 376 } 377 catch (IOException io_exception) 378 {} 379 380 _client.halt(); 381 382 return; 383 } 384 385 private client _client; 386 } 387 } 388