Updated Branches: refs/heads/master 0ef1e2eee -> 64998e2ac
THRIFT-2334 nodejs: add a tutorial for node JS Patch: Pierre Lamot Project: http://git-wip-us.apache.org/repos/asf/thrift/repo Commit: http://git-wip-us.apache.org/repos/asf/thrift/commit/64998e2a Tree: http://git-wip-us.apache.org/repos/asf/thrift/tree/64998e2a Diff: http://git-wip-us.apache.org/repos/asf/thrift/diff/64998e2a Branch: refs/heads/master Commit: 64998e2ac415d7085d7683e183535b43178cc0ca Parents: 0ef1e2e Author: Roger Meier <[email protected]> Authored: Mon Jan 27 21:15:56 2014 +0100 Committer: Roger Meier <[email protected]> Committed: Mon Jan 27 21:15:56 2014 +0100 ---------------------------------------------------------------------- configure.ac | 1 + tutorial/Makefile.am | 8 +++- tutorial/nodejs/Makefile.am | 38 +++++++++++++++++ tutorial/nodejs/NodeClient.js | 79 +++++++++++++++++++++++++++++++++++ tutorial/nodejs/NodeServer.js | 85 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 209 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/thrift/blob/64998e2a/configure.ac ---------------------------------------------------------------------- diff --git a/configure.ac b/configure.ac index 10b4779..bb09c7c 100755 --- a/configure.ac +++ b/configure.ac @@ -624,6 +624,7 @@ AC_CONFIG_FILES([ tutorial/go/Makefile tutorial/java/Makefile tutorial/js/Makefile + tutorial/nodejs/Makefile tutorial/py/Makefile tutorial/py.twisted/Makefile tutorial/py.tornado/Makefile http://git-wip-us.apache.org/repos/asf/thrift/blob/64998e2a/tutorial/Makefile.am ---------------------------------------------------------------------- diff --git a/tutorial/Makefile.am b/tutorial/Makefile.am index 5f65802..6fc9209 100755 --- a/tutorial/Makefile.am +++ b/tutorial/Makefile.am @@ -46,6 +46,10 @@ if WITH_GO SUBDIRS += go endif +if WITH_NODEJS +SUBDIRS += nodejs +endif + # # generate html for ThriftTest.thrift # @@ -53,8 +57,8 @@ all-local: $(top_builddir)/compiler/cpp/thrift --gen html -r $(top_srcdir)/tutorial/tutorial.thrift endif -# Any folders or files not listed above being added to SUBDIR need to be placed here in -# EXTRA_DIST to be included in the release +# Any folders or files not listed above being added to SUBDIR need to be placed here in +# EXTRA_DIST to be included in the release EXTRA_DIST = \ csharp \ d \ http://git-wip-us.apache.org/repos/asf/thrift/blob/64998e2a/tutorial/nodejs/Makefile.am ---------------------------------------------------------------------- diff --git a/tutorial/nodejs/Makefile.am b/tutorial/nodejs/Makefile.am new file mode 100644 index 0000000..2affb9f --- /dev/null +++ b/tutorial/nodejs/Makefile.am @@ -0,0 +1,38 @@ +# +# 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. +# + +THRIFT = $(top_builddir)/compiler/cpp/thrift + +gen-nodejs/Calculator.js gen-nodejs/SharedService.js: $(top_srcdir)/tutorial/tutorial.thrift + $(THRIFT) --gen js:node -r $< + +all-local: gen-nodejs/Calculator.js + +tutorialserver: all + NODE_PATH="$(top_builddir)/lib/nodejs:$(top_builddir)/lib/nodejs/lib:$(NODEPATH)" $(NODEJS) NodeServer.js + +tutorialclient: all + NODE_PATH="$(top_builddir)/lib/nodejs:$(top_builddir)/lib/nodejs/lib:$(NODEPATH)" $(NODEJS) NodeClient.js + +clean-local: + $(RM) -r gen-* + +EXTRA_DIST = \ + NodeServer.js \ + NodeClient.js http://git-wip-us.apache.org/repos/asf/thrift/blob/64998e2a/tutorial/nodejs/NodeClient.js ---------------------------------------------------------------------- diff --git a/tutorial/nodejs/NodeClient.js b/tutorial/nodejs/NodeClient.js new file mode 100644 index 0000000..3d09709 --- /dev/null +++ b/tutorial/nodejs/NodeClient.js @@ -0,0 +1,79 @@ +/* + * 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. + */ + +var thrift = require('thrift'); +var ThriftTransports = require('thrift/transport'); +var ThriftProtocols = require('thrift/protocol'); +var Calculator = require('./gen-nodejs/Calculator'); +var ttypes = require('./gen-nodejs/tutorial_types'); + + +transport = ThriftTransports.TBufferedTransport() +protocol = ThriftProtocols.TBinaryProtocol() + +var connection = thrift.createConnection("localhost", 9090, { + transport : transport, + protocol : protocol +}); + +connection.on('error', function(err) { + assert(false, err); +}); + +// Create a Calculator client with the connection +var client = thrift.createClient(Calculator, connection); + + +client.ping(function(err, response) { + console.log('ping()'); +}); + + +client.add(1,1, function(err, response) { + console.log("1+1=" + response); +}); + + +work = new ttypes.Work(); +work.op = ttypes.Operation.DIVIDE; +work.num1 = 1; +work.num2 = 0; + +client.calculate(1, work, function(err, message) { + if (err) { + console.log("InvalidOperation " + err); + } else { + console.log('Whoa? You know how to divide by zero?'); + } +}); + +work.op = ttypes.Operation.SUBTRACT; +work.num1 = 15; +work.num2 = 10; + +client.calculate(1, work, function(err, message) { + console.log('15-10=' + message.value); + + client.getStruct(1, function(err, message){ + console.log('Check log: ' + message.value); + + //close the connection once we're done + connection.end(); + }); +}); http://git-wip-us.apache.org/repos/asf/thrift/blob/64998e2a/tutorial/nodejs/NodeServer.js ---------------------------------------------------------------------- diff --git a/tutorial/nodejs/NodeServer.js b/tutorial/nodejs/NodeServer.js new file mode 100644 index 0000000..ba36105 --- /dev/null +++ b/tutorial/nodejs/NodeServer.js @@ -0,0 +1,85 @@ +/* + * 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. + */ + +var thrift = require("thrift"); +var Calculator = require("./gen-nodejs/Calculator"); +var ttypes = require("./gen-nodejs/tutorial_types"); +var SharedStruct = require("./gen-nodejs/shared_types").SharedStruct; + +var data = {}; + +var server = thrift.createServer(Calculator, { + ping: function(result) { + console.log("ping()"); + result(null); + }, + + add: function(n1, n2, result) { + console.log("add(", n1, ",", n2, ")"); + result(null, n1 + n2); + }, + + calculate: function(logid, work, result) { + console.log("calculate(", logid, ",", work, ")"); + + var val = 0; + if (work.op == ttypes.Operation.ADD) { + val = work.num1 + work.num2; + } else if (work.op === ttypes.Operation.SUBTRACT) { + val = work.num1 - work.num2; + } else if (work.op === ttypes.Operation.MULTIPLY) { + val = work.num1 * work.num2; + } else if (work.op === ttypes.Operation.DIVIDE) { + if (work.num2 === 0) { + var x = new ttypes.InvalidOperation(); + x.what = work.op; + x.why = 'Cannot divide by 0'; + result(x); + return; + } + val = work.num1 / work.num2; + } else { + var x = new ttypes.InvalidOperation(); + x.what = work.op; + x.why = 'Invalid operation'; + result(x); + return; + } + + var entry = new SharedStruct(); + entry.key = logid; + entry.value = ""+val; + data[logid] = entry; + + result(null, val); + }, + + getStruct: function(key, result) { + console.log("getStruct(", key, ")"); + result(null, data[key]); + }, + + zip: function() { + console.log("zip()"); + result(null); + } + +}); + +server.listen(9090);
