2
0

PeerServiceMessageContracts.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Channels
  5. {
  6. using System.Collections.Generic;
  7. using System.ServiceModel;
  8. using System.Diagnostics;
  9. using System.Runtime.Serialization;
  10. using System.ServiceModel.Diagnostics;
  11. [MessageContract(IsWrapped = false)]
  12. class ConnectInfo
  13. {
  14. [DataContract(Name = "ConnectInfo", Namespace = PeerStrings.Namespace)]
  15. class ConnectInfoDC
  16. {
  17. [DataMember(Name = "NodeId")]
  18. public ulong nodeId;
  19. [DataMember(Name = "Address")]
  20. public PeerNodeAddress address;
  21. public ConnectInfoDC() { }
  22. public ConnectInfoDC(ulong nodeId, PeerNodeAddress address)
  23. {
  24. this.nodeId = nodeId;
  25. this.address = address;
  26. }
  27. }
  28. [MessageBodyMember(Name = "Connect", Namespace = PeerStrings.Namespace)]
  29. ConnectInfoDC body;
  30. public ConnectInfo()
  31. {
  32. this.body = new ConnectInfoDC();
  33. }
  34. public ConnectInfo(ulong nodeId, PeerNodeAddress address)
  35. {
  36. this.body = new ConnectInfoDC(nodeId, address);
  37. }
  38. public PeerNodeAddress Address
  39. {
  40. get { return this.body.address; }
  41. }
  42. public ulong NodeId
  43. {
  44. get { return this.body.nodeId; }
  45. }
  46. public bool HasBody()
  47. {
  48. return body != null;
  49. }
  50. }
  51. [MessageContract(IsWrapped = false)]
  52. class DisconnectInfo
  53. {
  54. [DataContract(Name = "DisconnectInfo", Namespace = PeerStrings.Namespace)]
  55. class DisconnectInfoDC
  56. {
  57. [DataMember(Name = "Reason")]
  58. public DisconnectReason reason;
  59. [DataMember(Name = "Referrals")]
  60. public Referral[] referrals;
  61. public DisconnectInfoDC() { }
  62. public DisconnectInfoDC(DisconnectReason reason, Referral[] referrals)
  63. {
  64. this.reason = reason;
  65. this.referrals = referrals;
  66. }
  67. }
  68. [MessageBodyMember(Name = "Disconnect", Namespace = PeerStrings.Namespace)]
  69. DisconnectInfoDC body;
  70. public DisconnectInfo()
  71. {
  72. body = new DisconnectInfoDC();
  73. }
  74. public DisconnectInfo(DisconnectReason reason, Referral[] referrals)
  75. {
  76. this.body = new DisconnectInfoDC(reason, referrals);
  77. }
  78. public DisconnectReason Reason
  79. {
  80. get { return this.body.reason; }
  81. }
  82. public IList<Referral> Referrals
  83. {
  84. get
  85. {
  86. return this.body.referrals != null ? Array.AsReadOnly<Referral>(this.body.referrals) : null;
  87. }
  88. }
  89. public bool HasBody()
  90. {
  91. return body != null;
  92. }
  93. }
  94. // Reasons for sending a Disconnect message
  95. enum DisconnectReason
  96. {
  97. LeavingMesh = PeerCloseReason.LeavingMesh,
  98. NotUsefulNeighbor = PeerCloseReason.NotUsefulNeighbor,
  99. DuplicateNeighbor = PeerCloseReason.DuplicateNeighbor,
  100. DuplicateNodeId = PeerCloseReason.DuplicateNodeId,
  101. NodeBusy = PeerCloseReason.NodeBusy,
  102. InternalFailure = PeerCloseReason.InternalFailure,
  103. }
  104. //
  105. // Service contract used for neighbor-to-neighbor communication
  106. // Sending messages is asynchronous and processing incoming messages is synchronous.
  107. // Used for Service implementation
  108. //
  109. [ServiceContract(Name = PeerStrings.ServiceContractName,
  110. Namespace = PeerStrings.Namespace,
  111. SessionMode = SessionMode.Required,
  112. CallbackContract = typeof(IPeerServiceContract))]
  113. interface IPeerServiceContract
  114. {
  115. [OperationContract(IsOneWay = true, Action = PeerStrings.ConnectAction)]
  116. void Connect(ConnectInfo connectInfo);
  117. [OperationContract(IsOneWay = true, Action = PeerStrings.DisconnectAction)]
  118. void Disconnect(DisconnectInfo disconnectInfo);
  119. [OperationContract(IsOneWay = true, Action = PeerStrings.RefuseAction)]
  120. void Refuse(RefuseInfo refuseInfo);
  121. [OperationContract(IsOneWay = true, Action = PeerStrings.WelcomeAction)]
  122. void Welcome(WelcomeInfo welcomeInfo);
  123. [OperationContract(IsOneWay = true, Action = PeerStrings.FloodAction, AsyncPattern = true)]
  124. IAsyncResult BeginFloodMessage(Message floodedInfo, AsyncCallback callback, object state);
  125. void EndFloodMessage(IAsyncResult result);
  126. [OperationContract(IsOneWay = true, Action = PeerStrings.LinkUtilityAction)]
  127. void LinkUtility(UtilityInfo utilityInfo);
  128. [OperationContract(
  129. Action = TrustFeb2005Strings.RequestSecurityToken,
  130. ReplyAction = TrustFeb2005Strings.RequestSecurityTokenResponse)]
  131. Message ProcessRequestSecurityToken(Message message);
  132. [OperationContract(IsOneWay = true, Action = PeerStrings.PingAction)]
  133. void Ping(Message message);
  134. [OperationContract(IsOneWay = true, Action = Addressing10Strings.FaultAction)]
  135. void Fault(Message message);
  136. }
  137. [ServiceContract(Name = PeerStrings.ServiceContractName,
  138. Namespace = PeerStrings.Namespace,
  139. SessionMode = SessionMode.Required,
  140. CallbackContract = typeof(IPeerService))]
  141. interface IPeerProxy : IPeerServiceContract, IOutputChannel
  142. {
  143. }
  144. [ServiceContract(Name = PeerStrings.ServiceContractName,
  145. Namespace = PeerStrings.Namespace,
  146. SessionMode = SessionMode.Required,
  147. CallbackContract = typeof(IPeerProxy))]
  148. interface IPeerService : IPeerServiceContract
  149. {
  150. }
  151. static class PeerConnectorHelper
  152. {
  153. public static bool IsDefined(DisconnectReason value)
  154. {
  155. return ((value == DisconnectReason.LeavingMesh) ||
  156. (value == DisconnectReason.NotUsefulNeighbor) ||
  157. (value == DisconnectReason.DuplicateNeighbor) ||
  158. (value == DisconnectReason.DuplicateNodeId) ||
  159. (value == DisconnectReason.NodeBusy) ||
  160. (value == DisconnectReason.InternalFailure));
  161. }
  162. public static bool IsDefined(RefuseReason value)
  163. {
  164. return ((value == RefuseReason.DuplicateNodeId) ||
  165. (value == RefuseReason.DuplicateNeighbor) ||
  166. (value == RefuseReason.NodeBusy));
  167. }
  168. }
  169. [DataContract(Name = "Referral", Namespace = PeerStrings.Namespace)]
  170. class Referral
  171. {
  172. [DataMember(Name = "NodeId")]
  173. ulong nodeId; // Referral NodeId
  174. [DataMember(Name = "Address")]
  175. PeerNodeAddress address; // Referral address
  176. public Referral(ulong nodeId, PeerNodeAddress address)
  177. {
  178. this.nodeId = nodeId;
  179. this.address = address;
  180. }
  181. public PeerNodeAddress Address
  182. {
  183. get { return this.address; }
  184. set { this.address = value; }
  185. }
  186. public ulong NodeId
  187. {
  188. get { return this.nodeId; }
  189. set { this.nodeId = value; }
  190. }
  191. }
  192. [MessageContract(IsWrapped = false)]
  193. class RefuseInfo
  194. {
  195. [DataContract(Name = "RefuseInfo", Namespace = PeerStrings.Namespace)]
  196. class RefuseInfoDC
  197. {
  198. [DataMember(Name = "Reason")]
  199. public RefuseReason reason;
  200. [DataMember(Name = "Referrals")]
  201. public Referral[] referrals;
  202. public RefuseInfoDC() { }
  203. public RefuseInfoDC(RefuseReason reason, Referral[] referrals)
  204. {
  205. this.reason = reason;
  206. this.referrals = referrals;
  207. }
  208. }
  209. public RefuseInfo()
  210. {
  211. this.body = new RefuseInfoDC();
  212. }
  213. public RefuseInfo(RefuseReason reason, Referral[] referrals)
  214. {
  215. this.body = new RefuseInfoDC(reason, referrals);
  216. }
  217. [MessageBodyMember(Name = "Refuse", Namespace = PeerStrings.Namespace)]
  218. RefuseInfoDC body;
  219. public RefuseReason Reason
  220. {
  221. get { return this.body.reason; }
  222. }
  223. public IList<Referral> Referrals
  224. {
  225. get { return this.body.referrals != null ? Array.AsReadOnly<Referral>(this.body.referrals) : null; }
  226. }
  227. public bool HasBody()
  228. {
  229. return body != null;
  230. }
  231. }
  232. // Reasons for sending a Refuse message
  233. enum RefuseReason
  234. {
  235. DuplicateNeighbor = PeerCloseReason.DuplicateNeighbor,
  236. DuplicateNodeId = PeerCloseReason.DuplicateNodeId,
  237. NodeBusy = PeerCloseReason.NodeBusy,
  238. }
  239. [MessageContract(IsWrapped = false)]
  240. class UtilityInfo
  241. {
  242. [DataContract(Name = "LinkUtilityInfo", Namespace = PeerStrings.Namespace)]
  243. class UtilityInfoDC
  244. {
  245. [DataMember(Name = "Useful")]
  246. public uint useful;
  247. [DataMember(Name = "Total")]
  248. public uint total;
  249. public UtilityInfoDC() { }
  250. public UtilityInfoDC(uint useful, uint total)
  251. {
  252. this.useful = useful;
  253. this.total = total;
  254. }
  255. }
  256. public UtilityInfo()
  257. {
  258. this.body = new UtilityInfoDC();
  259. }
  260. public UtilityInfo(uint useful, uint total)
  261. {
  262. this.body = new UtilityInfoDC(useful, total);
  263. }
  264. [MessageBodyMember(Name = "LinkUtility", Namespace = PeerStrings.Namespace)]
  265. UtilityInfoDC body;
  266. public uint Useful
  267. {
  268. get { return body.useful; }
  269. }
  270. public uint Total
  271. {
  272. get { return body.total; }
  273. }
  274. public bool HasBody()
  275. {
  276. return body != null;
  277. }
  278. }
  279. [MessageContract(IsWrapped = false)]
  280. class WelcomeInfo
  281. {
  282. [DataContract(Name = "WelcomeInfo", Namespace = PeerStrings.Namespace)]
  283. class WelcomeInfoDC
  284. {
  285. [DataMember(Name = "NodeId")]
  286. public ulong nodeId;
  287. [DataMember(Name = "Referrals")]
  288. public Referral[] referrals;
  289. public WelcomeInfoDC() { }
  290. public WelcomeInfoDC(ulong nodeId, Referral[] referrals)
  291. {
  292. this.nodeId = nodeId;
  293. this.referrals = referrals;
  294. }
  295. }
  296. public WelcomeInfo()
  297. {
  298. this.body = new WelcomeInfoDC();
  299. }
  300. public WelcomeInfo(ulong nodeId, Referral[] referrals)
  301. {
  302. this.body = new WelcomeInfoDC(nodeId, referrals);
  303. }
  304. [MessageBodyMember(Name = "Welcome", Namespace = PeerStrings.Namespace)]
  305. WelcomeInfoDC body;
  306. public ulong NodeId
  307. {
  308. get { return this.body.nodeId; }
  309. }
  310. public IList<Referral> Referrals
  311. {
  312. get { return this.body.referrals != null ? Array.AsReadOnly<Referral>(this.body.referrals) : null; }
  313. }
  314. public bool HasBody()
  315. {
  316. return body != null;
  317. }
  318. }
  319. }