public bool Execute(CommandProcessorContext context, string[] args)
        {
            context.IsAsync();

            var streamByCorrId = new Dictionary <Guid, string>();

            var connection = context.Client.CreateTcpConnection(
                context,
                connectionEstablished: conn => { },
                handlePackage: (conn, pkg) => {
                switch (pkg.Command)
                {
                case TcpCommand.SubscriptionConfirmation: {
                    var dto = pkg.Data.Deserialize <TcpClientMessageDto.SubscriptionConfirmation>();
                    context.Log.Info(
                        "Subscription to <{stream}> WAS CONFIRMED! Subscribed at {lastIndexedPosition} ({lastEventNumber})",
                        streamByCorrId[pkg.CorrelationId], dto.LastIndexedPosition, dto.LastEventNumber);
                    break;
                }

                case TcpCommand.StreamEventAppeared: {
                    var dto = pkg.Data.Deserialize <TcpClientMessageDto.StreamEventAppeared>();
                    context.Log.Info("NEW EVENT:\n\n"
                                     + "\tEventStreamId: {stream}\n"
                                     + "\tEventNumber:   {eventNumber}\n"
                                     + "\tEventType:     {eventType}\n"
                                     + "\tData:          {data}\n"
                                     + "\tMetadata:      {metadata}\n",
                                     dto.Event.Event.EventStreamId,
                                     dto.Event.Event.EventNumber,
                                     dto.Event.Event.EventType,
                                     Common.Utils.Helper.UTF8NoBom.GetString(dto.Event.Event.Data ?? new byte[0]),
                                     Common.Utils.Helper.UTF8NoBom.GetString(dto.Event.Event.Metadata ?? new byte[0]));
                    break;
                }

                case TcpCommand.SubscriptionDropped: {
                    pkg.Data.Deserialize <TcpClientMessageDto.SubscriptionDropped>();
                    context.Log.Error("Subscription to <{stream}> WAS DROPPED!",
                                      streamByCorrId[pkg.CorrelationId]);
                    break;
                }

                default:
                    context.Fail(reason: string.Format("Unexpected TCP package: {0}.", pkg.Command));
                    break;
                }
            },
                connectionClosed: (c, error) => {
                if (error == SocketError.Success)
                {
                    context.Success();
                }
                else
                {
                    context.Fail();
                }
            });

            if (args.Length == 0)
            {
                context.Log.Info("SUBSCRIBING TO ALL STREAMS...");
                var  cmd           = new TcpClientMessageDto.SubscribeToStream(string.Empty, resolveLinkTos: false);
                Guid correlationId = Guid.NewGuid();
                streamByCorrId[correlationId] = "$all";
                connection.EnqueueSend(new TcpPackage(TcpCommand.SubscribeToStream, correlationId, cmd.Serialize())
                                       .AsByteArray());
            }
            else
            {
                foreach (var stream in args)
                {
                    context.Log.Info("SUBSCRIBING TO STREAM <{stream}>...", stream);
                    var cmd           = new TcpClientMessageDto.SubscribeToStream(stream, resolveLinkTos: false);
                    var correlationId = Guid.NewGuid();
                    streamByCorrId[correlationId] = stream;
                    connection.EnqueueSend(new TcpPackage(TcpCommand.SubscribeToStream, correlationId, cmd.Serialize())
                                           .AsByteArray());
                }
            }

            context.WaitForCompletion();
            return(true);
        }
Exemple #2
0
        public bool Execute(CommandProcessorContext context, string[] args)
        {
            context.IsAsync();

            var connection = context.Client.CreateTcpConnection(
                context,
                connectionEstablished: conn =>
            {
            },
                handlePackage: (conn, pkg) =>
            {
                switch (pkg.Command)
                {
                case TcpCommand.StreamEventAppeared:
                    {
                        var dto = pkg.Data.Deserialize <TcpClientMessageDto.StreamEventAppeared>();
                        context.Log.Info("NEW EVENT:\n\n"
                                         + "\tEventStreamId: {0}\n"
                                         + "\tEventNumber:   {1}\n"
                                         + "\tEventType:     {2}\n"
                                         + "\tData:          {3}\n"
                                         + "\tMetadata:      {4}\n",
                                         dto.EventStreamId,
                                         dto.EventNumber,
                                         dto.EventType,
                                         Encoding.UTF8.GetString(dto.Data ?? new byte[0]),
                                         Encoding.UTF8.GetString(dto.Metadata ?? new byte[0]));
                        break;
                    }

                case TcpCommand.SubscriptionDropped:
                    {
                        var dto = pkg.Data.Deserialize <TcpClientMessageDto.SubscriptionDropped>();
                        context.Log.Error("Subscription to <{0}> WAS DROPPED!", dto.EventStreamId);
                        break;
                    }

                case TcpCommand.SubscriptionToAllDropped:
                    {
                        var dto = pkg.Data.Deserialize <TcpClientMessageDto.SubscriptionToAllDropped>();
                        context.Log.Error("Subscription to ALL WAS DROPPED!");
                        break;
                    }

                default:
                    context.Fail(reason: string.Format("Unexpected TCP package: {0}.", pkg.Command));
                    break;
                }
            },
                connectionClosed: (c, error) =>
            {
                if (error == SocketError.Success)
                {
                    context.Success();
                }
                else
                {
                    context.Fail();
                }
            });

            if (args.Length == 0)
            {
                context.Log.Info("SUBSCRIBING TO ALL STREAMS...");
                var cmd = new TcpClientMessageDto.SubscribeToAllStreams();
                connection.EnqueueSend(new TcpPackage(TcpCommand.SubscribeToAllStreams, Guid.NewGuid(), cmd.Serialize()).AsByteArray());
            }
            else
            {
                foreach (var stream in args)
                {
                    context.Log.Info("SUBSCRIBING TO STREAM <{0}>...", stream);
                    var cmd = new TcpClientMessageDto.SubscribeToStream(stream);
                    connection.EnqueueSend(new TcpPackage(TcpCommand.SubscribeToStream, Guid.NewGuid(), cmd.Serialize()).AsByteArray());
                }
            }

            context.WaitForCompletion();
            return(true);
        }