/* 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. */ using System; using System.Threading; using System.IO; using com.pcbsys.nirvana.client; using com.pcbsys.nirvana.client.p2p; namespace com.pcbsys.nirvana.apps.nP2PEcho { class nP2PEcho :nReconnectHandler,nServiceStateListener { private static nP2PEcho mySelf; private static void processArgs(String[] args) { if (args.Length < 1) { Usage(); Environment.Exit(2); } } public static void Main(String[] args) { //Process command line arguments processArgs(args); // Get the realm details String RNAME = args[0]; if (RNAME == null) { Usage(); return; } // Trim the array String[] rpropertiesTrimmed = new String[1]; rpropertiesTrimmed[0] = RNAME; mySelf = new nP2PEcho(rpropertiesTrimmed, args.Length > 1); } public nP2PEcho(String[] rprops, bool isServer) { if (rprops == null) return; nServiceFactory factory = null; try { nSessionAttributes nsa; try { nsa = new nSessionAttributes(rprops); } catch (Exception) { Console.WriteLine("Error creating Session Attributes. Please check your RNAME"); return; } nSession session = nSessionFactory.create(nsa, this); Console.WriteLine("Initialising session"); session.init(); // // Create an instance of a Service Factory // Console.WriteLine("Creating factory"); factory = new nServiceFactory(session); if (isServer) { Console.WriteLine("Running as server"); nServerService server = factory.createEventService("echo", "Echo Server. sends all messages back to the sender"); while (true) { nEventService serv = (nEventService)server.accept(); handler hd = new handler(serv); Thread newThread = new Thread(new ThreadStart(hd.run)); newThread.Start(); } } else { // // Find the service by name // Console.WriteLine("Getting list of services"); nServiceInfo info = null; while (info == null) { info = factory.findService("echo"); if (info == null) { Thread.Sleep(300); } } // // now connect to this service // nEventService serv = (nEventService)factory.connectToService(info); serv.setStateListener(this); Console.WriteLine("Connected to service"); // // Now just read from the input stream and write to the service // dumpStream ds = new dumpStream(serv); Thread newThread = new Thread(new ThreadStart(ds.run)); newThread.Start(); System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding(); while (true) { string line = Console.ReadLine(); nEventProperties props = new nEventProperties(); serv.write(new nConsumeEvent(props, encoding.GetBytes(line))); } } } catch (Exception ex) { try { factory.close(); } catch (Exception) { } Console.WriteLine(ex.Message); } } // //--------------------------------------------------------------------------- // nP2PEcho Package Interface //--------------------------------------------------------------------------- // public class handler { private nEventService myService; public handler(nEventService serv) { myService = serv; } public void run() { try { while (true) { nConsumeEvent evt = myService.read(); myService.write(evt); } } catch (Exception) { } } } class dumpStream { private nEventService myIs; public dumpStream(nEventService eis) { myIs = eis; } public void run() { System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding(); try { while (true) { nConsumeEvent evt = myIs.read(); Console.WriteLine(encoding.GetString(evt.getEventData())); } } catch (Exception) { } } } // End of dumpStream Inner Class /** * Prints the usage string for this class */ private static void Usage() { Console.WriteLine("Usage ...\n"); Console.WriteLine("nP2PEcho [server]\n"); Console.WriteLine( " \n"); Console.WriteLine( " - the rname of the server to connect to"); Console.WriteLine( "\n[Optional Arguments] \n"); Console.WriteLine( "[server] - write 'server' to run echo server, or leave out to run echo client"); } public void disconnected(nService serv) { Console.WriteLine("You have been disconnected to the P2P Service"); } public void reconnected(nService srv) { Console.WriteLine("You have been reconnected to the P2P Service"); } public void reset(nService srv) { Console.WriteLine("The P2P Service Has been reset"); } public void disconnected(nSession anSession) { Console.WriteLine("You have been disconnected from the realm"); } public void reconnected(nSession anSession) { Console.WriteLine("You have been reconnected to the realm"); } public bool tryAgain(nSession anSession) { return true; } } }