Exemple #1
0
        public void Init()
        {
            mailResponder = new MailResponder();

            server = new SocketServer("localhost", 0, mailResponder);
            server.Start();

            transceiver = new SocketTransceiver("localhost", server.Port);
            proxy = new GenericRequestor(transceiver, MailResponder.Protocol);
        }
        private void AcceptCallback(IAsyncResult ar)
        {
            // Signal the main thread to continue.
            allDone.Set();

            // Get the socket that handles the client request.
            var listener = (Socket)ar.AsyncState;

            if (cancellationRequested)
            {
                return;
            }

            Socket socket = listener.EndAccept(ar);

            AddSocket(socket);

            // Create the state object.
            var xc = new SocketTransceiver(socket);

            while (true)
            {
                try
                {
                    IList <MemoryStream> request  = xc.ReadBuffers();
                    IList <MemoryStream> response = responder.Respond(request, xc);
                    xc.WriteBuffers(response);
                }
                catch (ObjectDisposedException)
                {
                    break;
                }
                catch (SocketException)
                {
                    break;
                }
                catch (AvroRuntimeException)
                {
                    break;
                }
                catch (Exception)
                {
                    break;
                }
            }

            try
            {
                xc.Disconnect();
            }
            catch (Exception) { }

            RemoveSocket(socket);
        }
        // AVRO-625 [Test] 
        // Currently, SocketTransceiver does not permit out-of-order requests on a stateful connection.
        public void Test()
        {
            var waitLatch = new CountdownLatch(1);
            var simpleResponder = new SimpleResponder(waitLatch);
            server = new SocketServer("localhost", 0, simpleResponder);

            server.Start();

            int port = server.Port;

            transceiver = new SocketTransceiver("localhost", port);
            proxy = new GenericRequestor(transceiver, SimpleResponder.Protocol);

            // Step 1:
            proxy.GetRemote(); // force handshake

            new Thread(x =>
                           {
                               // Step 2a:
                               waitLatch.Wait();

                               var ack = new GenericRecord(SimpleResponder.Protocol.Messages["ack"].Request);
                               // Step 2b:
                               proxy.Request("ack", ack);

                           }).Start();


            /*
             * 3. Execute the Client.hello("wait") RPC, which will block until the
             *    Client.ack() call has completed in the background thread.
             */

            var request = new GenericRecord(SimpleResponder.Protocol.Messages["hello"].Request);
            request.Add("greeting", "wait");

            var response = (string)proxy.Request("hello", request);

            // 4. If control reaches here, both RPCs have executed concurrently
            Assert.AreEqual("wait", response); 
        }
Exemple #4
0
 public Client(string hostname, int serverport)
 {
     var transceiver = new SocketTransceiver(hostname, serverport);
     _serverProxy = SpecificRequestor.CreateClient<HelloAvroProtocolCallback>(transceiver);
     _rng = new Random(); // this isn't cryptographically strong
 }
Exemple #5
0
        public void TestMultipleConnectionsCount()
        {
            Reset();

            var transceiver2 = new SocketTransceiver("localhost", server.Port);

            var proxy2 = new GenericRequestor(transceiver2, MailResponder.Protocol);

            FireAndForget(proxy, CreateMessage());
            FireAndForget(proxy2, CreateMessage());
            transceiver2.Disconnect();
        }
        public void TestSocketTransceiverWhenServerStops()
        {
            Responder responder = new SpecificResponder<Mail>(new MailImpl());
            var server = new SocketServer("localhost", 0, responder);

            server.Start();

            var transceiver = new SocketTransceiver("localhost", server.Port);
            var mail = SpecificRequestor.CreateClient<Mail>(transceiver);

            int[] successes = {0};
            int failures = 0;
            int[] quitOnFailure = {0};
            var threads = new List<Thread>();

            // Start a bunch of client threads that use the transceiver to send messages
            for (int i = 0; i < 100; i++)
            {
                var thread = new Thread(
                    () =>
                        {
                            while (true)
                            {
                                try
                                {
                                    mail.send(CreateMessage());
                                    Interlocked.Increment(ref successes[0]);
                                }
                                catch (Exception)
                                {
                                    Interlocked.Increment(ref failures);

                                    if (Interlocked.Add(ref quitOnFailure[0], 0) == 1)
                                    {
                                        return;
                                    }
                                }
                            }
                        });

                thread.Name = "Thread" + i;
                threads.Add(thread);
                thread.Start();
            }

            // Be sure the threads are running: wait until we get a good deal of successes
            while (Interlocked.Add(ref successes[0], 0) < 10000)
            {
                Thread.Sleep(50);
            }

            // Now stop the server
            server.Stop();

            // Server is stopped: successes should not increase anymore: wait until we're in that situation
            while (true)
            {
                int previousSuccesses = Interlocked.Add(ref successes[0], 0);
                Thread.Sleep(500);
                if (previousSuccesses == Interlocked.Add(ref successes[0], 0))
                {
                    break;
                }
            }

            server.Start();

            long now = CurrentTimeMillis();

            int previousSuccesses2 = successes[0];
            while (true)
            {
                Thread.Sleep(500);
                if (successes[0] > previousSuccesses2)
                {
                    break;
                }
                if (CurrentTimeMillis() - now > 5000)
                {
                    Console.WriteLine("FYI: requests don't continue immediately...");
                    break;
                }
            }

            // Stop our client, we would expect this to go on immediately
            Console.WriteLine("Stopping transceiver");

            Interlocked.Add(ref quitOnFailure[0], 1);
            now = CurrentTimeMillis();
            transceiver.Close();

            // Wait for all threads to quit
            while (true)
            {
                threads.RemoveAll(x => !x.IsAlive);

                if (threads.Count > 0)
                    Thread.Sleep(1000);
                else
                    break;
            }

            if (CurrentTimeMillis() - now > 10000)
            {
                Assert.Fail("Stopping NettyTransceiver and waiting for client threads to quit took too long.");
            }
            else
            {
                Console.WriteLine("Stopping NettyTransceiver and waiting for client threads to quit took "
                                  + (CurrentTimeMillis() - now) + " ms");
            }
        }
Exemple #7
0
        private void AcceptCallback(IAsyncResult ar)
        {
            // Signal the main thread to continue.
            allDone.Set();

            // Get the socket that handles the client request.
            var listener = (Socket) ar.AsyncState;

            if (cancellationRequested)
            {
                return;
            }

            Socket socket = listener.EndAccept(ar);
            AddSocket(socket);

            // Create the state object.
            var xc = new SocketTransceiver(socket);

            while (true)
            {
                try
                {
                    IList<MemoryStream> request = xc.ReadBuffers();
                    IList<MemoryStream> response = responder.Respond(request, xc);
                    xc.WriteBuffers(response);
                }
                catch (ObjectDisposedException)
                {
                    break;
                }
                catch (SocketException)
                {
                    break;
                }
                catch (AvroRuntimeException)
                {
                    break;
                }
                catch (Exception)
                {
                    break;
                }
            }

            try
            {
                xc.Disconnect();
            }
            catch (Exception) { }

            RemoveSocket(socket);
        }
Exemple #8
0
        public void Init()
        {
            var mailResponder = new SpecificResponder<All>(new AllImpl());

            server = new SocketServer("localhost", 0, mailResponder);
            server.Start();

            transceiver = new SocketTransceiver("localhost", server.Port);

            simpleClient = SpecificRequestor.CreateClient<All>(transceiver);
        }