private static TcpPackage WrapCreateStream(ClientMessage.CreateStream msg)
        {
            var dto = new TcpClientMessageDto.CreateStream(msg.EventStreamId, msg.Metadata, msg.AllowForwarding, msg.IsJson);

            return(new TcpPackage(TcpCommand.CreateStream, msg.CorrelationId, dto.Serialize()));
        }
Exemple #2
0
        public bool Execute(CommandProcessorContext context, string[] args)
        {
            var    eventStreamId = "test-stream";
            string metadata      = null;

            if (args.Length > 0)
            {
                if (args.Length > 2)
                {
                    return(false);
                }
                eventStreamId = args[0];
                if (args.Length > 1)
                {
                    metadata = args[1];
                }
            }

            context.IsAsync();
            var createStreamDto = new TcpClientMessageDto.CreateStream(
                eventStreamId,
                Encoding.UTF8.GetBytes(metadata ?? string.Format("{{\"StreamName\": \"{0}\"}}", eventStreamId)),
                true,
                metadata == null);
            var package = new TcpPackage(TcpCommand.CreateStream, Guid.NewGuid(), createStreamDto.Serialize());

            var sw = new Stopwatch();

            context.Client.CreateTcpConnection(
                context,
                connectionEstablished: conn =>
            {
                context.Log.Info("[{0}]: Trying to create stream '{1}'...", conn.EffectiveEndPoint, eventStreamId);
                sw.Start();
                conn.EnqueueSend(package.AsByteArray());
            },
                handlePackage: (conn, pkg) =>
            {
                if (pkg.Command != TcpCommand.CreateStreamCompleted)
                {
                    context.Fail(reason: string.Format("Unexpected TCP package: {0}.", pkg.Command));
                    return;
                }

                sw.Stop();

                var dto = pkg.Data.Deserialize <TcpClientMessageDto.CreateStreamCompleted>();
                if ((OperationErrorCode)dto.ErrorCode == OperationErrorCode.Success)
                {
                    context.Log.Info("Successfully created stream '{0}'.", dto.EventStreamId);
                    PerfUtils.LogTeamCityGraphData(string.Format("{0}-latency-ms", Keyword), (int)sw.ElapsedMilliseconds);
                }
                else
                {
                    context.Log.Info("Error while creating stream {0}: {1} ({2}).",
                                     eventStreamId,
                                     dto.Error,
                                     (OperationErrorCode)dto.ErrorCode);
                }

                context.Log.Info("Create stream request took: {0}.", sw.Elapsed);
                conn.Close();
                context.Success();
            },
                connectionClosed: (connection, error) =>
            {
                if (error == SocketError.Success)
                {
                    context.Success();
                }
                else
                {
                    context.Fail();
                }
            });

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