]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/server/TSimpleServer.hx
buildsys: switch source download to quincy
[ceph.git] / ceph / src / jaegertracing / thrift / lib / haxe / src / org / apache / thrift / server / TSimpleServer.hx
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 org.apache.thrift.server;
21
22 import org.apache.thrift.*;
23 import org.apache.thrift.protocol.*;
24 import org.apache.thrift.transport.*;
25 import org.apache.thrift.meta_data.*;
26
27 // Simple single-threaded server for testing
28 class TSimpleServer extends TServer {
29
30 private var stop : Bool = false;
31
32 //stops just after input transport returns EOF
33 //useful for limited scenarios, like embeding into php server
34 public var runOnce : Bool = false;
35
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,
44 logger);
45 }
46
47
48 public override function Serve() : Void
49 {
50 try
51 {
52 serverTransport.Listen();
53 }
54 catch (ttx : TTransportException)
55 {
56 logDelegate(ttx);
57 return;
58 }
59
60 // Fire the preServe server event when server is up,
61 // but before any client connections
62 if (serverEventHandler != null) {
63 serverEventHandler.preServe();
64 }
65
66 while( ! stop)
67 {
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;
74 try
75 {
76 client = serverTransport.Accept();
77 if (client != null) {
78 inputTransport = inputTransportFactory.getTransport( client);
79 outputTransport = outputTransportFactory.getTransport( client);
80 inputProtocol = inputProtocolFactory.getProtocol( inputTransport);
81 outputProtocol = outputProtocolFactory.getProtocol( outputTransport);
82
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);
87 }
88
89 // Process client requests until client disconnects
90 while( true) {
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);
97 }
98
99 //Process client request (blocks until transport is readable)
100 if( ! processor.process( inputProtocol, outputProtocol)) {
101 break;
102 }
103 }
104 }
105 }
106 catch( ttx : TTransportException)
107 {
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
112 Stop();
113 }
114 }
115 catch( pex : TProtocolException)
116 {
117 logDelegate('$pex ${pex.errorID} ${pex.errorMsg}'); // Unexpected
118 }
119 catch( e : Dynamic)
120 {
121 logDelegate(e); // Unexpected
122 }
123
124 if(client != null && !runOnce)
125 {
126 client.close();
127 }
128
129 // Fire deleteContext server event after client disconnects
130 if (serverEventHandler != null) {
131 serverEventHandler.deleteContext(connectionContext, inputProtocol, outputProtocol);
132 }
133 }
134 }
135
136 public override function Stop() : Void
137 {
138 stop = true;
139 serverTransport.Close();
140 }
141 }