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