PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/source/samples/ObviousCode.Interlace.BitTunnel/ObviousCode.Interlace.BitTunnelClient/BitTunnelClientService.cs

https://bitbucket.org/VahidN/interlace
C# | 227 lines | 175 code | 46 blank | 6 comment | 14 complexity | 945c63b01243bea75190d65498900aa5 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using Interlace.ReactorService;
  4. using ObviousCode.Interlace.BitTunnelLibrary;
  5. using ObviousCode.Interlace.BitTunnelLibrary.Events;
  6. using ObviousCode.Interlace.BitTunnelLibrary.File;
  7. using ObviousCode.Interlace.BitTunnelLibrary.Identification;
  8. using ObviousCode.Interlace.BitTunnelLibrary.Interfaces;
  9. using ObviousCode.Interlace.BitTunnelLibrary.Protocols;
  10. using ObviousCode.Interlace.BitTunnelLibrary.Services;
  11. namespace ObviousCode.Interlace.BitTunnelClient
  12. {
  13. public class BitTunnelClientService : BitTunnelService
  14. {
  15. FileRebuilderService _rebuilderService;
  16. public event EventHandler<FileListEventArgs> FullFileListReceived;
  17. public event EventHandler<FileListModificationEventArgs> FileListModificationsReceived;
  18. public event EventHandler<FileRequestEventArgs> FileRequestReceived;
  19. public event EventHandler<FileRequestResponseEventArgs> FileRequestResponseReceived;
  20. public event EventHandler<FileTransferEventArgs> FileTransferInitiated;
  21. public event EventHandler<FileTransferEventArgs> FileTransferProgressed;
  22. public event EventHandler<FileDescriptorEventArgs> FileTransferFailed;
  23. public event EventHandler<FileTransferCompletedEventArgs> FileTransferCompleted;
  24. string _clientName;
  25. IClientInstance _instance;
  26. Dictionary<string, FileDescriptor> _availableFiles;
  27. ServiceHost _host;
  28. public BitTunnelClientService(AppSettings settings, string clientName) : base(settings)
  29. {
  30. _availableFiles = new Dictionary<string, FileDescriptor>();
  31. _clientName = clientName;
  32. }
  33. public ConnectedClient ConnectionDetails
  34. {
  35. get
  36. {
  37. return ClientFactory.ConnectionDetails;
  38. }
  39. }
  40. #region IService Members
  41. protected override void OnServiceOpen(IServiceHost host)
  42. {
  43. _rebuilderService = new FileRebuilderService(Settings);
  44. _host = new ServiceHost();
  45. _host.AddService(_rebuilderService);
  46. _host.StartServiceHost();
  47. _host.OpenServices();
  48. }
  49. void _rebuilderService_NextChunkRequested(object sender, FileRequestEventArgs e)
  50. {
  51. ClientFactory.OnNextFileChunkRequested(e);
  52. }
  53. public override void Close(IServiceHost host)
  54. {
  55. ClientFactory.LoseConnection();
  56. _host.CloseServices();
  57. _host.StopServiceHost();
  58. }
  59. #endregion
  60. public override IInstance Instance
  61. {
  62. get { return _instance; }
  63. set
  64. {
  65. if (!(value is IClientInstance)) throw new InvalidOperationException();
  66. _instance = value as IClientInstance;
  67. }
  68. }
  69. protected override void OnLostConnection(Exception e)
  70. {
  71. }
  72. protected override BitTunnelProtocolFactory CreateFactory()
  73. {
  74. BitTunnelClientProtocolFactory factory = new BitTunnelClientProtocolFactory(Settings, _clientName);
  75. factory.FullFileListReceived += new EventHandler<FileListEventArgs>(factory_FullFileListReceived);
  76. factory.FileListModificationsReceived += new EventHandler<FileListModificationEventArgs>(factory_FileListModificationsReceived);
  77. factory.FileRequestReceived += new EventHandler<FileRequestEventArgs>(factory_FileRequestReceived);
  78. factory.FileRequestResponseReceived += new EventHandler<FileRequestResponseEventArgs>(factory_FileRequestResponseReceived);
  79. factory.FileTransferInitiated += new EventHandler<FileTransferEventArgs>(factory_FileTransferInitiated);
  80. factory.FileTransferProgressed += new EventHandler<FileTransferEventArgs>(factory_FileTransferProgressed);
  81. factory.FileTransferFailed += new EventHandler<FileDescriptorEventArgs>(factory_FileTransferFailed);
  82. _rebuilderService.TransferCompleted += new EventHandler<FileTransferCompletedEventArgs>(_rebuilderService_TransferCompleted);
  83. _rebuilderService.NextChunkRequested += new EventHandler<FileRequestEventArgs>(_rebuilderService_NextChunkRequested);
  84. return factory;
  85. }
  86. void _rebuilderService_TransferCompleted(object sender, FileTransferCompletedEventArgs e)
  87. {
  88. if (FileTransferCompleted != null)
  89. {
  90. FileTransferCompleted(sender, e);
  91. }
  92. }
  93. void factory_FileTransferFailed(object sender, FileDescriptorEventArgs e)
  94. {
  95. throw new NotImplementedException();
  96. }
  97. void factory_FileTransferProgressed(object sender, FileTransferEventArgs e)
  98. {
  99. if (FileTransferProgressed != null)
  100. {
  101. FileTransferProgressed(sender, e);
  102. }
  103. _rebuilderService.ReceiveChunk(e.FileChunk);
  104. }
  105. void factory_FileTransferInitiated(object sender, FileTransferEventArgs e)
  106. {
  107. if (FileTransferInitiated != null)
  108. {
  109. FileTransferInitiated(sender, e);
  110. }
  111. }
  112. void factory_FileRequestReceived(object sender, FileRequestEventArgs e)
  113. {
  114. if (FileRequestReceived != null)
  115. {
  116. FileRequestReceived(sender, e);
  117. }
  118. }
  119. void factory_FileRequestResponseReceived(object sender, FileRequestResponseEventArgs e)
  120. {
  121. if (FileRequestResponseReceived!= null)
  122. {
  123. FileRequestResponseReceived(sender, e);
  124. }
  125. }
  126. void factory_FullFileListReceived(object sender, FileListEventArgs e)
  127. {
  128. if (FullFileListReceived != null)
  129. {
  130. FullFileListReceived(sender, e);
  131. }
  132. }
  133. void factory_FileListModificationsReceived(object sender, FileListModificationEventArgs e)
  134. {
  135. if (FileListModificationsReceived != null)
  136. {
  137. FileListModificationsReceived(sender, e);
  138. }
  139. }
  140. protected override void ConnectReactor(IServiceHost host)
  141. {
  142. ClientFactory.ProtocolMadeConnection += new EventHandler(ClientFactory_ProtocolMadeConnection);
  143. host.Reactor.ConnectStream(Factory, Settings.ServerAddress, Settings.Port);
  144. }
  145. void ClientFactory_ProtocolMadeConnection(object sender, EventArgs e)
  146. {
  147. FireConnectionCompleted();
  148. }
  149. private BitTunnelClientProtocolFactory ClientFactory
  150. {
  151. get
  152. {
  153. return Factory as BitTunnelClientProtocolFactory;
  154. }
  155. }
  156. public void SendFileListModifications(List<FileModificationDescriptor> modifiedFiles)
  157. {
  158. //We are simply sending notification of this to server. Server will respond back to all clients,
  159. //at which time we will perform the necessary actions.
  160. //
  161. //This will assist in keeping things in sync in case of a comms error of some sort
  162. //(unlikely, unless the server goes down all together, in which case we have bigger problems,
  163. //and keeping in sync will be irrelevant, be lets be elegant anyway)
  164. ClientFactory.SendFileListModifications(modifiedFiles);
  165. }
  166. public void RequestFile(FileDescriptor file)
  167. {
  168. ClientFactory.SendInitialFileRequest(file);
  169. }
  170. public void RequestFullFileList()
  171. {
  172. ClientFactory.RequestFullFileList();
  173. }
  174. protected override void OnMessageReceived(IMessage message)
  175. {
  176. }
  177. protected override void OnMessageSending(IMessage message)
  178. {
  179. }
  180. }
  181. }