Jens Geyer | 7203424 | 2013-05-08 18:46:57 +0200 | [diff] [blame^] | 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 | * 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 | |
| 24 | using System; |
| 25 | using System.Text; |
| 26 | using Thrift.Transport; |
| 27 | using System.Collections.Generic; |
| 28 | |
| 29 | namespace Thrift.Protocol |
| 30 | { |
| 31 | |
| 32 | /** |
| 33 | * TMultiplexedProcessor is a TProcessor allowing a single TServer to provide multiple services. |
| 34 | * To do so, you instantiate the processor and then register additional processors with it, |
| 35 | * as shown in the following example: |
| 36 | * |
| 37 | * TMultiplexedProcessor processor = new TMultiplexedProcessor(); |
| 38 | * |
| 39 | * processor.registerProcessor( |
| 40 | * "Calculator", |
| 41 | * new Calculator.Processor(new CalculatorHandler())); |
| 42 | * |
| 43 | * processor.registerProcessor( |
| 44 | * "WeatherReport", |
| 45 | * new WeatherReport.Processor(new WeatherReportHandler())); |
| 46 | * |
| 47 | * TServerTransport t = new TServerSocket(9090); |
| 48 | * TSimpleServer server = new TSimpleServer(processor, t); |
| 49 | * |
| 50 | * server.serve(); |
| 51 | */ |
| 52 | public class TMultiplexedProcessor : TProcessor |
| 53 | { |
| 54 | private Dictionary<String,TProcessor> ServiceProcessorMap = new Dictionary<String,TProcessor>(); |
| 55 | |
| 56 | /** |
| 57 | * 'Register' a service with this TMultiplexedProcessor. This allows us to broker |
| 58 | * requests to individual services by using the service name to select them at request time. |
| 59 | * |
| 60 | * Args: |
| 61 | * - serviceName Name of a service, has to be identical to the name |
| 62 | * declared in the Thrift IDL, e.g. "WeatherReport". |
| 63 | * - processor Implementation of a service, ususally referred to as "handlers", |
| 64 | * e.g. WeatherReportHandler implementing WeatherReport.Iface. |
| 65 | */ |
| 66 | public void RegisterProcessor(String serviceName, TProcessor processor) |
| 67 | { |
| 68 | ServiceProcessorMap.Add(serviceName, processor); |
| 69 | } |
| 70 | |
| 71 | |
| 72 | private void Fail( TProtocol oprot, TMessage message, TApplicationException.ExceptionType extype, string etxt) |
| 73 | { |
| 74 | TApplicationException appex = new TApplicationException( extype, etxt); |
| 75 | |
| 76 | TMessage newMessage = new TMessage(message.Name, TMessageType.Exception, message.SeqID); |
| 77 | |
| 78 | oprot.WriteMessageBegin(newMessage); |
| 79 | appex.Write( oprot); |
| 80 | oprot.WriteMessageEnd(); |
| 81 | oprot.Transport.Flush(); |
| 82 | } |
| 83 | |
| 84 | |
| 85 | /** |
| 86 | * This implementation of process performs the following steps: |
| 87 | * |
| 88 | * - Read the beginning of the message. |
| 89 | * - Extract the service name from the message. |
| 90 | * - Using the service name to locate the appropriate processor. |
| 91 | * - Dispatch to the processor, with a decorated instance of TProtocol |
| 92 | * that allows readMessageBegin() to return the original TMessage. |
| 93 | * |
| 94 | * Throws an exception if |
| 95 | * - the message type is not CALL or ONEWAY, |
| 96 | * - the service name was not found in the message, or |
| 97 | * - the service name has not been RegisterProcessor()ed. |
| 98 | */ |
| 99 | public bool Process(TProtocol iprot, TProtocol oprot) |
| 100 | { |
| 101 | /* Use the actual underlying protocol (e.g. TBinaryProtocol) to read the |
| 102 | message header. This pulls the message "off the wire", which we'll |
| 103 | deal with at the end of this method. */ |
| 104 | |
| 105 | TMessage message = iprot.ReadMessageBegin(); |
| 106 | |
| 107 | if ((message.Type != TMessageType.Call) && (message.Type != TMessageType.Oneway)) |
| 108 | { |
| 109 | Fail( oprot, message, |
| 110 | TApplicationException.ExceptionType.InvalidMessageType, |
| 111 | "Message type CALL or ONEWAY expected"); |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | // Extract the service name |
| 116 | int index = message.Name.IndexOf(TMultiplexedProtocol.SEPARATOR); |
| 117 | if (index < 0) { |
| 118 | Fail( oprot, message, |
| 119 | TApplicationException.ExceptionType.InvalidProtocol, |
| 120 | "Service name not found in message name: " + message.Name + ". "+ |
| 121 | "Did you forget to use a TMultiplexProtocol in your client?"); |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | // Create a new TMessage, something that can be consumed by any TProtocol |
| 126 | string serviceName = message.Name.Substring(0, index); |
| 127 | TProcessor actualProcessor; |
| 128 | if( ! ServiceProcessorMap.TryGetValue(serviceName, out actualProcessor)) |
| 129 | { |
| 130 | Fail( oprot, message, |
| 131 | TApplicationException.ExceptionType.InternalError, |
| 132 | "Service name not found: " + serviceName + ". "+ |
| 133 | "Did you forget to call RegisterProcessor()?"); |
| 134 | return false; |
| 135 | } |
| 136 | |
| 137 | // Create a new TMessage, removing the service name |
| 138 | TMessage newMessage = new TMessage( |
| 139 | message.Name.Substring(serviceName.Length + TMultiplexedProtocol.SEPARATOR.Length), |
| 140 | message.Type, |
| 141 | message.SeqID); |
| 142 | |
| 143 | // Dispatch processing to the stored processor |
| 144 | return actualProcessor.Process(new StoredMessageProtocol(iprot, newMessage), oprot); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Our goal was to work with any protocol. In order to do that, we needed |
| 149 | * to allow them to call readMessageBegin() and get a TMessage in exactly |
| 150 | * the standard format, without the service name prepended to TMessage.name. |
| 151 | */ |
| 152 | private class StoredMessageProtocol : TProtocolDecorator |
| 153 | { |
| 154 | TMessage MsgBegin; |
| 155 | |
| 156 | public StoredMessageProtocol(TProtocol protocol, TMessage messageBegin) |
| 157 | :base(protocol) |
| 158 | { |
| 159 | this.MsgBegin = messageBegin; |
| 160 | } |
| 161 | |
| 162 | public override TMessage ReadMessageBegin() |
| 163 | { |
| 164 | return MsgBegin; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | } |
| 169 | } |