import uc.*; import java.io.*; import java.net.*; import java.util.*; public class Commando extends Thread implements UCMessageable { static final int MAJORVERSION = 1; static final int MINORVERSION = 4; static final String REMOTEHOST = "http://www.astrobastards.net/servlet/uc.UnsolicitedCommando"; static Random random = new Random(); static final String PREF_FILE = "pref.uc"; static Commando commando; static MainUCFrame frame; static public void main( String[] args ) throws IOException { frame = new MainUCFrame(); frame.loadPreferences(); frame.setVisible( true ); commando = new Commando(); commando.init(); commando.start(); } static public void textOut( String text ) { frame.out( text ); } static public void textOut( Object[] text ) { frame.out( text ); } static public String getVersion() { String version = Integer.toString( MAJORVERSION ) + '.' + Integer.toString( MINORVERSION ); return version; } long sleepTime = 30 * 60 * 1000; // 30 minute default ByteArrayOutputStream pendingMsgs; void Commando() { setPriority( Thread.MIN_PRIORITY ); } void init() throws IOException { pendingMsgs = new ByteArrayOutputStream(); } public void run() { boolean connected = true; while( true ) { try { // connect to www.astrobastards.net if( !connected ) { Thread.sleep( 60 * 1000 ); } textOut( "Connecting to www.astrobastards.net..." ); DataInputStream dataIn; HttpURLConnection httpCon; synchronized( pendingMsgs ) { ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); DataOutputStream dataOut = new DataOutputStream( byteOut ); pendingMsgs.writeTo( dataOut ); new RequestForOrdersUCMsg( MAJORVERSION, MINORVERSION, frame.getAllowCode() ).send( dataOut ); URL url = new URL( REMOTEHOST ); httpCon = (HttpURLConnection)url.openConnection(); httpCon.setDoOutput( true ); httpCon.setRequestMethod( "POST" ); httpCon.setRequestProperty( "Content-type", "application/octet-stream" ); OutputStream out = httpCon.getOutputStream(); byteOut.writeTo( out ); out.close(); dataIn = new DataInputStream( new BufferedInputStream( (InputStream)httpCon.getContent() ) ); pendingMsgs.reset(); } while( dataIn.available() > 0 ) { UCMsg msg = UCMsg.receive( dataIn ); msg.open( this ); } connected = true; // sleep long time = (long)( random.nextFloat() * sleepTime + random.nextFloat() * sleepTime ); textOut( "Sleeping until " + new Date( System.currentTimeMillis() + time ) ); Thread.sleep( time ); } catch( InterruptedException e ) { textOut( "InterruptedException in run()" ); return; } catch( MalformedURLException e ) { connected = false; textOut( "MalformedURLException in run()" ); } catch( IOException e ) { connected = false; String[] output = new String[ 2 ]; output[ 0 ] = "IOException trying to communicate with server"; output[ 1 ] = "Exception: " + e.toString(); textOut( output ); } } } public void unhandledMsg( UCMsg msg ) { textOut( "Unhandled Msg type: " + msg.getType() ); } public void processMsg( UnknownUCMsg msg ) { unhandledMsg( msg ); } public void processMsg( RequestForOrdersUCMsg msg ) { unhandledMsg( msg ); } public void processMsg( TacticalOrdersUCMsg msg ) { textOut( "Receiving TacOrd[" + Long.toHexString( msg.getID() ) + ']' ); new TacOrd( msg.getID(), msg.getOrders(), pendingMsgs, frame ).start(); } public void processMsg( MissionResultsUCMsg msg ) { textOut( "Mission results" ); unhandledMsg( msg ); } public void processMsg( SleepTimeUCMsg msg ) { textOut( "Sleep time msg" ); sleepTime = msg.getTime(); } public void processMsg( TextUCMsg msg ) { textOut( "Received TextUCMsg" ); if( msg.getKillOnClose() ) commando.interrupt(); MsgWindow window = new MsgWindow( msg.getText(), msg.getKillOnClose() ); window.setVisible( true ); } public void processMsg( MainFrameTextUCMsg msg ) { textOut( msg.getText() ); } }