Brunet.Applications.Examples.HelloWorldNodeDataHandler.Run C# (CSharp) Method

Run() public method

This is the work horse method.
public Run ( ) : void
return void
    public override void Run() {
      // This handles the whole process of preparing the Brunet.Node.
      _app_node = CreateNode(_node_config);
      // Each Brunet.Node contains a DemuxHandler, this object allows us to
      // request that any message with a specific PType arrive here at the
      // HandleData method.  In this case, we want the PType("HelloWorld"),
      // to arrive here, and without state.
      _app_node.Node.DemuxHandler.GetTypeSource(HW).Subscribe(this, null);

      // Services include XmlRpcManager and Dht over XmlRpcManager
      // Start the Brunet.Node and allow it to connect to remote nodes
      Thread thread = new Thread(_app_node.Node.Connect);
      thread.Start();

      // We finally are at the hello world
      // This is our address, you can copy and paste this locally and at other
      // sites to communicate
      Console.WriteLine("Your address is: " + _app_node.Node.Address + "\n");

      // We will continue on, until we get to the Disconnected states.  Assumming
      // you are running this on a supported platform, that would be triggered 
      // initially by ctrl-c
      while(_app_node.Node.ConState != Node.ConnectionState.Disconnected) {
        // First we need the address of the remote node
        Console.Write("Send message to: ");
        string address_string = Console.ReadLine().Trim(new char[] {' ', '\t'});
        Address addr = null;
        try {
          addr = AddressParser.Parse(address_string);
        } catch {
          Console.WriteLine("Invalid address!\n");
          continue;
        }

        // Get a message
        Console.Write("Message: ");
        string message = Console.ReadLine();

        //Call the Send Message passing in a MemBlock which happens to implement ICopyable
        SendMessage(addr, MemBlock.Reference(Encoding.UTF8.GetBytes(message)));
        Console.WriteLine("Sent...\n");
      }
    }

Usage Example

        public static int Main(string [] args)
        {
            // Create a new RuntimeParameters to parse the args
            RuntimeParameters parameters = new RuntimeParameters(
                "HelloWorldNodeDataHandler",
                "HelloWorld using IDataHandler/ISender paradigm");

            // Parse the args, if we don't get 0 back, there was an error
            if (parameters.Parse(args) != 0)
            {
                // Print the error and help
                Console.WriteLine(parameters.ErrorMessage);
                parameters.ShowHelp();
                // exit after error
                return(-1);
            }
            else if (parameters.Help)
            {
                // Caller asked for help, let's print it
                parameters.ShowHelp();
                // exit after printing help
                return(0);
            }

            // Instantiate a new inherited node of your choice
            HelloWorldNodeDataHandler hwn =
                new HelloWorldNodeDataHandler(parameters.NodeConfig);

            // And run it... this hijacks the current thread, we'll return once the node disconnects
            hwn.Run();

            Console.WriteLine("Exiting...");

            return(0);
        }
All Usage Examples Of Brunet.Applications.Examples.HelloWorldNodeDataHandler::Run