/* Copyright 1999-2011 (c) My-Channels Copyright (c) 2012-2014 Software AG, Darmstadt, Germany and/or Software AG USA Inc., Reston, VA, USA, and/or its subsidiaries and/or its affiliates and/or their licensors. Use, reproduction, transfer, publication or disclosure is prohibited except as specifically provided for in your License Agreement with Software AG. */ package com.pcbsys.nirvana.apps; import com.pcbsys.foundation.utils.fEnvironment; import com.pcbsys.nirvana.client.*; import com.pcbsys.nirvana.client.p2p.*; import java.util.StringTokenizer; import java.io.*; /** * P2P Echo service */ public class nP2PEcho implements nReconnectHandler, nServiceStateListener { private static nP2PEcho mySelf=null; private static void processArgs(String[] args){ switch (args.length){ case 1: if (args[0].equals("-?")) UsageEnv(); break; } } private static void processEnvironmentVariable(String variable){ String laxVAR=System.getProperty("lax.nl.env."+variable); if (laxVAR!=null) System.setProperty(variable,laxVAR); } public static void main( String[] args ) { //Process command line arguments processArgs(args); //Process Environment Variables processEnvironmentVariable("RNAME"); processEnvironmentVariable("LOGLEVEL"); processEnvironmentVariable("HPROXY"); processEnvironmentVariable("HAUTH"); processEnvironmentVariable("CKEYSTORE"); processEnvironmentVariable("CKEYSTOREPASSWD"); processEnvironmentVariable("CAKEYSTORE"); processEnvironmentVariable("CAKEYSTOREPASSWD"); // Install any proxy server settings fEnvironment.setProxyEnvironments(); // Install any ssl settings fEnvironment.setSSLEnvironments(); // Get the realm details String[] rproperties = new String[ 4 ]; int idx = 0; String RNAME = System.getProperty( "RNAME" ); if (RNAME == null) { Usage(); System.exit(1); } StringTokenizer st = new StringTokenizer( RNAME, "," ); while ( st.hasMoreTokens() ) { String someRNAME = (String)st.nextToken(); rproperties[ idx ] = someRNAME; idx++; } // Trim the array String[] rpropertiesTrimmed = new String[ idx ]; System.arraycopy( rproperties, 0, rpropertiesTrimmed, 0, idx ); if ( rpropertiesTrimmed.length == 0 ) { System.out.println( "No realm information supplied" ); System.exit( 1 ); } mySelf = new nP2PEcho( rpropertiesTrimmed ); } // //--------------------------------------------------------------------------- // nP2PEcho Constructor //--------------------------------------------------------------------------- // public nP2PEcho( String[] rprops ) { if (rprops == null) return; nServiceFactory factory = null; try { nSessionAttributes nsa = null; try { nsa = new nSessionAttributes( rprops ); } catch ( Exception ex ) { System.out.println( "Error creating Session Attributes. Please check your RNAME" ); System.exit( 1 ); } nSession session=nSessionFactory.create(nsa,this,"echoserviceclient"); System.out.println( "Initialising Session" ); session.init(); // // Create an instance of a Service Factory // System.out.println( "Creating factory" ); factory = new nServiceFactory( session ); // // Find the service by name // System.out.println( "Getting list of services" ); nServiceInfo info = factory.findService("echo"); // // now connect to this service // nEventService serv = (nEventService)factory.connectToService( info ); serv.setStateListener(this); System.out.println( "Connected to service" ); // // Now just read from the input stream and write to the service // new dumpStream( serv ); BufferedInputStream bis = new BufferedInputStream( System.in ); byte[] buffer = new byte[ 1 ]; ByteArrayOutputStream baos = new ByteArrayOutputStream(); while ( true ) { bis.read( buffer ); baos.write( buffer ); if ( buffer[ 0 ] == '\n' ) { baos.flush(); byte[] tmp = baos.toByteArray(); nEventProperties props = new nEventProperties(); serv.write(new nConsumeEvent(props, tmp)); baos.reset(); } } } catch ( Exception ex ) { try{ if (factory != null) factory.close(); } catch(Exception ex1){} ex.printStackTrace(); } } public void disconnected(nSession anSession) { System.out.println("You have been disconnected from the realm"); } public void reconnected(nSession anSession) { System.out.println("You have been reconnected to the realm"); } public boolean tryAgain(nSession anSession) { return true; } public void disconnected(nService serv) { System.out.println("You have been disconnected from the P2P Service"); } public void reconnected(nService srv) { System.out.println("You have been reconnected to the P2P Service"); } public void reset(nService srv) { System.out.println("The P2P Service has been reset"); } // //--------------------------------------------------------------------------- // nP2PEcho Package Interface //--------------------------------------------------------------------------- // class dumpStream extends Thread { private nEventService myIs; public dumpStream( nEventService is ) { myIs = is; start(); } public void run() { byte[] buffer = new byte[ 1 ]; try { while ( true ) { nConsumeEvent evt = myIs.read( ); System.out.print( new String( evt.getEventData() ) ); System.out.flush(); } } catch ( Exception ex ) { } } } // End of dumpStream Inner Class /** * Prints the usage string for this class */ private static void Usage() { System.out.println( "Usage ...\n" ); System.out.println("np2pecho \n"); System.out.println( "\n\nNote: -? provides help on environment variables \n"); } private static void UsageEnv() { Usage(); //Special case, show the usage string when -? is passed as no arguments are otherwise required System.out.println( "\n\n(Environment Variables) \n"); System.out.println( "(RNAME) - One or more RNAME entries in the form protocol://host:port" ); System.out.println( " protocol - Can be one of nsp, nhp, nsps, or nhps, where:" ); System.out.println( " nsp - Specifies Nirvana Socket Protocol (nsp)" ); System.out.println( " nhp - Specifies Nirvana HTTP Protocol (nhp)" ); System.out.println( " nsps - Specifies Nirvana Socket Protocol Secure (nsps), i.e. using SSL/TLS" ); System.out.println( " nhps - Specifies Nirvana HTTP Protocol Secure (nhps), i.e. using SSL/TLS" ); System.out.println( " port - The port number of the server" ); System.out.println( "\nHint: - For multiple RNAME entries, use comma separated values which will be attempted in connection weight order\n" ); System.out.println( "(LOGLEVEL) - This determines how much information the nirvana api will output 0 = verbose 7 = quiet\n" ); System.out.println( "(CKEYSTORE) - If using SSL, the location of the keystore containing the client cert\n"); System.out.println( "(CKEYSTOREPASSWD) - If using SSL, the password for the keystore containing the client cert\n"); System.out.println( "(CAKEYSTORE) - If using SSL, the location of the ca truststore\n"); System.out.println( "(CAKEYSTOREPASSWD) - If using SSL, the password for the ca truststore\n"); System.out.println( "(HPROXY) - HTTP Proxy details in the form proxyhost:proxyport, where:" ); System.out.println( " proxyhost - The HTTP proxy host" ); System.out.println( " proxyport - The HTTP proxy port\n" ); System.out.println( "(HAUTH) - HTTP Proxy authentication details in the form user:pass, where:" ); System.out.println( " user - The HTTP proxy authentication username" ); System.out.println( " pass - The HTTP proxy authentication password\n" ); System.exit(1); } } // End of nP2PEcho Class