PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/source/samples/ObviousCode.Interlace.BitTunnel/ObviousCode.Interlace.BitTunnelServer/BitTunnelServerService.cs

https://bitbucket.org/VahidN/interlace
C# | 194 lines | 155 code | 36 blank | 3 comment | 17 complexity | 9d1701df3f1a0efed471475961cacec2 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Interlace.ReactorCore;
  5. using Interlace.ReactorService;
  6. using ObviousCode.Interlace.BitTunnelLibrary;
  7. using ObviousCode.Interlace.BitTunnelLibrary.Events;
  8. using ObviousCode.Interlace.BitTunnelLibrary.File;
  9. using ObviousCode.Interlace.BitTunnelLibrary.Interfaces;
  10. using ObviousCode.Interlace.BitTunnelLibrary.Messages;
  11. using ObviousCode.Interlace.BitTunnelLibrary.Messages.Headers;
  12. using ObviousCode.Interlace.BitTunnelLibrary.Protocols;
  13. using ObviousCode.Interlace.BitTunnelLibrary.Services;
  14. namespace ObviousCode.Interlace.BitTunnelServer
  15. {
  16. public class BitTunnelServerService : BitTunnelService
  17. {
  18. public event EventHandler<IdentificationEventArgs> ClientConnected;
  19. public event EventHandler<IdentificationEventArgs> ClientIdentified;
  20. public event EventHandler<FileRequestEventArgs> FileRequested;
  21. public event EventHandler<FileListModificationEventArgs> AvailableFilesUpdated;
  22. public event EventHandler ClientDisconnected;
  23. AppSettings _settings;
  24. List<ConnectorHandle> _handles;
  25. string _serverName;
  26. List<FileServerNegotiator> _activeNegotiators;
  27. public BitTunnelServerService(AppSettings settings, string serverName)
  28. : base(settings)
  29. {
  30. _handles = new List<ConnectorHandle>();
  31. _serverName = serverName;
  32. _settings = settings;
  33. _activeNegotiators = new List<FileServerNegotiator>();
  34. }
  35. public override void Close(IServiceHost host)
  36. {
  37. foreach (ConnectorHandle handle in _handles)
  38. {
  39. handle.Close();
  40. }
  41. }
  42. protected override void OnLostConnection(Exception e)
  43. {
  44. }
  45. protected override BitTunnelProtocolFactory CreateFactory()
  46. {
  47. BitTunnelServerProtocolFactory factory = new BitTunnelServerProtocolFactory(this, _serverName);
  48. factory.ClientConnected += new EventHandler<IdentificationEventArgs>(factory_ClientConnected);
  49. factory.ClientIdentified += new EventHandler<IdentificationEventArgs>(factory_ClientIdentified);
  50. factory.ConnectionTerminated += new EventHandler(factory_ConnectionTerminated);
  51. factory.AvailableFilesUpdated += new EventHandler<FileListModificationEventArgs>(factory_AvailableFilesUpdated);
  52. return factory;
  53. }
  54. void factory_AvailableFilesUpdated(object sender, FileListModificationEventArgs e)
  55. {
  56. if (AvailableFilesUpdated != null)
  57. {
  58. AvailableFilesUpdated(sender, e);
  59. }
  60. }
  61. void factory_ClientIdentified(object sender, IdentificationEventArgs e)
  62. {
  63. if (ClientIdentified != null)
  64. {
  65. ClientIdentified(sender, e);
  66. }
  67. }
  68. void factory_ConnectionTerminated(object sender, EventArgs e)
  69. {
  70. if (ClientDisconnected != null)
  71. {
  72. ClientDisconnected(sender, e);
  73. }
  74. }
  75. protected override void ConnectReactor(IServiceHost host)
  76. {
  77. if (Settings.ServerIsRemote)
  78. {
  79. _handles.Add(host.Reactor.ListenStream(ServerFactory, Settings.Port, Settings.ServerAddress));
  80. }
  81. else
  82. {
  83. _handles.Add(host.Reactor.ListenStream(ServerFactory, Settings.Port));
  84. }
  85. FireConnectionCompleted();
  86. }
  87. protected override void OnMessageReceived(IMessage message)
  88. {
  89. }
  90. protected override void OnMessageSending(IMessage message)
  91. {
  92. }
  93. void factory_ClientConnected(object sender, IdentificationEventArgs e)
  94. {
  95. if (ClientConnected != null)
  96. {
  97. ClientConnected(sender, e);
  98. }
  99. }
  100. private BitTunnelServerProtocolFactory ServerFactory
  101. {
  102. get
  103. {
  104. return Factory as BitTunnelServerProtocolFactory;
  105. }
  106. }
  107. public int ConnectionCount
  108. {
  109. get
  110. {
  111. return ServerFactory.ConnectionCount;
  112. }
  113. }
  114. public override IInstance Instance { get; set; }
  115. internal void RequestFile(FileDescriptor file, long chunkIndex, Action<FileDescriptor, FileRequestMode> sendResponseCallback, Action<FileDescriptor, FileChunkMessage> sendChunkCallback)
  116. {
  117. FileRequestEventArgs e = new FileRequestEventArgs(file);
  118. //This is giving a chance for the Server application to globally block a file - it has nothing to do with the client's desire to server the file
  119. if (FileRequested != null)
  120. {
  121. FileRequested(this, e);
  122. }
  123. if (!e.Allow)
  124. {
  125. sendResponseCallback(file, FileRequestMode.NotAvailable);
  126. return;
  127. }
  128. if (ServerFactory.Protocols.Count(p => p.Files.Contains(file)) == 0)
  129. {
  130. sendResponseCallback(file, FileRequestMode.NotAvailable);
  131. return;
  132. }
  133. //Negotiate File Transfer
  134. _activeNegotiators.Add(
  135. new FileServerNegotiator(ServerFactory, file, sendResponseCallback, sendChunkCallback));
  136. _activeNegotiators[_activeNegotiators.Count - 1].TimedOut += new EventHandler(FileServiceNegotiator_TimedOut);
  137. _activeNegotiators[_activeNegotiators.Count - 1].Settings = _settings;
  138. _activeNegotiators[_activeNegotiators.Count - 1].Negotiate(chunkIndex);
  139. }
  140. internal void FileChunkReceived(FileChunkMessage chunkMessage)
  141. {
  142. FileServerNegotiator negotiator = _activeNegotiators.Where(n => n.Id == chunkMessage.Header.Id).FirstOrDefault();
  143. if (negotiator == null)
  144. {
  145. //Implement as event?
  146. throw new InvalidOperationException("File Chunk received, though no negotiator was available to accept it");
  147. }
  148. negotiator.ChunkReceived(chunkMessage);
  149. negotiator.TimedOut-= new EventHandler(FileServiceNegotiator_TimedOut);
  150. _activeNegotiators.Remove(negotiator);
  151. }
  152. void FileServiceNegotiator_TimedOut(object sender, EventArgs e)
  153. {
  154. (sender as FileServerNegotiator).TimedOut -= new EventHandler(FileServiceNegotiator_TimedOut);
  155. _activeNegotiators.Remove(sender as FileServerNegotiator);
  156. }
  157. }
  158. }