const std::string& option_string) :
t_oop_generator(program) {
(void) option_string;
-
+
std::map<std::string, std::string>::const_iterator iter;
-
+
iter = parsed_options.find("node");
gen_node_ = (iter != parsed_options.end());
-
+
iter = parsed_options.find("jquery");
gen_jquery_ = (iter != parsed_options.end());
*/
string t_js_generator::js_includes() {
if (gen_node_) {
- return string("var Thrift = require('thrift').Thrift;");
+ return string("var Thrift = require('thrift').Thrift;\nvar Q = require('q');");
}
string inc;
const std::vector<t_field*>& fields = arg_struct->get_members();
vector<t_field*>::const_iterator f_iter;
- f_service_ <<
- indent() << "this._handler." << tfunction->get_name() << "(";
+ // Shortcut out here for oneway functions
+ if (tfunction->is_oneway()) {
+ indent(f_service_) << "this._handler." << tfunction->get_name() << "(";
- bool first = true;
- for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- if (first) {
- first = false;
- } else {
- f_service_ << ", ";
+ bool first = true;
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ if (first) {
+ first = false;
+ } else {
+ f_service_ << ", ";
+ }
+ f_service_ << "args." << (*f_iter)->get_name();
}
- f_service_ << "args." << (*f_iter)->get_name();
- }
- // Shortcut out here for oneway functions
- if (tfunction->is_oneway()) {
f_service_ << ")" << endl;
scope_down(f_service_);
f_service_ << endl;
return;
}
- if (!first) {
- f_service_ << ", ";
+ f_service_ <<
+ indent() << "if (this._handler." << tfunction->get_name() << ".length === " << fields.size() <<") {" << endl;
+ indent_up();
+ indent(f_service_) << "Q.fcall(this._handler." << tfunction->get_name();
+
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ f_service_ << ", args." << (*f_iter)->get_name();
+ }
+
+ f_service_ << ")" << endl;
+ indent_up();
+ indent(f_service_) << ".then(function(result) {" << endl;
+ indent_up();
+ f_service_ <<
+ indent() << "var result = new " << resultname << "({success: result});" << endl <<
+ indent() << "output.writeMessageBegin(\"" << tfunction->get_name() <<
+ "\", Thrift.MessageType.REPLY, seqid);" << endl <<
+ indent() << "result.write(output);" << endl <<
+ indent() << "output.writeMessageEnd();" << endl <<
+ indent() << "output.flush();" << endl;
+ indent_down();
+ indent(f_service_) << "}, function (err) {" << endl;
+ indent_up();
+ f_service_ <<
+ indent() << "var result = new " << resultname << "(err);" << endl <<
+ indent() << "output.writeMessageBegin(\"" << tfunction->get_name() <<
+ "\", Thrift.MessageType.REPLY, seqid);" << endl <<
+ indent() << "result.write(output);" << endl <<
+ indent() << "output.writeMessageEnd();" << endl <<
+ indent() << "output.flush();" << endl;
+ indent_down();
+ indent(f_service_) << "});" << endl;
+ indent_down();
+ indent_down();
+ indent(f_service_) << "} else {" << endl;
+ indent_up();
+ indent(f_service_) << "this._handler." << tfunction->get_name() << "(";
+
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ f_service_ << "args." << (*f_iter)->get_name() << ", ";
}
- f_service_ << "function (err, result) {" << endl;
+
+ f_service_ << " function (err, result) {" << endl;
indent_up();
f_service_ <<
- indent() << "var result = new " << resultname << "((err != null ? err : {success: result}));" << endl <<
- indent() << "output.writeMessageBegin(\"" << tfunction->get_name() <<
+ indent() << "var result = new " << resultname << "((err != null ? err : {success: result}));" << endl <<
+ indent() << "output.writeMessageBegin(\"" << tfunction->get_name() <<
"\", Thrift.MessageType.REPLY, seqid);" << endl <<
- indent() << "result.write(output);" << endl <<
- indent() << "output.writeMessageEnd();" << endl <<
- indent() << "output.flush();" << endl;
+ indent() << "result.write(output);" << endl <<
+ indent() << "output.writeMessageEnd();" << endl <<
+ indent() << "output.flush();" << endl;
indent_down();
- indent(f_service_) << "})" << endl;
-
+ indent(f_service_) << "});" << endl;
+ indent_down();
+ indent(f_service_) << "}" << endl;
scope_down(f_service_);
f_service_ << endl;
}
if (gen_node_) {
f_service_ <<
- js_namespace(tservice->get_program()) << service_name_ << "Client = " <<
+ js_namespace(tservice->get_program()) << service_name_ << "Client = " <<
"exports.Client = function(output, pClass) {"<<endl;
} else {
f_service_ <<
if (gen_node_) { //Node.js output ./gen-nodejs
f_service_ <<
indent() << "this._seqid = this.new_seqid();" << endl <<
+ indent() << "if (callback === undefined) {" << endl;
+ indent_up();
+ f_service_ <<
+ indent() << "var _defer = Q.defer();" << endl <<
+ indent() << "this._reqs[this.seqid()] = function(error, result) {" << endl;
+ indent_up();
+ indent(f_service_) << "if (error) {" << endl;
+ indent_up();
+ indent(f_service_) << "_defer.reject(error);" << endl;
+ indent_down();
+ indent(f_service_) << "} else {" << endl;
+ indent_up();
+ indent(f_service_) << "_defer.resolve(result);" << endl;
+ indent_down();
+ indent(f_service_) << "}" << endl;
+ indent_down();
+ indent(f_service_) << "};" << endl;
+ f_service_ <<
+ indent() << "this.send_" << funname << "(" << arglist << ");" << endl <<
+ indent() << "return _defer.promise;" << endl;
+ indent_down();
+ indent(f_service_) << "} else {" << endl;
+ indent_up();
+ f_service_ <<
indent() << "this._reqs[this.seqid()] = callback;" << endl <<
indent() << "this.send_" << funname << "(" << arglist << ");" << endl;
+ indent_down();
+ indent(f_service_) << "}" << endl;
}
else if (gen_jquery_) { //jQuery output ./gen-js
f_service_ << indent() << "if (callback === undefined) {" << endl;
f_service_ << indent() << "}" << endl;
}
}
-
+
indent_down();
f_service_ << "};" << endl << endl;
}
f_service_ <<
- indent() << "var args = new " << argsname << "();" << endl;
+ indent() << "var args = new " << argsname << "();" << endl;
for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
f_service_ <<
if (gen_jquery_) {
f_service_ << indent() << "return this.output.getTransport().flush(callback);" << endl;
} else {
- f_service_ << indent() << "if (callback) {" << endl;
+ f_service_ << indent() << "if (callback) {" << endl;
f_service_ << indent() << " var self = this;" << endl;
f_service_ << indent() << " this.output.getTransport().flush(true, function() {" << endl;
f_service_ << indent() << " if (this.readyState == 4 && this.status == 200) {" << endl;
THRIFT_REGISTER_GENERATOR(js, "Javascript",
" jquery: Generate jQuery compatible code.\n"
" node: Generate node.js compatible code.\n")
-
-
"engines": { "node": ">= 0.2.4" },
"dependencies": {
"node-int64": "~0.3.0",
+ "q": "1.0.x",
"nodeunit": "~0.8.0"
},
"devDependencies": {
var ThriftProtocols = require('thrift/protocol');
var ThriftTest = require('./gen-nodejs/ThriftTest');
var ThriftTestDriver = require('./thrift_test_driver').ThriftTestDriver;
+var ThriftTestDriverPromise = require('./thrift_test_driver_promise').ThriftTestDriver;
var program = require('commander');
.option('-p, --protocol <protocol>', 'Set thrift protocol (binary|json) [protocol]')
.option('-t, --transport <transport>', 'Set thrift transport (buffered|framed) [transport]')
.option('--ssl', 'use SSL transport')
+ .option('--promise', 'test with promise style functions')
.parse(process.argv);
var protocol = undefined;
var transport = undefined;
+var testDriver = undefined;
if (program.protocol === "binary") {
protocol = ThriftProtocols.TBinaryProtocol;
transport = ThriftTransports.TBufferedTransport;
}
+if (program.promise) {
+ testDriver = ThriftTestDriverPromise;
+} else {
+ testDriver = ThriftTestDriver;
+}
+
var options = {
transport: transport,
protocol: protocol
assert(false, err);
});
-ThriftTestDriver(client, function (status) {
+testDriver(client, function (status) {
console.log(status);
connection.end();
});
var ThriftProtocols = require('thrift/protocol');
var ThriftTest = require('./gen-nodejs/ThriftTest');
var ThriftTestHandler = require('./test_handler').ThriftTestHandler;
+var ThriftTestHandlerPromise = require('./test_handler_promise').ThriftTestHandler;
var program = require('commander');
.option('-p, --protocol <protocol>', 'Set thift protocol (binary|json) [protocol]')
.option('-t, --transport <transport>', 'Set thift transport (buffered|framed) [transport]')
.option('--ssl', 'use ssl transport')
+ .option('--promise', 'test with promise style functions')
.parse(process.argv);
var protocol = undefined;
var transport = undefined;
+var handler = undefined;
if (program.protocol === "binary") {
protocol = ThriftProtocols.TBinaryProtocol;
transport = ThriftTransports.TBufferedTransport;
}
+if (program.promise) {
+ handler = ThriftTestHandlerPromise;
+} else {
+ handler = ThriftTestHandler;
+}
+
var options = {
protocol: protocol,
transport: transport
//ssl options
options.key = fs.readFileSync(path.resolve(__dirname, 'server.key'));
options.cert = fs.readFileSync(path.resolve(__dirname, 'server.crt'));
- thrift.createSSLServer(ThriftTest, ThriftTestHandler, options).listen(9090);
+ thrift.createSSLServer(ThriftTest, handler, options).listen(9090);
} else {
//default
- thrift.createServer(ThriftTest, ThriftTestHandler, options).listen(9090);
+ thrift.createServer(ThriftTest, handler, options).listen(9090);
}
testClientServer binary framed --ssl || TESTOK=1
testMultiplexedClientServer binary framed --ssl || TESTOK=1
+#test promise style
+testClientServer binary framed --promise || TESTOK=1
exit $TESTOK
--- /dev/null
+/*\r
+ * Licensed to the Apache Software Foundation (ASF) under one\r
+ * or more contributor license agreements. See the NOTICE file\r
+ * distributed with this work for additional information\r
+ * regarding copyright ownership. The ASF licenses this file\r
+ * to you under the Apache License, Version 2.0 (the\r
+ * 'License'); you may not use this file except in compliance\r
+ * with the License. You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing,\r
+ * software distributed under the License is distributed on an\r
+ * 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\r
+ * KIND, either express or implied. See the License for the\r
+ * specific language governing permissions and limitations\r
+ * under the License.\r
+ */\r
+\r
+//This is the server side Node test handler for the standard\r
+// Apache Thrift test service.\r
+\r
+var ttypes = require('./gen-nodejs/ThriftTest_types');\r
+var TException = require('thrift/thrift').TException;\r
+\r
+var ThriftTestHandler = exports.ThriftTestHandler = {\r
+ testVoid: function() {\r
+ console.log('testVoid()');\r
+ },\r
+ testString: function(thing) {\r
+ console.log('testString(\'' + thing + '\')');\r
+ return thing;\r
+ },\r
+ testByte: function(thing) {\r
+ console.log('testByte(' + thing + ')');\r
+ return thing;\r
+ },\r
+ testI32: function(thing) {\r
+ console.log('testI32(' + thing + ')');\r
+ return thing;\r
+ },\r
+ testI64: function(thing) {\r
+ console.log('testI64(' + thing + ')');\r
+ return thing;\r
+ },\r
+ testDouble: function(thing) {\r
+ console.log('testDouble(' + thing + ')');\r
+ return thing;\r
+ },\r
+ testStruct: function(thing) {\r
+ console.log('testStruct(');\r
+ console.log(thing);\r
+ console.log(')');\r
+ return thing;\r
+ },\r
+ testNest: function(nest) {\r
+ console.log('testNest(');\r
+ console.log(nest);\r
+ console.log(')');\r
+ return nest;\r
+ },\r
+ testMap: function(thing) {\r
+ console.log('testMap(');\r
+ console.log(thing);\r
+ console.log(')');\r
+ return thing;\r
+ },\r
+ testStringMap: function(thing) {\r
+ console.log('testStringMap(');\r
+ console.log(thing);\r
+ console.log(')');\r
+ return thing;\r
+ },\r
+ testSet: function(thing, result) {\r
+ console.log('testSet(');\r
+ console.log(thing);\r
+ console.log(')');\r
+ return thing;\r
+ },\r
+ testList: function(thing) {\r
+ console.log('testList(');\r
+ console.log(thing);\r
+ console.log(')');\r
+ return thing;\r
+ },\r
+ testEnum: function(thing) {\r
+ console.log('testEnum(' + thing + ')');\r
+ return thing;\r
+ },\r
+ testTypedef: function(thing) {\r
+ console.log('testTypedef(' + thing + ')');\r
+ return thing;\r
+ },\r
+ testMapMap: function(hello) {\r
+ console.log('testMapMap(' + hello + ')');\r
+\r
+ var mapmap = [];\r
+ var pos = [];\r
+ var neg = [];\r
+ for (var i = 1; i < 5; i++) {\r
+ pos[i] = i;\r
+ neg[-i] = -i;\r
+ }\r
+ mapmap[4] = pos;\r
+ mapmap[-4] = neg;\r
+\r
+ return mapmap;\r
+ },\r
+ testInsanity: function(argument) {\r
+ console.log('testInsanity(');\r
+ console.log(argument);\r
+ console.log(')');\r
+\r
+ var hello = new ttypes.Xtruct();\r
+ hello.string_thing = 'Hello2';\r
+ hello.byte_thing = 2;\r
+ hello.i32_thing = 2;\r
+ hello.i64_thing = 2;\r
+\r
+ var goodbye = new ttypes.Xtruct();\r
+ goodbye.string_thing = 'Goodbye4';\r
+ goodbye.byte_thing = 4;\r
+ goodbye.i32_thing = 4;\r
+ goodbye.i64_thing = 4;\r
+\r
+ var crazy = new ttypes.Insanity();\r
+ crazy.userMap = [];\r
+ crazy.userMap[ttypes.Numberz.EIGHT] = 8;\r
+ crazy.userMap[ttypes.Numberz.FIVE] = 5;\r
+ crazy.xtructs = [goodbye, hello];\r
+\r
+ var first_map = [];\r
+ var second_map = [];\r
+\r
+ first_map[ttypes.Numberz.TWO] = crazy;\r
+ first_map[ttypes.Numberz.THREE] = crazy;\r
+\r
+ var looney = new ttypes.Insanity();\r
+ second_map[ttypes.Numberz.SIX] = looney;\r
+\r
+ var insane = [];\r
+ insane[1] = first_map;\r
+ insane[2] = second_map;\r
+\r
+ console.log('insane result:');\r
+ console.log(insane);\r
+ return insane;\r
+ },\r
+ testMulti: function(arg0, arg1, arg2, arg3, arg4, arg5) {\r
+ console.log('testMulti()');\r
+\r
+ var hello = new ttypes.Xtruct();\r
+ hello.string_thing = 'Hello2';\r
+ hello.byte_thing = arg0;\r
+ hello.i32_thing = arg1;\r
+ hello.i64_thing = arg2;\r
+ return hello;\r
+ },\r
+ testException: function(arg) {\r
+ console.log('testException('+arg+')');\r
+ if (arg === 'Xception') {\r
+ var x = new ttypes.Xception();\r
+ x.errorCode = 1001;\r
+ x.message = arg;\r
+ throw x;\r
+ } else if (arg === 'TException') {\r
+ throw new TException(arg);\r
+ } else {\r
+ return;\r
+ }\r
+ },\r
+ testMultiException: function(arg0, arg1) {\r
+ console.log('testMultiException(' + arg0 + ', ' + arg1 + ')');\r
+ if (arg0 === ('Xception')) {\r
+ var x = new ttypes.Xception();\r
+ x.errorCode = 1001;\r
+ x.message = 'This is an Xception';\r
+ throw x;\r
+ } else if (arg0 === ('Xception2')) {\r
+ var x2 = new ttypes.Xception2();\r
+ x2.errorCode = 2002;\r
+ x2.struct_thing = new ttypes.Xtruct();\r
+ x2.struct_thing.string_thing = 'This is an Xception2';\r
+ throw x2;\r
+ }\r
+\r
+ var res = new ttypes.Xtruct();\r
+ res.string_thing = arg1;\r
+ return res;\r
+ },\r
+ testOneway: function(sleepFor) {\r
+ console.log('testOneway(' + sleepFor + ') => JavaScript (like Rust) never sleeps!');\r
+ }\r
+}; //ThriftTestSvcHandler\r
--- /dev/null
+/*\r
+ * Licensed to the Apache Software Foundation (ASF) under one\r
+ * or more contributor license agreements. See the NOTICE file\r
+ * distributed with this work for additional information\r
+ * regarding copyright ownership. The ASF licenses this file\r
+ * to you under the Apache License, Version 2.0 (the\r
+ * 'License'); you may not use this file except in compliance\r
+ * with the License. You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing,\r
+ * software distributed under the License is distributed on an\r
+ * 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\r
+ * KIND, either express or implied. See the License for the\r
+ * specific language governing permissions and limitations\r
+ * under the License.\r
+ */\r
+\r
+ // This is the Node test driver for the standard Apache Thrift\r
+ // test service. The driver invokes every function defined in the\r
+ // Thrift Test service with a representative range of parameters.\r
+ //\r
+ // The ThriftTestDriver function requires a client object\r
+ // connected to a server hosting the Thrift Test service and\r
+ // supports an optional callback function which is called with\r
+ // a status message when the test is complete.\r
+\r
+var assert = require('assert');\r
+var ttypes = require('./gen-nodejs/ThriftTest_types');\r
+\r
+var ThriftTestDriver = exports.ThriftTestDriver = function(client, callback) {\r
+\r
+// deepEqual doesn't work with fields using node-int64\r
+function checkRecursively(map1, map2) {\r
+ if (typeof map1 !== 'function' && typeof map2 !== 'function') {\r
+ if (!map1 || typeof map1 !== 'object') {\r
+ assert.equal(map1, map2);\r
+ } else {\r
+ for (var key in map1) {\r
+ checkRecursively(map1[key], map2[key]);\r
+ }\r
+ }\r
+ }\r
+}\r
+\r
+client.testVoid()\r
+ .then(function(response) {\r
+ assert.equal(undefined, response); //void\r
+ })\r
+ .fail(function() {\r
+ assert(false);\r
+ });\r
+\r
+\r
+client.testString("Test")\r
+ .then(function(response) {\r
+ assert.equal("Test", response);\r
+ })\r
+ .fail(function() {\r
+ assert(false);\r
+ });\r
+\r
+client.testString("")\r
+ .then(function(response) {\r
+ assert.equal("", response);\r
+ })\r
+ .fail(function() {\r
+ assert(false);\r
+ });\r
+\r
+//all Languages in UTF-8\r
+var stringTest = "Afrikaans, Alemannisch, Aragonés, العربية, مصرى, " +\r
+ "Asturianu, Aymar aru, Azərbaycan, Башҡорт, Boarisch, Žemaitėška, " +\r
+ "Беларуская, Беларуская (тарашкевіца), Български, Bamanankan, " +\r
+ "বাংলা, Brezhoneg, Bosanski, Català, Mìng-dĕ̤ng-ngṳ̄, Нохчийн, " +\r
+ "Cebuano, ᏣᎳᎩ, Česky, Словѣ́ньскъ / ⰔⰎⰑⰂⰡⰐⰠⰔⰍⰟ, Чӑвашла, Cymraeg, " +\r
+ "Dansk, Zazaki, ދިވެހިބަސް, Ελληνικά, Emiliàn e rumagnòl, English, " +\r
+ "Esperanto, Español, Eesti, Euskara, فارسی, Suomi, Võro, Føroyskt, " +\r
+ "Français, Arpetan, Furlan, Frysk, Gaeilge, 贛語, Gàidhlig, Galego, " +\r
+ "Avañe'ẽ, ગુજરાતી, Gaelg, עברית, हिन्दी, Fiji Hindi, Hrvatski, " +\r
+ "Kreyòl ayisyen, Magyar, Հայերեն, Interlingua, Bahasa Indonesia, " +\r
+ "Ilokano, Ido, Íslenska, Italiano, 日本語, Lojban, Basa Jawa, " +\r
+ "ქართული, Kongo, Kalaallisut, ಕನ್ನಡ, 한국어, Къарачай-Малкъар, " +\r
+ "Ripoarisch, Kurdî, Коми, Kernewek, Кыргызча, Latina, Ladino, " +\r
+ "Lëtzebuergesch, Limburgs, Lingála, ລາວ, Lietuvių, Latviešu, Basa " +\r
+ "Banyumasan, Malagasy, Македонски, മലയാളം, मराठी, مازِرونی, Bahasa " +\r
+ "Melayu, Nnapulitano, Nedersaksisch, नेपाल भाषा, Nederlands, \202a" +\r
+ "Norsk (nynorsk)\202c, \202aNorsk (bokmål)\202c, Nouormand, Diné bizaad, " +\r
+ "Occitan, Иронау, Papiamentu, Deitsch, Polski, پنجابی, پښتو, " +\r
+ "Norfuk / Pitkern, Português, Runa Simi, Rumantsch, Romani, Română, " +\r
+ "Русский, Саха тыла, Sardu, Sicilianu, Scots, Sámegiella, Simple " +\r
+ "English, Slovenčina, Slovenščina, Српски / Srpski, Seeltersk, " +\r
+ "Svenska, Kiswahili, தமிழ், తెలుగు, Тоҷикӣ, ไทย, Türkmençe, Tagalog, " +\r
+ "Türkçe, Татарча/Tatarça, Українська, اردو, Tiếng Việt, Volapük, " +\r
+ "Walon, Winaray, 吴语, isiXhosa, ייִדיש, Yorùbá, Zeêuws, 中文, " +\r
+ "Bân-lâm-gú, 粵語";\r
+\r
+client.testString(stringTest)\r
+ .then(function(response) {\r
+ assert.equal(stringTest, response);\r
+ })\r
+ .fail(function() {\r
+ assert(false);\r
+ });\r
+\r
+\r
+var specialCharacters = 'quote: \" backslash:' +\r
+ ' forwardslash-escaped: \/ ' +\r
+ ' backspace: \b formfeed: \f newline: \n return: \r tab: ' +\r
+ ' now-all-of-them-together: "\\\/\b\n\r\t' +\r
+ ' now-a-bunch-of-junk: !@#$%&()(&%$#{}{}<><><' +\r
+ ' char-to-test-json-parsing: ]] \"]] \\" }}}{ [[[ ';\r
+client.testString(specialCharacters)\r
+ .then(function(response) {\r
+ assert.equal(specialCharacters, response);\r
+ })\r
+ .fail(function() {\r
+ assert(false);\r
+ });\r
+\r
+\r
+client.testByte(1)\r
+ .then(function(response) {\r
+ assert.equal(1, response);\r
+ })\r
+ .fail(function() {\r
+ assert(false);\r
+ });\r
+\r
+\r
+client.testByte(0)\r
+ .then(function(response) {\r
+ assert.equal(0, response);\r
+ })\r
+ .fail(function() {\r
+ assert(false);\r
+ });\r
+\r
+client.testByte(-1)\r
+ .then(function(response) {\r
+ assert.equal(-1, response);\r
+ })\r
+ .fail(function() {\r
+ assert(false);\r
+ });\r
+\r
+client.testByte(-127)\r
+ .then(function(response) {\r
+ assert.equal(-127, response);\r
+ })\r
+ .fail(function() {\r
+ assert(false);\r
+ });\r
+\r
+client.testI32(-1)\r
+ .then(function(response) {\r
+ assert.equal(-1, response);\r
+ })\r
+ .fail(function() {\r
+ assert(false);\r
+ });\r
+\r
+client.testI64(5)\r
+ .then(function(response) {\r
+ assert.equal(5, response);\r
+ })\r
+ .fail(function() {\r
+ assert(false);\r
+ });\r
+\r
+client.testI64(-5)\r
+ .then(function(response) {\r
+ assert.equal(-5, response);\r
+ })\r
+ .fail(function() {\r
+ assert(false);\r
+ });\r
+\r
+client.testI64(-34359738368)\r
+ .then(function(response) {\r
+ assert.equal(-34359738368, response);\r
+ })\r
+ .fail(function() {\r
+ assert(false);\r
+ });\r
+\r
+client.testDouble(-5.2098523)\r
+ .then(function(response) {\r
+ assert.equal(-5.2098523, response);\r
+ })\r
+ .fail(function() {\r
+ assert(false);\r
+ });\r
+\r
+client.testDouble(7.012052175215044)\r
+ .then(function(response) {\r
+ assert.equal(7.012052175215044, response);\r
+ })\r
+ .fail(function() {\r
+ assert(false);\r
+ });\r
+\r
+var out = new ttypes.Xtruct({\r
+ string_thing: 'Zero',\r
+ byte_thing: 1,\r
+ i32_thing: -3,\r
+ i64_thing: 1000000\r
+});\r
+client.testStruct(out)\r
+ .then(function(response) {\r
+ checkRecursively(out, response);\r
+ })\r
+ .fail(function() {\r
+ assert(false);\r
+ });\r
+\r
+var out2 = new ttypes.Xtruct2();\r
+out2.byte_thing = 1;\r
+out2.struct_thing = out;\r
+out2.i32_thing = 5;\r
+client.testNest(out2)\r
+ .then(function(response) {\r
+ checkRecursively(out2, response);\r
+ })\r
+ .fail(function() {\r
+ assert(false);\r
+ });\r
+\r
+var mapout = {};\r
+for (var i = 0; i < 5; ++i) {\r
+ mapout[i] = i-10;\r
+}\r
+client.testMap(mapout)\r
+ .then(function(response) {\r
+ assert.deepEqual(mapout, response);\r
+ })\r
+ .fail(function() {\r
+ assert(false);\r
+ });\r
+\r
+var mapTestInput = {\r
+ "a":"123", "a b":"with spaces ", "same":"same", "0":"numeric key",\r
+ "longValue":stringTest, stringTest:"long key"\r
+};\r
+client.testStringMap(mapTestInput)\r
+ .then(function(response) {\r
+ assert.deepEqual(mapTestInput, response);\r
+ })\r
+ .fail(function() {\r
+ assert(false);\r
+ });\r
+\r
+var setTestInput = [1,2,3];\r
+client.testSet(setTestInput)\r
+ .then(function(response) {\r
+ assert.deepEqual(setTestInput, response);\r
+ })\r
+ .fail(function() {\r
+ assert(false);\r
+ });\r
+client.testList(setTestInput)\r
+ .then(function(response) {\r
+ assert.deepEqual(setTestInput, response);\r
+ })\r
+ .fail(function() {\r
+ assert(false);\r
+ });\r
+\r
+client.testEnum(ttypes.Numberz.ONE)\r
+ .then(function(response) {\r
+ assert.equal(ttypes.Numberz.ONE, response);\r
+ })\r
+ .fail(function() {\r
+ assert(false);\r
+ });\r
+\r
+client.testTypedef(69)\r
+ .then(function(response) {\r
+ assert.equal(69, response);\r
+ })\r
+ .fail(function() {\r
+ assert(false);\r
+ });\r
+\r
+var mapMapTest = {\r
+ "4": {"1":1, "2":2, "3":3, "4":4},\r
+ "-4": {"-4":-4, "-3":-3, "-2":-2, "-1":-1}\r
+};\r
+client.testMapMap(mapMapTest)\r
+ .then(function(response) {\r
+ assert.deepEqual(mapMapTest, response);\r
+ })\r
+ .fail(function() {\r
+ assert(false);\r
+ });\r
+\r
+var crazy = new ttypes.Insanity({\r
+ "userMap":{ "5":5, "8":8 },\r
+ "xtructs":[new ttypes.Xtruct({\r
+ "string_thing":"Goodbye4",\r
+ "byte_thing":4,\r
+ "i32_thing":4,\r
+ "i64_thing":4\r
+ }), new ttypes.Xtruct({\r
+ "string_thing":"Hello2",\r
+ "byte_thing":2,\r
+ "i32_thing":2,\r
+ "i64_thing":2\r
+ })]\r
+});\r
+\r
+var insanity = {\r
+ "1":{ "2": crazy, "3": crazy },\r
+ "2":{ "6":{ "userMap":null, "xtructs":null } }\r
+};\r
+client.testInsanity(crazy)\r
+ .then(function(response) {\r
+ checkRecursively(insanity, response);\r
+ })\r
+ .fail(function() {\r
+ assert(false);\r
+ });\r
+\r
+client.testException('TException')\r
+ .then(function() {\r
+ assert(false);\r
+ });\r
+\r
+client.testException('Xception')\r
+ .then(function(response) {\r
+ assert.equal(err.errorCode, 1001);\r
+ assert.equal('Xception', err.message)\r
+ })\r
+ .fail(function() {\r
+ assert(false);\r
+ });\r
+\r
+client.testException('no Exception')\r
+ .then(function(response) {\r
+ assert.equal(undefined, response); //void\r
+ })\r
+ .fail(function() {\r
+ assert(false);\r
+ });\r
+\r
+client.testOneway(0, function(error, response) {\r
+ assert(false); //should not answer\r
+});\r
+\r
+(function() {\r
+ var test_complete = false;\r
+ var retrys = 0;\r
+ var retry_limit = 30;\r
+ var retry_interval = 100;\r
+ /**\r
+ * redo a simple test after the oneway to make sure we aren't "off by one" --\r
+ * if the server treated oneway void like normal void, this next test will\r
+ * fail since it will get the void confirmation rather than the correct\r
+ * result. In this circumstance, the client will throw the exception:\r
+ *\r
+ * Because this is the last test against the server, when it completes\r
+ * the entire suite is complete by definition (the tests run serially).\r
+ */\r
+ client.testI32(-1)\r
+ .then(function(response) {\r
+ assert.equal(-1, response);\r
+ test_complete = true\r
+ })\r
+ .fail(function() {\r
+ assert(false);\r
+ });\r
+\r
+//We wait up to retry_limit * retry_interval for the test suite to complete\r
+ function TestForCompletion() {\r
+ if(test_complete) {\r
+ if (callback) {\r
+ callback("Server successfully tested!");\r
+ }\r
+ } else {\r
+ if (++retrys < retry_limit) {\r
+ setTimeout(TestForCompletion, retry_interval);\r
+ } else {\r
+ if (callback) {\r
+ callback("Server test failed to complete after " +\r
+ (retry_limit*retry_interval/1000) + " seconds");\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ setTimeout(TestForCompletion, retry_interval);\r
+})();\r
+}\r
tutorialclient: all
NODE_PATH="$(top_builddir)/lib/nodejs:$(top_builddir)/lib/nodejs/lib:$(NODEPATH)" $(NODEJS) NodeClient.js
+tutorialserver_promise: all
+ NODE_PATH="$(top_builddir)/lib/nodejs:$(top_builddir)/lib/nodejs/lib:$(NODEPATH)" $(NODEJS) NodeServerPromise.js
+
+tutorialclient_promise: all
+ NODE_PATH="$(top_builddir)/lib/nodejs:$(top_builddir)/lib/nodejs/lib:$(NODEPATH)" $(NODEJS) NodeClientPromise.js
+
+
clean-local:
$(RM) -r gen-*
EXTRA_DIST = \
NodeServer.js \
- NodeClient.js
+ NodeClient.js \
+ NodeServerPromise.js \
+ NodeClientPromise.js
work.num2 = 10;
client.calculate(1, work, function(err, message) {
- console.log('15-10=' + message.value);
+ console.log('15-10=' + message);
client.getStruct(1, function(err, message){
console.log('Check log: ' + message.value);
--- /dev/null
+/*
+ * 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()
+ .then(function() {
+ console.log('ping()');
+ });
+
+client.add(1,1)
+ .then(function(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)
+ .then(function(message) {
+ console.log('Whoa? You know how to divide by zero?');
+ })
+ .fail(function(err) {
+ console.log("InvalidOperation " + err);
+ });
+
+
+work.op = ttypes.Operation.SUBTRACT;
+work.num1 = 15;
+work.num2 = 10;
+
+client.calculate(1, work)
+ .then(function(value) {
+ console.log('15-10=' + value);
+ return client.getStruct(1);
+ })
+ .then(function(message) {
+ console.log('Check log: ' + message.value);
+ })
+ .fin(function() {
+ //close the connection once we're done
+ connection.end();
+ });
--- /dev/null
+/*
+ * 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() {
+ console.log("ping()");
+ },
+
+ add: function(n1, n2) {
+ console.log("add(", n1, ",", n2, ")");
+ return n1 + n2;
+ },
+
+ calculate: function(logid, work) {
+ 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';
+ throw x;
+ }
+ val = work.num1 / work.num2;
+ } else {
+ var x = new ttypes.InvalidOperation();
+ x.what = work.op;
+ x.why = 'Invalid operation';
+ throw x;
+ }
+
+ var entry = new SharedStruct();
+ entry.key = logid;
+ entry.value = ""+val;
+ data[logid] = entry;
+ return val;
+ },
+
+ getStruct: function(key) {
+ console.log("getStruct(", key, ")");
+ return data[key];
+ },
+
+ zip: function() {
+ console.log("zip()");
+ }
+
+});
+
+server.listen(9090);