diff options
author | Jake Farrell <jfarrell@apache.org> | 2012-03-22 21:49:10 +0000 |
---|---|---|
committer | Jake Farrell <jfarrell@apache.org> | 2012-03-22 21:49:10 +0000 |
commit | b95b0ffa720ffdebd829861ed420d10deab6c852 (patch) | |
tree | 2ca9cee28c31cc7fdc507039bc7e7018c64c6485 /tutorial/d | |
parent | f485e2bdb39483e776b34dd70d1ab4c9e3a7f533 (diff) | |
download | thrift-b95b0ffa720ffdebd829861ed420d10deab6c852.tar.gz |
THRIFT-1500: d programming language support
Client: D
Patch: David Nadlinger
D program language library and additions
git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1304085 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'tutorial/d')
-rw-r--r-- | tutorial/d/Makefile | 37 | ||||
-rw-r--r-- | tutorial/d/async_client.d | 86 | ||||
-rw-r--r-- | tutorial/d/client.d | 64 | ||||
-rw-r--r-- | tutorial/d/server.d | 111 |
4 files changed, 298 insertions, 0 deletions
diff --git a/tutorial/d/Makefile b/tutorial/d/Makefile new file mode 100644 index 000000000..efbb93e81 --- /dev/null +++ b/tutorial/d/Makefile @@ -0,0 +1,37 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +LIB_D_DIR = ../../lib/d + +GEN_SRC = ../gen-d/share/SharedService.d ../gen-d/share/shared_types.d \ + ../gen-d/tutorial/tutorial_types.d ../gen-d/tutorial/Calculator.d + +default: server client async_client + +server: server.d + dmd -I${LIB_D_DIR}/src -L-L${LIB_D_DIR} -L-lthriftd server.d ${GEN_SRC} + +client: client.d + dmd -I${LIB_D_DIR}/src -L-L${LIB_D_DIR} -L-lthriftd client.d ${GEN_SRC} + +async_client: async_client.d + dmd -I${LIB_D_DIR}/src -L-L${LIB_D_DIR} -L-lthriftd -L-lthriftd-event -L-levent async_client.d ${GEN_SRC} + +clean: + $(RM) -f server client async_client diff --git a/tutorial/d/async_client.d b/tutorial/d/async_client.d new file mode 100644 index 000000000..1defce082 --- /dev/null +++ b/tutorial/d/async_client.d @@ -0,0 +1,86 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +module async_client; + +import std.exception; +import std.stdio; +import thrift.async.libevent; +import thrift.async.socket; +import thrift.base; +import thrift.codegen.async_client; +import thrift.protocol.binary; +import thrift.transport.buffered; + +import tutorial.Calculator; +import tutorial.tutorial_types; + +void main() { + auto asyncManager = new TLibeventAsyncManager; + + // If we are done, gracefully stop the async manager to avoid hanging on + // appplication shutdown. + scope (exit) asyncManager.stop(); + + auto socket = new TAsyncSocket(asyncManager, "localhost", 9090); + auto client = new TAsyncClient!Calculator( + socket, + new TBufferedTransportFactory, + new TBinaryProtocolFactory!TBufferedTransport + ); + + socket.open(); + + // Invoke all the methods. + auto pingResult = client.ping(); + + auto addResult = client.add(1, 1); + + auto work = Work(); + work.op = Operation.DIVIDE; + work.num1 = 1; + work.num2 = 0; + auto quotientResult = client.calculate(1, work); + + work.op = Operation.SUBTRACT; + work.num1 = 15; + work.num2 = 10; + auto diffResult = client.calculate(1, work); + + auto logResult = client.getStruct(1); + + // Await the responses. + pingResult.waitGet(); + writeln("ping()"); + + int sum = addResult.waitGet(); + writefln("1 + 1 = %s", sum); + + try { + quotientResult.waitGet(); + writeln("Whoa we can divide by 0"); + } catch (InvalidOperation io) { + writeln("Invalid operation: " ~ io.why); + } + + writefln("15 - 10 = %s", diffResult.waitGet()); + + // TFuture is implicitly convertible to the result type via »alias this«, + // for which it (eagerly, of course) awaits completion. + writefln("Check log: %s", logResult.value); +} diff --git a/tutorial/d/client.d b/tutorial/d/client.d new file mode 100644 index 000000000..1867a17bc --- /dev/null +++ b/tutorial/d/client.d @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +module client; + +import std.stdio; +import thrift.base; +import thrift.codegen.client; +import thrift.protocol.binary; +import thrift.transport.buffered; +import thrift.transport.socket; + +import tutorial.Calculator; +import tutorial.tutorial_types; + +void main() { + auto socket = new TSocket("localhost", 9090); + auto transport = new TBufferedTransport(socket); + auto protocol = tBinaryProtocol(transport); + auto client = tClient!Calculator(protocol); + + transport.open(); + + client.ping(); + writeln("ping()"); + + int sum = client.add(1, 1); + writefln("1 + 1 = %s", sum); + + auto work = Work(); + work.op = Operation.DIVIDE; + work.num1 = 1; + work.num2 = 0; + try { + int quotient = client.calculate(1, work); + writeln("Whoa we can divide by 0"); + } catch (InvalidOperation io) { + writeln("Invalid operation: " ~ io.why); + } + + work.op = Operation.SUBTRACT; + work.num1 = 15; + work.num2 = 10; + int diff = client.calculate(1, work); + writefln("15 - 10 = %s", diff); + + auto log = client.getStruct(1); + writefln("Check log: %s", log.value); +} diff --git a/tutorial/d/server.d b/tutorial/d/server.d new file mode 100644 index 000000000..d32b9374d --- /dev/null +++ b/tutorial/d/server.d @@ -0,0 +1,111 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +module server; + +import std.conv : to; +import std.stdio; +import thrift.codegen.processor; +import thrift.protocol.binary; +import thrift.server.simple; +import thrift.server.transport.socket; +import thrift.transport.buffered; + +import share.SharedService; +import share.shared_types; +import tutorial.Calculator; +import tutorial.tutorial_types; + +/** + * The actual implementation of the Calculator interface that is called by + * the server to answer the requests. + */ +class CalculatorHandler : Calculator { + void ping() { + writeln("ping()"); + } + + int add(int n1, int n2) { + writefln("add(%s,%s)", n1, n2); + return n1 + n2; + } + + int calculate(int logid, ref const(Work) work) { + writefln("calculate(%s, {%s, %s, %s})", logid, work.op, work.num1, work.num2); + int val; + + switch (work.op) { + case Operation.ADD: + val = work.num1 + work.num2; + break; + case Operation.SUBTRACT: + val = work.num1 - work.num2; + break; + case Operation.MULTIPLY: + val = work.num1 * work.num2; + break; + case Operation.DIVIDE: + if (work.num2 == 0) { + auto io = new InvalidOperation(); + io.what = work.op; + io.why = "Cannot divide by 0"; + throw io; + } + val = work.num1 / work.num2; + break; + default: + auto io = new InvalidOperation(); + io.what = work.op; + io.why = "Invalid Operation"; + throw io; + } + + auto ss = SharedStruct(); + ss.key = logid; + ss.value = to!string(val); + log[logid] = ss; + + return val; + } + + SharedStruct getStruct(int logid) { + writefln("getStruct(%s)", logid); + return log[logid]; + } + + void zip() { + writeln("zip()"); + } + +protected: + SharedStruct[int] log; +} + +void main() { + auto protocolFactory = new TBinaryProtocolFactory!(); + auto processor = new TServiceProcessor!Calculator(new CalculatorHandler); + auto serverTransport = new TServerSocket(9090); + auto transportFactory = new TBufferedTransportFactory; + + auto server = new TSimpleServer( + processor, serverTransport, transportFactory, protocolFactory); + + writeln("Starting the server on port 9090..."); + server.serve(); + writeln("done."); +} |