blob: 6c7d656ec604f46a242edb0b0bbedd9bf721e6a3 [file] [log] [blame]
Christian Lavoieafc6d8f2011-02-20 02:39:19 +00001/*
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
20package thrift
21
22import (
23 "os"
24)
25
26
27/**
28 * Simple singlethreaded server for testing.
29 *
30 */
31type TSimpleServer struct {
32 stopped bool
33
34 processorFactory TProcessorFactory
35 serverTransport TServerTransport
36 inputTransportFactory TTransportFactory
37 outputTransportFactory TTransportFactory
38 inputProtocolFactory TProtocolFactory
39 outputProtocolFactory TProtocolFactory
40}
41
42func NewTSimpleServer2(processor TProcessor, serverTransport TServerTransport) *TSimpleServer {
43 return NewTSimpleServerFactory2(NewTProcessorFactory(processor), serverTransport)
44}
45
46func NewTSimpleServer4(processor TProcessor, serverTransport TServerTransport, transportFactory TTransportFactory, protocolFactory TProtocolFactory) *TSimpleServer {
47 return NewTSimpleServerFactory4(NewTProcessorFactory(processor),
48 serverTransport,
49 transportFactory,
50 protocolFactory,
51 )
52}
53
54func NewTSimpleServer6(processor TProcessor, serverTransport TServerTransport, inputTransportFactory TTransportFactory, outputTransportFactory TTransportFactory, inputProtocolFactory TProtocolFactory, outputProtocolFactory TProtocolFactory) *TSimpleServer {
55 return NewTSimpleServerFactory6(NewTProcessorFactory(processor),
56 serverTransport,
57 inputTransportFactory,
58 outputTransportFactory,
59 inputProtocolFactory,
60 outputProtocolFactory,
61 )
62}
63
64func NewTSimpleServerFactory2(processorFactory TProcessorFactory, serverTransport TServerTransport) *TSimpleServer {
65 return NewTSimpleServerFactory6(processorFactory,
66 serverTransport,
67 NewTTransportFactory(),
68 NewTTransportFactory(),
69 NewTBinaryProtocolFactoryDefault(),
70 NewTBinaryProtocolFactoryDefault(),
71 )
72}
73
74func NewTSimpleServerFactory4(processorFactory TProcessorFactory, serverTransport TServerTransport, transportFactory TTransportFactory, protocolFactory TProtocolFactory) *TSimpleServer {
75 return NewTSimpleServerFactory6(processorFactory,
76 serverTransport,
77 transportFactory,
78 transportFactory,
79 protocolFactory,
80 protocolFactory,
81 )
82}
83
84func NewTSimpleServerFactory6(processorFactory TProcessorFactory, serverTransport TServerTransport, inputTransportFactory TTransportFactory, outputTransportFactory TTransportFactory, inputProtocolFactory TProtocolFactory, outputProtocolFactory TProtocolFactory) *TSimpleServer {
85 return &TSimpleServer{processorFactory: processorFactory,
86 serverTransport: serverTransport,
87 inputTransportFactory: inputTransportFactory,
88 outputTransportFactory: outputTransportFactory,
89 inputProtocolFactory: inputProtocolFactory,
90 outputProtocolFactory: outputProtocolFactory,
91 }
92}
93
94func (p *TSimpleServer) ProcessorFactory() TProcessorFactory {
95 return p.processorFactory
96}
97
98func (p *TSimpleServer) ServerTransport() TServerTransport {
99 return p.serverTransport
100}
101
102func (p *TSimpleServer) InputTransportFactory() TTransportFactory {
103 return p.inputTransportFactory
104}
105
106func (p *TSimpleServer) OutputTransportFactory() TTransportFactory {
107 return p.outputTransportFactory
108}
109
110func (p *TSimpleServer) InputProtocolFactory() TProtocolFactory {
111 return p.inputProtocolFactory
112}
113
114func (p *TSimpleServer) OutputProtocolFactory() TProtocolFactory {
115 return p.outputProtocolFactory
116}
117
118func (p *TSimpleServer) Serve() os.Error {
119 p.stopped = false
120 err := p.serverTransport.Listen()
121 if err != nil {
122 return err
123 }
124 for !p.stopped {
125 client, err := p.serverTransport.Accept()
126 if err != nil {
127 return err
128 }
129 if client != nil {
130 p.processRequest(client)
131 }
132 }
133 return nil
134}
135
136func (p *TSimpleServer) Stop() os.Error {
137 p.stopped = true
138 p.serverTransport.Interrupt()
139 return nil
140}
141
142func (p *TSimpleServer) processRequest(client TTransport) {
143 processor := p.processorFactory.GetProcessor(client)
144 inputTransport := p.inputTransportFactory.GetTransport(client)
145 outputTransport := p.outputTransportFactory.GetTransport(client)
146 inputProtocol := p.inputProtocolFactory.GetProtocol(inputTransport)
147 outputProtocol := p.outputProtocolFactory.GetProtocol(outputTransport)
148 if inputTransport != nil {
149 defer inputTransport.Close()
150 }
151 if outputTransport != nil {
152 defer outputTransport.Close()
153 }
154 for {
155 ok, e := processor.Process(inputProtocol, outputProtocol)
156 if e != nil {
157 if !p.stopped {
158 // TODO(pomack) log error
159 break
160 }
161 }
162 if !ok {
163 break
164 }
165 }
166}