public bool Execute(CommandProcessorContext context, string[] args) { bool forward = true; long commitPos = 0; long preparePos = 0; bool posOverriden = false; bool resolveLinkTos = false; bool requireMaster = false; if (args.Length > 0) { if (args.Length != 1 && args.Length != 3 && args.Length != 4) return false; if (args[0].ToUpper() == "F") forward = true; else if (args[0].ToUpper() == "B") forward = false; else return false; if (args.Length >= 3) { posOverriden = true; if (!long.TryParse(args[1], out commitPos) || !long.TryParse(args[2], out preparePos)) return false; } if (args.Length >= 4) requireMaster = bool.Parse(args[3]); } if (!posOverriden) { commitPos = forward ? 0 : -1; preparePos = forward ? 0 : -1; } context.IsAsync(); int total = 0; var sw = new Stopwatch(); var tcpCommand = forward ? TcpCommand.ReadAllEventsForward : TcpCommand.ReadAllEventsBackward; context.Client.CreateTcpConnection( context, connectionEstablished: conn => { context.Log.Info("[{0}, L{1}]: Reading all {2}...", conn.RemoteEndPoint, conn.LocalEndPoint, forward ? "FORWARD" : "BACKWARD"); var readDto = new TcpClientMessageDto.ReadAllEvents(commitPos, preparePos, 10, resolveLinkTos, requireMaster); var package = new TcpPackage(tcpCommand, Guid.NewGuid(), readDto.Serialize()).AsByteArray(); sw.Start(); conn.EnqueueSend(package); }, handlePackage: (conn, pkg) => { if (pkg.Command != tcpCommand) { context.Fail(reason: string.Format("Unexpected TCP package: {0}.", pkg.Command)); return; } var dto = pkg.Data.Deserialize<TcpClientMessageDto.ReadAllEventsCompleted>(); if (dto.Events.IsEmpty()) { sw.Stop(); context.Log.Info("=== Reading ALL {2} completed in {0}. Total read: {1}", sw.Elapsed, total, forward ? "FORWARD" : "BACKWARD"); context.Success(); conn.Close(); return; } var sb = new StringBuilder(); for (int i = 0; i < dto.Events.Length; ++i) { var evnt = dto.Events[i].Event; sb.AppendFormat("\n{0}:\tStreamId: {1},\n\tEventNumber: {2},\n\tData:\n{3},\n\tEventType: {4}\n", total, evnt.EventStreamId, evnt.EventNumber, Helper.UTF8NoBom.GetString(evnt.Data), evnt.EventType); total += 1; } context.Log.Info("Next {0} events read:\n{1}", dto.Events.Length, sb.ToString()); var readDto = new TcpClientMessageDto.ReadAllEvents(dto.NextCommitPosition, dto.NextPreparePosition, 10, resolveLinkTos, requireMaster); var package = new TcpPackage(tcpCommand, Guid.NewGuid(), readDto.Serialize()).AsByteArray(); conn.EnqueueSend(package); }, connectionClosed: (connection, error) => context.Fail(reason: "Connection was closed prematurely.")); context.WaitForCompletion(); return true; }
public bool Execute(CommandProcessorContext context, string[] args) { bool forward = true; long commitPos = 0; long preparePos = 0; bool posOverriden = false; if (args.Length > 0) { if (args.Length != 1 && args.Length != 3) return false; if (args[0].ToUpper() == "F") forward = true; else if (args[0].ToUpper() == "B") forward = false; else return false; if (args.Length == 3) { posOverriden = true; if (!long.TryParse(args[1], out commitPos) || !long.TryParse(args[2], out preparePos)) return false; } } if (!posOverriden) { commitPos = forward ? 0 : -1; preparePos = forward ? 0 : -1; } context.IsAsync(); int total = 0; var sw = new Stopwatch(); context.Client.CreateTcpConnection( context, connectionEstablished: conn => { context.Log.Info("[{0}]: Reading all {0}...", conn.EffectiveEndPoint, forward ? "FORWARD" : "BACKWARD"); sw.Start(); var readDto = new TcpClientMessageDto.ReadAllEvents(commitPos, preparePos, 10, false); var package = new TcpPackage(forward ? TcpCommand.ReadAllEventsForward : TcpCommand.ReadAllEventsBackward, Guid.NewGuid(), readDto.Serialize()); conn.EnqueueSend(package.AsByteArray()); }, handlePackage: (conn, pkg) => { TcpClientMessageDto.ResolvedEvent[] records; long nextCommitPos; long nextPreparePos; if (forward) { if (pkg.Command != TcpCommand.ReadAllEventsForwardCompleted) { context.Fail(reason: string.Format("Unexpected TCP package: {0}.", pkg.Command)); return; } var dto = pkg.Data.Deserialize<TcpClientMessageDto.ReadAllEventsCompleted>(); records = dto.Events; nextCommitPos = dto.NextCommitPosition; nextPreparePos = dto.NextPreparePosition; } else { if (pkg.Command != TcpCommand.ReadAllEventsBackwardCompleted) { context.Fail(reason: string.Format("Unexpected TCP package: {0}.", pkg.Command)); return; } var dto = pkg.Data.Deserialize<TcpClientMessageDto.ReadAllEventsCompleted>(); records = dto.Events; nextCommitPos = dto.NextCommitPosition; nextPreparePos = dto.NextPreparePosition; } if (records == null || records.Length == 0) { sw.Stop(); context.Log.Info("=== Reading ALL {2} completed in {0}. Total read: {1}", sw.Elapsed, total, forward ? "FORWARD" : "BACKWARD"); conn.Close(); context.Success(); return; } var sb = new StringBuilder(); for (int i = 0; i < records.Length; ++i) { var evnt = records[i].Event; sb.AppendFormat("\n{0}:\tStreamId: {1},\n\tEventNumber: {2},\n\tData:\n{3},\n\tEventType: {4}\n", total, evnt.EventStreamId, evnt.EventNumber, Encoding.UTF8.GetString(evnt.Data), evnt.EventType); total += 1; } context.Log.Info("Next {0} events read:\n{1}", records.Length, sb.ToString()); var readDto = new TcpClientMessageDto.ReadAllEvents(nextCommitPos, nextPreparePos, 10, false); var package = new TcpPackage(forward ? TcpCommand.ReadAllEventsForward : TcpCommand.ReadAllEventsBackward, Guid.NewGuid(), readDto.Serialize()); conn.EnqueueSend(package.AsByteArray()); }, connectionClosed: (connection, error) => { if (error == SocketError.Success) context.Success(); else context.Fail(); }); context.WaitForCompletion(); return true; }