]> git.proxmox.com Git - ceph.git/blame - ceph/src/jaegertracing/thrift/contrib/Rebus/ServiceImpl/Client.cs
buildsys: switch source download to quincy
[ceph.git] / ceph / src / jaegertracing / thrift / contrib / Rebus / ServiceImpl / Client.cs
CommitLineData
f67539c2
TL
1/**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20using Rebus;
21using Rebus.Configuration;
22using Rebus.Messages;
23using Rebus.RabbitMQ;
24using System;
25using System.Collections.Generic;
26using System.IO;
27using Thrift.Protocol;
28using Thrift.Transport;
29
30/*
31 * The client emits calls to BasicMathServers
32 *
33 * The client implements the BasicMathClient service.
34 * If the server has processed our request, we get the results back through this service
35 */
36
37namespace RebusSample.Client
38{
39
40 // handler to be registered with Rebus
41 class MathResponseCallHandler : IHandleMessages<MathResponseCall>
42 {
43 public void Handle(MathResponseCall message)
44 {
45 // Thrift protocol/transport stack
46 var stm = new MemoryStream(message.rawBytes);
47 var trns = new TStreamTransport(stm, null);
48 var prot = new TBinaryProtocol(trns);
49
50 // create a processor and let him handle the call
51 var hndl = new MathResponsesHandler();
52 var proc = new BasicMathClient.Processor(hndl);
53 proc.Process(prot, null); // oneway only
54 }
55 }
56
57
58 // serves incoming responses with calculation results
59 internal class MathResponsesHandler : BasicMathClient.Iface
60 {
61 public void FourResults(int added, int multiplied, int subtracted, int divided)
62 {
63 Console.WriteLine("added = {0}", added);
64 Console.WriteLine("multiplied= {0}", multiplied);
65 Console.WriteLine("subtracted = {0}", subtracted);
66 Console.WriteLine("divided = {0}", divided);
67
68 PingAndDoAnotherCalculation();
69 }
70
71
72 public void ThreeResults(int added, int multiplied, int subtracted)
73 {
74 Console.WriteLine("added = {0}", added);
75 Console.WriteLine("multiplied= {0}", multiplied);
76 Console.WriteLine("subtracted = {0}", subtracted);
77 Console.WriteLine("DIV/0 error during division");
78
79 PingAndDoAnotherCalculation();
80 }
81
82
83 public void Pong(long value)
84 {
85 var latency = DateTime.Now.Ticks - value;
86 Console.WriteLine("Ping took {0} ms", new DateTime(latency).Millisecond);
87 }
88
89
90 private void PingAndDoAnotherCalculation()
91 {
92 var random = new Random();
93 var client = new MathRequestClient("localhost");
94 client.Ping(DateTime.Now.Ticks);
95 client.DoTheMath(random.Next(), random.Next());
96 }
97 }
98
99
100 // provides the client-side interface for calculation requests
101 internal class MathRequestClient : BasicMathServer.Iface
102 {
103 private BuiltinContainerAdapter MQAdapter;
104
105
106 public MathRequestClient(string server)
107 {
108 MQAdapter = new BuiltinContainerAdapter();
109 Configure.With(MQAdapter)
110 .Transport(t => t.UseRabbitMqInOneWayMode("amqp://" + server)) // we need send only
111 .MessageOwnership(o => o.FromRebusConfigurationSection())
112 .CreateBus().Start();
113 }
114
115
116 public void SerializeThriftCall(Action<BasicMathServer.Iface> action)
117 {
118 // Thrift protocol/transport stack
119 var stm = new MemoryStream();
120 var trns = new TStreamTransport(null, stm);
121 var prot = new TBinaryProtocol(trns);
122
123 // serialize the call into a bunch of bytes
124 var client = new BasicMathServer.Client(prot);
125 if( action != null)
126 action(client);
127 else
128 throw new ArgumentException("action must not be null");
129
130 // make sure everything is written to the MemoryStream
131 trns.Flush();
132
133 // send the message
134 var msg = new MathRequestCall() { rawBytes = stm.ToArray() };
135 MQAdapter.Bus.Send(msg);
136 }
137
138
139 public void Ping(long value)
140 {
141 SerializeThriftCall(client =>
142 {
143 client.Ping(value);
144 });
145 }
146
147
148 public void DoTheMath( int arg1, int arg2)
149 {
150 SerializeThriftCall(client =>
151 {
152 client.DoTheMath(arg1, arg2);
153 });
154 }
155 }
156}
157