Christian Lavoie | afc6d8f | 2011-02-20 02:39:19 +0000 | [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 | |
| 20 | package thrift |
| 21 | |
| 22 | import ( |
| 23 | "os" |
| 24 | ) |
| 25 | |
| 26 | /** |
| 27 | * A nonblocking TServer implementation. This allows for fairness amongst all |
| 28 | * connected clients in terms of invocations. |
| 29 | * |
| 30 | * This server is inherently single-threaded. If you want a limited thread pool |
| 31 | * coupled with invocation-fairness, see THsHaServer. |
| 32 | * |
| 33 | * To use this server, you MUST use a TFramedTransport at the outermost |
| 34 | * transport, otherwise this server will be unable to determine when a whole |
| 35 | * method call has been read off the wire. Clients must also use TFramedTransport. |
| 36 | */ |
| 37 | type TNonblockingServer struct { |
| 38 | /** Flag for stopping the server */ |
| 39 | stopped bool |
| 40 | |
| 41 | processorFactory TProcessorFactory |
| 42 | serverTransport TServerTransport |
| 43 | inputTransportFactory TTransportFactory |
| 44 | outputTransportFactory TTransportFactory |
| 45 | inputProtocolFactory TProtocolFactory |
| 46 | outputProtocolFactory TProtocolFactory |
| 47 | } |
| 48 | |
| 49 | |
| 50 | func NewTNonblockingServer2(processor TProcessor, serverTransport TServerTransport) *TNonblockingServer { |
| 51 | return NewTNonblockingServerFactory2(NewTProcessorFactory(processor), serverTransport) |
| 52 | } |
| 53 | |
| 54 | func NewTNonblockingServer4(processor TProcessor, serverTransport TServerTransport, transportFactory TTransportFactory, protocolFactory TProtocolFactory) *TNonblockingServer { |
| 55 | return NewTNonblockingServerFactory4(NewTProcessorFactory(processor), |
| 56 | serverTransport, |
| 57 | transportFactory, |
| 58 | protocolFactory, |
| 59 | ) |
| 60 | } |
| 61 | |
| 62 | func NewTNonblockingServer6(processor TProcessor, serverTransport TServerTransport, inputTransportFactory TTransportFactory, outputTransportFactory TTransportFactory, inputProtocolFactory TProtocolFactory, outputProtocolFactory TProtocolFactory) *TNonblockingServer { |
| 63 | return NewTNonblockingServerFactory6(NewTProcessorFactory(processor), |
| 64 | serverTransport, |
| 65 | inputTransportFactory, |
| 66 | outputTransportFactory, |
| 67 | inputProtocolFactory, |
| 68 | outputProtocolFactory, |
| 69 | ) |
| 70 | } |
| 71 | |
| 72 | func NewTNonblockingServerFactory2(processorFactory TProcessorFactory, serverTransport TServerTransport) *TNonblockingServer { |
| 73 | return NewTNonblockingServerFactory6(processorFactory, |
| 74 | serverTransport, |
| 75 | NewTTransportFactory(), |
| 76 | NewTTransportFactory(), |
| 77 | NewTBinaryProtocolFactoryDefault(), |
| 78 | NewTBinaryProtocolFactoryDefault(), |
| 79 | ) |
| 80 | } |
| 81 | |
| 82 | func NewTNonblockingServerFactory4(processorFactory TProcessorFactory, serverTransport TServerTransport, transportFactory TTransportFactory, protocolFactory TProtocolFactory) *TNonblockingServer { |
| 83 | return NewTNonblockingServerFactory6(processorFactory, |
| 84 | serverTransport, |
| 85 | transportFactory, |
| 86 | transportFactory, |
| 87 | protocolFactory, |
| 88 | protocolFactory, |
| 89 | ) |
| 90 | } |
| 91 | |
| 92 | func NewTNonblockingServerFactory6(processorFactory TProcessorFactory, serverTransport TServerTransport, inputTransportFactory TTransportFactory, outputTransportFactory TTransportFactory, inputProtocolFactory TProtocolFactory, outputProtocolFactory TProtocolFactory) *TNonblockingServer { |
| 93 | return &TNonblockingServer{processorFactory: processorFactory, |
| 94 | serverTransport: serverTransport, |
| 95 | inputTransportFactory: inputTransportFactory, |
| 96 | outputTransportFactory: outputTransportFactory, |
| 97 | inputProtocolFactory: inputProtocolFactory, |
| 98 | outputProtocolFactory: outputProtocolFactory, |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | func (p *TNonblockingServer) ProcessorFactory() TProcessorFactory { |
| 103 | return p.processorFactory |
| 104 | } |
| 105 | |
| 106 | func (p *TNonblockingServer) ServerTransport() TServerTransport { |
| 107 | return p.serverTransport |
| 108 | } |
| 109 | |
| 110 | func (p *TNonblockingServer) InputTransportFactory() TTransportFactory { |
| 111 | return p.inputTransportFactory |
| 112 | } |
| 113 | |
| 114 | func (p *TNonblockingServer) OutputTransportFactory() TTransportFactory { |
| 115 | return p.outputTransportFactory |
| 116 | } |
| 117 | |
| 118 | func (p *TNonblockingServer) InputProtocolFactory() TProtocolFactory { |
| 119 | return p.inputProtocolFactory |
| 120 | } |
| 121 | |
| 122 | func (p *TNonblockingServer) OutputProtocolFactory() TProtocolFactory { |
| 123 | return p.outputProtocolFactory |
| 124 | } |
| 125 | |
| 126 | func (p *TNonblockingServer) Serve() os.Error { |
| 127 | p.stopped = false |
| 128 | err := p.serverTransport.Listen() |
| 129 | if err != nil { |
| 130 | return err |
| 131 | } |
| 132 | for !p.stopped { |
| 133 | client, err := p.serverTransport.Accept() |
| 134 | if err != nil { |
| 135 | return err |
| 136 | } |
| 137 | if client != nil { |
| 138 | go p.processRequest(client) |
| 139 | } |
| 140 | } |
| 141 | return nil |
| 142 | } |
| 143 | |
| 144 | func (p *TNonblockingServer) Stop() os.Error { |
| 145 | p.stopped = true |
| 146 | p.serverTransport.Interrupt() |
| 147 | return nil |
| 148 | } |
| 149 | |
| 150 | func (p *TNonblockingServer) IsStopped() bool { |
| 151 | return p.stopped |
| 152 | } |
| 153 | |
| 154 | func (p *TNonblockingServer) processRequest(client TTransport) { |
| 155 | processor := p.processorFactory.GetProcessor(client) |
| 156 | inputTransport := p.inputTransportFactory.GetTransport(client) |
| 157 | outputTransport := p.outputTransportFactory.GetTransport(client) |
| 158 | inputProtocol := p.inputProtocolFactory.GetProtocol(inputTransport) |
| 159 | outputProtocol := p.outputProtocolFactory.GetProtocol(outputTransport) |
| 160 | if inputTransport != nil { |
| 161 | defer inputTransport.Close() |
| 162 | } |
| 163 | if outputTransport != nil { |
| 164 | defer outputTransport.Close() |
| 165 | } |
| 166 | for { |
| 167 | ok, e := processor.Process(inputProtocol, outputProtocol) |
| 168 | if e != nil { |
| 169 | if !p.stopped { |
| 170 | // TODO(pomack) log error |
| 171 | break |
| 172 | } |
| 173 | } |
| 174 | if !ok { |
| 175 | break |
| 176 | } |
| 177 | } |
| 178 | } |