csharptest | 68d831e | 2011-05-03 13:47:34 -0500 | [diff] [blame] | 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 |
|
| 35 | using System;
|
| 36 | using Google.ProtocolBuffers;
|
| 37 | using Google.ProtocolBuffers.TestProtos;
|
| 38 | using NUnit.Framework;
|
| 39 |
|
| 40 | namespace Google.ProtocolBuffers
|
| 41 | {
|
| 42 | /// <summary>
|
| 43 | /// This class verifies the correct code is generated from unittest_rpc_interop.proto and provides a small demonstration
|
| 44 | /// of using the new IRpcDispatch to write a client/server
|
| 45 | /// </summary>
|
| 46 | [TestFixture]
|
| 47 | public class TestRpcGenerator
|
| 48 | {
|
| 49 | /// <summary>
|
| 50 | /// A sample implementation of the ISearchService for testing
|
| 51 | /// </summary>
|
| 52 | class ExampleSearchImpl : ISearchService {
|
| 53 | SearchResponse ISearchService.Search(SearchRequest searchRequest) {
|
| 54 | if (searchRequest.CriteriaCount == 0) {
|
| 55 | throw new ArgumentException("No criteria specified.", new InvalidOperationException());
|
| 56 | }
|
| 57 | SearchResponse.Builder resp = SearchResponse.CreateBuilder();
|
| 58 | foreach (string criteria in searchRequest.CriteriaList) {
|
| 59 | resp.AddResults(SearchResponse.Types.ResultItem.CreateBuilder().SetName(criteria).SetUrl("http://search.com").Build());
|
| 60 | }
|
| 61 | return resp.Build();
|
| 62 | }
|
| 63 |
|
| 64 | SearchResponse ISearchService.RefineSearch(RefineSearchRequest refineSearchRequest) {
|
| 65 | SearchResponse.Builder resp = refineSearchRequest.PreviousResults.ToBuilder();
|
| 66 | foreach (string criteria in refineSearchRequest.CriteriaList) {
|
| 67 | resp.AddResults(SearchResponse.Types.ResultItem.CreateBuilder().SetName(criteria).SetUrl("http://refine.com").Build());
|
| 68 | }
|
| 69 | return resp.Build();
|
| 70 | }
|
| 71 | }
|
| 72 |
|
| 73 | /// <summary>
|
| 74 | /// An example extraction of the wire protocol
|
| 75 | /// </summary>
|
| 76 | interface IWireTransfer
|
| 77 | {
|
| 78 | byte[] Execute(string method, byte[] message);
|
| 79 | }
|
| 80 |
|
| 81 | /// <summary>
|
| 82 | /// An example of a server responding to a wire request
|
| 83 | /// </summary>
|
| 84 | class ExampleServerHost : IWireTransfer
|
| 85 | {
|
| 86 | readonly IRpcServerStub _stub;
|
| 87 | public ExampleServerHost(ISearchService implementation)
|
| 88 | {
|
| 89 | //on the server, we create a dispatch to call the appropriate method by name
|
| 90 | IRpcDispatch dispatch = new SearchService.Dispatch(implementation);
|
| 91 | //we then wrap that dispatch in a server stub which will deserialize the wire bytes to the message
|
| 92 | //type appropriate for the method name being invoked.
|
| 93 | _stub = new SearchService.ServerStub(dispatch);
|
| 94 | }
|
| 95 |
|
| 96 | byte[] IWireTransfer.Execute(string method, byte[] message)
|
| 97 | {
|
| 98 | //now when we recieve a wire transmission to invoke a method by name with a byte[] or stream payload
|
| 99 | //we just simply call the sub:
|
| 100 | IMessageLite response = _stub.CallMethod(method, CodedInputStream.CreateInstance(message), ExtensionRegistry.Empty);
|
| 101 | //now we return the expected response message:
|
| 102 | return response.ToByteArray();
|
| 103 | }
|
| 104 | }
|
| 105 |
|
| 106 | /// <summary>
|
| 107 | /// An example of a client sending a wire request
|
| 108 | /// </summary>
|
| 109 | class ExampleClient : IRpcDispatch
|
| 110 | {
|
| 111 | readonly IWireTransfer _wire;
|
| 112 | public ExampleClient(IWireTransfer wire)
|
| 113 | {
|
| 114 | _wire = wire;
|
| 115 | }
|
| 116 |
|
| 117 | TMessage IRpcDispatch.CallMethod<TMessage, TBuilder>(string method, IMessageLite request, IBuilderLite<TMessage, TBuilder> response)
|
| 118 | {
|
| 119 | byte[] rawResponse = _wire.Execute(method, request.ToByteArray());
|
| 120 | response.MergeFrom(rawResponse);
|
| 121 | return response.Build();
|
| 122 | }
|
| 123 | }
|
| 124 |
|
| 125 | /// <summary>
|
| 126 | /// Put it all together to create one seamless client/server experience full of rich-type goodness ;)
|
| 127 | /// All you need to do is send/recieve the method name and message bytes across the wire.
|
| 128 | /// </summary>
|
| 129 | [Test]
|
| 130 | public void TestClientServerDispatch()
|
| 131 | {
|
| 132 | ExampleServerHost server = new ExampleServerHost(new ExampleSearchImpl());
|
| 133 | //obviously if this was a 'real' transport we would not use the server, rather the server would be listening, the client transmitting
|
| 134 | IWireTransfer wire = server;
|
| 135 |
|
| 136 | ISearchService client = new SearchService(new ExampleClient(wire));
|
| 137 | //now the client has a real, typed, interface to work with:
|
| 138 | SearchResponse result = client.Search(SearchRequest.CreateBuilder().AddCriteria("Test").Build());
|
| 139 | Assert.AreEqual(1, result.ResultsCount);
|
| 140 | Assert.AreEqual("Test", result.ResultsList[0].Name);
|
| 141 | Assert.AreEqual("http://search.com", result.ResultsList[0].Url);
|
| 142 |
|
| 143 | //The test part of this, call the only other method
|
| 144 | result = client.RefineSearch(RefineSearchRequest.CreateBuilder().SetPreviousResults(result).AddCriteria("Refine").Build());
|
| 145 | Assert.AreEqual(2, result.ResultsCount);
|
| 146 | Assert.AreEqual("Test", result.ResultsList[0].Name);
|
| 147 | Assert.AreEqual("http://search.com", result.ResultsList[0].Url);
|
| 148 |
|
| 149 | Assert.AreEqual("Refine", result.ResultsList[1].Name);
|
| 150 | Assert.AreEqual("http://refine.com", result.ResultsList[1].Url);
|
| 151 | }
|
| 152 | }
|
| 153 | } |