/* Copyright 1999-2011 (c) My-Channels Copyright (c) 2012-2015 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.nConsumeEvent; import com.pcbsys.nirvana.client.nReconnectHandler; import com.pcbsys.nirvana.client.nSession; import com.pcbsys.nirvana.client.nSessionAttributes; import com.pcbsys.nirvana.client.nSessionFactory; import com.pcbsys.nirvana.client.p2p.nEventService; import com.pcbsys.nirvana.client.p2p.nServerService; import com.pcbsys.nirvana.client.p2p.nServiceFactory; import java.util.StringTokenizer; /** * This class provides a simple echo service */ public class nP2PEchoServer implements nReconnectHandler{ private static nP2PEchoServer mySelf=null; private static String serviceName = "echo"; private static void processArgs(String[] args){ switch (args.length){ case 1: if (args[0].equals("-?")) { UsageEnv(); } else { serviceName = args[0]; } 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 nP2PEchoServer( rpropertiesTrimmed , serviceName); } // //--------------------------------------------------------------------------- // nP2PEchoServer Constructor //--------------------------------------------------------------------------- // public nP2PEchoServer( String[] rprops , String serviceName) { if (rprops == null) return; 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); System.out.println( "Initialising Session" ); session.init(); nServiceFactory factory = new nServiceFactory( session ); nServerService server = factory.createEventService( serviceName, "Echo Server. sends all messages back to the sender" ); System.out.println( "Service Created" ); while ( true ) { nEventService serv = (nEventService)server.accept(); new handler( serv ); } } catch ( Exception ex ) { 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; } // //--------------------------------------------------------------------------- // nP2PEchoServer Interface //--------------------------------------------------------------------------- // public class handler extends Thread { private nEventService myService; private int count = 0; public handler( nEventService serv ) { this.setDaemon( true ); myService = serv; start(); } public void run() { try { while ( true ) { nConsumeEvent evt = myService.read(); myService.write(evt); } } catch ( Exception ex ) { } } } /** * Prints the usage string for this class */ private static void Usage() { System.out.println( "Usage ...\n" ); System.out.println("np2pechoserver \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 nP2PEchoServer Class