Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +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 | |
| 20 | package thrift |
| 21 | |
| 22 | import ( |
| 23 | "log" |
Jens Geyer | 91cfb99 | 2014-05-17 01:07:28 +0200 | [diff] [blame] | 24 | "runtime/debug" |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | // Simple, non-concurrent server for testing. |
| 28 | type TSimpleServer struct { |
Jens Geyer | c975bbc | 2014-03-06 21:11:46 +0100 | [diff] [blame] | 29 | quit chan struct{} |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 30 | |
| 31 | processorFactory TProcessorFactory |
| 32 | serverTransport TServerTransport |
| 33 | inputTransportFactory TTransportFactory |
| 34 | outputTransportFactory TTransportFactory |
| 35 | inputProtocolFactory TProtocolFactory |
| 36 | outputProtocolFactory TProtocolFactory |
| 37 | } |
| 38 | |
| 39 | func NewTSimpleServer2(processor TProcessor, serverTransport TServerTransport) *TSimpleServer { |
| 40 | return NewTSimpleServerFactory2(NewTProcessorFactory(processor), serverTransport) |
| 41 | } |
| 42 | |
| 43 | func NewTSimpleServer4(processor TProcessor, serverTransport TServerTransport, transportFactory TTransportFactory, protocolFactory TProtocolFactory) *TSimpleServer { |
| 44 | return NewTSimpleServerFactory4(NewTProcessorFactory(processor), |
| 45 | serverTransport, |
| 46 | transportFactory, |
| 47 | protocolFactory, |
| 48 | ) |
| 49 | } |
| 50 | |
| 51 | func NewTSimpleServer6(processor TProcessor, serverTransport TServerTransport, inputTransportFactory TTransportFactory, outputTransportFactory TTransportFactory, inputProtocolFactory TProtocolFactory, outputProtocolFactory TProtocolFactory) *TSimpleServer { |
| 52 | return NewTSimpleServerFactory6(NewTProcessorFactory(processor), |
| 53 | serverTransport, |
| 54 | inputTransportFactory, |
| 55 | outputTransportFactory, |
| 56 | inputProtocolFactory, |
| 57 | outputProtocolFactory, |
| 58 | ) |
| 59 | } |
| 60 | |
| 61 | func NewTSimpleServerFactory2(processorFactory TProcessorFactory, serverTransport TServerTransport) *TSimpleServer { |
| 62 | return NewTSimpleServerFactory6(processorFactory, |
| 63 | serverTransport, |
| 64 | NewTTransportFactory(), |
| 65 | NewTTransportFactory(), |
| 66 | NewTBinaryProtocolFactoryDefault(), |
| 67 | NewTBinaryProtocolFactoryDefault(), |
| 68 | ) |
| 69 | } |
| 70 | |
| 71 | func NewTSimpleServerFactory4(processorFactory TProcessorFactory, serverTransport TServerTransport, transportFactory TTransportFactory, protocolFactory TProtocolFactory) *TSimpleServer { |
| 72 | return NewTSimpleServerFactory6(processorFactory, |
| 73 | serverTransport, |
| 74 | transportFactory, |
| 75 | transportFactory, |
| 76 | protocolFactory, |
| 77 | protocolFactory, |
| 78 | ) |
| 79 | } |
| 80 | |
| 81 | func NewTSimpleServerFactory6(processorFactory TProcessorFactory, serverTransport TServerTransport, inputTransportFactory TTransportFactory, outputTransportFactory TTransportFactory, inputProtocolFactory TProtocolFactory, outputProtocolFactory TProtocolFactory) *TSimpleServer { |
Jens Geyer | c975bbc | 2014-03-06 21:11:46 +0100 | [diff] [blame] | 82 | return &TSimpleServer{ |
| 83 | processorFactory: processorFactory, |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 84 | serverTransport: serverTransport, |
| 85 | inputTransportFactory: inputTransportFactory, |
| 86 | outputTransportFactory: outputTransportFactory, |
| 87 | inputProtocolFactory: inputProtocolFactory, |
| 88 | outputProtocolFactory: outputProtocolFactory, |
Jens Geyer | c975bbc | 2014-03-06 21:11:46 +0100 | [diff] [blame] | 89 | quit: make(chan struct{}, 1), |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
| 93 | func (p *TSimpleServer) ProcessorFactory() TProcessorFactory { |
| 94 | return p.processorFactory |
| 95 | } |
| 96 | |
| 97 | func (p *TSimpleServer) ServerTransport() TServerTransport { |
| 98 | return p.serverTransport |
| 99 | } |
| 100 | |
| 101 | func (p *TSimpleServer) InputTransportFactory() TTransportFactory { |
| 102 | return p.inputTransportFactory |
| 103 | } |
| 104 | |
| 105 | func (p *TSimpleServer) OutputTransportFactory() TTransportFactory { |
| 106 | return p.outputTransportFactory |
| 107 | } |
| 108 | |
| 109 | func (p *TSimpleServer) InputProtocolFactory() TProtocolFactory { |
| 110 | return p.inputProtocolFactory |
| 111 | } |
| 112 | |
| 113 | func (p *TSimpleServer) OutputProtocolFactory() TProtocolFactory { |
| 114 | return p.outputProtocolFactory |
| 115 | } |
| 116 | |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 117 | func (p *TSimpleServer) Listen() error { |
| 118 | return p.serverTransport.Listen() |
| 119 | } |
Jens Geyer | c975bbc | 2014-03-06 21:11:46 +0100 | [diff] [blame] | 120 | |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 121 | func (p *TSimpleServer) AcceptLoop() error { |
Jens Geyer | c975bbc | 2014-03-06 21:11:46 +0100 | [diff] [blame] | 122 | for { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 123 | client, err := p.serverTransport.Accept() |
| 124 | if err != nil { |
Jens Geyer | 57cd421 | 2014-12-08 21:25:00 +0100 | [diff] [blame] | 125 | select { |
| 126 | case <-p.quit: |
| 127 | return nil |
| 128 | default: |
| 129 | } |
| 130 | return err |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 131 | } |
| 132 | if client != nil { |
Jens Geyer | 7d95246 | 2013-07-26 01:01:11 +0200 | [diff] [blame] | 133 | go func() { |
Jens Geyer | 91cfb99 | 2014-05-17 01:07:28 +0200 | [diff] [blame] | 134 | if err := p.processRequests(client); err != nil { |
Jens Geyer | 7d95246 | 2013-07-26 01:01:11 +0200 | [diff] [blame] | 135 | log.Println("error processing request:", err) |
| 136 | } |
| 137 | }() |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 138 | } |
| 139 | } |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | func (p *TSimpleServer) Serve() error { |
| 143 | err := p.Listen() |
| 144 | if err != nil { |
| 145 | return err |
| 146 | } |
| 147 | p.AcceptLoop() |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 148 | return nil |
| 149 | } |
| 150 | |
| 151 | func (p *TSimpleServer) Stop() error { |
Jens Geyer | c975bbc | 2014-03-06 21:11:46 +0100 | [diff] [blame] | 152 | p.quit <- struct{}{} |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 153 | p.serverTransport.Interrupt() |
| 154 | return nil |
| 155 | } |
| 156 | |
Jens Geyer | 91cfb99 | 2014-05-17 01:07:28 +0200 | [diff] [blame] | 157 | func (p *TSimpleServer) processRequests(client TTransport) error { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 158 | processor := p.processorFactory.GetProcessor(client) |
| 159 | inputTransport := p.inputTransportFactory.GetTransport(client) |
| 160 | outputTransport := p.outputTransportFactory.GetTransport(client) |
| 161 | inputProtocol := p.inputProtocolFactory.GetProtocol(inputTransport) |
| 162 | outputProtocol := p.outputProtocolFactory.GetProtocol(outputTransport) |
Jens Geyer | 91cfb99 | 2014-05-17 01:07:28 +0200 | [diff] [blame] | 163 | defer func() { |
| 164 | if e := recover(); e != nil { |
| 165 | log.Printf("panic in processor: %s: %s", e, debug.Stack()) |
| 166 | } |
| 167 | }() |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 168 | if inputTransport != nil { |
| 169 | defer inputTransport.Close() |
| 170 | } |
| 171 | if outputTransport != nil { |
| 172 | defer outputTransport.Close() |
| 173 | } |
| 174 | for { |
| 175 | ok, err := processor.Process(inputProtocol, outputProtocol) |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 176 | if err, ok := err.(TTransportException); ok && err.TypeId() == END_OF_FILE { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 177 | return nil |
| 178 | } else if err != nil { |
Jens Geyer | 91cfb99 | 2014-05-17 01:07:28 +0200 | [diff] [blame] | 179 | log.Printf("error processing request: %s", err) |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 180 | return err |
| 181 | } |
Jens Geyer | 0997250 | 2014-05-02 01:30:13 +0200 | [diff] [blame] | 182 | if !ok { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 183 | break |
| 184 | } |
| 185 | } |
| 186 | return nil |
| 187 | } |