Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            // Parse commandline arguments
            if (args.Length >= 1)
            {
                Port = int.Parse(args[0]);
            }

            // Set log level and start logger
            Logger.LogLevel = Level.DEBUG;
            Logger.Start();

            // Add logger to server
            Server.Logger = Logger;

            // Bind event handlers
            Server.OnStart          = ServerStarted;
            Server.OnStop           = ServerStopped;
            Server.OnDataReceived  += DataReceived;
            Server.OnDataSent      += DataSent;
            Server.OnError         += ServerError;
            Server.OnPeerConnected += PeerConnected;

            // Start server
            Server.Start(Port);

            // Enter into a loop
            int MenuSelection = 0;

            while (MenuSelection != 4)
            {
                // Manually connect to a peer
                if (MenuSelection == 1)
                {
                    Logger.Log(Level.INFO, "Enter a URL:");
                    string Url = Console.ReadLine();
                    Logger.Log(Level.INFO, "Enter a port:");
                    int Port = int.Parse(Console.ReadLine());
                    Server.Connect(new Connection(Url, Port, ""));
                }

                // Broadcast a test packet
                else if (MenuSelection == 2)
                {
                    // Create a response
                    Handshake.Request Request = new Handshake.Request
                    {
                        NodeData = new NodeData()
                        {
                            NetworkId = GlobalsConfig.NETWORK_ID,
                            Version   = 1,
                            Port      = 8090,
                            LocalTime = GeneralUtilities.GetTimestamp(),
                            PeerId    = Server.PeerId
                        },
                        PayloadData = new CoreSyncData()
                        {
                            CurrentHeight = Globals.DAEMON_BLOCK_HEIGHT,
                            TopId         = Globals.DAEMON_TOP_ID
                        }
                    };

                    // Get body bytes
                    byte[] BodyBytes = Request.Serialize();

                    // Create a header
                    BucketHead2 Header = new BucketHead2
                    {
                        Signature        = GlobalsConfig.LEVIN_SIGNATURE,
                        ResponseRequired = false,
                        PayloadSize      = (ulong)BodyBytes.Length,
                        CommandCode      = (uint)Handshake.Id,
                        ProtocolVersion  = GlobalsConfig.LEVIN_VERSION,
                        Flags            = LevinProtocol.LEVIN_PACKET_RESPONSE,
                        ReturnCode       = LevinProtocol.LEVIN_RETCODE_SUCCESS
                    };

                    Logger?.Log(Level.DEBUG, "[OUT] Sending Handshake Request:");
                    Logger?.Log(Level.DEBUG, "- Node Data:");
                    Logger?.Log(Level.DEBUG, "  - Network ID: {0}", Encoding.StringToHexString(Request.NodeData.NetworkId));
                    Logger?.Log(Level.DEBUG, "  - Peer ID: {0}", Request.NodeData.PeerId);
                    Logger?.Log(Level.DEBUG, "  - Version: {0}", Request.NodeData.Version);
                    Logger?.Log(Level.DEBUG, "  - Local Time: {0}", Request.NodeData.LocalTime);
                    Logger?.Log(Level.DEBUG, "  - Port: {0}", Request.NodeData.Port);
                    Logger?.Log(Level.DEBUG, "- Core Sync Data:");
                    Logger?.Log(Level.DEBUG, "  - Current Height: {0}", Request.PayloadData.CurrentHeight);
                    Logger?.Log(Level.DEBUG, "  - Top ID: {0}", Encoding.StringToHexString(Request.PayloadData.TopId));

                    // Send notification
                    Server.Broadcast(Encoding.AppendToByteArray(BodyBytes, Header.Serialize()));
                }

                // Show peer list
                else if (MenuSelection == 3)
                {
                    Server.Prune();
                    string Peers = "";
                    List <PeerConnection> PeerList = Server.GetPeerList();
                    foreach (PeerConnection Peer in PeerList)
                    {
                        Peers += Peer.Address + " ";
                    }
                    Logger.Log(Level.DEBUG, "Peers:");
                    Logger.Log(Level.DEBUG, Peers);
                }

                // Write menu
                Logger.Log(Level.INFO, "Menu:");
                Logger.Log(Level.INFO, " 1\tConnect to a Server");
                Logger.Log(Level.INFO, " 2\tTest Packet");
                Logger.Log(Level.INFO, " 3\tShow Peer List");
                Logger.Log(Level.INFO, " 4\tExit");
                Logger.Log(Level.INFO, "Enter Selection:");

                // Get menu selection
                MenuSelection = int.Parse(Console.ReadLine());
            }

            // Close all connections
            Server.Close();

            // Close logger
            Logger.Stop();
        }
Ejemplo n.º 2
0
        // Run daemon
        public void Start()
        {
            // Check if running
            if (!Running)
            {
                return;
            }

            /*
             *
             * This is basically all debugging still
             *
             */

            // Enter into a loop
            int MenuSelection = 0;

            while (MenuSelection != 4 && Running)
            {
                // Manually connect to a peer
                if (MenuSelection == 1)
                {
                    ConsoleMessage.WriteLine(ConsoleMessage.DefaultColor, "Enter a URL:", LogLevel.INFO);
                    string Url = Console.ReadLine();
                    ConsoleMessage.WriteLine(ConsoleMessage.DefaultColor, "Enter a port:", LogLevel.INFO);
                    int Port = int.Parse(Console.ReadLine());
                    Server.Connect(new Connection(Url, Port, ""));
                }

                // Broadcast a test packet
                else if (MenuSelection == 2)
                {
                    // Create a response
                    Handshake.Request Request = new Handshake.Request
                    {
                        NodeData = new NodeData()
                        {
                            NetworkId = GlobalsConfig.NETWORK_ID,
                            Version   = 1,
                            Port      = 8090,
                            LocalTime = GeneralUtilities.GetTimestamp(),
                            PeerId    = Server.PeerId
                        },
                        PayloadData = new CoreSyncData()
                        {
                            CurrentHeight = Globals.DAEMON_BLOCK_HEIGHT,
                            TopId         = Globals.DAEMON_TOP_ID
                        }
                    };

                    // Get body bytes
                    byte[] BodyBytes = Request.Serialize();

                    // Create a header
                    BucketHead2 Header = new BucketHead2
                    {
                        Signature        = GlobalsConfig.LEVIN_SIGNATURE,
                        ResponseRequired = false,
                        PayloadSize      = (ulong)BodyBytes.Length,
                        CommandCode      = (uint)Handshake.Id,
                        ProtocolVersion  = GlobalsConfig.LEVIN_VERSION,
                        Flags            = LevinProtocol.LEVIN_PACKET_RESPONSE,
                        ReturnCode       = LevinProtocol.LEVIN_RETCODE_SUCCESS
                    };

                    ConsoleMessage.WriteLine(ConsoleMessage.DefaultColor, "[OUT] Sending Handshake Request:", LogLevel.DEBUG);
                    ConsoleMessage.WriteLine(ConsoleMessage.DefaultColor, "- Node Data:", LogLevel.DEBUG);
                    ConsoleMessage.WriteLine(ConsoleMessage.DefaultColor, "  - Network ID: " + Encoding.StringToHexString(Request.NodeData.NetworkId), LogLevel.DEBUG);
                    ConsoleMessage.WriteLine(ConsoleMessage.DefaultColor, "  - Peer ID: " + Request.NodeData.PeerId, LogLevel.DEBUG);
                    ConsoleMessage.WriteLine(ConsoleMessage.DefaultColor, "  - Version: " + Request.NodeData.Version, LogLevel.DEBUG);
                    ConsoleMessage.WriteLine(ConsoleMessage.DefaultColor, "  - Local Time: " + Request.NodeData.LocalTime, LogLevel.DEBUG);
                    ConsoleMessage.WriteLine(ConsoleMessage.DefaultColor, "  - Port: " + Request.NodeData.Port, LogLevel.DEBUG);
                    ConsoleMessage.WriteLine(ConsoleMessage.DefaultColor, "- Core Sync Data:", LogLevel.DEBUG);
                    ConsoleMessage.WriteLine(ConsoleMessage.DefaultColor, "  - Current Height: " + Request.PayloadData.CurrentHeight, LogLevel.DEBUG);
                    ConsoleMessage.WriteLine(ConsoleMessage.DefaultColor, "  - Top ID: " + Encoding.StringToHexString(Request.PayloadData.TopId), LogLevel.DEBUG);

                    // Send notification
                    Server.Broadcast(Encoding.AppendToByteArray(BodyBytes, Header.Serialize()));
                }

                // Show peer list
                else if (MenuSelection == 3)
                {
                    Server.Prune();
                    string Peers = "";
                    List <PeerConnection> PeerList = Server.GetPeerList();
                    foreach (PeerConnection Peer in PeerList)
                    {
                        Peers += Peer.Address + " ";
                    }
                    ConsoleMessage.WriteLine(ConsoleMessage.DefaultColor, "Peers:", LogLevel.DEBUG);
                    ConsoleMessage.WriteLine(ConsoleMessage.DefaultColor, Peers, LogLevel.DEBUG);
                }

                // Write menu
                ConsoleMessage.WriteLine(ConsoleMessage.DefaultColor, "Menu:", LogLevel.INFO);
                ConsoleMessage.WriteLine(ConsoleMessage.DefaultColor, " 1\tConnect to a Server", LogLevel.INFO);
                ConsoleMessage.WriteLine(ConsoleMessage.DefaultColor, " 2\tTest Packet", LogLevel.INFO);
                ConsoleMessage.WriteLine(ConsoleMessage.DefaultColor, " 3\tShow Peer List", LogLevel.INFO);
                ConsoleMessage.WriteLine(ConsoleMessage.DefaultColor, " 4\tExit\n", LogLevel.INFO);
                ConsoleMessage.WriteLine(ConsoleMessage.DefaultColor, "Enter Selection:", LogLevel.INFO);

                // Get menu selection
                MenuSelection = int.Parse(Console.ReadLine());
            }

            // Stop daemon
            Stop();
        }