TestRpcGenerator.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #region Copyright notice and license
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2008 Google Inc. All rights reserved.
  4. // http://github.com/jskeet/dotnet-protobufs/
  5. // Original C++/Java/Python code:
  6. // http://code.google.com/p/protobuf/
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions are
  10. // met:
  11. //
  12. // * Redistributions of source code must retain the above copyright
  13. // notice, this list of conditions and the following disclaimer.
  14. // * Redistributions in binary form must reproduce the above
  15. // copyright notice, this list of conditions and the following disclaimer
  16. // in the documentation and/or other materials provided with the
  17. // distribution.
  18. // * Neither the name of Google Inc. nor the names of its
  19. // contributors may be used to endorse or promote products derived from
  20. // this software without specific prior written permission.
  21. //
  22. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  25. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  26. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  28. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  30. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. #endregion
  34. using System;
  35. using Google.ProtocolBuffers;
  36. using Google.ProtocolBuffers.TestProtos;
  37. using NUnit.Framework;
  38. namespace Google.ProtocolBuffers
  39. {
  40. /// <summary>
  41. /// This class verifies the correct code is generated from unittest_rpc_interop.proto and provides a small demonstration
  42. /// of using the new IRpcDispatch to write a client/server
  43. /// </summary>
  44. [TestFixture]
  45. public class TestRpcGenerator
  46. {
  47. /// <summary>
  48. /// A sample implementation of the ISearchService for testing
  49. /// </summary>
  50. private class ExampleSearchImpl : ISearchService
  51. {
  52. SearchResponse ISearchService.Search(SearchRequest searchRequest)
  53. {
  54. if (searchRequest.CriteriaCount == 0)
  55. {
  56. throw new ArgumentException("No criteria specified.", new InvalidOperationException());
  57. }
  58. SearchResponse.Builder resp = SearchResponse.CreateBuilder();
  59. foreach (string criteria in searchRequest.CriteriaList)
  60. {
  61. resp.AddResults(
  62. SearchResponse.Types.ResultItem.CreateBuilder().SetName(criteria).SetUrl("http://search.com").
  63. Build());
  64. }
  65. return resp.Build();
  66. }
  67. SearchResponse ISearchService.RefineSearch(RefineSearchRequest refineSearchRequest)
  68. {
  69. SearchResponse.Builder resp = refineSearchRequest.PreviousResults.ToBuilder();
  70. foreach (string criteria in refineSearchRequest.CriteriaList)
  71. {
  72. resp.AddResults(
  73. SearchResponse.Types.ResultItem.CreateBuilder().SetName(criteria).SetUrl("http://refine.com").
  74. Build());
  75. }
  76. return resp.Build();
  77. }
  78. }
  79. /// <summary>
  80. /// An example extraction of the wire protocol
  81. /// </summary>
  82. private interface IWireTransfer
  83. {
  84. byte[] Execute(string method, byte[] message);
  85. }
  86. /// <summary>
  87. /// An example of a server responding to a wire request
  88. /// </summary>
  89. private class ExampleServerHost : IWireTransfer
  90. {
  91. private readonly IRpcServerStub _stub;
  92. public ExampleServerHost(ISearchService implementation)
  93. {
  94. //on the server, we create a dispatch to call the appropriate method by name
  95. IRpcDispatch dispatch = new SearchService.Dispatch(implementation);
  96. //we then wrap that dispatch in a server stub which will deserialize the wire bytes to the message
  97. //type appropriate for the method name being invoked.
  98. _stub = new SearchService.ServerStub(dispatch);
  99. }
  100. byte[] IWireTransfer.Execute(string method, byte[] message)
  101. {
  102. //now when we recieve a wire transmission to invoke a method by name with a byte[] or stream payload
  103. //we just simply call the sub:
  104. IMessageLite response = _stub.CallMethod(method, CodedInputStream.CreateInstance(message),
  105. ExtensionRegistry.Empty);
  106. //now we return the expected response message:
  107. return response.ToByteArray();
  108. }
  109. }
  110. /// <summary>
  111. /// An example of a client sending a wire request
  112. /// </summary>
  113. private class ExampleClient : IRpcDispatch
  114. {
  115. private readonly IWireTransfer _wire;
  116. public ExampleClient(IWireTransfer wire)
  117. {
  118. _wire = wire;
  119. }
  120. TMessage IRpcDispatch.CallMethod<TMessage, TBuilder>(string method, IMessageLite request,
  121. IBuilderLite<TMessage, TBuilder> response)
  122. {
  123. byte[] rawResponse = _wire.Execute(method, request.ToByteArray());
  124. response.MergeFrom(rawResponse);
  125. return response.Build();
  126. }
  127. }
  128. /// <summary>
  129. /// Put it all together to create one seamless client/server experience full of rich-type goodness ;)
  130. /// All you need to do is send/recieve the method name and message bytes across the wire.
  131. /// </summary>
  132. [Test]
  133. public void TestClientServerDispatch()
  134. {
  135. ExampleServerHost server = new ExampleServerHost(new ExampleSearchImpl());
  136. //obviously if this was a 'real' transport we would not use the server, rather the server would be listening, the client transmitting
  137. IWireTransfer wire = server;
  138. ISearchService client = new SearchService(new ExampleClient(wire));
  139. //now the client has a real, typed, interface to work with:
  140. SearchResponse result = client.Search(SearchRequest.CreateBuilder().AddCriteria("Test").Build());
  141. Assert.AreEqual(1, result.ResultsCount);
  142. Assert.AreEqual("Test", result.ResultsList[0].Name);
  143. Assert.AreEqual("http://search.com", result.ResultsList[0].Url);
  144. //The test part of this, call the only other method
  145. result =
  146. client.RefineSearch(
  147. RefineSearchRequest.CreateBuilder().SetPreviousResults(result).AddCriteria("Refine").Build());
  148. Assert.AreEqual(2, result.ResultsCount);
  149. Assert.AreEqual("Test", result.ResultsList[0].Name);
  150. Assert.AreEqual("http://search.com", result.ResultsList[0].Url);
  151. Assert.AreEqual("Refine", result.ResultsList[1].Name);
  152. Assert.AreEqual("http://refine.com", result.ResultsList[1].Url);
  153. }
  154. }
  155. }