public void TestStreamingTransportServer()
        {
            BlockingCollection <string> queue    = new BlockingCollection <string>();
            List <string>            events      = new List <string>();
            IStreamingCodec <string> stringCodec = _injector.GetInstance <StringStreamingCodec>();

            IPEndPoint endpoint      = new IPEndPoint(IPAddress.Any, 0);
            var        remoteHandler = Observer.Create <TransportEvent <string> >(tEvent => queue.Add(tEvent.Data));

            using (
                var server = new StreamingTransportServer <string>(endpoint.Address,
                                                                   remoteHandler,
                                                                   _tcpPortProvider,
                                                                   stringCodec))
            {
                server.Run();

                IPEndPoint remoteEndpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), server.LocalEndpoint.Port);
                using (var client = new StreamingTransportClient <string>(remoteEndpoint, stringCodec, _tcpClientFactory))
                {
                    client.Send("TestStreamingTransportServer - 1");
                    client.Send("TestStreamingTransportServer - 2");
                    client.Send("TestStreamingTransportServer - 3");

                    events.Add(queue.Take());
                    events.Add(queue.Take());
                    events.Add(queue.Take());
                }
            }

            Assert.Equal(3, events.Count);
            Assert.Equal("TestStreamingTransportServer - 1", events[0]);
            Assert.Equal("TestStreamingTransportServer - 2", events[1]);
            Assert.Equal("TestStreamingTransportServer - 3", events[2]);
        }
Exemple #2
0
        public void TestStreamingTransportSenderStage()
        {
            IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, 0);

            List <string> events = new List <string>();
            BlockingCollection <string> queue       = new BlockingCollection <string>();
            IStreamingCodec <string>    stringCodec = _injector.GetInstance <StringStreamingCodec>();

            // Server echoes the message back to the client
            var remoteHandler = Observer.Create <TransportEvent <string> >(tEvent => tEvent.Link.Write(tEvent.Data));

            using (var server = new StreamingTransportServer <string>(endpoint.Address, remoteHandler, _tcpPortProvider, stringCodec))
            {
                server.Run();

                var        clientHandler  = Observer.Create <TransportEvent <string> >(tEvent => queue.Add(tEvent.Data));
                IPEndPoint remoteEndpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), server.LocalEndpoint.Port);
                using (var client = new StreamingTransportClient <string>(remoteEndpoint, clientHandler, stringCodec))
                {
                    client.Send("Hello");
                    client.Send(", ");
                    client.Send(" World");

                    events.Add(queue.Take());
                    events.Add(queue.Take());
                    events.Add(queue.Take());
                }
            }

            Assert.Equal(3, events.Count);
            Assert.Equal(events[0], "Hello");
            Assert.Equal(events[1], ", ");
            Assert.Equal(events[2], " World");
        }
Exemple #3
0
        public async Task TestCodecToStreamingCodec()
        {
            var config = TangFactory.GetTang().NewConfigurationBuilder()
                         .BindImplementation(GenericType <ICodec <int> > .Class, GenericType <IntCodec> .Class)
                         .BindImplementation(GenericType <IStreamingCodec <int> > .Class,
                                             GenericType <CodecToStreamingCodec <int> > .Class)
                         .Build();

            IStreamingCodec <int> streamingCodec =
                TangFactory.GetTang().NewInjector(config).GetInstance <IStreamingCodec <int> >();

            CancellationToken token = new CancellationToken();

            int         obj    = 5;
            var         stream = new MemoryStream();
            IDataWriter writer = new StreamDataWriter(stream);

            streamingCodec.Write(obj, writer);
            await streamingCodec.WriteAsync(obj + 1, writer, token);

            stream.Position = 0;
            IDataReader reader = new StreamDataReader(stream);
            int         res1   = streamingCodec.Read(reader);
            int         res2   = await streamingCodec.ReadAsync(reader, token);

            Assert.Equal(obj, res1);
            Assert.Equal(obj + 1, res2);
        }
        public void TestStreamingCommunicationThreeNodesOneWay()
        {
            IPAddress listeningAddress = IPAddress.Parse("127.0.0.1");

            BlockingCollection <string> queue = new BlockingCollection <string>();
            List <string>            events   = new List <string>();
            IStreamingCodec <string> codec    = TangFactory.GetTang().NewInjector().GetInstance <StringStreamingCodec>();

            using (var remoteManager1 = _remoteManagerFactory1.GetInstance <string>(listeningAddress, codec))
                using (var remoteManager2 = _remoteManagerFactory1.GetInstance <string>(listeningAddress, codec))
                    using (var remoteManager3 = _remoteManagerFactory1.GetInstance <string>(listeningAddress, codec))
                    {
                        var remoteEndpoint = new IPEndPoint(listeningAddress, 0);
                        var observer       = Observer.Create <string>(queue.Add);
                        remoteManager3.RegisterObserver(remoteEndpoint, observer);

                        var remoteObserver1 = remoteManager1.GetRemoteObserver(remoteManager3.LocalEndpoint);
                        var remoteObserver2 = remoteManager2.GetRemoteObserver(remoteManager3.LocalEndpoint);

                        remoteObserver2.OnNext("abc");
                        remoteObserver1.OnNext("def");
                        remoteObserver2.OnNext("ghi");
                        remoteObserver1.OnNext("jkl");
                        remoteObserver2.OnNext("mno");

                        for (int i = 0; i < 5; i++)
                        {
                            events.Add(queue.Take());
                        }
                    }

            Assert.Equal(5, events.Count);
        }
        public void TestStreamingRegisterObserverByType()
        {
            IPAddress listeningAddress = IPAddress.Parse("127.0.0.1");

            BlockingCollection <string> queue = new BlockingCollection <string>();
            List <string> events = new List <string>();

            IStreamingCodec <string> codec = TangFactory.GetTang().NewInjector().GetInstance <StringStreamingCodec>();

            using (var remoteManager1 = _remoteManagerFactory1.GetInstance <string>(listeningAddress, codec))
                using (var remoteManager2 = _remoteManagerFactory1.GetInstance <string>(listeningAddress, codec))
                {
                    // RemoteManager2 listens and records events of type IRemoteEvent<string>
                    var observer = Observer.Create <IRemoteMessage <string> >(message => queue.Add(message.Message));
                    remoteManager2.RegisterObserver(observer);

                    // Remote manager 1 sends 3 events to remote manager 2
                    var remoteObserver = remoteManager1.GetRemoteObserver(remoteManager2.LocalEndpoint);
                    remoteObserver.OnNext("abc");
                    remoteObserver.OnNext("def");
                    remoteObserver.OnNext("ghi");

                    events.Add(queue.Take());
                    events.Add(queue.Take());
                    events.Add(queue.Take());
                }

            Assert.Equal(3, events.Count);
        }
        /// <summary>
        /// Constructs a DefaultRemoteManager listening on the specified address and
        /// a specific port.
        /// </summary>
        /// <param name="localAddress">The address to listen on</param>
        /// <param name="tcpPortProvider">Tcp port provider</param>
        /// <param name="streamingCodec">Streaming codec</param>
        /// <param name="tcpClientFactory">provides TcpClient for given endpoint</param>
        internal StreamingRemoteManager(IPAddress localAddress,
                                        ITcpPortProvider tcpPortProvider,
                                        IStreamingCodec <T> streamingCodec,
                                        ITcpClientConnectionFactory tcpClientFactory)
        {
            if (localAddress == null)
            {
                throw new ArgumentNullException("localAddress");
            }

            _tcpClientFactory  = tcpClientFactory;
            _observerContainer = new ObserverContainer <T>();
            _cachedClients     = new Dictionary <IPEndPoint, ProxyObserver>();
            _remoteEventCodec  = new RemoteEventStreamingCodec <T>(streamingCodec);

            // Begin to listen for incoming messages
            _server = new StreamingTransportServer <IRemoteEvent <T> >(localAddress,
                                                                       _observerContainer,
                                                                       tcpPortProvider,
                                                                       _remoteEventCodec);
            _server.Run();

            LocalEndpoint = _server.LocalEndpoint;
            Identifier    = new SocketRemoteIdentifier(LocalEndpoint);
        }
        public void TestStreamingOneWayCommunication()
        {
            IPAddress listeningAddress = IPAddress.Parse("127.0.0.1");

            BlockingCollection <string> queue = new BlockingCollection <string>();
            List <string>            events   = new List <string>();
            IStreamingCodec <string> codec    = TangFactory.GetTang().NewInjector().GetInstance <StringStreamingCodec>();

            using (var remoteManager1 = _remoteManagerFactory1.GetInstance <string>(listeningAddress, codec))
                using (var remoteManager2 = _remoteManagerFactory1.GetInstance <string>(listeningAddress, codec))
                {
                    var        observer  = Observer.Create <string>(queue.Add);
                    IPEndPoint endpoint1 = new IPEndPoint(listeningAddress, 0);
                    remoteManager2.RegisterObserver(endpoint1, observer);

                    var remoteObserver = remoteManager1.GetRemoteObserver(remoteManager2.LocalEndpoint);
                    remoteObserver.OnNext("abc");
                    remoteObserver.OnNext("def");
                    remoteObserver.OnNext("ghi");

                    events.Add(queue.Take());
                    events.Add(queue.Take());
                    events.Add(queue.Take());
                }

            Assert.Equal(3, events.Count);
        }
        public void TestMapInputWithControlMessageCodec()
        {
            float[] baseMessage = { 0, 1 };

            var config = TangFactory.GetTang().NewConfigurationBuilder()
                         .BindImplementation(GenericType <IStreamingCodec <float[]> > .Class,
                                             GenericType <FloatArrayStreamingCodec> .Class)
                         .Build();

            IStreamingCodec <MapInputWithControlMessage <float[]> > codec =
                TangFactory.GetTang().NewInjector(config).GetInstance <MapInputWithControlMessageCodec <float[]> >();

            MemoryStream stream = new MemoryStream();
            IDataWriter  writer = new StreamDataWriter(stream);

            codec.Write(new MapInputWithControlMessage <float[]>(baseMessage, MapControlMessage.AnotherRound), writer);
            codec.Write(new MapInputWithControlMessage <float[]>(MapControlMessage.Stop), writer);

            stream.Position = 0;
            IDataReader reader = new StreamDataReader(stream);

            var message1 = codec.Read(reader);
            var message2 = codec.Read(reader);

            Assert.Equal(message1.Message[0], baseMessage[0]);
            Assert.Equal(message1.Message[1], baseMessage[1]);
            Assert.Null(message2.Message);
            Assert.Equal(message1.ControlMessage, MapControlMessage.AnotherRound);
            Assert.Equal(message2.ControlMessage, MapControlMessage.Stop);
        }
        public async Task TestCodecToStreamingCodecConfiguration()
        {
            var config = CodecToStreamingCodecConfiguration <int> .Conf
                         .Set(CodecToStreamingCodecConfiguration <int> .Codec, GenericType <IntCodec> .Class)
                         .Build();

            IStreamingCodec <PipelineMessage <int> > streamingCodec =
                TangFactory.GetTang().NewInjector(config).GetInstance <IStreamingCodec <PipelineMessage <int> > >();

            CancellationToken token = new CancellationToken();

            int obj = 5;
            PipelineMessage <int> message = new PipelineMessage <int>(obj, true);
            var         stream            = new MemoryStream();
            IDataWriter writer            = new StreamDataWriter(stream);

            streamingCodec.Write(message, writer);
            PipelineMessage <int> message1 = new PipelineMessage <int>(obj + 1, false);
            await streamingCodec.WriteAsync(message1, writer, token);

            stream.Position = 0;
            IDataReader reader = new StreamDataReader(stream);
            var         res1   = streamingCodec.Read(reader);
            var         res2   = await streamingCodec.ReadAsync(reader, token);

            Assert.Equal(obj, res1.Data);
            Assert.Equal(obj + 1, res2.Data);
            Assert.Equal(true, res1.IsLast);
            Assert.Equal(false, res2.IsLast);
        }
 /// <summary>
 /// Construct a TransportClient.
 /// Used to send messages to the specified remote endpoint.
 /// </summary>
 /// <param name="remoteEndpoint">The endpoint of the remote server to connect to</param>
 /// <param name="observer">Callback used when receiving responses from remote host</param>
 /// <param name="streamingCodec">Streaming codec</param>
 internal StreamingTransportClient(IPEndPoint remoteEndpoint,
                                   IObserver <TransportEvent <T> > observer,
                                   IStreamingCodec <T> streamingCodec)
     : this(remoteEndpoint, streamingCodec)
 {
     _observer = observer;
     Task.Run(() => ResponseLoop());
 }
Exemple #11
0
        /// <summary>
        /// Construct a TransportClient.
        /// Used to send messages to the specified remote endpoint.
        /// </summary>
        /// <param name="remoteEndpoint">The endpoint of the remote server to connect to</param>
        /// <param name="streamingCodec">Streaming codec</param>
        /// <param name="clientFactory">TcpClient factory</param>
        internal StreamingTransportClient(IPEndPoint remoteEndpoint, IStreamingCodec <T> streamingCodec, ITcpClientConnectionFactory clientFactory)
        {
            Exceptions.ThrowIfArgumentNull(remoteEndpoint, "remoteEndpoint", Logger);

            _link = new StreamingLink <T>(remoteEndpoint, streamingCodec, clientFactory);
            _cancellationSource = new CancellationTokenSource();
            _disposed           = false;
        }
Exemple #12
0
 /// <summary>
 /// Construct a TransportClient.
 /// Used to send messages to the specified remote endpoint.
 /// </summary>
 /// <param name="remoteEndpoint">The endpoint of the remote server to connect to</param>
 /// <param name="observer">Callback used when receiving responses from remote host</param>
 /// <param name="streamingCodec">Streaming codec</param>
 /// <param name="clientFactory">TcpClient factory</param>
 internal StreamingTransportClient(IPEndPoint remoteEndpoint,
                                   IObserver <TransportEvent <T> > observer,
                                   IStreamingCodec <T> streamingCodec,
                                   ITcpClientConnectionFactory clientFactory)
     : this(remoteEndpoint, streamingCodec, clientFactory)
 {
     _observer = observer;
     Task.Run(() => ResponseLoop());
 }
Exemple #13
0
        public void TestDoubleArrayStreamingCodecsNullException()
        {
            IInjector injector = TangFactory.GetTang().NewInjector();
            IStreamingCodec <double[]> doubleArrCodec = injector.GetInstance <DoubleArrayStreamingCodec>();
            var         stream = new MemoryStream();
            IDataWriter writer = new StreamDataWriter(stream);

            Assert.Throws <ArgumentNullException>(() => doubleArrCodec.Write(null, writer));
        }
 /// <summary>
 /// Construct a TransportClient.
 /// Used to send messages to the specified remote endpoint.
 /// </summary>
 /// <param name="remoteEndpoint">The endpoint of the remote server to connect to</param>
 /// <param name="observer">Callback used when receiving responses from remote host</param>
 /// <param name="streamingCodec">Streaming codec</param>
 /// <param name="clientFactory">TcpClient factory</param>
 internal StreamingTransportClient(IPEndPoint remoteEndpoint,
                                   IObserver <TransportEvent <T> > observer,
                                   IStreamingCodec <T> streamingCodec,
                                   ITcpClientConnectionFactory clientFactory)
     : this(remoteEndpoint, streamingCodec, clientFactory)
 {
     _observer = observer;
     Task.Factory.StartNew(() => ResponseLoop(), TaskCreationOptions.LongRunning);
 }
        public void TestFloatArrayStreamingCodecsNullException()
        {
            IInjector injector = TangFactory.GetTang().NewInjector();
            IStreamingCodec <float[]> floatArrCodec = injector.GetInstance <FloatArrayStreamingCodec>();
            var         stream = new MemoryStream();
            IDataWriter writer = new StreamDataWriter(stream);

            floatArrCodec.Write(null, writer);
        }
        public void TestStreamingRaceCondition()
        {
            BlockingCollection <string> queue    = new BlockingCollection <string>();
            List <string>            events      = new List <string>();
            IStreamingCodec <string> stringCodec = _injector.GetInstance <StringStreamingCodec>();
            int numEventsExpected = 150;

            IPEndPoint endpoint      = new IPEndPoint(IPAddress.Any, 0);
            var        remoteHandler = Observer.Create <TransportEvent <string> >(tEvent => queue.Add(tEvent.Data));

            using (
                var server = new StreamingTransportServer <string>(endpoint.Address,
                                                                   remoteHandler,
                                                                   _tcpPortProvider,
                                                                   stringCodec))
            {
                server.Run();

                int counter = 0;
                for (int i = 0; i < numEventsExpected / 3; i++)
                {
                    Task.Run(() =>
                    {
                        IPEndPoint remoteEndpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"),
                                                                   server.LocalEndpoint.Port);
                        using (
                            var client = new StreamingTransportClient <string>(remoteEndpoint,
                                                                               stringCodec,
                                                                               _tcpClientFactory))
                        {
                            for (int j = 0; j < 3; j++)
                            {
                                client.Send("TestStreamingRaceCondition - " + counter++);
                            }
                        }
                    });
                }

                for (int i = 0; i < numEventsExpected; i++)
                {
                    string e;
                    do
                    {
                        e = queue.Take();
                    }while (e == null);

                    events.Add(e);
                }
            }

            Assert.Equal(numEventsExpected, events.Count);
            foreach (var e in events)
            {
                Assert.True(e.StartsWith("TestStreamingRaceCondition - "), "Unexpected event [" + e + "]");
            }
        }
Exemple #17
0
 private WriteResultHandler(
     IStreamingCodec <TResult> resultCodec,
     IFileSystem fileSystem,
     [Parameter(typeof(ResultOutputLocation))] string fileName)
 {
     _resultCodec    = resultCodec;
     _fileSystem     = fileSystem;
     _remoteFileName = fileName;
     _localFilename  = GenerateLocalFilename();
 }
Exemple #18
0
 private IMRURunner(MapFunctions <TMapInput, TMapOutput> mapfunctions,
                    IReduceFunction <TMapOutput> reduceTask,
                    IUpdateFunction <TMapInput, TMapOutput, TResult> updateTask,
                    InputCodecWrapper <TMapInput> mapInputCodec,
                    OutputCodecWrapper <TMapOutput> mapOutputCodec)
 {
     _mapfunctions   = mapfunctions.Mappers;
     _reduceTask     = reduceTask;
     _updateTask     = updateTask;
     _mapInputCodec  = mapInputCodec.Codec;
     _mapOutputCodec = mapOutputCodec.Codec;
 }
 /// <summary>
 /// Create new CommunicationGroupMessage.
 /// </summary>
 /// <param name="groupName">The name of the communication group</param>
 /// <param name="operatorName">The name of the MPI operator</param>
 /// <param name="source">The message source</param>
 /// <param name="destination">The message destination</param>
 /// <param name="message">The actual Writable message array</param>
 /// <param name="messageType">The type of message to send</param>
 /// <param name="codec">Streaming Codec</param>
 public GroupCommunicationMessage(
     string groupName,
     string operatorName,
     string source,
     string destination,
     T[] message,
     MessageType messageType,
     IStreamingCodec <T> codec)
     : base(groupName, operatorName, source, destination, messageType)
 {
     _codec = codec;
     Data   = message;
 }
Exemple #20
0
 /// <summary>
 /// Constructs a TransportServer to listen for remote events.
 /// Listens on the specified remote endpoint.  When it receives a remote
 /// event, it will invoke the specified remote handler.
 /// </summary>
 /// <param name="address">Endpoint address to listen on</param>
 /// <param name="remoteHandler">The handler to invoke when receiving incoming
 /// remote messages</param>
 /// <param name="tcpPortProvider">Find port numbers if listenport is 0</param>
 /// <param name="streamingCodec">Streaming codec</param>
 internal StreamingTransportServer(
     IPAddress address,
     IObserver <TransportEvent <T> > remoteHandler,
     ITcpPortProvider tcpPortProvider,
     IStreamingCodec <T> streamingCodec)
 {
     _listener           = new TcpListener(address, 0);
     _remoteObserver     = remoteHandler;
     _tcpPortProvider    = tcpPortProvider;
     _cancellationSource = new CancellationTokenSource();
     _cancellationSource.Token.ThrowIfCancellationRequested();
     _streamingCodec = streamingCodec;
     _disposed       = false;
 }
Exemple #21
0
        /// <summary>
        /// Constructs a Link object.
        /// Connects to the specified remote endpoint.
        /// </summary>
        /// <param name="remoteEndpoint">The remote endpoint to connect to</param>
        /// <param name="streamingCodec">Streaming codec</param>
        /// <param name="tcpClientFactory">TcpClient factory</param>
        internal StreamingLink(IPEndPoint remoteEndpoint, IStreamingCodec <T> streamingCodec, ITcpClientConnectionFactory tcpClientFactory)
        {
            if (remoteEndpoint == null)
            {
                throw new ArgumentNullException("remoteEndpoint");
            }

            _client = tcpClientFactory.Connect(remoteEndpoint);
            var stream = _client.GetStream();

            _localEndpoint  = GetLocalEndpoint();
            _disposed       = false;
            _reader         = new StreamDataReader(stream);
            _writer         = new StreamDataWriter(stream);
            _streamingCodec = streamingCodec;
        }
Exemple #22
0
        /// <summary>
        /// Constructs a Link object.
        /// Uses the already connected TcpClient.
        /// </summary>
        /// <param name="client">The already connected client</param>
        /// <param name="streamingCodec">Streaming codec</param>
        internal StreamingLink(TcpClient client, IStreamingCodec <T> streamingCodec)
        {
            if (client == null)
            {
                throw new ArgumentNullException("client");
            }

            _client = client;
            var stream = _client.GetStream();

            _localEndpoint  = GetLocalEndpoint();
            _disposed       = false;
            _reader         = new StreamDataReader(stream);
            _writer         = new StreamDataWriter(stream);
            _streamingCodec = streamingCodec;
        }
        public void TestStreamingTwoWayCommunication()
        {
            IPAddress listeningAddress = IPAddress.Parse("127.0.0.1");

            BlockingCollection <string> queue1 = new BlockingCollection <string>();
            BlockingCollection <string> queue2 = new BlockingCollection <string>();
            List <string> events1 = new List <string>();
            List <string> events2 = new List <string>();

            IStreamingCodec <string> codec = TangFactory.GetTang().NewInjector().GetInstance <StringStreamingCodec>();

            using (var remoteManager1 = _remoteManagerFactory1.GetInstance <string>(listeningAddress, codec))
                using (var remoteManager2 = _remoteManagerFactory1.GetInstance <string>(listeningAddress, codec))
                {
                    // Register observers for remote manager 1 and remote manager 2
                    var remoteEndpoint = new IPEndPoint(listeningAddress, 0);
                    var observer1      = Observer.Create <string>(queue1.Add);
                    var observer2      = Observer.Create <string>(queue2.Add);
                    remoteManager1.RegisterObserver(remoteEndpoint, observer1);
                    remoteManager2.RegisterObserver(remoteEndpoint, observer2);

                    // Remote manager 1 sends 3 events to remote manager 2
                    var remoteObserver1 = remoteManager1.GetRemoteObserver(remoteManager2.LocalEndpoint);
                    remoteObserver1.OnNext("abc");
                    remoteObserver1.OnNext("def");
                    remoteObserver1.OnNext("ghi");

                    // Remote manager 2 sends 4 events to remote manager 1
                    var remoteObserver2 = remoteManager2.GetRemoteObserver(remoteManager1.LocalEndpoint);
                    remoteObserver2.OnNext("jkl");
                    remoteObserver2.OnNext("mno");
                    remoteObserver2.OnNext("pqr");
                    remoteObserver2.OnNext("stu");

                    events1.Add(queue1.Take());
                    events1.Add(queue1.Take());
                    events1.Add(queue1.Take());
                    events1.Add(queue1.Take());

                    events2.Add(queue2.Take());
                    events2.Add(queue2.Take());
                    events2.Add(queue2.Take());
                }

            Assert.Equal(4, events1.Count);
            Assert.Equal(3, events2.Count);
        }
Exemple #24
0
        /// <summary>
        /// Constructs a Link object.
        /// Uses the already connected TcpClient.
        /// </summary>
        /// <param name="client">The already connected client</param>
        /// <param name="streamingCodec">Streaming codec</param>
        internal StreamingLink(TcpClient client, IStreamingCodec <T> streamingCodec)
        {
            if (client == null)
            {
                throw new ArgumentNullException("client");
            }

            _client = client;
            var stream = _client.GetStream();

            _localEndpoint  = GetLocalEndpoint();
            _disposed       = false;
            _reader         = new StreamDataReader(stream);
            _writer         = new StreamDataWriter(stream);
            _streamingCodec = streamingCodec;
            Logger.Log(Level.Info, string.Format("Remote IP is: {0} with port: {1}", RemoteEndpoint.Address, RemoteEndpoint.Port));
        }
 /// <summary>
 /// Construct a TransportClient.
 /// Used to send messages to the specified remote endpoint.
 /// </summary>
 /// <param name="remoteEndpoint">The endpoint of the remote server to connect to</param>
 /// <param name="observer">Callback used when receiving responses from remote host</param>
 /// <param name="streamingCodec">Streaming codec</param>
 /// <param name="clientFactory">TcpClient factory</param>
 internal StreamingTransportClient(IPEndPoint remoteEndpoint,
                                   IObserver <TransportEvent <T> > observer,
                                   IStreamingCodec <T> streamingCodec,
                                   ITcpClientConnectionFactory clientFactory)
     : this(remoteEndpoint, streamingCodec, clientFactory)
 {
     _observer = observer;
     try
     {
         Task.Factory.StartNew(() => ResponseLoop(), TaskCreationOptions.LongRunning);
     }
     catch (Exception e)
     {
         Logger.Log(Level.Warning, "StreamingTransportClient get exception from ResponseLoop: {0}.", e.GetType());
         throw e;
     }
 }
        public void TestStreamingRaceCondition()
        {
            BlockingCollection <string> queue    = new BlockingCollection <string>();
            List <string>            events      = new List <string>();
            IStreamingCodec <string> stringCodec = _injector.GetInstance <StringStreamingCodec>();
            int numEventsExpected = 150;

            IPEndPoint endpoint      = new IPEndPoint(IPAddress.Any, 0);
            var        remoteHandler = Observer.Create <TransportEvent <string> >(tEvent => queue.Add(tEvent.Data));

            using (
                var server = new StreamingTransportServer <string>(endpoint.Address,
                                                                   remoteHandler,
                                                                   _tcpPortProvider,
                                                                   stringCodec))
            {
                server.Run();

                for (int i = 0; i < numEventsExpected / 3; i++)
                {
                    Task.Run(() =>
                    {
                        IPEndPoint remoteEndpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"),
                                                                   server.LocalEndpoint.Port);
                        using (
                            var client = new StreamingTransportClient <string>(remoteEndpoint,
                                                                               stringCodec,
                                                                               _tcpClientFactory))
                        {
                            client.Send("Hello");
                            client.Send(", ");
                            client.Send("World!");
                        }
                    });
                }

                for (int i = 0; i < numEventsExpected; i++)
                {
                    events.Add(queue.Take());
                }
            }

            Assert.Equal(numEventsExpected, events.Count);
        }
Exemple #27
0
        private OperatorTopology(
            [Parameter(typeof(GroupCommConfigurationOptions.OperatorName))] string operatorName,
            [Parameter(typeof(GroupCommConfigurationOptions.CommunicationGroupName))] string groupName,
            [Parameter(typeof(TaskConfigurationOptions.Identifier))] string taskId,
            [Parameter(typeof(GroupCommConfigurationOptions.DriverId))] string driverId,
            [Parameter(typeof(GroupCommConfigurationOptions.Timeout))] int timeout,
            [Parameter(typeof(GroupCommConfigurationOptions.RetryCount))] int retryCount,
            [Parameter(typeof(GroupCommConfigurationOptions.TopologyRootTaskId))] string rootId,
            [Parameter(typeof(GroupCommConfigurationOptions.TopologyChildTaskIds))] ISet <string> childIds,
            WritableNetworkService <GeneralGroupCommunicationMessage> networkService,
            Sender sender,
            IStreamingCodec <T> codec)
        {
            _operatorName  = operatorName;
            _groupName     = groupName;
            _selfId        = taskId;
            _driverId      = driverId;
            _timeout       = timeout;
            _retryCount    = retryCount;
            _nameClient    = networkService.NamingClient;
            _sender        = sender;
            _nodesWithData = new BlockingCollection <NodeStruct <T> >();
            _children      = new List <NodeStruct <T> >();
            _idToNodeMap   = new Dictionary <string, NodeStruct <T> >();
            _codec         = codec;

            if (_selfId.Equals(rootId))
            {
                _parent = null;
            }
            else
            {
                _parent = new NodeStruct <T>(rootId);
                _idToNodeMap[rootId] = _parent;
            }
            foreach (var childId in childIds)
            {
                var node = new NodeStruct <T>(childId);
                _children.Add(node);
                _idToNodeMap[childId] = node;
            }
        }
        public void TestConfigurationBroadcastSpec()
        {
            FlatTopology <int> topology = new FlatTopology <int>("Operator", "Operator", "task1", "driverid",
                                                                 new BroadcastOperatorSpec("Sender", GetDefaultCodecConfig(), GetDefaultDataConverterConfig()));

            topology.AddTask("task1");
            var conf = topology.GetTaskConfiguration("task1");

            IStreamingCodec <int> codec = TangFactory.GetTang().NewInjector(conf).GetInstance <IStreamingCodec <int> >();

            var         stream = new MemoryStream();
            IDataWriter writer = new StreamDataWriter(stream);

            codec.Write(3, writer);
            stream.Position = 0;
            IDataReader reader = new StreamDataReader(stream);
            int         res    = codec.Read(reader);

            Assert.Equal(3, res);
        }
        public void TestStreamingRemoteSenderCallback()
        {
            IPAddress listeningAddress = IPAddress.Parse("127.0.0.1");

            BlockingCollection <string> queue = new BlockingCollection <string>();
            List <string> events = new List <string>();

            IStreamingCodec <string> codec = TangFactory.GetTang().NewInjector().GetInstance <StringStreamingCodec>();

            using (var remoteManager1 = _remoteManagerFactory1.GetInstance <string>(listeningAddress, codec))
                using (var remoteManager2 = _remoteManagerFactory1.GetInstance <string>(listeningAddress, codec))
                {
                    // Register handler for when remote manager 2 receives events; respond
                    // with an ack
                    var remoteEndpoint  = new IPEndPoint(listeningAddress, 0);
                    var remoteObserver2 = remoteManager2.GetRemoteObserver(remoteManager1.LocalEndpoint);

                    var receiverObserver = Observer.Create <string>(
                        message => remoteObserver2.OnNext("received message: " + message));
                    remoteManager2.RegisterObserver(remoteEndpoint, receiverObserver);

                    // Register handler for remote manager 1 to record the ack
                    var senderObserver = Observer.Create <string>(queue.Add);
                    remoteManager1.RegisterObserver(remoteEndpoint, senderObserver);

                    // Begin to send messages
                    var remoteObserver1 = remoteManager1.GetRemoteObserver(remoteManager2.LocalEndpoint);
                    remoteObserver1.OnNext("hello");
                    remoteObserver1.OnNext("there");
                    remoteObserver1.OnNext("buddy");

                    events.Add(queue.Take());
                    events.Add(queue.Take());
                    events.Add(queue.Take());
                }

            Assert.Equal(3, events.Count);
            Assert.Equal("received message: hello", events[0]);
            Assert.Equal("received message: there", events[1]);
            Assert.Equal("received message: buddy", events[2]);
        }
 private StreamingPipelineMessageCodec(IStreamingCodec <T> baseCodec)
 {
     BaseCodec = baseCodec;
 }