// copyright (c) 1997,1998 stephen f. white // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2, or (at your option) // any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; see the file COPYING. If not, write to // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. package vnet; import java.io.IOException; import java.io.EOFException; import java.net.*; class ClientThread extends Thread { VFieldInputStream in; VFieldOutputStream out; boolean connected; Socket socket; String username; String avatarURL; ClientThreadObserver observer; String host; int port; int userVid; final static boolean debug = false; static private int TRIES_MAX_NUMBER= 20; // -------------------------------------------------------------------------------- void socketConnect() throws IOException { socket = new Socket(host, port); in = new VFieldInputStream(socket.getInputStream()); out = new VFieldOutputStream(socket.getOutputStream()); connected= true; } // -------------------------------------------------------------------------------- ClientThread( ClientThreadObserver observer, String host, int port, String username, String avatarURL) { this.observer = observer; this.host = host; this.port = port; this.username = username; this.avatarURL = avatarURL; try { socketConnect(); } catch (IOException e) { System.err.println("Failed connect: " + host + ":" + port + ", " + e.getMessage()); observer.onError(e.getMessage()); connected= false; return; } } // --------------------------------------------------------------------------- private boolean getNetInput() { if ( !connected ) return false; try { int vid = in.readInt(); short field = in.readShort(); VField value = in.readField(); if (debug) System.err.println(userVid + ": recd: " + VIP.msgToString(vid, field, value)); observer.onNetInput(vid, field, value); } catch (EOFException e) { // e.printStackTrace(); connected= false; return false; } catch (IOException e) { // e.printStackTrace(); connected= false; return false; } return true; } // --------------------------------------------------------------------------- public String getUserName() { return username; } // --------------------------------------------------------------------------- public void run() { if ( connected ) { int iTriesNumber= 0; setPriority(Thread.MIN_PRIORITY); while ( iTriesNumber< TRIES_MAX_NUMBER ) try { out.writeUTF(username + (( iTriesNumber== 0 ) ? "": String.valueOf( iTriesNumber ))); out.writeUTF(avatarURL); userVid = in.readInt(); if ( userVid>= 0 ) { username += (( iTriesNumber== 0 ) ? "": String.valueOf( iTriesNumber )); break; } iTriesNumber++; socketConnect(); } catch (IOException e) { System.err.println("ClientThread blew up trying to send username"); e.printStackTrace(); observer.onNetConnect(-2); return; } observer.onNetConnect(userVid); if (userVid >= 0) { while ( true ) { if ( !getNetInput() ) break; } observer.onNetDisconnect(); //System.err.println(userVid + " has left"); } } } // --------------------------------------------------------------------------- void send(int vid, short field, VField value) throws IOException { if (debug) { System.err.println(userVid + ": sent: " + VIP.msgToString(vid, field, value)); } out.writeInt(vid); out.writeShort(field); out.writeField(value); out.flush(); } // --------------------------------------------------------------------------- // WriterThreadObserver callback public void onError(Exception e) { e.printStackTrace(); } }