From e32699915bfb91672eefeaa2cf322606210584f7 Mon Sep 17 00:00:00 2001 From: Adrian Macal Date: Sat, 12 Nov 2016 16:59:12 +0100 Subject: [PATCH] Switch more components to leak bus implementation. --- sources/Leak.Core/Cando/CandoConfiguration.cs | 6 ++- sources/Leak.Core/Cando/CandoContext.cs | 8 ++- sources/Leak.Core/Cando/CandoMap.cs | 6 +++ sources/Leak.Core/Cando/CandoService.cs | 7 +++ .../Cando/Events/ExtensionExchanged.cs | 11 ++++ sources/Leak.Core/Client/PeerClientContext.cs | 7 +-- .../Callbacks/PeerCollectorToListener.cs | 30 +---------- .../Collector/Criterions/OrderByBitfield.cs | 7 +-- .../Collector/PeerCollectorConfiguration.cs | 6 ++- .../Collector/PeerCollectorContext.cs | 6 +++ .../Collector/PeerCollectorSubscriber.cs | 53 +++++++++++++++++++ sources/Leak.Core/Core/LeakBus.cs | 7 +-- sources/Leak.Core/Core/LeakPipeline.cs | 12 +++++ sources/Leak.Core/Leak.Core.csproj | 5 ++ .../Listener/Events/ListenerAccepted.cs | 14 +++++ .../Listener/Events/ListenerAccepting.cs | 11 ++++ .../Listener/Events/ListenerStarted.cs | 2 +- sources/Leak.Core/Listener/PeerListener.cs | 47 +++++++++------- .../Listener/PeerListenerCallback.cs | 2 - .../Listener/PeerListenerCallbackBase.cs | 4 -- .../Leak.Core/Loop/ConnectionLoopHandler.cs | 6 +++ .../Leak.Core/Loop/Events/MessageArrived.cs | 11 ++++ sources/Leak/Commands/DownloadOptions.cs | 3 ++ sources/Leak/Commands/LoggerFactory.cs | 20 +++++++ sources/Leak/Leak.csproj | 4 ++ sources/Leak/Loggers/ExtensionLogger.cs | 24 +++++++++ sources/Leak/Loggers/ExtensionLoggerNormal.cs | 6 +++ .../Leak/Loggers/ExtensionLoggerVerbose.cs | 23 ++++++++ sources/Leak/Loggers/ListenerLogger.cs | 10 ++-- sources/Leak/Loggers/ListenerLoggerNormal.cs | 12 +++-- sources/Leak/Loggers/ListenerLoggerVerbose.cs | 27 ++++++++++ 31 files changed, 317 insertions(+), 80 deletions(-) create mode 100644 sources/Leak.Core/Cando/Events/ExtensionExchanged.cs create mode 100644 sources/Leak.Core/Collector/PeerCollectorSubscriber.cs create mode 100644 sources/Leak.Core/Listener/Events/ListenerAccepted.cs create mode 100644 sources/Leak.Core/Listener/Events/ListenerAccepting.cs create mode 100644 sources/Leak.Core/Loop/Events/MessageArrived.cs create mode 100644 sources/Leak/Loggers/ExtensionLogger.cs create mode 100644 sources/Leak/Loggers/ExtensionLoggerNormal.cs create mode 100644 sources/Leak/Loggers/ExtensionLoggerVerbose.cs create mode 100644 sources/Leak/Loggers/ListenerLoggerVerbose.cs diff --git a/sources/Leak.Core/Cando/CandoConfiguration.cs b/sources/Leak.Core/Cando/CandoConfiguration.cs index d3aa4356..091f3e5c 100644 --- a/sources/Leak.Core/Cando/CandoConfiguration.cs +++ b/sources/Leak.Core/Cando/CandoConfiguration.cs @@ -1,9 +1,13 @@ -namespace Leak.Core.Cando +using Leak.Core.Core; + +namespace Leak.Core.Cando { public class CandoConfiguration { public CandoBuilder Extensions { get; set; } public CandoCallback Callback { get; set; } + + public LeakBus Bus { get; set; } } } \ No newline at end of file diff --git a/sources/Leak.Core/Cando/CandoContext.cs b/sources/Leak.Core/Cando/CandoContext.cs index 8240af89..baea06b2 100644 --- a/sources/Leak.Core/Cando/CandoContext.cs +++ b/sources/Leak.Core/Cando/CandoContext.cs @@ -1,4 +1,5 @@ -using System; +using Leak.Core.Core; +using System; namespace Leak.Core.Cando { @@ -35,6 +36,11 @@ public CandoCallback Callback get { return configuration.Callback; } } + public LeakBus Bus + { + get { return configuration.Bus; } + } + public CandoCollection Collection { get { return collection; } diff --git a/sources/Leak.Core/Cando/CandoMap.cs b/sources/Leak.Core/Cando/CandoMap.cs index e4c600e3..3c609299 100644 --- a/sources/Leak.Core/Cando/CandoMap.cs +++ b/sources/Leak.Core/Cando/CandoMap.cs @@ -1,5 +1,6 @@ using Leak.Core.Bencoding; using System.Collections.Generic; +using System.Linq; namespace Leak.Core.Cando { @@ -23,6 +24,11 @@ public void Add(byte value, string name) } } + public string[] All() + { + return byName.Keys.ToArray(); + } + public string Translate(byte id) { string name; diff --git a/sources/Leak.Core/Cando/CandoService.cs b/sources/Leak.Core/Cando/CandoService.cs index 32533e72..51f3392e 100644 --- a/sources/Leak.Core/Cando/CandoService.cs +++ b/sources/Leak.Core/Cando/CandoService.cs @@ -1,4 +1,5 @@ using Leak.Core.Bencoding; +using Leak.Core.Cando.Events; using Leak.Core.Common; using Leak.Core.Connector; using Leak.Core.Listener; @@ -120,6 +121,12 @@ private void SendHandshakeIfRequested(CandoEntry entry, Extended payload) CallHandshakeOnEachHandler(entry, handshake); CallHandshakeIfRequired(entry); + + context.Bus.Publish("extension-exchanged", new ExtensionExchanged + { + Peer = entry.Session.Peer, + Extensions = entry.Remote.All() + }); } } diff --git a/sources/Leak.Core/Cando/Events/ExtensionExchanged.cs b/sources/Leak.Core/Cando/Events/ExtensionExchanged.cs new file mode 100644 index 00000000..9ab09612 --- /dev/null +++ b/sources/Leak.Core/Cando/Events/ExtensionExchanged.cs @@ -0,0 +1,11 @@ +using Leak.Core.Common; + +namespace Leak.Core.Cando.Events +{ + public class ExtensionExchanged + { + public PeerHash Peer; + + public string[] Extensions; + } +} \ No newline at end of file diff --git a/sources/Leak.Core/Client/PeerClientContext.cs b/sources/Leak.Core/Client/PeerClientContext.cs index 3a32111a..a7b19f20 100644 --- a/sources/Leak.Core/Client/PeerClientContext.cs +++ b/sources/Leak.Core/Client/PeerClientContext.cs @@ -62,6 +62,7 @@ public PeerClientContext(Action configurer) collector = new PeerCollector(with => { + with.Bus = bus; with.Callback = new PeerClientToCollector(this); with.Countries = configuration.Countries; @@ -119,15 +120,15 @@ public PeerClientContext(Action configurer) }); } + worker.Start(); + pipeline.Start(); + bus?.Start(pipeline); network?.Start(pipeline); connector?.Start(pipeline); listener?.Start(pipeline); telegraph?.Start(pipeline); collector?.Start(pipeline); - - worker.Start(); - pipeline.Start(); } /// diff --git a/sources/Leak.Core/Collector/Callbacks/PeerCollectorToListener.cs b/sources/Leak.Core/Collector/Callbacks/PeerCollectorToListener.cs index bc595f53..6116c075 100644 --- a/sources/Leak.Core/Collector/Callbacks/PeerCollectorToListener.cs +++ b/sources/Leak.Core/Collector/Callbacks/PeerCollectorToListener.cs @@ -1,5 +1,4 @@ -using Leak.Core.Collector.Events; -using Leak.Core.Common; +using Leak.Core.Common; using Leak.Core.Listener; using Leak.Core.Network; @@ -28,33 +27,6 @@ public override void OnConnecting(PeerListenerConnecting connecting) context.Callback.OnConnectingFrom(connecting.Connection.Remote); } - public override void OnConnected(NetworkConnection connection) - { - int total = 0; - bool accepted = false; - - lock (context.Synchronized) - { - if (context.Bouncer.AcceptRemote(connection)) - { - accepted = true; - total = context.Bouncer.Count(); - } - } - - if (accepted) - { - PeerAddress peer = connection.Remote; - PeerCollectorConnected connected = new PeerCollectorConnected(peer, total); - - context.Callback.OnConnectedFrom(connected); - } - else - { - connection.Terminate(); - } - } - public override void OnRejected(NetworkConnection connection) { context.Callback.OnRejected(connection.Remote); diff --git a/sources/Leak.Core/Collector/Criterions/OrderByBitfield.cs b/sources/Leak.Core/Collector/Criterions/OrderByBitfield.cs index af2aee47..3cc43b26 100644 --- a/sources/Leak.Core/Collector/Criterions/OrderByBitfield.cs +++ b/sources/Leak.Core/Collector/Criterions/OrderByBitfield.cs @@ -18,13 +18,10 @@ public IEnumerable Accept(IEnumerable sessions, PeerCo Completed = context.Battlefield.Get(session)?.Completed }; - if (peer.Completed != null) - { - result.Add(peer); - } + result.Add(peer); } - return result.OrderByDescending(x => x.Completed.Value).Select(x => x.Session); + return result.OrderByDescending(x => x.Completed).Select(x => x.Session); } private struct Peer diff --git a/sources/Leak.Core/Collector/PeerCollectorConfiguration.cs b/sources/Leak.Core/Collector/PeerCollectorConfiguration.cs index f84237d1..cf88fc65 100644 --- a/sources/Leak.Core/Collector/PeerCollectorConfiguration.cs +++ b/sources/Leak.Core/Collector/PeerCollectorConfiguration.cs @@ -1,7 +1,11 @@ -namespace Leak.Core.Collector +using Leak.Core.Core; + +namespace Leak.Core.Collector { public class PeerCollectorConfiguration { + public LeakBus Bus { get; set; } + public PeerCollectorCallback Callback { get; set; } public PeerCollectorExtensionBuilder Extensions { get; set; } diff --git a/sources/Leak.Core/Collector/PeerCollectorContext.cs b/sources/Leak.Core/Collector/PeerCollectorContext.cs index e1eea9e8..a60c533a 100644 --- a/sources/Leak.Core/Collector/PeerCollectorContext.cs +++ b/sources/Leak.Core/Collector/PeerCollectorContext.cs @@ -26,6 +26,7 @@ public class PeerCollectorContext private readonly RankingService ranking; private readonly BattlefieldService battlefield; private readonly PeerCollectorBlockFactory blockFactory; + private readonly PeerCollectorSubscriber subscriber; public PeerCollectorContext(Action configurer) { @@ -68,7 +69,9 @@ public PeerCollectorContext(Action configurer) cando = new CandoService(with => { + with.Bus = configuration.Bus; with.Callback = new PeerCollectorToCando(this); + configuration.Extensions.Apply(with); }); @@ -85,6 +88,9 @@ public PeerCollectorContext(Action configurer) synchronized = new object(); blockFactory = new PeerCollectorBlockFactory(); + + subscriber = new PeerCollectorSubscriber(this); + configuration.Bus.Subscribe(subscriber.Handle); } public object Synchronized diff --git a/sources/Leak.Core/Collector/PeerCollectorSubscriber.cs b/sources/Leak.Core/Collector/PeerCollectorSubscriber.cs new file mode 100644 index 00000000..10a6ca86 --- /dev/null +++ b/sources/Leak.Core/Collector/PeerCollectorSubscriber.cs @@ -0,0 +1,53 @@ +using Leak.Core.Collector.Events; +using Leak.Core.Common; +using Leak.Core.Network; + +namespace Leak.Core.Collector +{ + public class PeerCollectorSubscriber + { + private readonly PeerCollectorContext context; + + public PeerCollectorSubscriber(PeerCollectorContext context) + { + this.context = context; + } + + public void Handle(string name, dynamic payload) + { + switch (name) + { + case "listener-accepted": + HandleConnected(payload.Connection); + break; + } + } + + private void HandleConnected(NetworkConnection connection) + { + int total = 0; + bool accepted = false; + + lock (context.Synchronized) + { + if (context.Bouncer.AcceptRemote(connection)) + { + accepted = true; + total = context.Bouncer.Count(); + } + } + + if (accepted) + { + PeerAddress peer = connection.Remote; + PeerCollectorConnected connected = new PeerCollectorConnected(peer, total); + + context.Callback.OnConnectedFrom(connected); + } + else + { + connection.Terminate(); + } + } + } +} \ No newline at end of file diff --git a/sources/Leak.Core/Core/LeakBus.cs b/sources/Leak.Core/Core/LeakBus.cs index f27ddf70..9e2d3782 100644 --- a/sources/Leak.Core/Core/LeakBus.cs +++ b/sources/Leak.Core/Core/LeakBus.cs @@ -73,7 +73,7 @@ private class Trigger : LeakPipelineTrigger private readonly LeakBus bus; private readonly ConcurrentQueue events; - private ManualResetEvent ready; + private ManualResetEvent onReady; public Trigger(LeakBus bus) { @@ -83,7 +83,7 @@ public Trigger(LeakBus bus) public void Register(ManualResetEvent watch) { - ready = watch; + onReady = watch; } public void Publish(string name, object payload) @@ -94,12 +94,13 @@ public void Publish(string name, object payload) Payload = payload }); - ready.Set(); + onReady.Set(); } public void Execute() { Event data; + onReady.Reset(); while (events.TryDequeue(out data)) { diff --git a/sources/Leak.Core/Core/LeakPipeline.cs b/sources/Leak.Core/Core/LeakPipeline.cs index 624ce321..6897b59b 100644 --- a/sources/Leak.Core/Core/LeakPipeline.cs +++ b/sources/Leak.Core/Core/LeakPipeline.cs @@ -23,6 +23,7 @@ public LeakPipeline() public void Start() { + Register(new Terminator()); worker.Start(); } @@ -81,5 +82,16 @@ private void Execute() } } } + + private class Terminator : LeakPipelineTrigger + { + public void Register(ManualResetEvent watch) + { + } + + public void Execute() + { + } + } } } \ No newline at end of file diff --git a/sources/Leak.Core/Leak.Core.csproj b/sources/Leak.Core/Leak.Core.csproj index 4b454a2f..abd4a9fc 100644 --- a/sources/Leak.Core/Leak.Core.csproj +++ b/sources/Leak.Core/Leak.Core.csproj @@ -71,6 +71,7 @@ + @@ -98,6 +99,7 @@ + @@ -113,7 +115,10 @@ + + + diff --git a/sources/Leak.Core/Listener/Events/ListenerAccepted.cs b/sources/Leak.Core/Listener/Events/ListenerAccepted.cs new file mode 100644 index 00000000..834a029e --- /dev/null +++ b/sources/Leak.Core/Listener/Events/ListenerAccepted.cs @@ -0,0 +1,14 @@ +using Leak.Core.Common; +using Leak.Core.Network; + +namespace Leak.Core.Listener.Events +{ + public class ListenerAccepted + { + public PeerHash Local; + + public PeerAddress Remote; + + public NetworkConnection Connection; + } +} \ No newline at end of file diff --git a/sources/Leak.Core/Listener/Events/ListenerAccepting.cs b/sources/Leak.Core/Listener/Events/ListenerAccepting.cs new file mode 100644 index 00000000..c08b7d1c --- /dev/null +++ b/sources/Leak.Core/Listener/Events/ListenerAccepting.cs @@ -0,0 +1,11 @@ +using Leak.Core.Common; + +namespace Leak.Core.Listener.Events +{ + public class ListenerAccepting + { + public PeerHash Local; + + public PeerAddress Remote; + } +} \ No newline at end of file diff --git a/sources/Leak.Core/Listener/Events/ListenerStarted.cs b/sources/Leak.Core/Listener/Events/ListenerStarted.cs index 6fb95057..2d3d9d07 100644 --- a/sources/Leak.Core/Listener/Events/ListenerStarted.cs +++ b/sources/Leak.Core/Listener/Events/ListenerStarted.cs @@ -4,7 +4,7 @@ namespace Leak.Core.Listener.Events { public class ListenerStarted { - public PeerHash Peer; + public PeerHash Local; public int Port; } diff --git a/sources/Leak.Core/Listener/PeerListener.cs b/sources/Leak.Core/Listener/PeerListener.cs index 7e11095b..5cad93ac 100644 --- a/sources/Leak.Core/Listener/PeerListener.cs +++ b/sources/Leak.Core/Listener/PeerListener.cs @@ -28,39 +28,51 @@ public PeerListener(Action configurer) public void Start(LeakPipeline pipeline) { - int port = configuration.Port; - PeerHash peer = configuration.Peer; - - socket.Bind(port); + socket.Bind(configuration.Port); socket.Listen(8); socket.Accept(OnAccept); configuration.Bus.Publish("listener-started", new ListenerStarted { - Peer = peer, - Port = port + Local = configuration.Peer, + Port = configuration.Port }); } private void OnAccept(TcpSocketAccept data) { - if (data.Status == TcpSocketStatus.OK) + if (data.Status != TcpSocketStatus.NotSocket) { data.Socket.Accept(OnAccept); + } + + if (data.Status == TcpSocketStatus.OK) + { + IPEndPoint endpoint = data.GetRemote(); + PeerAddress remote = PeerAddress.Parse(endpoint); + + configuration.Bus.Publish("listener-accepting", new ListenerAccepted + { + Local = configuration.Peer, + Remote = remote + }); - if (OnConnecting(data.GetRemote()) == false) + if (OnConnecting(remote) == false) { data.Connection.Dispose(); return; } - TcpSocket accepted = data.Connection; - IPEndPoint remote = data.GetRemote(); - - NetworkConnection connection = configuration.Pool.Create(accepted, NetworkDirection.Incoming, remote); + NetworkConnection connection = configuration.Pool.Create(data.Connection, NetworkDirection.Incoming, endpoint); PeerListenerNegotiatorContext context = new PeerListenerNegotiatorContext(configuration, connection); - OnConnected(connection); + configuration.Bus.Publish("listener-accepted", new ListenerAccepted + { + Local = configuration.Peer, + Remote = remote, + Connection = connection + }); + Negotiate(context, connection); } else @@ -69,22 +81,17 @@ private void OnAccept(TcpSocketAccept data) } } - private bool OnConnecting(IPEndPoint endpoint) + private bool OnConnecting(PeerAddress remote) { bool accepted = true; - NetworkConnectionInfo connection = new NetworkConnectionInfo(PeerAddress.Parse(endpoint), NetworkDirection.Incoming); + NetworkConnectionInfo connection = new NetworkConnectionInfo(remote, NetworkDirection.Incoming); PeerListenerConnecting connecting = new PeerListenerConnecting(connection, () => accepted = false); configuration.Callback.OnConnecting(connecting); return accepted; } - private void OnConnected(NetworkConnection connection) - { - configuration.Callback.OnConnected(connection); - } - private void Negotiate(PeerListenerNegotiatorContext context, NetworkConnection connection) { new HandshakeNegotiatorPassive(configuration.Pool, connection, context).Execute(); diff --git a/sources/Leak.Core/Listener/PeerListenerCallback.cs b/sources/Leak.Core/Listener/PeerListenerCallback.cs index f4f7dad8..c50fc227 100644 --- a/sources/Leak.Core/Listener/PeerListenerCallback.cs +++ b/sources/Leak.Core/Listener/PeerListenerCallback.cs @@ -9,8 +9,6 @@ public interface PeerListenerCallback void OnConnecting(PeerListenerConnecting connecting); - void OnConnected(NetworkConnection connection); - void OnRejected(NetworkConnection connection); void OnHandshake(NetworkConnection connection, PeerListenerHandshake handshake); diff --git a/sources/Leak.Core/Listener/PeerListenerCallbackBase.cs b/sources/Leak.Core/Listener/PeerListenerCallbackBase.cs index e7d2d2c7..389c431c 100644 --- a/sources/Leak.Core/Listener/PeerListenerCallbackBase.cs +++ b/sources/Leak.Core/Listener/PeerListenerCallbackBase.cs @@ -13,10 +13,6 @@ public virtual void OnConnecting(PeerListenerConnecting connecting) { } - public virtual void OnConnected(NetworkConnection connection) - { - } - public virtual void OnRejected(NetworkConnection connection) { } diff --git a/sources/Leak.Core/Loop/ConnectionLoopHandler.cs b/sources/Leak.Core/Loop/ConnectionLoopHandler.cs index 414ff283..afc40b1b 100644 --- a/sources/Leak.Core/Loop/ConnectionLoopHandler.cs +++ b/sources/Leak.Core/Loop/ConnectionLoopHandler.cs @@ -31,7 +31,9 @@ private void OnMessageData(NetworkIncomingMessage message) { if (message.Length == 4) { + Notify("keep-alive"); Dispatch(message, configuration.Callback.OnKeepAlive); + return; } @@ -77,6 +79,10 @@ private void Ignore(NetworkIncomingMessage message) connection.Receive(OnMessageHeader, 4); } + private void Notify(string type) + { + } + private void Dispatch(NetworkIncomingMessage message, Action callback) { message.Acknowledge(ConnectionLoopMessage.GetMessageSize(message) + 4); diff --git a/sources/Leak.Core/Loop/Events/MessageArrived.cs b/sources/Leak.Core/Loop/Events/MessageArrived.cs new file mode 100644 index 00000000..084df0b8 --- /dev/null +++ b/sources/Leak.Core/Loop/Events/MessageArrived.cs @@ -0,0 +1,11 @@ +using Leak.Core.Common; + +namespace Leak.Core.Loop.Events +{ + public class MessageArrived + { + public PeerHash Peer; + + public string Type; + } +} \ No newline at end of file diff --git a/sources/Leak/Commands/DownloadOptions.cs b/sources/Leak/Commands/DownloadOptions.cs index 50780ad2..b60fa6cb 100644 --- a/sources/Leak/Commands/DownloadOptions.cs +++ b/sources/Leak/Commands/DownloadOptions.cs @@ -46,6 +46,9 @@ public class DownloadOptions [Option("--logging-listener")] public string LoggingListener { get; set; } + [Option("--logging-extension")] + public string LoggingExtension { get; set; } + [Option("--logging-network")] public string LoggingNetwork { get; set; } diff --git a/sources/Leak/Commands/LoggerFactory.cs b/sources/Leak/Commands/LoggerFactory.cs index c07bc675..728fdf33 100644 --- a/sources/Leak/Commands/LoggerFactory.cs +++ b/sources/Leak/Commands/LoggerFactory.cs @@ -17,6 +17,7 @@ public LeakBusCallback Subscriber() BouncerLogger bounder = Bouncer(); ConnectorLogger connector = Connector(); HashLogger hash = Hash(); + ExtensionLogger extension = Extension(); ListenerLogger listener = Listener(); NetworkLogger network = Network(); PeerLogger peer = Peer(); @@ -26,6 +27,7 @@ public LeakBusCallback Subscriber() bounder.Handle(name, payload); connector.Handle(name, payload); hash.Handle(name, payload); + extension.Handle(name, payload); listener.Handle(name, payload); network.Handle(name, payload); peer.Handle(name, payload); @@ -54,6 +56,21 @@ public HashLogger Hash() } } + public ExtensionLogger Extension() + { + switch (Severity(options.LoggingExtension)) + { + case "off": + return ExtensionLogger.Off(); + + case "verbose": + return ExtensionLogger.Verbose(); + + default: + return ExtensionLogger.Normal(); + } + } + public ListenerLogger Listener() { switch (Severity(options.LoggingListener)) @@ -61,6 +78,9 @@ public ListenerLogger Listener() case "off": return ListenerLogger.Off(); + case "verbose": + return ListenerLogger.Verbose(); + default: return ListenerLogger.Normal(); } diff --git a/sources/Leak/Leak.csproj b/sources/Leak/Leak.csproj index 01b7126f..b0920a94 100644 --- a/sources/Leak/Leak.csproj +++ b/sources/Leak/Leak.csproj @@ -56,10 +56,14 @@ + + + + diff --git a/sources/Leak/Loggers/ExtensionLogger.cs b/sources/Leak/Loggers/ExtensionLogger.cs new file mode 100644 index 00000000..17c877fa --- /dev/null +++ b/sources/Leak/Loggers/ExtensionLogger.cs @@ -0,0 +1,24 @@ +namespace Leak.Loggers +{ + public class ExtensionLogger + { + public static ExtensionLogger Off() + { + return new ExtensionLogger(); + } + + public static ExtensionLogger Normal() + { + return new ExtensionLoggerNormal(); + } + + public static ExtensionLogger Verbose() + { + return new ExtensionLoggerVerbose(); + } + + public virtual void Handle(string name, object payload) + { + } + } +} \ No newline at end of file diff --git a/sources/Leak/Loggers/ExtensionLoggerNormal.cs b/sources/Leak/Loggers/ExtensionLoggerNormal.cs new file mode 100644 index 00000000..af983ab6 --- /dev/null +++ b/sources/Leak/Loggers/ExtensionLoggerNormal.cs @@ -0,0 +1,6 @@ +namespace Leak.Loggers +{ + public class ExtensionLoggerNormal : ExtensionLogger + { + } +} \ No newline at end of file diff --git a/sources/Leak/Loggers/ExtensionLoggerVerbose.cs b/sources/Leak/Loggers/ExtensionLoggerVerbose.cs new file mode 100644 index 00000000..187dfc8a --- /dev/null +++ b/sources/Leak/Loggers/ExtensionLoggerVerbose.cs @@ -0,0 +1,23 @@ +using System; + +namespace Leak.Loggers +{ + public class ExtensionLoggerVerbose : ExtensionLoggerNormal + { + public override void Handle(string name, object payload) + { + HandleMessage(name, payload); + base.Handle(name, payload); + } + + private void HandleMessage(string name, dynamic payload) + { + switch (name) + { + case "extension-exchanged": + Console.WriteLine($"{payload.Peer}: extensions exchanged; supported: {String.Join(", ", payload.Extensions)}"); + break; + } + } + } +} \ No newline at end of file diff --git a/sources/Leak/Loggers/ListenerLogger.cs b/sources/Leak/Loggers/ListenerLogger.cs index cb962ac1..ce71ad4f 100644 --- a/sources/Leak/Loggers/ListenerLogger.cs +++ b/sources/Leak/Loggers/ListenerLogger.cs @@ -1,6 +1,4 @@ -using System; - -namespace Leak.Loggers +namespace Leak.Loggers { public class ListenerLogger { @@ -14,12 +12,12 @@ public static ListenerLogger Normal() return new ListenerLoggerNormal(); } - public void Handle(string name, dynamic payload) + public static ListenerLogger Verbose() { - Handle(name, payload, new Action(() => { })); + return new ListenerLoggerVerbose(); } - protected virtual void Handle(string name, dynamic payload, Action next) + public virtual void Handle(string name, object payload) { } } diff --git a/sources/Leak/Loggers/ListenerLoggerNormal.cs b/sources/Leak/Loggers/ListenerLoggerNormal.cs index aaad6fc4..6ae037f0 100644 --- a/sources/Leak/Loggers/ListenerLoggerNormal.cs +++ b/sources/Leak/Loggers/ListenerLoggerNormal.cs @@ -4,16 +4,20 @@ namespace Leak.Loggers { public class ListenerLoggerNormal : ListenerLogger { - protected override void Handle(string name, dynamic payload, Action next) + public override void Handle(string name, object payload) + { + HandleMessage(name, payload); + base.Handle(name, payload); + } + + private void HandleMessage(string name, dynamic payload) { switch (name) { case "listener-started": - Console.WriteLine($"{payload.Peer}: listener started on port {payload.Port}"); + Console.WriteLine($"{payload.Local}: started listening on port {payload.Port}"); break; } - - next.Invoke(); } } } \ No newline at end of file diff --git a/sources/Leak/Loggers/ListenerLoggerVerbose.cs b/sources/Leak/Loggers/ListenerLoggerVerbose.cs new file mode 100644 index 00000000..7fbf3431 --- /dev/null +++ b/sources/Leak/Loggers/ListenerLoggerVerbose.cs @@ -0,0 +1,27 @@ +using System; + +namespace Leak.Loggers +{ + public class ListenerLoggerVerbose : ListenerLoggerNormal + { + public override void Handle(string name, object payload) + { + HandleMessage(name, payload); + base.Handle(name, payload); + } + + private void HandleMessage(string name, dynamic payload) + { + switch (name) + { + case "listener-accepting": + Console.WriteLine($"{payload.Local}: accepting {payload.Remote}"); + break; + + case "listener-accepted": + Console.WriteLine($"{payload.Local}: accepted {payload.Remote}"); + break; + } + } + } +} \ No newline at end of file