예제 #1
0
 private static void RemoteHubSwitch_ConnectionErrorOccurred(object sender, ConnectionExceptionWithAdapterEventArgs e)
 {
     Console.WriteLine("Connection Error (or closed): " + e.Exception.Message);
     if (e.IsFatal)
     {
         var adapter = e.Adapter;
         remoteHubSwitch.RemoveAdapter(adapter);
     }
 }
        static void ConnectAndDisconnectSwitchTest(RemoteHubSwitch remoteHubSwitch1, StreamAdapter <byte[]> streamAdaptersOnSwitch1)
        {
            //Sending test messages
            Task sending = Task.Run(async() => await SendTestMessages());

            sending.Wait();

            //disconnect switches
            Console.WriteLine("Disconnecting...");
            remoteHubSwitch1.RemoveAdapter(streamAdaptersOnSwitch1);

            //Sending test messages
            Console.WriteLine("There should be some messages missing due to disconnection.");
            sending = Task.Run(async() => await SendTestMessages());
            sending.Wait();

            //reconnect switches
            Console.WriteLine("Reconnecting...");
            remoteHubSwitch1.AddAdapter(streamAdaptersOnSwitch1);

            //Sending test messages
            sending = Task.Run(async() => await SendTestMessages());
            sending.Wait();
        }
        static void VirtualHostTest(RemoteHubSwitch remoteHubSwitch1, StreamAdapter <byte[]> streamAdaptersOnSwitch1, IRemoteHub <string> sender)
        {
            sender.RemoteClientUpdated += Sender_RemoteClientUpdated;

            //reg
            Console.WriteLine("Please wait for several seconds and press any key to reg virtual hosts...");
            Console.ReadKey(true);

            Guid virtualHostId = Guid.NewGuid();

            foreach (var client in clients)
            {
                client.ApplyVirtualHosts(new KeyValuePair <Guid, VirtualHostSetting>(virtualHostId, new VirtualHostSetting(0, 1)));
            }
            //clients[0].ApplyVirtualHosts(new KeyValuePair<Guid, VirtualHostSetting>(virtualHostId, new VirtualHostSetting(1, 1))); //this command will add higher setting on only clients[0] which will suppress all other clients on the same virtual host.

            Console.WriteLine("Please wait for several seconds and press any key to continue sending test...");
            Console.ReadKey(true);

            //send
            for (int i = 0; i < 100; i++)
            {
                if (sender.TryResolveVirtualHost(virtualHostId, out var hostId))
                {
                    string testMessage = string.Format("<-- {0:D2}: To Virtual Host on {1} -->", i, clientNames[hostId]);
                    waitingTexts.Add(testMessage);
                    sender.SendMessage(hostId, testMessage);
                }
            }

            Console.WriteLine("Press any key to continue...");
            Console.ReadKey(true);
            if (waitingTexts.Count > 0)
            {
                foreach (var text in waitingTexts)
                {
                    Console.WriteLine("Missing message: " + text);
                }
                waitingTexts.Clear();
            }
            else
            {
                Console.WriteLine("All message received.");
            }

            //disconnect switch
            Console.WriteLine("Disconnecting...");
            remoteHubSwitch1.RemoveAdapter(streamAdaptersOnSwitch1);

            //add switch direct link
            Console.WriteLine("Adding client...");
            RemoteHubSwitchDirect <string> clientDirect = new RemoteHubSwitchDirect <string>(Guid.NewGuid(), Received);

            adapterNamesForSwitch1[clientDirect] = "To SwitchDirect"; //name the new created adapter as To SwitchDirect
            clients.Add(clientDirect);
            clientNames.Add(clientDirect.ClientId, "SwitchDirect");   //name the client as SwitchDirect
            remoteHubSwitch1.AddAdapter(clientDirect);

            //set another virtual host
            clientDirect.ApplyVirtualHosts(new KeyValuePair <Guid, VirtualHostSetting>(virtualHostId, new VirtualHostSetting(0, 3)));
            Console.WriteLine("Please wait a while for client (SwitchDirect) discovery and press any key to continue...");

            //resume switch connection
            Console.WriteLine("Reconnecting...");
            remoteHubSwitch1.AddAdapter(streamAdaptersOnSwitch1);

            Console.ReadKey(true);
            //send
            for (int i = 0; i < 100; i++)
            {
                if (sender.TryResolveVirtualHost(virtualHostId, out var hostId))
                {
                    string testMessage = string.Format("<-- {0:D2}: To Virtual Host on {1} -->", i, clientNames[hostId]);
                    waitingTexts.Add(testMessage);
                    sender.SendMessage(hostId, testMessage);
                }
            }

            Console.WriteLine("Press any key to continue...");
            Console.ReadKey(true);

            if (waitingTexts.Count > 0)
            {
                foreach (var text in waitingTexts)
                {
                    Console.WriteLine("Missing message: " + text);
                }
                waitingTexts.Clear();
            }
            else
            {
                Console.WriteLine("All message received.");
            }
        }
예제 #4
0
        static void AddRemoveClientTest(RemoteHubSwitch remoteHubSwitch2)
        {
            //adding a new client to Redis
            string redisConnectionString = "localhost";                                                    //define redis connection string

            clients.Add(new RemoteHubOverRedis <string>(Guid.NewGuid(), redisConnectionString, Received)); //create one new client to redis and add it to the clients list
            clientNames.Add(clients[4].ClientId, "New Redis Client");                                      //name the client as New Redis Client
            clients[4].Start();                                                                            //start the new created client

            //adding a new client and a connected adapter in pair to Switch 2
            TcpListener tcpListener = new TcpListener(IPAddress.Any, 60005);     //open one tcp listener

            tcpListener.Start();                                                 //start tcp listener
            Task <TcpClient> acceptingTask = tcpListener.AcceptTcpClientAsync(); //waiting for connection

            TcpClient[] tcpClients = new TcpClient[]                             //prepare 2 tcp links, connected in pair
            {
                new TcpClient("localhost", 60005),
                acceptingTask.Result
            };
            tcpListener.Stop();                                                                                                         //stop listener
            NetworkStream[]        streamsOfTcpClients    = Array.ConvertAll(tcpClients, i => i.GetStream());                           //get network streams from tcp links.
            StreamAdapter <byte[]> streamAdapterOnSwitch2 = new StreamAdapter <byte[]>(streamsOfTcpClients[0], streamsOfTcpClients[0]); //create adapter from one stream

            adapterNamesForSwitch2[streamAdapterOnSwitch2] = "To New Stream Client";                                                    //name the new created adapter as To New Stream Client
            clients.Add(new RemoteHubOverStream <string>(Guid.NewGuid(), streamsOfTcpClients[1], streamsOfTcpClients[1], Received));    //create one client based on the other stream which is connected to the stream adapter.
            clientNames.Add(clients[5].ClientId, "New Stream Client");                                                                  //name the client as New Stream Client
            clients[5].Start();                                                                                                         //start the new created client
            remoteHubSwitch2.AddAdapter(streamAdapterOnSwitch2);                                                                        //add the switch adapter to Switch 2
            Console.WriteLine("Please wait a while for clients (New Stream Client & New Redis Client) discovery.");

            //Sending test messages
            Task sending = Task.Run(async() => await SendTestMessages());

            sending.Wait();

            //removing the new added clients
            remoteHubSwitch2.RemoveAdapter(streamAdapterOnSwitch2);
            streamAdapterOnSwitch2.Stop();
            streamAdapterOnSwitch2.Dispose();
            adapterNamesForSwitch2.Remove(streamAdapterOnSwitch2);
            clients[5].Stop();
            ((IDisposable)clients[5]).Dispose();
            clients.RemoveAt(5);
            clients[4].Stop();
            ((IDisposable)clients[4]).Dispose();
            clients.RemoveAt(4);
            foreach (var stream in streamsOfTcpClients)
            {
                stream.Close();
                stream.Dispose();
            }
            foreach (var tcpClient in tcpClients)
            {
                tcpClient.Close();
                tcpClient.Dispose();
            }

            Console.WriteLine("Please wait a while for clients (New Stream Client & New Redis Client) removal.");

            //Sending test messages
            sending = Task.Run(async() => await SendTestMessages());
            sending.Wait();
        }