blob: 35d138c9660f3e33f71122acd25db003bb1bc67f [file] [log] [blame]
Jens Geyer72034242013-05-08 18:46:57 +02001/**
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 * Contains some contributions under the Thrift Software License.
20 * Please see doc/old-thrift-license.txt in the Thrift distribution for
21 * details.
22 */
23
24using System;
25using System.Text;
26using Thrift.Transport;
27using System.Collections.Generic;
Jens Geyer6b9e1c62013-07-06 09:29:19 +020028using System.IO;
Jens Geyer72034242013-05-08 18:46:57 +020029
Jens Geyerd5436f52014-10-03 19:50:38 +020030namespace Thrift.Protocol
Jens Geyer72034242013-05-08 18:46:57 +020031{
32
33 /**
34 * TMultiplexedProcessor is a TProcessor allowing a single TServer to provide multiple services.
Jens Geyerd5436f52014-10-03 19:50:38 +020035 * To do so, you instantiate the processor and then register additional processors with it,
Jens Geyer72034242013-05-08 18:46:57 +020036 * as shown in the following example:
37 *
38 * TMultiplexedProcessor processor = new TMultiplexedProcessor();
39 *
40 * processor.registerProcessor(
41 * "Calculator",
42 * new Calculator.Processor(new CalculatorHandler()));
43 *
44 * processor.registerProcessor(
45 * "WeatherReport",
46 * new WeatherReport.Processor(new WeatherReportHandler()));
47 *
48 * TServerTransport t = new TServerSocket(9090);
49 * TSimpleServer server = new TSimpleServer(processor, t);
50 *
51 * server.serve();
52 */
Jens Geyerd5436f52014-10-03 19:50:38 +020053 public class TMultiplexedProcessor : TProcessor
Jens Geyer72034242013-05-08 18:46:57 +020054 {
55 private Dictionary<String,TProcessor> ServiceProcessorMap = new Dictionary<String,TProcessor>();
56
57 /**
Jens Geyerd5436f52014-10-03 19:50:38 +020058 * 'Register' a service with this TMultiplexedProcessor. This allows us to broker
Jens Geyer72034242013-05-08 18:46:57 +020059 * requests to individual services by using the service name to select them at request time.
60 *
Jens Geyerd5436f52014-10-03 19:50:38 +020061 * Args:
Jens Geyer72034242013-05-08 18:46:57 +020062 * - serviceName Name of a service, has to be identical to the name
63 * declared in the Thrift IDL, e.g. "WeatherReport".
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +010064 * - processor Implementation of a service, usually referred to as "handlers",
Jens Geyer72034242013-05-08 18:46:57 +020065 * e.g. WeatherReportHandler implementing WeatherReport.Iface.
66 */
Jens Geyerd5436f52014-10-03 19:50:38 +020067 public void RegisterProcessor(String serviceName, TProcessor processor)
Jens Geyer72034242013-05-08 18:46:57 +020068 {
69 ServiceProcessorMap.Add(serviceName, processor);
70 }
71
Jens Geyerd5436f52014-10-03 19:50:38 +020072
Jens Geyer72034242013-05-08 18:46:57 +020073 private void Fail( TProtocol oprot, TMessage message, TApplicationException.ExceptionType extype, string etxt)
74 {
75 TApplicationException appex = new TApplicationException( extype, etxt);
76
77 TMessage newMessage = new TMessage(message.Name, TMessageType.Exception, message.SeqID);
78
79 oprot.WriteMessageBegin(newMessage);
80 appex.Write( oprot);
81 oprot.WriteMessageEnd();
82 oprot.Transport.Flush();
83 }
Jens Geyerd5436f52014-10-03 19:50:38 +020084
85
Jens Geyer72034242013-05-08 18:46:57 +020086 /**
87 * This implementation of process performs the following steps:
88 *
89 * - Read the beginning of the message.
90 * - Extract the service name from the message.
91 * - Using the service name to locate the appropriate processor.
92 * - Dispatch to the processor, with a decorated instance of TProtocol
93 * that allows readMessageBegin() to return the original TMessage.
Jens Geyerd5436f52014-10-03 19:50:38 +020094 *
95 * Throws an exception if
96 * - the message type is not CALL or ONEWAY,
97 * - the service name was not found in the message, or
98 * - the service name has not been RegisterProcessor()ed.
Jens Geyer72034242013-05-08 18:46:57 +020099 */
100 public bool Process(TProtocol iprot, TProtocol oprot)
101 {
102 /* Use the actual underlying protocol (e.g. TBinaryProtocol) to read the
103 message header. This pulls the message "off the wire", which we'll
104 deal with at the end of this method. */
105
Jens Geyer6b9e1c62013-07-06 09:29:19 +0200106 try
Jens Geyer72034242013-05-08 18:46:57 +0200107 {
Jens Geyer6b9e1c62013-07-06 09:29:19 +0200108 TMessage message = iprot.ReadMessageBegin();
Jens Geyer72034242013-05-08 18:46:57 +0200109
Jens Geyer6b9e1c62013-07-06 09:29:19 +0200110 if ((message.Type != TMessageType.Call) && (message.Type != TMessageType.Oneway))
111 {
112 Fail(oprot, message,
113 TApplicationException.ExceptionType.InvalidMessageType,
114 "Message type CALL or ONEWAY expected");
115 return false;
116 }
Jens Geyer72034242013-05-08 18:46:57 +0200117
Jens Geyer6b9e1c62013-07-06 09:29:19 +0200118 // Extract the service name
119 int index = message.Name.IndexOf(TMultiplexedProtocol.SEPARATOR);
120 if (index < 0)
121 {
122 Fail(oprot, message,
123 TApplicationException.ExceptionType.InvalidProtocol,
124 "Service name not found in message name: " + message.Name + ". " +
125 "Did you forget to use a TMultiplexProtocol in your client?");
126 return false;
127 }
128
129 // Create a new TMessage, something that can be consumed by any TProtocol
130 string serviceName = message.Name.Substring(0, index);
131 TProcessor actualProcessor;
132 if (!ServiceProcessorMap.TryGetValue(serviceName, out actualProcessor))
133 {
134 Fail(oprot, message,
135 TApplicationException.ExceptionType.InternalError,
136 "Service name not found: " + serviceName + ". " +
137 "Did you forget to call RegisterProcessor()?");
138 return false;
139 }
140
141 // Create a new TMessage, removing the service name
142 TMessage newMessage = new TMessage(
143 message.Name.Substring(serviceName.Length + TMultiplexedProtocol.SEPARATOR.Length),
144 message.Type,
145 message.SeqID);
146
147 // Dispatch processing to the stored processor
148 return actualProcessor.Process(new StoredMessageProtocol(iprot, newMessage), oprot);
149
150 }
151 catch (IOException)
Jens Geyer72034242013-05-08 18:46:57 +0200152 {
Jens Geyer6b9e1c62013-07-06 09:29:19 +0200153 return false; // similar to all other processors
Jens Geyer72034242013-05-08 18:46:57 +0200154 }
155
Jens Geyer72034242013-05-08 18:46:57 +0200156 }
157
158 /**
159 * Our goal was to work with any protocol. In order to do that, we needed
160 * to allow them to call readMessageBegin() and get a TMessage in exactly
161 * the standard format, without the service name prepended to TMessage.name.
162 */
Jens Geyerd5436f52014-10-03 19:50:38 +0200163 private class StoredMessageProtocol : TProtocolDecorator
Jens Geyer72034242013-05-08 18:46:57 +0200164 {
165 TMessage MsgBegin;
166
Jens Geyerd5436f52014-10-03 19:50:38 +0200167 public StoredMessageProtocol(TProtocol protocol, TMessage messageBegin)
Jens Geyer72034242013-05-08 18:46:57 +0200168 :base(protocol)
169 {
170 this.MsgBegin = messageBegin;
171 }
172
Jens Geyerd5436f52014-10-03 19:50:38 +0200173 public override TMessage ReadMessageBegin()
Jens Geyer72034242013-05-08 18:46:57 +0200174 {
175 return MsgBegin;
176 }
177 }
178
179 }
180}