Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +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; |
| 21 | |
| 22 | import org.apache.thrift.*; |
| 23 | import org.apache.thrift.protocol.*; |
| 24 | import org.apache.thrift.transport.*; |
| 25 | import org.apache.thrift.server.*; |
| 26 | import org.apache.thrift.meta_data.*; |
| 27 | |
| 28 | import thrift.test.*; // generated code |
| 29 | |
| 30 | |
| 31 | class TestServer |
| 32 | { |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 33 | public static function Execute(args : Arguments) : Void |
| 34 | { |
| 35 | try |
| 36 | { |
| 37 | // Transport |
| 38 | var transport : TServerTransport = null; |
| 39 | switch( args.transport) { |
| 40 | case socket: |
| 41 | trace("- socket port "+args.port); |
| 42 | transport = new TServerSocket( args.port); |
| 43 | case http: |
| 44 | trace("- http"); |
Oleksii Prudkyi | 39a09ac | 2016-05-19 16:55:11 +0300 | [diff] [blame] | 45 | #if !phpwebserver |
| 46 | throw "HTTP server not implemented yet"; |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 47 | //transport = new THttpServer( targetHost); |
Oleksii Prudkyi | 39a09ac | 2016-05-19 16:55:11 +0300 | [diff] [blame] | 48 | #else |
| 49 | transport = new TWrappingServerTransport( |
| 50 | new TStreamTransport( |
| 51 | new TFileStream("php://input", Read), |
| 52 | new TFileStream("php://output", Append) |
| 53 | ) |
| 54 | ); |
| 55 | |
| 56 | #end |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 57 | default: |
| 58 | throw "Unhandled transport"; |
| 59 | } |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 60 | |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 61 | // optional: layered transport |
| 62 | var transfactory : TTransportFactory = null; |
| 63 | if ( args.framed) { |
| 64 | trace("- framed transport"); |
| 65 | transfactory = new TFramedTransportFactory(); |
| 66 | } |
| 67 | if ( args.buffered) { |
| 68 | trace("- buffered transport"); |
Jens Geyer | d35f616 | 2014-11-29 19:23:03 +0100 | [diff] [blame] | 69 | transfactory = new TBufferedTransportFactory(); |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 70 | } |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 71 | |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 72 | // protocol |
| 73 | var protfactory : TProtocolFactory = null; |
| 74 | switch( args.protocol) |
| 75 | { |
| 76 | case binary: |
| 77 | trace("- binary protocol"); |
| 78 | protfactory = new TBinaryProtocolFactory(); |
| 79 | case json: |
| 80 | trace("- json protocol"); |
| 81 | protfactory = new TJSONProtocolFactory(); |
Jens Geyer | 426ab86 | 2015-03-02 23:37:15 +0100 | [diff] [blame] | 82 | case compact: |
| 83 | trace("- compact protocol"); |
| 84 | protfactory = new TCompactProtocolFactory(); |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 85 | } |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 86 | |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 87 | |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 88 | // Processor |
| 89 | var handler = new TestServerHandler(); |
| 90 | var processor = new ThriftTestProcessor(handler); |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 91 | |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 92 | // Simple Server |
| 93 | var server : TServer = null; |
| 94 | switch( args.servertype) |
| 95 | { |
| 96 | case simple: |
Oleksii Prudkyi | 39a09ac | 2016-05-19 16:55:11 +0300 | [diff] [blame] | 97 | var simpleServer = new TSimpleServer( processor, transport, transfactory, protfactory); |
| 98 | #if phpwebserver |
| 99 | simpleServer.runOnce = true; |
| 100 | #end |
| 101 | server = simpleServer; |
| 102 | |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 103 | default: |
| 104 | throw "Unhandled server type"; |
| 105 | } |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 106 | |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 107 | |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 108 | /* |
| 109 | // Server event handler |
| 110 | if( args.serverEvents) { |
| 111 | var events = new TestServerEventHandler(); |
| 112 | server.setEventHandler(serverEvents); |
| 113 | handler.server = serverEngine; |
| 114 | } |
| 115 | */ |
| 116 | |
| 117 | // Run it |
| 118 | server.Serve(); |
| 119 | trace("done."); |
| 120 | |
| 121 | } |
| 122 | catch (x : TException) |
| 123 | { |
Jens Geyer | 1d20a37 | 2016-03-15 23:04:27 +0200 | [diff] [blame] | 124 | trace('$x ${x.errorID} ${x.errorMsg}'); |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 125 | } |
| 126 | catch (x : Dynamic) |
| 127 | { |
| 128 | trace('$x'); |
| 129 | } |
| 130 | } |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 131 | } |