├── .gitignore ├── Client ├── .gitignore ├── App.config ├── App.xaml ├── App.xaml.cs ├── Client.csproj ├── Comms │ ├── HeartBeatClient.cs │ ├── IHeartBeatClient.cs │ ├── ITickerClient.cs │ ├── NetMQHeartBeatClient.cs │ ├── NetMQTickerClient.cs │ ├── ServiceClientBase.cs │ ├── TickerClient.cs │ └── Transport │ │ ├── Connection.cs │ │ ├── ConnectionInfo.cs │ │ ├── ConnectionProvider.cs │ │ ├── ConnectionStatus.cs │ │ ├── IConnection.cs │ │ ├── IConnectionProvider - Copy.cs │ │ └── IConnectionProvider.cs ├── Factory │ ├── ITickerFactory.cs │ ├── Ticker.cs │ └── TickerFactory.cs ├── IOC │ └── Bootstrapper.cs ├── MainWindow.xaml ├── MainWindow.xaml.cs ├── Properties │ └── AssemblyInfo.cs ├── Repositories │ ├── ITickerRepository.cs │ └── TickerRepository.cs ├── Services │ ├── ConcurrencyService.cs │ ├── IConcurrencyService.cs │ ├── IReactiveTrader.cs │ ├── IUserProvider.cs │ ├── ReactiveTrader.cs │ └── UserProvider.cs ├── ValueConverters │ └── DecimalPlaceFormatter.cs ├── ViewModels │ ├── ConnectivityStatusViewModel.cs │ ├── INPCBase.cs │ ├── MainWindowViewModel.cs │ ├── TickerViewModel.cs │ ├── TickerViewModelFactory.cs │ └── TickersViewModel.cs └── packages.config ├── Common ├── .gitignore ├── Common.csproj ├── Extensions │ └── ObservableExtensions.cs ├── Properties │ └── AssemblyInfo.cs ├── SnapshotProtocol.cs ├── StreamingProtocol.cs ├── TickerDto.cs ├── ViewModels │ └── DelegateCommand.cs └── packages.config ├── NetMQDemo.sln ├── NetMQServer ├── .gitignore ├── App.config ├── App.xaml ├── App.xaml.cs ├── IOC │ └── Bootstrapper.cs ├── MainWindow.xaml ├── MainWindow.xaml.cs ├── MainWindowViewModel.cs ├── NetMQServer.csproj ├── Properties │ └── AssemblyInfo.cs ├── Ticker │ ├── ITickerPublisher.cs │ ├── ITickerRepository.cs │ ├── NetMQPublisher.cs │ └── TickerRepository.cs ├── log4net.xml └── packages.config └── Video ├── NetMQRx.camproj └── capture-2.trec /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.sln.docstates 8 | 9 | # Build results 10 | [Dd]ebug/ 11 | [Dd]ebugPublic/ 12 | [Rr]elease/ 13 | [Rr]eleases/ 14 | x64/ 15 | x86/ 16 | build/ 17 | bld/ 18 | [Bb]in/ 19 | [Oo]bj/ 20 | 21 | # Roslyn cache directories 22 | *.ide/ 23 | 24 | # MSTest test Results 25 | [Tt]est[Rr]esult*/ 26 | [Bb]uild[Ll]og.* 27 | 28 | #NUNIT 29 | *.VisualState.xml 30 | TestResult.xml 31 | 32 | # Build Results of an ATL Project 33 | [Dd]ebugPS/ 34 | [Rr]eleasePS/ 35 | dlldata.c 36 | 37 | *_i.c 38 | *_p.c 39 | *_i.h 40 | *.ilk 41 | *.meta 42 | *.obj 43 | *.pch 44 | *.pdb 45 | *.pgc 46 | *.pgd 47 | *.rsp 48 | *.sbr 49 | *.tlb 50 | *.tli 51 | *.tlh 52 | *.tmp 53 | *.tmp_proj 54 | *.log 55 | *.vspscc 56 | *.vssscc 57 | .builds 58 | *.pidb 59 | *.svclog 60 | *.scc 61 | 62 | # Chutzpah Test files 63 | _Chutzpah* 64 | 65 | # Visual C++ cache files 66 | ipch/ 67 | *.aps 68 | *.ncb 69 | *.opensdf 70 | *.sdf 71 | *.cachefile 72 | 73 | # Visual Studio profiler 74 | *.psess 75 | *.vsp 76 | *.vspx 77 | 78 | # TFS 2012 Local Workspace 79 | $tf/ 80 | 81 | # Guidance Automation Toolkit 82 | *.gpState 83 | 84 | # ReSharper is a .NET coding add-in 85 | _ReSharper*/ 86 | *.[Rr]e[Ss]harper 87 | *.DotSettings.user 88 | 89 | # JustCode is a .NET coding addin-in 90 | .JustCode 91 | 92 | # TeamCity is a build add-in 93 | _TeamCity* 94 | 95 | # DotCover is a Code Coverage Tool 96 | *.dotCover 97 | 98 | # NCrunch 99 | _NCrunch_* 100 | .*crunch*.local.xml 101 | 102 | # MightyMoose 103 | *.mm.* 104 | AutoTest.Net/ 105 | 106 | # Web workbench (sass) 107 | .sass-cache/ 108 | 109 | # Installshield output folder 110 | [Ee]xpress/ 111 | 112 | # DocProject is a documentation generator add-in 113 | DocProject/buildhelp/ 114 | DocProject/Help/*.HxT 115 | DocProject/Help/*.HxC 116 | DocProject/Help/*.hhc 117 | DocProject/Help/*.hhk 118 | DocProject/Help/*.hhp 119 | DocProject/Help/Html2 120 | DocProject/Help/html 121 | 122 | # Click-Once directory 123 | publish/ 124 | 125 | # Publish Web Output 126 | *.[Pp]ublish.xml 127 | *.azurePubxml 128 | # TODO: Comment the next line if you want to checkin your web deploy settings 129 | # but database connection strings (with potential passwords) will be unencrypted 130 | *.pubxml 131 | *.publishproj 132 | 133 | # NuGet Packages 134 | *.nupkg 135 | # The packages folder can be ignored because of Package Restore 136 | **/packages/* 137 | # except build/, which is used as an MSBuild target. 138 | !**/packages/build/ 139 | # If using the old MSBuild-Integrated Package Restore, uncomment this: 140 | #!**/packages/repositories.config 141 | 142 | # Windows Azure Build Output 143 | csx/ 144 | *.build.csdef 145 | 146 | # Windows Store app package directory 147 | AppPackages/ 148 | 149 | # Others 150 | sql/ 151 | *.Cache 152 | ClientBin/ 153 | [Ss]tyle[Cc]op.* 154 | ~$* 155 | *~ 156 | *.dbmdl 157 | *.dbproj.schemaview 158 | *.pfx 159 | *.publishsettings 160 | node_modules/ 161 | 162 | # RIA/Silverlight projects 163 | Generated_Code/ 164 | 165 | # Backup & report files from converting an old project file 166 | # to a newer Visual Studio version. Backup files are not needed, 167 | # because we have git ;-) 168 | _UpgradeReport_Files/ 169 | Backup*/ 170 | UpgradeLog*.XML 171 | UpgradeLog*.htm 172 | 173 | # SQL Server files 174 | *.mdf 175 | *.ldf 176 | 177 | # Business Intelligence projects 178 | *.rdl.data 179 | *.bim.layout 180 | *.bim_*.settings 181 | 182 | # Microsoft Fakes 183 | FakesAssemblies/ 184 | -------------------------------------------------------------------------------- /Client/.gitignore: -------------------------------------------------------------------------------- 1 | # Build Folders (you can keep bin if you'd like, to store dlls and pdbs) 2 | [Bb]in/ 3 | [Oo]bj/ 4 | 5 | # mstest test results 6 | TestResults 7 | 8 | ## Ignore Visual Studio temporary files, build results, and 9 | ## files generated by popular Visual Studio add-ons. 10 | 11 | # User-specific files 12 | *.suo 13 | *.user 14 | *.sln.docstates 15 | 16 | # Build results 17 | [Dd]ebug/ 18 | [Rr]elease/ 19 | x64/ 20 | *_i.c 21 | *_p.c 22 | *.ilk 23 | *.meta 24 | *.obj 25 | *.pch 26 | *.pdb 27 | *.pgc 28 | *.pgd 29 | *.rsp 30 | *.sbr 31 | *.tlb 32 | *.tli 33 | *.tlh 34 | *.tmp 35 | *.log 36 | *.vspscc 37 | *.vssscc 38 | .builds 39 | 40 | # Visual C++ cache files 41 | ipch/ 42 | *.aps 43 | *.ncb 44 | *.opensdf 45 | *.sdf 46 | 47 | # Visual Studio profiler 48 | *.psess 49 | *.vsp 50 | *.vspx 51 | 52 | # Guidance Automation Toolkit 53 | *.gpState 54 | 55 | # ReSharper is a .NET coding add-in 56 | _ReSharper* 57 | 58 | # NCrunch 59 | *.ncrunch* 60 | .*crunch*.local.xml 61 | 62 | # Installshield output folder 63 | [Ee]xpress 64 | 65 | # DocProject is a documentation generator add-in 66 | DocProject/buildhelp/ 67 | DocProject/Help/*.HxT 68 | DocProject/Help/*.HxC 69 | DocProject/Help/*.hhc 70 | DocProject/Help/*.hhk 71 | DocProject/Help/*.hhp 72 | DocProject/Help/Html2 73 | DocProject/Help/html 74 | 75 | # Click-Once directory 76 | publish 77 | 78 | # Publish Web Output 79 | *.Publish.xml 80 | 81 | # NuGet Packages Directory 82 | packages 83 | 84 | # Windows Azure Build Output 85 | csx 86 | *.build.csdef 87 | 88 | # Others 89 | [Bb]in 90 | [Oo]bj 91 | sql 92 | TestResults 93 | [Tt]est[Rr]esult* 94 | *.Cache 95 | ClientBin 96 | [Ss]tyle[Cc]op.* 97 | ~$* 98 | *.dbmdl 99 | Generated_Code #added for RIA/Silverlight projects 100 | 101 | # Backup & report files from converting an old project file to a newer 102 | # Visual Studio version. Backup files are not needed, because we have git ;-) 103 | _UpgradeReport_Files/ 104 | Backup*/ 105 | UpgradeLog*.XML -------------------------------------------------------------------------------- /Client/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 |
5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /Client/App.xaml: -------------------------------------------------------------------------------- 1 |  4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Client/App.xaml.cs: -------------------------------------------------------------------------------- 1 | using System.Threading; 2 | using System.Windows; 3 | using Autofac; 4 | using Client.Comms; 5 | using Client.IOC; 6 | using Client.Services; 7 | using log4net; 8 | 9 | 10 | namespace Client 11 | { 12 | 13 | public partial class App : Application 14 | { 15 | private static readonly ILog Log = LogManager.GetLogger(typeof(App)); 16 | 17 | protected override void OnStartup(StartupEventArgs e) 18 | { 19 | base.OnStartup(e); 20 | InitializeLogging(); 21 | Start(); 22 | } 23 | 24 | private void Start() 25 | { 26 | var bootstrapper = new Bootstrapper(); 27 | var container = bootstrapper.Build(); 28 | 29 | Log.Info("Initializing reactive trader API..."); 30 | var reactiveTraderApi = container.Resolve(); 31 | 32 | var username = container.Resolve().Username; 33 | reactiveTraderApi.Initialize(username, "localhost"); 34 | 35 | var mainWindow = container.Resolve(); 36 | //var vm = container.Resolve(); 37 | //mainWindow.DataContext = vm; 38 | mainWindow.Show(); 39 | 40 | 41 | 42 | 43 | var netMQHeartClient = NetMQHeartBeatClient.CreateInstance(null, "ghhasa"); 44 | 45 | 46 | } 47 | 48 | private void InitializeLogging() 49 | { 50 | Thread.CurrentThread.Name = "UI"; 51 | log4net.Config.XmlConfigurator.Configure(); 52 | Log.Info(@"SignalRSelfHost started"); 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /Client/Client.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {B43264BB-E309-4168-8770-EC0127C63912} 8 | Exe 9 | Properties 10 | Client 11 | Client 12 | v4.5 13 | 512 14 | 15 | 16 | AnyCPU 17 | true 18 | full 19 | false 20 | bin\Debug\ 21 | DEBUG;TRACE 22 | prompt 23 | 4 24 | 25 | 26 | AnyCPU 27 | pdbonly 28 | true 29 | bin\Release\ 30 | TRACE 31 | prompt 32 | 4 33 | 34 | 35 | Client.App 36 | 37 | 38 | 39 | ..\packages\Autofac.3.5.2\lib\net40\Autofac.dll 40 | 41 | 42 | ..\packages\log4net.2.0.3\lib\net40-full\log4net.dll 43 | 44 | 45 | False 46 | ..\packages\Blend.Interactivity.Wpf.1.0.1340.0\lib\net\Microsoft.Expression.Interactions.dll 47 | 48 | 49 | ..\packages\NetMQ.3.3.0.11\lib\net40\NetMQ.dll 50 | 51 | 52 | False 53 | ..\packages\Newtonsoft.Json.6.0.4\lib\net45\Newtonsoft.Json.dll 54 | 55 | 56 | 57 | 58 | 59 | 60 | ..\packages\Rx-Core.2.2.5\lib\net45\System.Reactive.Core.dll 61 | 62 | 63 | ..\packages\Rx-Interfaces.2.2.5\lib\net45\System.Reactive.Interfaces.dll 64 | 65 | 66 | ..\packages\Rx-Linq.2.2.5\lib\net45\System.Reactive.Linq.dll 67 | 68 | 69 | ..\packages\Rx-PlatformServices.2.2.5\lib\net45\System.Reactive.PlatformServices.dll 70 | 71 | 72 | ..\packages\Rx-XAML.2.2.5\lib\net45\System.Reactive.Windows.Threading.dll 73 | 74 | 75 | False 76 | ..\packages\Blend.Interactivity.Wpf.1.0.1340.0\lib\net\System.Windows.Interactivity.dll 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | App.xaml 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | Code 97 | 98 | 99 | Code 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | MainWindow.xaml 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | Designer 130 | 131 | 132 | 133 | 134 | {09ecea5a-a3cd-4f9a-92dc-7d282be5bc0e} 135 | Common 136 | 137 | 138 | 139 | 140 | MSBuild:Compile 141 | Designer 142 | 143 | 144 | 145 | 146 | MSBuild:Compile 147 | Designer 148 | 149 | 150 | 151 | 152 | 159 | -------------------------------------------------------------------------------- /Client/Comms/HeartBeatClient.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reactive.Disposables; 5 | using System.Reactive.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | using Client.Comms; 9 | using Client.Comms.Transport; 10 | using NetMQ; 11 | 12 | namespace Client.Hub 13 | { 14 | public class HeartBeatClient : IHeartBeatClient 15 | { 16 | public IObservable ConnectionStatusStream() 17 | { 18 | return Observable.Create(observer => 19 | { 20 | NetMQHeartBeatClient.Instance.InitialiseComms(); 21 | 22 | var disposable = NetMQHeartBeatClient.Instance.GetConnectionStatusStream().Subscribe(observer); 23 | return new CompositeDisposable { disposable }; 24 | }) 25 | .Publish() 26 | .RefCount(); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Client/Comms/IHeartBeatClient.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using Client.Comms.Transport; 7 | using Common; 8 | 9 | namespace Client.Hub 10 | { 11 | public interface IHeartBeatClient 12 | { 13 | IObservable ConnectionStatusStream(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Client/Comms/ITickerClient.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using Common; 7 | 8 | namespace Client.Comms 9 | { 10 | internal interface ITickerClient 11 | { 12 | IObservable GetTickerStream(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Client/Comms/NetMQHeartBeatClient.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reactive.Linq; 5 | using System.Reactive.Subjects; 6 | using System.Text; 7 | using System.Threading; 8 | using System.Threading.Tasks; 9 | using Client.Factory; 10 | using Client.Comms.Transport; 11 | using Common; 12 | using NetMQ; 13 | using NetMQ.Actors; 14 | using NetMQ.InProcActors; 15 | using NetMQ.Sockets; 16 | using NetMQ.zmq; 17 | using Newtonsoft.Json; 18 | using Poller = NetMQ.Poller; 19 | 20 | namespace Client.Comms 21 | { 22 | public class NetMQHeartBeatClient 23 | { 24 | private readonly NetMQContext context; 25 | private readonly string address; 26 | private Actor actor; 27 | private Subject subject; 28 | private static NetMQHeartBeatClient instance = null; 29 | private static object syncLock = new object(); 30 | protected int requiresInitialisation = 1; 31 | 32 | class ShimHandler : IShimHandler 33 | { 34 | private NetMQContext context; 35 | private SubscriberSocket subscriberSocket; 36 | private Subject subject; 37 | private string address; 38 | private Poller poller; 39 | private NetMQTimer timeoutTimer; 40 | private NetMQHeartBeatClient parent; 41 | 42 | public ShimHandler(NetMQContext context, Subject subject, string address) 43 | { 44 | this.context = context; 45 | this.address = address; 46 | this.subject = subject; 47 | } 48 | 49 | public void Initialise(object state) 50 | { 51 | parent = (NetMQHeartBeatClient) state; 52 | } 53 | 54 | public void RunPipeline(PairSocket shim) 55 | { 56 | // we should signal before running the poller but this will block the application 57 | shim.SignalOK(); 58 | 59 | this.poller = new Poller(); 60 | 61 | shim.ReceiveReady += OnShimReady; 62 | poller.AddSocket(shim); 63 | 64 | timeoutTimer = new NetMQTimer(StreamingProtocol.Timeout); 65 | timeoutTimer.Elapsed += TimeoutElapsed; 66 | poller.AddTimer(timeoutTimer); 67 | 68 | Connect(); 69 | 70 | poller.Start(); 71 | 72 | if (subscriberSocket != null) 73 | { 74 | subscriberSocket.Dispose(); 75 | } 76 | } 77 | 78 | private void Connect() 79 | { 80 | subscriberSocket = context.CreateSubscriberSocket(); 81 | subscriberSocket.Subscribe(StreamingProtocol.HeartbeatTopic); 82 | subscriberSocket.Connect(string.Format("tcp://{0}:{1}", address, StreamingProtocol.Port)); 83 | 84 | subject.OnNext(new ConnectionInfo(ConnectionStatus.Connecting, this.address)); 85 | 86 | 87 | subscriberSocket.ReceiveReady += OnSubscriberReady; 88 | 89 | poller.AddSocket(subscriberSocket); 90 | 91 | 92 | 93 | // reset timeout timer 94 | timeoutTimer.Enable = false; 95 | timeoutTimer.Enable = true; 96 | } 97 | 98 | private void TimeoutElapsed(object sender, NetMQTimerEventArgs e) 99 | { 100 | // no need to reconnect, the client would be recreated because of RX 101 | 102 | // because of RX internal stuff invoking on the poller thread block the entire application, so calling on Thread Pool 103 | Task.Run(() => 104 | { 105 | parent.requiresInitialisation = 1; 106 | subject.OnNext(new ConnectionInfo(ConnectionStatus.Closed, this.address)); 107 | }); 108 | } 109 | 110 | private void OnShimReady(object sender, NetMQSocketEventArgs e) 111 | { 112 | string command = e.Socket.ReceiveString(); 113 | 114 | if (command == ActorKnownMessages.END_PIPE) 115 | { 116 | poller.Stop(false); 117 | } 118 | } 119 | 120 | private void OnSubscriberReady(object sender, NetMQSocketEventArgs e) 121 | { 122 | string topic = subscriberSocket.ReceiveString(); 123 | 124 | if (topic == StreamingProtocol.HeartbeatTopic) 125 | { 126 | subject.OnNext(new ConnectionInfo(ConnectionStatus.Connected, this.address)); 127 | 128 | // reset timeout timer 129 | timeoutTimer.Enable = false; 130 | timeoutTimer.Enable = true; 131 | } 132 | } 133 | 134 | } 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | private NetMQHeartBeatClient(NetMQContext context, string address) 143 | { 144 | this.context = context; 145 | this.address = address; 146 | InitialiseComms(); 147 | } 148 | 149 | public static NetMQHeartBeatClient CreateInstance(NetMQContext context, string address) 150 | { 151 | if (instance == null) 152 | { 153 | lock (syncLock) 154 | { 155 | if (instance == null) 156 | { 157 | instance = new NetMQHeartBeatClient(context,address); 158 | } 159 | } 160 | } 161 | return instance; 162 | } 163 | 164 | public void InitialiseComms() 165 | { 166 | 167 | if (Interlocked.CompareExchange(ref requiresInitialisation, 0, 1) == 1) 168 | { 169 | if (actor != null) 170 | { 171 | this.actor.Dispose(); 172 | } 173 | 174 | subject = new Subject(); 175 | this.actor = new Actor(context, new ShimHandler(context, subject, address), this); 176 | } 177 | 178 | 179 | } 180 | 181 | public IObservable GetConnectionStatusStream() 182 | { 183 | return subject.AsObservable(); 184 | } 185 | 186 | 187 | public static NetMQHeartBeatClient Instance 188 | { 189 | get { return instance; } 190 | 191 | } 192 | 193 | 194 | 195 | 196 | 197 | } 198 | } 199 | -------------------------------------------------------------------------------- /Client/Comms/NetMQTickerClient.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reactive.Disposables; 5 | using System.Reactive.Linq; 6 | using System.Reactive.Subjects; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | using Client.Factory; 10 | using Client.Comms.Transport; 11 | using Common; 12 | using NetMQ; 13 | using NetMQ.Actors; 14 | using NetMQ.InProcActors; 15 | using NetMQ.Sockets; 16 | using NetMQ.zmq; 17 | using Newtonsoft.Json; 18 | using Poller = NetMQ.Poller; 19 | 20 | namespace Client.Comms 21 | { 22 | public class NetMQTickerClient : IDisposable 23 | { 24 | private Actor actor; 25 | private Subject subject; 26 | private CompositeDisposable disposables = new CompositeDisposable(); 27 | 28 | class ShimHandler : IShimHandler 29 | { 30 | private NetMQContext context; 31 | private SubscriberSocket subscriberSocket; 32 | private Subject subject; 33 | private string address; 34 | private Poller poller; 35 | private NetMQTimer timeoutTimer; 36 | 37 | public ShimHandler(NetMQContext context, Subject subject, string address) 38 | { 39 | this.context = context; 40 | this.address = address; 41 | this.subject = subject; 42 | } 43 | 44 | public void Initialise(object state) 45 | { 46 | 47 | } 48 | 49 | public void RunPipeline(PairSocket shim) 50 | { 51 | // we should signal before running the poller but this will block the application 52 | shim.SignalOK(); 53 | 54 | this.poller = new Poller(); 55 | 56 | shim.ReceiveReady += OnShimReady; 57 | poller.AddSocket(shim); 58 | 59 | timeoutTimer = new NetMQTimer(StreamingProtocol.Timeout); 60 | timeoutTimer.Elapsed += TimeoutElapsed; 61 | poller.AddTimer(timeoutTimer); 62 | 63 | Connect(); 64 | 65 | poller.Start(); 66 | 67 | if (subscriberSocket != null) 68 | { 69 | subscriberSocket.Dispose(); 70 | } 71 | } 72 | 73 | private void Connect() 74 | { 75 | // getting the snapshot 76 | using (RequestSocket requestSocket = context.CreateRequestSocket()) 77 | { 78 | 79 | requestSocket.Connect(string.Format("tcp://{0}:{1}", address, SnapshotProtocol.Port)); 80 | 81 | requestSocket.Send(SnapshotProtocol.GetTradessCommand); 82 | 83 | string json; 84 | 85 | requestSocket.Options.ReceiveTimeout = SnapshotProtocol.RequestTimeout; 86 | 87 | try 88 | { 89 | json = requestSocket.ReceiveString(); 90 | } 91 | catch (AgainException ex) 92 | { 93 | // Fail to receive trades, we call on error and don't try to do anything with subscriber 94 | // calling on error from poller thread block the application 95 | Task.Run(() => subject.OnError(new Exception("No response from server"))); 96 | return; 97 | } 98 | 99 | while (json != SnapshotProtocol.EndOfTickers) 100 | { 101 | PublishTicker(json); 102 | 103 | json = requestSocket.ReceiveString(); 104 | } 105 | } 106 | 107 | subscriberSocket = context.CreateSubscriberSocket(); 108 | subscriberSocket.Subscribe(StreamingProtocol.TradesTopic); 109 | subscriberSocket.Subscribe(StreamingProtocol.HeartbeatTopic); 110 | subscriberSocket.Connect(string.Format("tcp://{0}:{1}", address, StreamingProtocol.Port)); 111 | subscriberSocket.ReceiveReady += OnSubscriberReady; 112 | 113 | poller.AddSocket(subscriberSocket); 114 | 115 | // reset timeout timer 116 | timeoutTimer.Enable = false; 117 | timeoutTimer.Enable = true; 118 | } 119 | 120 | private void TimeoutElapsed(object sender, NetMQTimerEventArgs e) 121 | { 122 | // no need to reconnect, the client would be recreated because of RX 123 | 124 | // because of RX internal stuff invoking on the poller thread block the entire application, so calling on Thread Pool 125 | Task.Run(() => subject.OnError(new Exception("Disconnected from server"))); 126 | } 127 | 128 | private void OnShimReady(object sender, NetMQSocketEventArgs e) 129 | { 130 | string command = e.Socket.ReceiveString(); 131 | 132 | if (command == ActorKnownMessages.END_PIPE) 133 | { 134 | poller.Stop(false); 135 | } 136 | } 137 | 138 | private void OnSubscriberReady(object sender, NetMQSocketEventArgs e) 139 | { 140 | string topic = subscriberSocket.ReceiveString(); 141 | 142 | if (topic == StreamingProtocol.TradesTopic) 143 | { 144 | string json = subscriberSocket.ReceiveString(); 145 | PublishTicker(json); 146 | 147 | // reset timeout timer also when a quote is received 148 | timeoutTimer.Enable = false; 149 | timeoutTimer.Enable = true; 150 | } 151 | else if (topic == StreamingProtocol.HeartbeatTopic) 152 | { 153 | // reset timeout timer 154 | timeoutTimer.Enable = false; 155 | timeoutTimer.Enable = true; 156 | } 157 | } 158 | 159 | private void PublishTicker(string json) 160 | { 161 | TickerDto tickerDto = JsonConvert.DeserializeObject(json); 162 | subject.OnNext(tickerDto); 163 | } 164 | } 165 | 166 | public NetMQTickerClient(NetMQContext context, string address) 167 | { 168 | subject = new Subject(); 169 | 170 | this.actor = new Actor(context, new ShimHandler(context, subject, address), null); 171 | this.disposables.Add(this.actor); 172 | 173 | this.disposables.Add(NetMQHeartBeatClient.Instance.GetConnectionStatusStream() 174 | .Where(x => x.ConnectionStatus == ConnectionStatus.Closed) 175 | .Subscribe(x => 176 | this.subject.OnError(new InvalidOperationException("Connection to server has been lost")))); 177 | } 178 | 179 | public IObservable GetTickerStream() 180 | { 181 | return subject.AsObservable(); 182 | } 183 | 184 | public void Dispose() 185 | { 186 | this.disposables.Dispose(); 187 | } 188 | } 189 | } 190 | -------------------------------------------------------------------------------- /Client/Comms/ServiceClientBase.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reactive; 3 | using System.Reactive.Linq; 4 | using Client.Hub.Transport; 5 | using Common.Extensions; 6 | 7 | 8 | namespace Client.Hub 9 | { 10 | internal class ServiceClientBase 11 | { 12 | private readonly IConnectionProvider _connectionProvider; 13 | 14 | protected ServiceClientBase(IConnectionProvider connectionProvider) 15 | { 16 | _connectionProvider = connectionProvider; 17 | } 18 | 19 | protected IObservable GetResilientStream(Func> streamFactory, TimeSpan connectionTimeout) 20 | { 21 | var activeConnections = (from connection in _connectionProvider.GetActiveConnection() 22 | from status in connection.StatusStream 23 | where status.ConnectionStatus == ConnectionStatus.Connected || status.ConnectionStatus == ConnectionStatus.Reconnected 24 | select connection) 25 | .Publish() 26 | .RefCount(); 27 | 28 | // get the first connection 29 | var firstConnection = activeConnections.Take(1).Timeout(connectionTimeout); 30 | 31 | // 1 - notifies when the first connection gets disconnected 32 | var firstDisconnection = from connection in firstConnection 33 | from status in connection.StatusStream 34 | where status.ConnectionStatus == ConnectionStatus.Reconnecting || status.ConnectionStatus == ConnectionStatus.Closed 35 | select Unit.Default; 36 | 37 | // 2- connection provider created a new connection it means the active one has droped 38 | var subsequentConnection = activeConnections.Skip(1).Select(_ => Unit.Default).Take(1); 39 | 40 | // OnError when we get 1 or 2 41 | var disconnected = firstDisconnection.Merge(subsequentConnection) 42 | .Select(_ => Notification.CreateOnError(new Exception("Connection was closed."))) 43 | .Dematerialize(); 44 | 45 | // create a stream which will OnError as soon as the connection drops 46 | return (from connection in firstConnection 47 | from t in streamFactory(connection) 48 | select t) 49 | .Merge(disconnected) 50 | .Publish() 51 | .RefCount(); 52 | } 53 | 54 | protected IObservable RequestUponConnection(Func> factory, TimeSpan connectionTimeout) 55 | { 56 | return (from connection in _connectionProvider.GetActiveConnection().Take(1).Timeout(connectionTimeout) 57 | from t in factory(connection) 58 | select t) 59 | .CacheFirstResult(); 60 | } 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /Client/Comms/TickerClient.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reactive.Disposables; 5 | using System.Reactive.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | using Client.Comms.Transport; 9 | using Common; 10 | using NetMQ; 11 | 12 | namespace Client.Comms 13 | { 14 | public class TickerClient : ITickerClient 15 | { 16 | private readonly NetMQContext context; 17 | private readonly string address; 18 | 19 | public TickerClient(NetMQContext context, string address) 20 | { 21 | this.context = context; 22 | this.address = address; 23 | } 24 | 25 | public IObservable GetTickerStream() 26 | { 27 | return Observable.Create(observer => 28 | { 29 | NetMQTickerClient client = new NetMQTickerClient(context, address); 30 | 31 | 32 | 33 | var disposable = client.GetTickerStream().Subscribe(observer); 34 | return new CompositeDisposable { client, disposable }; 35 | }) 36 | .Publish() 37 | .RefCount(); 38 | } 39 | 40 | 41 | public IObservable ConnectionStatusStream() 42 | { 43 | return Observable.Create(observer => 44 | { 45 | NetMQHeartBeatClient.Instance.InitialiseComms(); 46 | 47 | var disposable = NetMQHeartBeatClient.Instance.GetConnectionStatusStream().Subscribe(observer); 48 | return new CompositeDisposable { disposable }; 49 | }) 50 | .Publish() 51 | .RefCount(); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /Client/Comms/Transport/Connection.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reactive; 5 | using System.Reactive.Disposables; 6 | using System.Reactive.Linq; 7 | using System.Reactive.Subjects; 8 | using System.Text; 9 | using System.Threading.Tasks; 10 | using Common; 11 | using Common.Extensions; 12 | using log4net; 13 | using Microsoft.AspNet.SignalR.Client; 14 | 15 | namespace Client.Hub.Transport 16 | { 17 | internal class Connection : IConnection 18 | { 19 | private readonly ISubject _statusStream; 20 | private readonly HubConnection hubConnection; 21 | 22 | private bool _initialized; 23 | private static readonly ILog log = LogManager.GetLogger(typeof(Connection)); 24 | 25 | public Connection(string address, string username) 26 | { 27 | _statusStream = new BehaviorSubject(new ConnectionInfo(ConnectionStatus.Uninitialized, address)); 28 | Address = address; 29 | hubConnection = new HubConnection(address); 30 | //hubConnection.Headers.Add(ServiceConstants.Server.UsernameHeader, username); 31 | CreateStatus().Subscribe( 32 | s => _statusStream.OnNext(new ConnectionInfo(s, address)), 33 | _statusStream.OnError, 34 | _statusStream.OnCompleted); 35 | hubConnection.Error += exception => log.Error("There was a connection error with " + address, exception); 36 | 37 | TickerHubProxy = hubConnection.CreateHubProxy(ServiceConstants.Server.TickerHub); 38 | 39 | } 40 | 41 | public IObservable Initialize() 42 | { 43 | if (_initialized) 44 | { 45 | throw new InvalidOperationException("Connection has already been initialized"); 46 | } 47 | _initialized = true; 48 | 49 | return Observable.Create(async observer => 50 | { 51 | _statusStream.OnNext(new ConnectionInfo(ConnectionStatus.Connecting, Address)); 52 | 53 | try 54 | { 55 | log.InfoFormat("Connecting to {0}", Address); 56 | await hubConnection.Start(); 57 | _statusStream.OnNext(new ConnectionInfo(ConnectionStatus.Connected, Address)); 58 | observer.OnNext(Unit.Default); 59 | } 60 | catch (Exception e) 61 | { 62 | log.Error("An error occurred when starting SignalR connection", e); 63 | observer.OnError(e); 64 | } 65 | 66 | return Disposable.Create(() => 67 | { 68 | try 69 | { 70 | log.Info("Stoping connection..."); 71 | hubConnection.Stop(); 72 | log.Info("Connection stopped"); 73 | } 74 | catch (Exception e) 75 | { 76 | // we must never throw in a disposable 77 | log.Error("An error occurred while stoping connection", e); 78 | } 79 | }); 80 | }) 81 | .Publish() 82 | .RefCount(); 83 | } 84 | 85 | private IObservable CreateStatus() 86 | { 87 | var closed = Observable.FromEvent(h => hubConnection.Closed += h, h => hubConnection.Closed -= h).Select(_ => ConnectionStatus.Closed); 88 | var connectionSlow = Observable.FromEvent(h => hubConnection.ConnectionSlow += h, h => hubConnection.ConnectionSlow -= h).Select(_ => ConnectionStatus.ConnectionSlow); 89 | var reconnected = Observable.FromEvent(h => hubConnection.Reconnected += h, h => hubConnection.Reconnected -= h).Select(_ => ConnectionStatus.Reconnected); 90 | var reconnecting = Observable.FromEvent(h => hubConnection.Reconnecting += h, h => hubConnection.Reconnecting -= h).Select(_ => ConnectionStatus.Reconnecting); 91 | return Observable.Merge(closed, connectionSlow, reconnected, reconnecting) 92 | .TakeUntilInclusive(status => status == ConnectionStatus.Closed); // complete when the connection is closed (it's terminal, SignalR will not attempt to reconnect anymore) 93 | } 94 | 95 | public IObservable StatusStream 96 | { 97 | get { return _statusStream; } 98 | } 99 | 100 | public string Address { get; private set; } 101 | 102 | public IHubProxy TickerHubProxy { get; private set; } 103 | 104 | public void SetAuthToken(string authToken) 105 | { 106 | //hubConnection.Headers[AuthTokenProvider.AuthTokenKey] = authToken; 107 | } 108 | 109 | public override string ToString() 110 | { 111 | return string.Format("Address: {0}", Address); 112 | } 113 | } 114 | 115 | } 116 | -------------------------------------------------------------------------------- /Client/Comms/Transport/ConnectionInfo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Client.Comms.Transport 8 | { 9 | public class ConnectionInfo 10 | { 11 | public ConnectionStatus ConnectionStatus { get; private set; } 12 | public string Server { get; private set; } 13 | 14 | public ConnectionInfo(ConnectionStatus connectionStatus, string server) 15 | { 16 | ConnectionStatus = connectionStatus; 17 | Server = server; 18 | } 19 | 20 | public override string ToString() 21 | { 22 | return string.Format("ConnectionStatus: {0}, Server: {1}", ConnectionStatus, Server); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Client/Comms/Transport/ConnectionProvider.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reactive.Disposables; 5 | using System.Reactive.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | using log4net; 9 | using Common.Extensions; 10 | 11 | 12 | namespace Client.Hub.Transport 13 | { 14 | /// 15 | /// Connection provider provides always the same connection until it fails then create a new one a yield it 16 | /// Connection provider randomizes the list of server specified in configuration and then round robin through the list 17 | /// 18 | internal class ConnectionProvider : IConnectionProvider, IDisposable 19 | { 20 | private readonly SingleAssignmentDisposable disposable = new SingleAssignmentDisposable(); 21 | private readonly string username; 22 | private readonly IObservable connectionSequence; 23 | private readonly string server; 24 | private int _currentIndex; 25 | private static readonly ILog log = LogManager.GetLogger(typeof(ConnectionProvider)); 26 | 27 | public ConnectionProvider(string username, string server) 28 | { 29 | this.username = username; 30 | this.server = server; 31 | connectionSequence = CreateConnectionSequence(); 32 | } 33 | 34 | public IObservable GetActiveConnection() 35 | { 36 | return connectionSequence; 37 | } 38 | 39 | public void Dispose() 40 | { 41 | disposable.Dispose(); 42 | } 43 | 44 | private IObservable CreateConnectionSequence() 45 | { 46 | return Observable.Create(o => 47 | { 48 | log.Info("Creating new connection..."); 49 | var connection = GetNextConnection(); 50 | 51 | var statusSubscription = connection.StatusStream.Subscribe( 52 | _ => { }, 53 | ex => o.OnCompleted(), 54 | () => 55 | { 56 | log.Info("Status subscription completed"); 57 | o.OnCompleted(); 58 | }); 59 | 60 | var connectionSubscription = 61 | connection.Initialize().Subscribe( 62 | _ => o.OnNext(connection), 63 | ex => o.OnCompleted(), 64 | o.OnCompleted); 65 | 66 | return new CompositeDisposable { statusSubscription, connectionSubscription }; 67 | }) 68 | .Repeat() 69 | .Replay(1) 70 | .LazilyConnect(disposable); 71 | } 72 | 73 | private IConnection GetNextConnection() 74 | { 75 | return new Client.Hub.Transport.Connection(server, username); 76 | } 77 | } 78 | 79 | } 80 | -------------------------------------------------------------------------------- /Client/Comms/Transport/ConnectionStatus.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Client.Comms.Transport 8 | { 9 | public enum ConnectionStatus 10 | { 11 | Connecting, 12 | Connected, 13 | Closed 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Client/Comms/Transport/IConnection.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reactive; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using Microsoft.AspNet.SignalR.Client; 8 | 9 | namespace Client.Hub.Transport 10 | { 11 | internal interface IConnection 12 | { 13 | IObservable StatusStream { get; } 14 | IObservable Initialize(); 15 | string Address { get; } 16 | void SetAuthToken(string authToken); 17 | IHubProxy TickerHubProxy { get; } 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Client/Comms/Transport/IConnectionProvider - Copy.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Client.Hub.Transport 8 | { 9 | /// 10 | /// Connection provider provides always the same connection until it fails then create a new one a yield it 11 | /// Connection provider randomizes the list of server specified in configuration and then round robin through the list 12 | /// 13 | internal class ConnectionProvider : IConnectionProvider, IDisposable 14 | { 15 | private readonly SingleAssignmentDisposable _disposable = new SingleAssignmentDisposable(); 16 | private readonly string _username; 17 | private readonly IObservable _connectionSequence; 18 | private readonly string[] _servers; 19 | private readonly ILoggerFactory _loggerFactory; 20 | 21 | private int _currentIndex; 22 | private ILog _log; 23 | 24 | public ConnectionProvider(string username, string[] servers, ILoggerFactory loggerFactory) 25 | { 26 | _username = username; 27 | _servers = servers; 28 | _loggerFactory = loggerFactory; 29 | _servers.Shuffle(); 30 | _log = _loggerFactory.Create(typeof(ConnectionProvider)); 31 | 32 | _connectionSequence = CreateConnectionSequence(); 33 | } 34 | 35 | public IObservable GetActiveConnection() 36 | { 37 | return _connectionSequence; 38 | } 39 | 40 | public void Dispose() 41 | { 42 | _disposable.Dispose(); 43 | } 44 | 45 | private IObservable CreateConnectionSequence() 46 | { 47 | return Observable.Create(o => 48 | { 49 | _log.Info("Creating new connection..."); 50 | var connection = GetNextConnection(); 51 | 52 | var statusSubscription = connection.StatusStream.Subscribe( 53 | _ => { }, 54 | ex => o.OnCompleted(), 55 | () => 56 | { 57 | _log.Info("Status subscription completed"); 58 | o.OnCompleted(); 59 | }); 60 | 61 | var connectionSubscription = 62 | connection.Initialize().Subscribe( 63 | _ => o.OnNext(connection), 64 | ex => o.OnCompleted(), 65 | o.OnCompleted); 66 | 67 | return new CompositeDisposable { statusSubscription, connectionSubscription }; 68 | }) 69 | .Repeat() 70 | .Replay(1) 71 | .LazilyConnect(_disposable); 72 | } 73 | 74 | private IConnection GetNextConnection() 75 | { 76 | var connection = new Connection(_servers[_currentIndex++], _username, _loggerFactory); 77 | if (_currentIndex == _servers.Length) 78 | { 79 | _currentIndex = 0; 80 | } 81 | return connection; 82 | } 83 | } 84 | 85 | } 86 | -------------------------------------------------------------------------------- /Client/Comms/Transport/IConnectionProvider.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Client.Hub.Transport 8 | { 9 | internal interface IConnectionProvider 10 | { 11 | IObservable GetActiveConnection(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Client/Factory/ITickerFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using Common; 7 | 8 | namespace Client.Factory 9 | { 10 | interface ITickerFactory 11 | { 12 | Ticker Create(TickerDto ticker); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Client/Factory/Ticker.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Client.Factory 8 | { 9 | public class Ticker 10 | { 11 | public Ticker(string name, decimal price) 12 | { 13 | Name = name; 14 | Price = price; 15 | } 16 | 17 | public string Name { get; set; } 18 | public decimal Price { get; set; } 19 | 20 | public override string ToString() 21 | { 22 | return string.Format("Name: {0}, Price: {1}", Name, Price); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Client/Factory/TickerFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using Common; 7 | 8 | namespace Client.Factory 9 | { 10 | internal class TickerFactory : ITickerFactory 11 | { 12 | public Ticker Create(TickerDto ticker) 13 | { 14 | return new Ticker( 15 | ticker.Name, 16 | ticker.Price); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Client/IOC/Bootstrapper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using Autofac; 7 | using Client.Factory; 8 | using Client.Services; 9 | using Client.ViewModels; 10 | using Client.ViewModels.MainWindow; 11 | 12 | namespace Client.IOC 13 | { 14 | public class Bootstrapper 15 | { 16 | public IContainer Build() 17 | { 18 | var builder = new ContainerBuilder(); 19 | 20 | 21 | builder.RegisterType().As().SingleInstance(); 22 | builder.RegisterType().As(); 23 | builder.RegisterType().As(); 24 | 25 | 26 | // UI 27 | builder.RegisterType().SingleInstance(); 28 | builder.RegisterType().SingleInstance(); 29 | builder.RegisterType().SingleInstance(); 30 | builder.RegisterType().SingleInstance(); 31 | builder.RegisterType().SingleInstance(); 32 | 33 | 34 | return builder.Build(); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Client/MainWindow.xaml: -------------------------------------------------------------------------------- 1 |  9 | 10 | 11 | 12 | 13 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 52 | 53 | 54 | 55 | 56 | 62 | 63 | 67 | 68 | 70 | 72 | 73 | 74 | 75 | 76 | 77 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | -------------------------------------------------------------------------------- /Client/MainWindow.xaml.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using System.Windows; 7 | using System.Windows.Controls; 8 | using System.Windows.Data; 9 | using System.Windows.Documents; 10 | using System.Windows.Input; 11 | using System.Windows.Media; 12 | using System.Windows.Media.Imaging; 13 | using System.Windows.Navigation; 14 | using System.Windows.Shapes; 15 | using Client.ViewModels.MainWindow; 16 | 17 | namespace Client 18 | { 19 | /// 20 | /// Interaction logic for MainWindow.xaml 21 | /// 22 | public partial class MainWindow : Window 23 | { 24 | public MainWindow( 25 | MainWindowViewModel mainWindowViewModel) 26 | { 27 | this.DataContext = mainWindowViewModel; 28 | InitializeComponent(); 29 | } 30 | 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Client/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Client")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Client")] 13 | [assembly: AssemblyCopyright("Copyright © 2014")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("afec2c7d-48d2-41d9-bfe5-61e192c8be1c")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Client/Repositories/ITickerRepository.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using Client.Factory; 7 | using Common; 8 | 9 | namespace Client.Repositories 10 | { 11 | public interface ITickerRepository 12 | { 13 | IObservable GetTickerStream(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Client/Repositories/TickerRepository.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reactive.Linq; 5 | using Client.Factory; 6 | using Client.Comms; 7 | 8 | namespace Client.Repositories 9 | { 10 | class TickerRepository : ITickerRepository 11 | { 12 | private readonly ITickerClient tickerClient; 13 | private readonly ITickerFactory tickerFactory; 14 | 15 | public TickerRepository(ITickerClient tickerClient, ITickerFactory tickerFactory) 16 | { 17 | this.tickerClient = tickerClient; 18 | this.tickerFactory = tickerFactory; 19 | } 20 | 21 | public IObservable GetTickerStream() 22 | { 23 | return Observable.Defer(() => tickerClient.GetTickerStream()) 24 | .Select(tickerFactory.Create) 25 | .Catch(Observable.Empty()) 26 | .Repeat() 27 | .Publish() 28 | .RefCount(); 29 | } 30 | 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Client/Services/ConcurrencyService.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reactive.Concurrency; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace Client.Services 9 | { 10 | public sealed class ConcurrencyService : IConcurrencyService 11 | { 12 | 13 | public IScheduler Dispatcher 14 | { 15 | get { return DispatcherScheduler.Current; } 16 | } 17 | 18 | public IScheduler TaskPool 19 | { 20 | get { return ThreadPoolScheduler.Instance; } 21 | } 22 | 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Client/Services/IConcurrencyService.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reactive.Concurrency; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace Client.Services 9 | { 10 | public interface IConcurrencyService 11 | { 12 | 13 | IScheduler Dispatcher { get; } 14 | 15 | 16 | IScheduler TaskPool { get; } 17 | 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Client/Services/IReactiveTrader.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using Client.Comms.Transport; 7 | using Client.Repositories; 8 | using log4net.Repository.Hierarchy; 9 | 10 | namespace Client.Services 11 | { 12 | public interface IReactiveTrader 13 | { 14 | ITickerRepository TickerRepository { get; } 15 | IObservable ConnectionStatusStream { get; } 16 | void Initialize(string username, string server); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Client/Services/IUserProvider.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Client.Services 8 | { 9 | 10 | public interface IUserProvider 11 | { 12 | string Username { get; } 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /Client/Services/ReactiveTrader.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reactive.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using Client.Factory; 8 | using Client.Comms; 9 | using Client.Comms.Transport; 10 | using Client.Hub; 11 | using Client.Repositories; 12 | using log4net; 13 | using NetMQ; 14 | 15 | namespace Client.Services 16 | { 17 | public class ReactiveTrader : IReactiveTrader, IDisposable 18 | { 19 | 20 | private static readonly ILog log = LogManager.GetLogger(typeof(ReactiveTrader)); 21 | 22 | private NetMQContext context; 23 | 24 | 25 | public void Initialize(string username, string server) 26 | { 27 | var concurrencyService = new ConcurrencyService(); 28 | 29 | context = NetMQContext.Create(); 30 | 31 | var tickerClient = new TickerClient(context, server); 32 | var netMQHeartBeatClient = NetMQHeartBeatClient.CreateInstance(context, server); 33 | HeartBeatClient = new HeartBeatClient(); 34 | 35 | var tickerFactory = new TickerFactory(); 36 | TickerRepository = new TickerRepository(tickerClient, tickerFactory); 37 | } 38 | 39 | public ITickerRepository TickerRepository { get; private set; } 40 | public IHeartBeatClient HeartBeatClient { get; private set; } 41 | 42 | 43 | public IObservable ConnectionStatusStream 44 | { 45 | get 46 | { 47 | return HeartBeatClient.ConnectionStatusStream() 48 | .Repeat() 49 | .Publish() 50 | .RefCount(); 51 | } 52 | } 53 | 54 | public void Dispose() 55 | { 56 | context.Dispose(); 57 | } 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /Client/Services/UserProvider.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Client.Services 8 | { 9 | 10 | 11 | internal class UserProvider : IUserProvider 12 | { 13 | public string Username 14 | { 15 | get { return "WPF-" + new Random().Next(1000); } 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Client/ValueConverters/DecimalPlaceFormatter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Globalization; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using System.Windows; 8 | using System.Windows.Data; 9 | 10 | namespace Client.ValueConverters 11 | { 12 | [ValueConversion(typeof(decimal), typeof(string))] 13 | public class DoubleToStringConverter : IValueConverter 14 | { 15 | private DoubleToStringConverter() 16 | { 17 | 18 | } 19 | static DoubleToStringConverter() 20 | { 21 | Instance = new DoubleToStringConverter(); 22 | } 23 | 24 | public static DoubleToStringConverter Instance { get; private set; } 25 | 26 | public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 27 | { 28 | return value == null ? null : ((decimal)value).ToString("#,0.##"); 29 | } 30 | 31 | public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 32 | { 33 | decimal retValue; 34 | if (decimal.TryParse(value as string, out retValue)) 35 | { 36 | return retValue; 37 | } 38 | return DependencyProperty.UnsetValue; 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Client/ViewModels/ConnectivityStatusViewModel.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reactive.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using Client.Comms.Transport; 8 | using Client.Services; 9 | using log4net; 10 | 11 | namespace Client.ViewModels 12 | { 13 | public class ConnectivityStatusViewModel : INPCBase 14 | { 15 | private static readonly ILog log = LogManager.GetLogger(typeof(ConnectivityStatusViewModel)); 16 | private string server; 17 | private string status; 18 | private bool disconnected; 19 | 20 | 21 | 22 | public ConnectivityStatusViewModel( 23 | IReactiveTrader reactiveTrader, 24 | IConcurrencyService concurrencyService) 25 | { 26 | 27 | 28 | reactiveTrader.ConnectionStatusStream 29 | .ObserveOn(concurrencyService.Dispatcher) 30 | .SubscribeOn(concurrencyService.TaskPool) 31 | .Subscribe( 32 | OnStatusChange, 33 | ex => log.Error("An error occurred within the connection status stream.", ex)); 34 | 35 | } 36 | 37 | 38 | private void OnStatusChange(ConnectionInfo connectionInfo) 39 | { 40 | Server = connectionInfo.Server; 41 | 42 | switch (connectionInfo.ConnectionStatus) 43 | { 44 | case ConnectionStatus.Connecting: 45 | Status = "Connecting..."; 46 | Disconnected = true; 47 | break; 48 | case ConnectionStatus.Connected: 49 | Status = "Connected"; 50 | Disconnected = false; 51 | break; 52 | case ConnectionStatus.Closed: 53 | Status = "Disconnected"; 54 | Disconnected = true; 55 | break; 56 | default: 57 | throw new ArgumentOutOfRangeException(); 58 | } 59 | } 60 | 61 | 62 | public string Server 63 | { 64 | get { return this.server; } 65 | set 66 | { 67 | this.server = value; 68 | base.OnPropertyChanged("Server"); 69 | } 70 | } 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | public string Status 79 | { 80 | get { return this.status; } 81 | set 82 | { 83 | this.status = value; 84 | base.OnPropertyChanged("Status"); 85 | } 86 | } 87 | 88 | 89 | 90 | public bool Disconnected 91 | { 92 | get { return this.disconnected; } 93 | set 94 | { 95 | this.disconnected = value; 96 | base.OnPropertyChanged("Disconnected"); 97 | } 98 | } 99 | 100 | } 101 | 102 | } 103 | -------------------------------------------------------------------------------- /Client/ViewModels/INPCBase.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace Client.ViewModels 9 | { 10 | public class INPCBase : INotifyPropertyChanged 11 | { 12 | public event PropertyChangedEventHandler PropertyChanged; 13 | 14 | protected virtual void OnPropertyChanged(string propertyName) 15 | { 16 | var handler = PropertyChanged; 17 | if (handler != null) 18 | { 19 | handler(this, new PropertyChangedEventArgs(propertyName)); 20 | } 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Client/ViewModels/MainWindowViewModel.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Collections.ObjectModel; 4 | using System.Linq; 5 | using System.Reactive.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | using System.Windows.Input; 9 | using Client.Factory; 10 | using Client.Repositories; 11 | using Client.Services; 12 | using Common; 13 | using Common.ViewModels; 14 | using log4net; 15 | 16 | 17 | namespace Client.ViewModels.MainWindow 18 | { 19 | public class MainWindowViewModel 20 | { 21 | public MainWindowViewModel( 22 | TickersViewModel tickersViewModel, 23 | ConnectivityStatusViewModel connectivityStatusViewModel 24 | 25 | ) 26 | { 27 | this.TickersViewModel = tickersViewModel; 28 | this.ConnectivityStatusViewModel = connectivityStatusViewModel; 29 | } 30 | 31 | 32 | public TickersViewModel TickersViewModel { get; private set; } 33 | public ConnectivityStatusViewModel ConnectivityStatusViewModel { get; private set; } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Client/ViewModels/TickerViewModel.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reactive.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using Client.Comms.Transport; 8 | using Client.Services; 9 | using log4net; 10 | 11 | namespace Client.ViewModels 12 | { 13 | public class TickerViewModel : INPCBase 14 | { 15 | private decimal price; 16 | private bool isUp; 17 | private bool stale; 18 | private bool disconnected; 19 | private static readonly ILog log = LogManager.GetLogger(typeof(TickerViewModel)); 20 | 21 | 22 | public TickerViewModel( 23 | IReactiveTrader reactiveTrader, 24 | IConcurrencyService concurrencyService, 25 | string name) 26 | { 27 | 28 | this.Name = name; 29 | 30 | reactiveTrader.ConnectionStatusStream 31 | .ObserveOn(concurrencyService.Dispatcher) 32 | .SubscribeOn(concurrencyService.TaskPool) 33 | .Subscribe( 34 | OnStatusChange, 35 | ex => log.Error("An error occurred within the connection status stream.", ex)); 36 | } 37 | 38 | 39 | public string Name { get; private set; } 40 | 41 | 42 | public void AcceptNewPrice(decimal newPrice) 43 | { 44 | IsUp = newPrice > price; 45 | Price = newPrice; 46 | } 47 | 48 | 49 | public decimal Price 50 | { 51 | get { return this.price; } 52 | private set 53 | { 54 | this.price = value; 55 | base.OnPropertyChanged("Price"); 56 | } 57 | } 58 | 59 | public bool IsUp 60 | { 61 | get { return this.isUp; } 62 | private set 63 | { 64 | this.isUp = value; 65 | base.OnPropertyChanged("IsUp"); 66 | } 67 | } 68 | 69 | public bool Stale 70 | { 71 | get { return this.stale; } 72 | set 73 | { 74 | this.stale = value; 75 | base.OnPropertyChanged("Stale"); 76 | } 77 | } 78 | 79 | public bool Disconnected 80 | { 81 | get { return this.disconnected; } 82 | set 83 | { 84 | this.disconnected = value; 85 | base.OnPropertyChanged("Disconnected"); 86 | } 87 | } 88 | 89 | 90 | private void OnStatusChange(ConnectionInfo connectionInfo) 91 | { 92 | 93 | switch (connectionInfo.ConnectionStatus) 94 | { 95 | case ConnectionStatus.Connecting: 96 | Disconnected = true; 97 | break; 98 | case ConnectionStatus.Connected: 99 | Disconnected = false; 100 | break; 101 | case ConnectionStatus.Closed: 102 | Disconnected = true; 103 | break; 104 | default: 105 | throw new ArgumentOutOfRangeException(); 106 | } 107 | } 108 | 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /Client/ViewModels/TickerViewModelFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using Client.Services; 7 | 8 | namespace Client.ViewModels 9 | { 10 | public class TickerViewModelFactory 11 | { 12 | private readonly IReactiveTrader reactiveTrader; 13 | private readonly IConcurrencyService concurrencyService; 14 | 15 | 16 | public TickerViewModelFactory( 17 | IReactiveTrader reactiveTrader, 18 | IConcurrencyService concurrencyService) 19 | { 20 | this.reactiveTrader = reactiveTrader; 21 | this.concurrencyService = concurrencyService; 22 | } 23 | 24 | public TickerViewModel Create(string name) 25 | { 26 | return new TickerViewModel(reactiveTrader, concurrencyService, name); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Client/ViewModels/TickersViewModel.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Collections.ObjectModel; 4 | using System.Linq; 5 | using System.Reactive.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | using System.Windows.Input; 9 | using Client.Factory; 10 | using Client.Repositories; 11 | using Client.Services; 12 | using Common; 13 | using Common.ViewModels; 14 | using log4net; 15 | 16 | 17 | namespace Client.ViewModels 18 | { 19 | public class TickersViewModel : INPCBase 20 | { 21 | private readonly ITickerRepository tickerRepository; 22 | private readonly IConcurrencyService concurrencyService; 23 | private bool stale = false; 24 | private static readonly ILog log = LogManager.GetLogger(typeof(TickersViewModel)); 25 | 26 | public TickersViewModel(IReactiveTrader reactiveTrader, 27 | IConcurrencyService concurrencyService, 28 | TickerViewModelFactory tickerViewModelFactory) 29 | { 30 | Tickers = new ObservableCollection(); 31 | Tickers.Add(tickerViewModelFactory.Create("Yahoo")); 32 | Tickers.Add(tickerViewModelFactory.Create("Google")); 33 | Tickers.Add(tickerViewModelFactory.Create("Apple")); 34 | Tickers.Add(tickerViewModelFactory.Create("Facebook")); 35 | Tickers.Add(tickerViewModelFactory.Create("Microsoft")); 36 | Tickers.Add(tickerViewModelFactory.Create("Twitter")); 37 | this.tickerRepository = reactiveTrader.TickerRepository; 38 | this.concurrencyService = concurrencyService; 39 | LoadTrades(); 40 | 41 | 42 | 43 | } 44 | 45 | 46 | public ObservableCollection Tickers { get; private set; } 47 | 48 | private void LoadTrades() 49 | { 50 | tickerRepository.GetTickerStream() 51 | .ObserveOn(concurrencyService.Dispatcher) 52 | .SubscribeOn(concurrencyService.TaskPool) 53 | .Subscribe( 54 | AddTicker, 55 | ex => log.Error("An error occurred within the trade stream", ex)); 56 | } 57 | 58 | private void AddTicker(Ticker ticker) 59 | { 60 | //var allTickers = incomingTickers as IList ?? incomingTickers.ToList(); 61 | //if (!allTickers.Any()) 62 | //{ 63 | // // empty list of trades means we are disconnected 64 | // stale = true; 65 | //} 66 | //else 67 | //{ 68 | // if (stale) 69 | // { 70 | // stale = false; 71 | // } 72 | //} 73 | 74 | //foreach (var ticker in allTickers) 75 | //{ 76 | // Tickers.Single(x => x.Name == ticker.Name) 77 | // .AcceptNewPrice(ticker.Price); 78 | //} 79 | 80 | Tickers.Single(x => x.Name == ticker.Name) 81 | .AcceptNewPrice(ticker.Price); 82 | } 83 | 84 | 85 | private void UpdateTickerViewModelsStaleState(bool stale) 86 | { 87 | foreach (var tickerViewModel in Tickers) 88 | { 89 | tickerViewModel.Stale = stale; 90 | } 91 | } 92 | 93 | 94 | 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /Client/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /Common/.gitignore: -------------------------------------------------------------------------------- 1 | # Build Folders (you can keep bin if you'd like, to store dlls and pdbs) 2 | [Bb]in/ 3 | [Oo]bj/ 4 | 5 | # mstest test results 6 | TestResults 7 | 8 | ## Ignore Visual Studio temporary files, build results, and 9 | ## files generated by popular Visual Studio add-ons. 10 | 11 | # User-specific files 12 | *.suo 13 | *.user 14 | *.sln.docstates 15 | 16 | # Build results 17 | [Dd]ebug/ 18 | [Rr]elease/ 19 | x64/ 20 | *_i.c 21 | *_p.c 22 | *.ilk 23 | *.meta 24 | *.obj 25 | *.pch 26 | *.pdb 27 | *.pgc 28 | *.pgd 29 | *.rsp 30 | *.sbr 31 | *.tlb 32 | *.tli 33 | *.tlh 34 | *.tmp 35 | *.log 36 | *.vspscc 37 | *.vssscc 38 | .builds 39 | 40 | # Visual C++ cache files 41 | ipch/ 42 | *.aps 43 | *.ncb 44 | *.opensdf 45 | *.sdf 46 | 47 | # Visual Studio profiler 48 | *.psess 49 | *.vsp 50 | *.vspx 51 | 52 | # Guidance Automation Toolkit 53 | *.gpState 54 | 55 | # ReSharper is a .NET coding add-in 56 | _ReSharper* 57 | 58 | # NCrunch 59 | *.ncrunch* 60 | .*crunch*.local.xml 61 | 62 | # Installshield output folder 63 | [Ee]xpress 64 | 65 | # DocProject is a documentation generator add-in 66 | DocProject/buildhelp/ 67 | DocProject/Help/*.HxT 68 | DocProject/Help/*.HxC 69 | DocProject/Help/*.hhc 70 | DocProject/Help/*.hhk 71 | DocProject/Help/*.hhp 72 | DocProject/Help/Html2 73 | DocProject/Help/html 74 | 75 | # Click-Once directory 76 | publish 77 | 78 | # Publish Web Output 79 | *.Publish.xml 80 | 81 | # NuGet Packages Directory 82 | packages 83 | 84 | # Windows Azure Build Output 85 | csx 86 | *.build.csdef 87 | 88 | # Others 89 | [Bb]in 90 | [Oo]bj 91 | sql 92 | TestResults 93 | [Tt]est[Rr]esult* 94 | *.Cache 95 | ClientBin 96 | [Ss]tyle[Cc]op.* 97 | ~$* 98 | *.dbmdl 99 | Generated_Code #added for RIA/Silverlight projects 100 | 101 | # Backup & report files from converting an old project file to a newer 102 | # Visual Studio version. Backup files are not needed, because we have git ;-) 103 | _UpgradeReport_Files/ 104 | Backup*/ 105 | UpgradeLog*.XML -------------------------------------------------------------------------------- /Common/Common.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {09ECEA5A-A3CD-4F9A-92DC-7D282BE5BC0E} 8 | Library 9 | Properties 10 | Common 11 | Common 12 | v4.5 13 | 512 14 | 15 | 16 | true 17 | full 18 | false 19 | bin\Debug\ 20 | DEBUG;TRACE 21 | prompt 22 | 4 23 | 24 | 25 | pdbonly 26 | true 27 | bin\Release\ 28 | TRACE 29 | prompt 30 | 4 31 | 32 | 33 | 34 | 35 | 36 | 37 | ..\packages\Rx-Core.2.2.5\lib\net45\System.Reactive.Core.dll 38 | 39 | 40 | ..\packages\Rx-Interfaces.2.2.5\lib\net45\System.Reactive.Interfaces.dll 41 | 42 | 43 | ..\packages\Rx-Linq.2.2.5\lib\net45\System.Reactive.Linq.dll 44 | 45 | 46 | ..\packages\Rx-PlatformServices.2.2.5\lib\net45\System.Reactive.PlatformServices.dll 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 73 | -------------------------------------------------------------------------------- /Common/Extensions/ObservableExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reactive.Disposables; 5 | using System.Reactive.Linq; 6 | using System.Reactive.Subjects; 7 | using System.Text; 8 | using System.Threading; 9 | using System.Threading.Tasks; 10 | 11 | namespace Common.Extensions 12 | { 13 | public static class ObservableExtensions 14 | { 15 | public static IObservable LazilyConnect(this IConnectableObservable connectable, SingleAssignmentDisposable futureDisposable) 16 | { 17 | var connected = 0; 18 | return Observable.Create(observer => 19 | { 20 | var subscription = connectable.Subscribe(observer); 21 | if (Interlocked.CompareExchange(ref connected, 1, 0) == 0) 22 | { 23 | if (!futureDisposable.IsDisposed) 24 | { 25 | futureDisposable.Disposable = connectable.Connect(); 26 | } 27 | } 28 | return subscription; 29 | }).AsObservable(); 30 | } 31 | 32 | 33 | public static IObservable TakeUntilInclusive(this IObservable source, Func predicate) 34 | { 35 | return Observable.Create( 36 | observer => source.Subscribe( 37 | item => 38 | { 39 | observer.OnNext(item); 40 | if (predicate(item)) 41 | observer.OnCompleted(); 42 | }, 43 | observer.OnError, 44 | observer.OnCompleted 45 | ) 46 | ); 47 | } 48 | 49 | public static IObservable CacheFirstResult(this IObservable observable) 50 | { 51 | // We are happy to lose the underlying subscription here because we have .Take(1) the source stream. 52 | return observable.Take(1).PublishLast().LazilyConnect(new SingleAssignmentDisposable()); 53 | } 54 | 55 | 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /Common/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Common")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Common")] 13 | [assembly: AssemblyCopyright("Copyright © 2014")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("95fd9a72-1453-4564-b24d-f451e3a0733b")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Common/SnapshotProtocol.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Common 8 | { 9 | public static class SnapshotProtocol 10 | { 11 | public const string GetTradessCommand = "GT"; 12 | public const string EndOfTickers = "~~~EOT"; 13 | public const int Port = 5264; 14 | public static readonly TimeSpan RequestTimeout = TimeSpan.FromSeconds(5); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Common/StreamingProtocol.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Common 8 | { 9 | public static class StreamingProtocol 10 | { 11 | public const int Port = 5263; 12 | public const string TradesTopic = "Trades"; 13 | public const string HeartbeatTopic = "HB"; 14 | public static readonly TimeSpan HeartbeatInterval = TimeSpan.FromSeconds(2); 15 | public static readonly TimeSpan Timeout = TimeSpan.FromSeconds(5); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Common/TickerDto.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Common 8 | { 9 | public class TickerDto 10 | { 11 | public string Name { get; set; } 12 | public decimal Price { get; set; } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Common/ViewModels/DelegateCommand.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using System.Windows.Input; 7 | 8 | namespace Common.ViewModels 9 | { 10 | public class DelegateCommand : ICommand 11 | { 12 | private readonly Func _canExecute; 13 | private readonly Action _execute; 14 | 15 | public DelegateCommand(Action execute) 16 | : this(execute, null) 17 | { } 18 | 19 | public DelegateCommand(Action execute, Func canExecute) 20 | { 21 | _execute = execute; 22 | _canExecute = canExecute; 23 | } 24 | 25 | public bool CanExecute(object parameter) 26 | { 27 | if (_canExecute == null) 28 | { 29 | return true; 30 | } 31 | return _canExecute(); 32 | } 33 | 34 | public void Execute(object parameter) 35 | { 36 | _execute(); 37 | } 38 | 39 | public event EventHandler CanExecuteChanged 40 | { 41 | add { CommandManager.RequerySuggested += value; } 42 | remove { CommandManager.RequerySuggested -= value; } 43 | } 44 | 45 | 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Common/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /NetMQDemo.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2013 4 | VisualStudioVersion = 12.0.30723.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Client", "Client\Client.csproj", "{B43264BB-E309-4168-8770-EC0127C63912}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Common", "Common\Common.csproj", "{09ECEA5A-A3CD-4F9A-92DC-7D282BE5BC0E}" 9 | EndProject 10 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{07435065-E5AF-45BF-9073-55A8B22FEEEF}" 11 | ProjectSection(SolutionItems) = preProject 12 | TODO.txt = TODO.txt 13 | EndProjectSection 14 | EndProject 15 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetMQServer", "NetMQServer\NetMQServer.csproj", "{D3646EFA-1CB6-4CF6-83C7-1963525FC3F0}" 16 | EndProject 17 | Global 18 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 19 | Debug|Any CPU = Debug|Any CPU 20 | Release|Any CPU = Release|Any CPU 21 | EndGlobalSection 22 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 23 | {B43264BB-E309-4168-8770-EC0127C63912}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 24 | {B43264BB-E309-4168-8770-EC0127C63912}.Debug|Any CPU.Build.0 = Debug|Any CPU 25 | {B43264BB-E309-4168-8770-EC0127C63912}.Release|Any CPU.ActiveCfg = Release|Any CPU 26 | {B43264BB-E309-4168-8770-EC0127C63912}.Release|Any CPU.Build.0 = Release|Any CPU 27 | {09ECEA5A-A3CD-4F9A-92DC-7D282BE5BC0E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 28 | {09ECEA5A-A3CD-4F9A-92DC-7D282BE5BC0E}.Debug|Any CPU.Build.0 = Debug|Any CPU 29 | {09ECEA5A-A3CD-4F9A-92DC-7D282BE5BC0E}.Release|Any CPU.ActiveCfg = Release|Any CPU 30 | {09ECEA5A-A3CD-4F9A-92DC-7D282BE5BC0E}.Release|Any CPU.Build.0 = Release|Any CPU 31 | {D3646EFA-1CB6-4CF6-83C7-1963525FC3F0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 32 | {D3646EFA-1CB6-4CF6-83C7-1963525FC3F0}.Debug|Any CPU.Build.0 = Debug|Any CPU 33 | {D3646EFA-1CB6-4CF6-83C7-1963525FC3F0}.Release|Any CPU.ActiveCfg = Release|Any CPU 34 | {D3646EFA-1CB6-4CF6-83C7-1963525FC3F0}.Release|Any CPU.Build.0 = Release|Any CPU 35 | EndGlobalSection 36 | GlobalSection(SolutionProperties) = preSolution 37 | HideSolutionNode = FALSE 38 | EndGlobalSection 39 | EndGlobal 40 | -------------------------------------------------------------------------------- /NetMQServer/.gitignore: -------------------------------------------------------------------------------- 1 | # Build Folders (you can keep bin if you'd like, to store dlls and pdbs) 2 | [Bb]in/ 3 | [Oo]bj/ 4 | 5 | # mstest test results 6 | TestResults 7 | 8 | ## Ignore Visual Studio temporary files, build results, and 9 | ## files generated by popular Visual Studio add-ons. 10 | 11 | # User-specific files 12 | *.suo 13 | *.user 14 | *.sln.docstates 15 | 16 | # Build results 17 | [Dd]ebug/ 18 | [Rr]elease/ 19 | x64/ 20 | *_i.c 21 | *_p.c 22 | *.ilk 23 | *.meta 24 | *.obj 25 | *.pch 26 | *.pdb 27 | *.pgc 28 | *.pgd 29 | *.rsp 30 | *.sbr 31 | *.tlb 32 | *.tli 33 | *.tlh 34 | *.tmp 35 | *.log 36 | *.vspscc 37 | *.vssscc 38 | .builds 39 | 40 | # Visual C++ cache files 41 | ipch/ 42 | *.aps 43 | *.ncb 44 | *.opensdf 45 | *.sdf 46 | 47 | # Visual Studio profiler 48 | *.psess 49 | *.vsp 50 | *.vspx 51 | 52 | # Guidance Automation Toolkit 53 | *.gpState 54 | 55 | # ReSharper is a .NET coding add-in 56 | _ReSharper* 57 | 58 | # NCrunch 59 | *.ncrunch* 60 | .*crunch*.local.xml 61 | 62 | # Installshield output folder 63 | [Ee]xpress 64 | 65 | # DocProject is a documentation generator add-in 66 | DocProject/buildhelp/ 67 | DocProject/Help/*.HxT 68 | DocProject/Help/*.HxC 69 | DocProject/Help/*.hhc 70 | DocProject/Help/*.hhk 71 | DocProject/Help/*.hhp 72 | DocProject/Help/Html2 73 | DocProject/Help/html 74 | 75 | # Click-Once directory 76 | publish 77 | 78 | # Publish Web Output 79 | *.Publish.xml 80 | 81 | # NuGet Packages Directory 82 | packages 83 | 84 | # Windows Azure Build Output 85 | csx 86 | *.build.csdef 87 | 88 | # Others 89 | [Bb]in 90 | [Oo]bj 91 | sql 92 | TestResults 93 | [Tt]est[Rr]esult* 94 | *.Cache 95 | ClientBin 96 | [Ss]tyle[Cc]op.* 97 | ~$* 98 | *.dbmdl 99 | Generated_Code #added for RIA/Silverlight projects 100 | 101 | # Backup & report files from converting an old project file to a newer 102 | # Visual Studio version. Backup files are not needed, because we have git ;-) 103 | _UpgradeReport_Files/ 104 | Backup*/ 105 | UpgradeLog*.XML -------------------------------------------------------------------------------- /NetMQServer/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 |
5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /NetMQServer/App.xaml: -------------------------------------------------------------------------------- 1 |  4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /NetMQServer/App.xaml.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Configuration; 4 | using System.Data; 5 | using System.Linq; 6 | using System.Threading; 7 | using System.Threading.Tasks; 8 | using System.Windows; 9 | using Autofac; 10 | using NetMQServer.Ticker; 11 | using NetMQServer.IOC; 12 | using log4net; 13 | 14 | namespace NetMQServer 15 | { 16 | public partial class App : Application 17 | { 18 | private static readonly ILog Log = LogManager.GetLogger(typeof(App)); 19 | public static IContainer Container; 20 | 21 | protected override void OnStartup(StartupEventArgs e) 22 | { 23 | base.OnStartup(e); 24 | InitializeLogging(); 25 | Start(); 26 | } 27 | 28 | private void Start() 29 | { 30 | var bootstrapper = new Bootstrapper(); 31 | var container = bootstrapper.Build(); 32 | 33 | // expose via static variable so SignalR can pick it up in Startup class 34 | Container = container; 35 | 36 | var mainWindow = container.Resolve(); 37 | var vm = container.Resolve(); 38 | mainWindow.DataContext = vm; 39 | vm.Start(); 40 | mainWindow.Show(); 41 | } 42 | 43 | private void InitializeLogging() 44 | { 45 | Thread.CurrentThread.Name = "UI"; 46 | 47 | log4net.Config.XmlConfigurator.Configure(); 48 | 49 | Log.Info(@"NetMQServer started"); 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /NetMQServer/IOC/Bootstrapper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using Autofac; 7 | using NetMQ; 8 | using NetMQServer.Ticker; 9 | 10 | namespace NetMQServer.IOC 11 | { 12 | public class Bootstrapper 13 | { 14 | public IContainer Build() 15 | { 16 | var builder = new ContainerBuilder(); 17 | 18 | 19 | // NetMQ 20 | builder.RegisterInstance(NetMQContext.Create()).SingleInstance(); 21 | builder.RegisterType().As().SingleInstance(); 22 | 23 | 24 | builder.RegisterType().As().SingleInstance(); 25 | 26 | // UI 27 | builder.RegisterType().SingleInstance(); 28 | builder.RegisterType().SingleInstance(); 29 | 30 | return builder.Build(); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /NetMQServer/MainWindow.xaml: -------------------------------------------------------------------------------- 1 |  5 | 6 |