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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
20 package org.apache.thrift.server;
22 import org.apache.thrift.*;
23 import org.apache.thrift.protocol.*;
24 import org.apache.thrift.transport.*;
25 import org.apache.thrift.meta_data.*;
27 // Simple single-threaded server for testing
28 class TSimpleServer extends TServer {
30 private var stop : Bool = false;
32 //stops just after input transport returns EOF
33 //useful for limited scenarios, like embeding into php server
34 public var runOnce : Bool = false;
36 public function new( processor : TProcessor,
37 serverTransport : TServerTransport,
38 transportFactory : TTransportFactory = null,
39 protocolFactory : TProtocolFactory = null,
40 logger : Dynamic->Void = null) {
41 super( processor, serverTransport,
42 transportFactory, transportFactory,
43 protocolFactory, protocolFactory,
48 public override function Serve() : Void
52 serverTransport.Listen();
54 catch (ttx : TTransportException)
60 // Fire the preServe server event when server is up,
61 // but before any client connections
62 if (serverEventHandler != null) {
63 serverEventHandler.preServe();
68 var client : TTransport = null;
69 var inputTransport : TTransport = null;
70 var outputTransport : TTransport = null;
71 var inputProtocol : TProtocol = null;
72 var outputProtocol : TProtocol = null;
73 var connectionContext : Dynamic = null;
76 client = serverTransport.Accept();
78 inputTransport = inputTransportFactory.getTransport( client);
79 outputTransport = outputTransportFactory.getTransport( client);
80 inputProtocol = inputProtocolFactory.getProtocol( inputTransport);
81 outputProtocol = outputProtocolFactory.getProtocol( outputTransport);
83 // Recover event handler (if any) and fire createContext
84 // server event when a client connects
85 if (serverEventHandler != null) {
86 connectionContext = serverEventHandler.createContext(inputProtocol, outputProtocol);
89 // Process client requests until client disconnects
91 // Fire processContext server event
92 // N.B. This is the pattern implemented in C++ and the event fires provisionally.
93 // That is to say it may be many minutes between the event firing and the client request
94 // actually arriving or the client may hang up without ever makeing a request.
95 if (serverEventHandler != null) {
96 serverEventHandler.processContext(connectionContext, inputTransport);
99 //Process client request (blocks until transport is readable)
100 if( ! processor.process( inputProtocol, outputProtocol)) {
106 catch( ttx : TTransportException)
108 // Usually a client disconnect, expected
109 if(runOnce && ttx.errorID == TTransportException.END_OF_FILE) {
110 //input returns eof, exit
111 //follows lib/cpp/src/thrift/server/TServerFramework.cpp
115 catch( pex : TProtocolException)
117 logDelegate('$pex ${pex.errorID} ${pex.errorMsg}'); // Unexpected
121 logDelegate(e); // Unexpected
124 if(client != null && !runOnce)
129 // Fire deleteContext server event after client disconnects
130 if (serverEventHandler != null) {
131 serverEventHandler.deleteContext(connectionContext, inputProtocol, outputProtocol);
136 public override function Stop() : Void
139 serverTransport.Close();