Guest User

Untitled

a guest
Feb 13th, 2012
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1. using System;
  2. using System.Runtime.Remoting;
  3. using System.ServiceModel;
  4. using System.Windows;
  5. using Gui.MultiplayerService;
  6. using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
  7.  
  8. namespace Gui.Classes
  9. {
  10. /// <summary>
  11. /// Clase Client. Se encarga de manejar la transferecia entre aplicaciones por LAN
  12. /// </summary>
  13. public class Client
  14. {
  15.  
  16. private static Client _cliente;
  17. private static readonly Object Semaforo = new object();
  18. private readonly TransferServiceClient _clientServiceClient;
  19. private int _id = 1;
  20.  
  21. /// <summary>
  22. /// Constructor de la clase Client
  23. /// </summary>
  24. protected Client()
  25. {
  26. _clientServiceClient = new TransferServiceClient("NetTcpBinding_ITransferService");
  27. }
  28.  
  29. /// <summary>
  30. /// Gets the Client
  31. /// </summary>
  32. public TransferServiceClient ClientServiceClient
  33. {
  34. get { return _clientServiceClient; }
  35. }
  36.  
  37. /// <summary>
  38. /// Gets the id of the client
  39. /// </summary>
  40. public int Id
  41. {
  42. get { return _id; }
  43. set { _id = value; }
  44. }
  45.  
  46. /// <summary>
  47. /// Metodo que permite transferir datos al servidor
  48. /// </summary>
  49. /// <param name="tdata">Informacion a transferir</param>
  50. public void Transfer(TransferData tdata)
  51. {
  52. try
  53. {
  54. _clientServiceClient.SendDataClient(tdata);
  55. }
  56. catch (Exception ex)
  57. {
  58. //throw new ServerException("The Client had troubles connecting to the server", ex);
  59. if (ex is EndpointNotFoundException || ex is ServerException || ex is CommunicationObjectFaultedException || ex is CommunicationException)
  60. ExceptionPolicy.HandleException(ex, "ServerException");
  61. else
  62. ExceptionPolicy.HandleException(ex, "General");
  63.  
  64. }
  65. finally
  66. {
  67. if (StringManager.Instance().CurrentLanguage == "english")
  68. MessageBox.Show("The Client had problems connecting to the server, please contact your System Administrator", "Connetion Error", MessageBoxButton.OK,
  69. MessageBoxImage.Error);
  70. else
  71. {
  72. MessageBox.Show("El cliente tuvo problemas al conectarse al servidor, por favor contacte al Administrador de su Sistema", "Error de Conexion", MessageBoxButton.OK,
  73. MessageBoxImage.Error);
  74. }
  75. }
  76. /*
  77. if (_clientServiceClient.State == CommunicationState.Opened)
  78. _clientServiceClient.SendDataClient(tdata);
  79. * */
  80. }
  81.  
  82. /// <summary>
  83. /// Metodo que permite recibir datos del servidor
  84. /// </summary>
  85. public TransferData Receive()
  86. {
  87. try
  88. {
  89. return _clientServiceClient.DownloadDataServer(_id);
  90. }
  91. catch (Exception ex)
  92. {
  93. //throw new ServerException("The Client had troubles connecting to the server", ex);
  94. if (ex is EndpointNotFoundException || ex is ServerException || ex is CommunicationObjectFaultedException || ex is CommunicationException)
  95. ExceptionPolicy.HandleException(ex, "ServerException");
  96. else
  97. ExceptionPolicy.HandleException(ex, "General");
  98. }
  99. finally
  100. {
  101. if (StringManager.Instance().CurrentLanguage == "english")
  102. MessageBox.Show("The Client had problems connecting to the server, please contact your System Administrator", "Connetion Error", MessageBoxButton.OK,
  103. MessageBoxImage.Error);
  104. else
  105. MessageBox.Show("El cliente tuvo problemas al conectarse al servidor, por favor contacte al Administrador de su Sistema", "Error de Conexion", MessageBoxButton.OK,
  106. MessageBoxImage.Error);
  107. }
  108. return null;
  109.  
  110. /*
  111. if (_clientServiceClient.State != CommunicationState.Closed || _clientServiceClient.State != CommunicationState.Closing)
  112. return _clientServiceClient.DownloadDataServer(_id);
  113. return null;
  114. * */
  115. }
  116.  
  117. #region Singleton
  118. /// <summary>
  119. /// Metodo que sirve para llamar la clase Client desde un patron Singleton
  120. /// </summary>
  121. /// <returns></returns>
  122. public static Client Instance()
  123. {
  124. if (_cliente == null)
  125. {
  126. lock (Semaforo)
  127. {
  128. if (_cliente == null)
  129. {
  130. _cliente = new Client();
  131. }
  132. }
  133. }
  134. return _cliente;
  135. }
  136.  
  137. #endregion
  138.  
  139.  
  140.  
  141. }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment