blob: 149d513c65274105024865425a88a17170c1b831 [file] [log] [blame]
Jens Geyerb39b5ea2014-03-07 19:42:28 +01001/**
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 server implements the BasicMathServer service .
32 * All results are sent back to the client via the BasicMathClient service
33 */
34
35
36namespace RebusSample.Server
37{
38 // handler to be registered with Rebus
39 class MathRequestCallHandler : IHandleMessages<MathRequestCall>
40 {
41 public void Handle(MathRequestCall message)
42 {
43 // Thrift protocol/transport stack
44 var stm = new MemoryStream(message.rawBytes);
45 var trns = new TStreamTransport(stm, null);
46 var prot = new TBinaryProtocol(trns);
47
48 // create a processor and let him handle the call
49 var hndl = new MathRequestsHandler();
50 var proc = new BasicMathServer.Processor(hndl);
51 proc.Process(prot, null); // oneway only
52 }
53 }
54
55
56 // serves incoming calculation requests
57 internal class MathRequestsHandler : BasicMathServer.Iface
58 {
59 public void Ping(long value)
60 {
61 var client = new MathResponseClient("localhost");
62 client.Pong(value);
63 }
64
65
66 public void DoTheMath(int arg1, int arg2)
67 {
68 var client = new MathResponseClient("localhost");
69 if( arg2 != 0)
70 client.FourResults( arg1+arg2, arg1*arg2, arg1-arg2, arg1/arg2);
71 else
72 client.ThreeResults( arg1+arg2, arg1*arg2, arg1-arg2);
73 }
74 }
75
76
77 // provides the client-side interface for calculation responses
78 internal class MathResponseClient : BasicMathClient.Iface
79 {
80 private BuiltinContainerAdapter MQAdapter;
81
82
83 public MathResponseClient(string server)
84 {
85 MQAdapter = new BuiltinContainerAdapter();
86 Configure.With(MQAdapter)
87 .Transport(t => t.UseRabbitMqInOneWayMode("amqp://" + server)) // we need send only
88 .MessageOwnership(o => o.FromRebusConfigurationSection())
89 .CreateBus().Start();
90 }
91
92
93 public void SerializeThriftCall(Action<BasicMathClient.Iface> action)
94 {
95 // Thrift protocol/transport stack
96 var stm = new MemoryStream();
97 var trns = new TStreamTransport(null, stm);
98 var prot = new TBinaryProtocol(trns);
99
100 // serialize the call into a bunch of bytes
101 var client = new BasicMathClient.Client(prot);
102 if (action != null)
103 action(client);
104 else
105 throw new ArgumentException("action must not be null");
106
107 // make sure everything is written to the MemoryStream
108 trns.Flush();
109
110 // send the message
111 var msg = new MathResponseCall() { rawBytes = stm.ToArray() };
112 MQAdapter.Bus.Send(msg);
113 }
114
115
116 public void Pong(long value)
117 {
118 SerializeThriftCall(client =>
119 {
120 client.Pong(value);
121 });
122 }
123
124
125 public void ThreeResults(int added, int multiplied, int suctracted)
126 {
127 SerializeThriftCall(client =>
128 {
129 client.ThreeResults(added, multiplied, suctracted);
130 });
131 }
132
133
134 public void FourResults(int added, int multiplied, int suctracted, int divided)
135 {
136 SerializeThriftCall(client =>
137 {
138 client.FourResults(added, multiplied, suctracted, divided);
139 });
140 }
141 }
142}
143