diff options
Diffstat (limited to 'src/jaegertracing/thrift/tutorial/d')
-rw-r--r-- | src/jaegertracing/thrift/tutorial/d/Makefile.am | 53 | ||||
-rw-r--r-- | src/jaegertracing/thrift/tutorial/d/async_client.d | 86 | ||||
-rw-r--r-- | src/jaegertracing/thrift/tutorial/d/client.d | 64 | ||||
-rw-r--r-- | src/jaegertracing/thrift/tutorial/d/server.d | 111 |
4 files changed, 314 insertions, 0 deletions
diff --git a/src/jaegertracing/thrift/tutorial/d/Makefile.am b/src/jaegertracing/thrift/tutorial/d/Makefile.am new file mode 100644 index 000000000..358294ce5 --- /dev/null +++ b/src/jaegertracing/thrift/tutorial/d/Makefile.am @@ -0,0 +1,53 @@ +# +# 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 = $(top_srcdir)/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 + +$(GEN_SRC): $(top_srcdir)/tutorial/tutorial.thrift + $(top_builddir)/compiler/cpp/thrift --gen d -r $< + +server: server.d $(GEN_SRC) + $(DMD) -I${LIB_D_DIR}/src -L-L${LIB_D_DIR} -L-lthriftd server.d ${GEN_SRC} + +client: client.d $(GEN_SRC) + $(DMD) -I${LIB_D_DIR}/src -L-L${LIB_D_DIR} -L-lthriftd client.d ${GEN_SRC} + +PROGS = server client + +if WITH_D_EVENT_TESTS +async_client: async_client.d $(GEN_SRC) + $(DMD) -I${LIB_D_DIR}/src -L-L${LIB_D_DIR} -L-lthriftd-event -L-lthriftd -L-levent async_client.d ${GEN_SRC} + +PROGS += async_client +endif + +all-local: $(PROGS) + +clean: + $(RM) -f $(PROGS) + $(RM) -r gen-d/ + find . -type f -name '*.o' | xargs rm -f + +dist-hook: + $(RM) -f $(distdir)/$(PROGS) + $(RM) -r $(distdir)/gen-d/ + find $(destdir) -type f -name '*.o' | xargs rm -f diff --git a/src/jaegertracing/thrift/tutorial/d/async_client.d b/src/jaegertracing/thrift/tutorial/d/async_client.d new file mode 100644 index 000000000..1defce082 --- /dev/null +++ b/src/jaegertracing/thrift/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/src/jaegertracing/thrift/tutorial/d/client.d b/src/jaegertracing/thrift/tutorial/d/client.d new file mode 100644 index 000000000..1867a17bc --- /dev/null +++ b/src/jaegertracing/thrift/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/src/jaegertracing/thrift/tutorial/d/server.d b/src/jaegertracing/thrift/tutorial/d/server.d new file mode 100644 index 000000000..cbcedccf3 --- /dev/null +++ b/src/jaegertracing/thrift/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.whatOp = work.op; + io.why = "Cannot divide by 0"; + throw io; + } + val = work.num1 / work.num2; + break; + default: + auto io = new InvalidOperation(); + io.whatOp = 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."); +} |