diff options
Diffstat (limited to 'js/demo/Ice/hello')
-rw-r--r-- | js/demo/Ice/hello/.gitignore | 1 | ||||
-rw-r--r-- | js/demo/Ice/hello/Client.js | 195 | ||||
-rw-r--r-- | js/demo/Ice/hello/Hello.ice | 22 | ||||
-rw-r--r-- | js/demo/Ice/hello/Makefile | 16 | ||||
-rw-r--r-- | js/demo/Ice/hello/Makefile.mak | 16 | ||||
-rw-r--r-- | js/demo/Ice/hello/README | 29 | ||||
-rw-r--r-- | js/demo/Ice/hello/browser/Client.js | 265 | ||||
-rw-r--r-- | js/demo/Ice/hello/build.js | 10 | ||||
-rwxr-xr-x | js/demo/Ice/hello/expect.py | 31 | ||||
-rw-r--r-- | js/demo/Ice/hello/index.html | 195 |
10 files changed, 780 insertions, 0 deletions
diff --git a/js/demo/Ice/hello/.gitignore b/js/demo/Ice/hello/.gitignore new file mode 100644 index 00000000000..48762ead96c --- /dev/null +++ b/js/demo/Ice/hello/.gitignore @@ -0,0 +1 @@ +Hello.js diff --git a/js/demo/Ice/hello/Client.js b/js/demo/Ice/hello/Client.js new file mode 100644 index 00000000000..2d65c23a83e --- /dev/null +++ b/js/demo/Ice/hello/Client.js @@ -0,0 +1,195 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2014 ZeroC, Inc. All rights reserved. +// +// This copy of Ice is licensed to you under the terms described in the +// ICE_LICENSE file included in this distribution. +// +// ********************************************************************** + +(function(){ + +require("Ice"); +require("./Hello"); + +function menu() +{ + process.stdout.write( + "usage:\n" + + "t: send greeting as twoway\n" + + "o: send greeting as oneway\n" + + "O: send greeting as batch oneway\n" + + "f: flush all batch requests\n" + + "T: set a timeout\n" + + "P: set a server delay\n" + + //"S: switch secure mode on/off\n" + + "s: shutdown server\n" + + "x: exit\n" + + "?: help\n" + + "\n"); +} + +var communicator; +Ice.Promise.try( + function() + { + communicator = Ice.initialize(); + var proxy = communicator.stringToProxy("hello:default -p 10000").ice_twoway().ice_timeout(-1).ice_secure(false); + var secure = false; + var timeout = -1; + var delay = 0; + + return Demo.HelloPrx.checkedCast(proxy).then( + function(twoway) + { + var oneway = twoway.ice_oneway(); + var batchOneway = twoway.ice_batchOneway(); + + menu(); + process.stdout.write("==> "); + var loop = new Ice.Promise(); + function processKey(key) + { + if(key == "x") + { + loop.succeed(); + return; + } + + if(key == "t") + { + return twoway.sayHello(delay); + } + else if(key == "o") + { + return oneway.sayHello(delay); + } + else if(key == "O") + { + return batchOneway.sayHello(delay); + } + else if(key == "f") + { + return communicator.flushBatchRequests(); + } + else if(key == "T") + { + if(timeout == -1) + { + timeout = 2000; + } + else + { + timeout = -1; + } + + twoway = twoway.ice_timeout(timeout); + oneway = oneway.ice_timeout(timeout); + batchOneway = batchOneway.ice_timeout(timeout); + + if(timeout == -1) + { + console.log("timeout is now switched off"); + } + else + { + console.log("timeout is now set to 2000ms"); + } + } + else if(key == "P") + { + if(delay === 0) + { + delay = 2500; + } + else + { + delay = 0; + } + + if(delay === 0) + { + console.log("server delay is now deactivated"); + } + else + { + console.log("server delay is now set to 2500ms"); + } + } + else if(key == "s") + { + return twoway.shutdown(); + } + else if(key == "?") + { + process.stdout.write("\n"); + menu(); + } + else + { + console.log("unknown command `" + key + "'"); + process.stdout.write("\n"); + menu(); + } + } + + // + // Process keys sequentially. We chain the promise objects + // returned by processKey(). Once we have process all the + // keys we print the prompt and resume the standard input. + // + process.stdin.resume(); + var promise = new Ice.Promise().succeed(); + process.stdin.on("data", + function(buffer) + { + process.stdin.pause(); + var data = buffer.toString("utf-8").trim().split(""); + // Process each key + data.forEach(function(key) + { + promise = promise.then( + function() + { + return processKey(key); + } + ).exception( + function(ex) + { + console.log(ex.toString()); + }); + }); + // Once we're done, print the prompt + promise.then(function() + { + if(!loop.completed()) + { + process.stdout.write("==> "); + process.stdin.resume(); + } + }); + data = []; + }); + + return loop; + }); + } +).finally( + function() + { + if(communicator) + { + return communicator.destroy(); + } + } +).then( + function() + { + process.exit(0); + }, + function(ex) + { + console.log(ex.toString()); + process.exit(1); + }); +}()); diff --git a/js/demo/Ice/hello/Hello.ice b/js/demo/Ice/hello/Hello.ice new file mode 100644 index 00000000000..28e16b8de3e --- /dev/null +++ b/js/demo/Ice/hello/Hello.ice @@ -0,0 +1,22 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2014 ZeroC, Inc. All rights reserved. +// +// This copy of Ice is licensed to you under the terms described in the +// ICE_LICENSE file included in this distribution. +// +// ********************************************************************** + +#pragma once + +module Demo +{ + +interface Hello +{ + idempotent void sayHello(int delay); + void shutdown(); +}; + +}; + diff --git a/js/demo/Ice/hello/Makefile b/js/demo/Ice/hello/Makefile new file mode 100644 index 00000000000..3d389b91775 --- /dev/null +++ b/js/demo/Ice/hello/Makefile @@ -0,0 +1,16 @@ +# ********************************************************************** +# +# Copyright (c) 2003-2014 ZeroC, Inc. All rights reserved. +# +# This copy of Ice is licensed to you under the terms described in the +# ICE_LICENSE file included in this distribution. +# +# ********************************************************************** + +top_srcdir = ../../.. + +TARGETS = Hello.js + +include $(top_srcdir)/config/Make.rules.js + +SLICE2JSFLAGS := $(SLICE2JSFLAGS) -I$(slicedir) diff --git a/js/demo/Ice/hello/Makefile.mak b/js/demo/Ice/hello/Makefile.mak new file mode 100644 index 00000000000..dcd0cc79cf8 --- /dev/null +++ b/js/demo/Ice/hello/Makefile.mak @@ -0,0 +1,16 @@ +# ********************************************************************** +# +# Copyright (c) 2003-2014 ZeroC, Inc. All rights reserved. +# +# This copy of Ice is licensed to you under the terms described in the +# ICE_LICENSE file included in this distribution. +# +# ********************************************************************** + +top_srcdir = ..\..\.. + +TARGETS = Hello.js + +!include $(top_srcdir)\config\Make.rules.mak.js + +SLICE2JSFLAGS = $(SLICE2JSFLAGS) -I"$(slicedir)" diff --git a/js/demo/Ice/hello/README b/js/demo/Ice/hello/README new file mode 100644 index 00000000000..d49c149416b --- /dev/null +++ b/js/demo/Ice/hello/README @@ -0,0 +1,29 @@ +This demo illustrates how to invoke ordinary (twoway) operations, as +well as how to make oneway and batched invocations. + +To run the demo, first you need to start the Ice hello server. This +distribution includes server implementations in Python and C++, and +each one has its own README file with instructions for starting the +server. Please refer to the Python or C++ README in the appropriate +demo subdirectory for more information. + +Note: + + * To use the Python server you'll need a Python installation that is + compatible with the Ice for Python module included in Ice 3.5.1. + + * To use the C++ server you'll need a C++ compiler compatible with + the Ice 3.5.1 C++ distribution. + +After starting the server, open a separate window and start the +client: + +$ node Client.js + +To test timeouts you can use 'T' to set a timeout on the client proxy +and 'P' to set a delayed response in the server to cause a timeout. +You will notice that two "Hello World!" messages will be printed by +the server in this case. This is because the sayHello method is marked +as idempotent in the slice, meaning that Ice does not need to follow +the at-most-once retry semantics. See the manual for more information +about retry behavior. diff --git a/js/demo/Ice/hello/browser/Client.js b/js/demo/Ice/hello/browser/Client.js new file mode 100644 index 00000000000..23a720cca9e --- /dev/null +++ b/js/demo/Ice/hello/browser/Client.js @@ -0,0 +1,265 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2014 ZeroC, Inc. All rights reserved. +// +// This copy of Ice is licensed to you under the terms described in the +// ICE_LICENSE file included in this distribution. +// +// ********************************************************************** + +(function(){ + +var communicator = Ice.initialize(); + +var flushEnabled = false; +var batch = 0; + +// +// Create the hello proxy. +// +function createProxy() +{ + var hostname = document.location.hostname || "127.0.0.1"; + var proxy = communicator.stringToProxy("hello" + + ":ws -h " + hostname + " -p 8080 -r /demows" + + ":wss -h " + hostname + " -p 9090 -r /demowss"); + + // + // Set or clear the timeout. + // + var timeout = $("#timeout").val(); + proxy = proxy.ice_timeout(timeout > 0 ? timeout : -1); + + // + // Set the mode and protocol + // + var mode = $("#mode").val(); + if(mode == "twoway") + { + proxy = proxy.ice_twoway(); + } + else if(mode == "twoway-secure") + { + proxy = proxy.ice_twoway().ice_secure(true); + } + else if(mode == "oneway") + { + proxy = proxy.ice_oneway(); + } + else if(mode == "oneway-secure") + { + proxy = proxy.ice_oneway().ice_secure(true); + } + else if(mode == "oneway-batch") + { + proxy = proxy.ice_batchOneway(); + } + else if(mode == "oneway-batch-secure") + { + proxy = proxy.ice_batchOneway().ice_secure(true); + } + return Demo.HelloPrx.uncheckedCast(proxy); +} + +// +// Invoke sayHello. +// +function sayHello() +{ + setState(State.SendRequest); + + var proxy = createProxy(); + if(proxy.ice_isBatchOneway()) + { + batch++; + } + + return proxy.sayHello($("#delay").val()); +} + +// +// Flush batch requests. +// +function flush() +{ + batch = 0; + setState(State.FlushBatchRequests); + return communicator.flushBatchRequests(); +} + +// +// Shutdown the server. +// +function shutdown() +{ + setState(State.SendRequest); + + var proxy = createProxy(); + if(proxy.ice_isBatchOneway()) + { + batch++; + } + + return proxy.shutdown(); +} + +// +// Return an event handler suitable for "click" methods. The +// event handler calls the given function, handles exceptions +// and resets the state to Idle when the promise returned by +// the function is fulfilled. +// +var performEventHandler = function(fn) +{ + return function() + { + Ice.Promise.try( + function() + { + return fn.call(); + } + ).exception( + function(ex) + { + $("#output").val(ex.toString()); + } + ).finally( + function() + { + setState(State.Idle); + } + ); + return false; + } +} +var sayHelloClickHandler = performEventHandler(sayHello); +var shutdownClickHandler = performEventHandler(shutdown); +var flushClickHandler = performEventHandler(flush); + +// +// Handle the client state. +// +var State = { + Idle:0, + SendRequest:1, + FlushBatchRequests:2 +}; + +var state; + +function setState(newState, ex) +{ + function assert(v) + { + if(!v) + { + throw new Error("Assertion failed"); + } + } + + assert(state !== newState); + + switch(newState) + { + case State.Idle: + { + assert(state === undefined || state === State.SendRequest || state === State.FlushBatchRequests); + + // + // Hide the progress indicator. + // + $("#progress").hide(); + $("body").removeClass("waiting"); + + // + // Enable buttons. + // + $("#hello").removeClass("disabled").click(sayHelloClickHandler); + $("#shutdown").removeClass("disabled").click(shutdownClickHandler); + if(batch > 0) + { + $("#flush").removeClass("disabled").click(flushClickHandler); + } + break; + } + case State.SendRequest: + case State.FlushBatchRequests: + { + assert(state === State.Idle); + + // + // Reset the output. + // + $("#output").val(""); + + // + // Disable buttons. + // + $("#hello").addClass("disabled").off("click"); + $("#shutdown").addClass("disabled").off("click"); + $("#flush").addClass("disabled").off("click"); + + // + // Display the progress indicator and set the wait cursor. + // + $("#progress .message").text( + newState === State.SendRequest ? "Sending Request..." : "Flush Batch Requests..."); + $("#progress").show(); + $("body").addClass("waiting"); + break; + } + } + state = newState; +}; + +// +// Start in the idle state +// +setState(State.Idle); + +// +// Extract the url GET variables and put them in the _GET object. +// +var _GET = {}; +if(window.location.search.length > 1) +{ + window.location.search.substr(1).split("&").forEach( + function(pair) + { + pair = pair.split("="); + if(pair.length > 0) + { + _GET[decodeURIComponent(pair[0])] = pair.length > 1 ? decodeURIComponent(pair[1]) : ""; + } + }); +} + +// +// If the mode param is set, initialize the mode select box with that value. +// +if(_GET["mode"]) +{ + $("#mode").val(_GET["mode"]); +} + +// +// If the user selects a secure mode, ensure that the page is loaded over HTTPS +// so the web server SSL certificate is obtained. +// +$("#mode").on("change", + function(e) + { + var newMode = $(this).val(); + + if(document.location.protocol === "http:" && + (newMode === "twoway-secure" || newMode === "oneway-secure" || newMode === "oneway-batch-secure")) + { + var href = document.location.protocol + "//" + document.location.host + + document.location.pathname + "?mode=" + newMode; + href = href.replace("http", "https"); + href = href.replace("8080", "9090"); + document.location.assign(href); + } + }); + +}()); diff --git a/js/demo/Ice/hello/build.js b/js/demo/Ice/hello/build.js new file mode 100644 index 00000000000..3694185c575 --- /dev/null +++ b/js/demo/Ice/hello/build.js @@ -0,0 +1,10 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2014 ZeroC, Inc. All rights reserved. +// +// This copy of Ice is licensed to you under the terms described in the +// ICE_LICENSE file included in this distribution. +// +// ********************************************************************** + +var build = require ("../../../config/build").build(__dirname, ["Hello.ice"]); diff --git a/js/demo/Ice/hello/expect.py b/js/demo/Ice/hello/expect.py new file mode 100755 index 00000000000..85ce83f0fe3 --- /dev/null +++ b/js/demo/Ice/hello/expect.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python +# ********************************************************************** +# +# Copyright (c) 2003-2014 ZeroC, Inc. All rights reserved. +# +# This copy of Ice is licensed to you under the terms described in the +# ICE_LICENSE file included in this distribution. +# +# ********************************************************************** + +import sys, os + +path = [ ".", "..", "../..", "../../..", "../../../.." ] +head = os.path.dirname(sys.argv[0]) +if len(head) > 0: + path = [os.path.join(head, p) for p in path] +path = [os.path.abspath(p) for p in path if os.path.exists(os.path.join(p, "demoscript")) ] +if len(path) == 0: + raise RuntimeError("can't find toplevel directory!") +sys.path.append(path[0]) + +from demoscript import Util +from demoscript.Ice import hello + +server = Util.spawn('./server --Ice.PrintAdapterReady --Ice.Warn.Connections=0', Util.getMirrorDir("cpp"), mapping="cpp") +server.expect('.* ready') + +client = Util.spawn('node Client.js --Ice.Warn.Connections=0') +client.expect('.*==>') + +hello.run(client, server, False, False) diff --git a/js/demo/Ice/hello/index.html b/js/demo/Ice/hello/index.html new file mode 100644 index 00000000000..f4131b132cf --- /dev/null +++ b/js/demo/Ice/hello/index.html @@ -0,0 +1,195 @@ +<!doctype html> +<html class="no-js" lang="en"> + <head> + <meta charset="utf-8" /> + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> + <title>Hello Demo | Ice for JavaScript</title> + <!-- Bundle with all the stylesheets used to build the user interface, + see config/Makefile in your distribution for details --> + <link rel="stylesheet" type="text/css" href="../../../assets/common.css" /> + <link rel="icon" type="image/x-icon" href="../../../assets/favicon.ico"> + </head> + <body> + <!-- Header section that contains title and navigation bar --> + <section id="header"> + <nav class="top-bar" data-topbar> + <ul class="title-area"> + <li class="name"> + <h1><a href="../../../index.html">Ice for JavaScript</a></h1> + </li> + <li class="toggle-topbar menu-icon"><a href="#">Menu</a></li> + </ul> + <section class="top-bar-section"> + <!-- Right Nav Section --> + <ul class="right"> + <li class="divider"></li> + <li><a href="#" id="viewReadme">Readme</a></li> + <li><a href="#" id="viewSource">Source</a></li> + </ul> + </section> + </nav> + <ul class="breadcrumbs"> + <li><a href="../../../index.html">Ice</a></li> + <li><a href="../../index.html">Demos</a></li> + <li class="current"><a href="#">Hello</a></li> + </ul> + </section> + + <!-- Main section that contains the user interface --> + <section role="main" id="body"> + <div class="row"> + <div class="large-12 medium-12 columns"> + <form> + <div class="row"> + <div class="small-3 columns"> + <label class="right inline" for="mode">Mode:</label> + </div> + <div class="small-9 columns"> + <select id="mode"> + <option value="twoway">Twoway</option> + <option value="twoway-secure">Twoway Secure</option> + <option value="oneway">Oneway</option> + <option value="oneway-secure">Oneway Secure</option> + <option value="oneway-batch">Oneway Batch</option> + <option value="oneway-batch-secure">Oneway Batch Secure</option> + </select> + </div> + </div> + <div class="row"> + <div class="small-3 columns"> + <label class="right inline">Timeout:</label> + </div> + <div class="small-9 columns"> + <div id="timeout" class="noUiSlider"></div> + </div> + </div> + <br/> + <div class="row"> + <div class="small-3 columns"> + <label class="right inline">Delay:</label> + </div> + <div class="small-9 columns"> + <div id="delay" class="noUiSlider"></div> + </div> + </div> + <div class="row"> + <div class="small-12 columns"> + <a href="#" class="button small" id="hello">Hello World!</a> + <a href="#" class="button small" id="shutdown">Shutdown</a> + <a href="#" class="button disabled small" id="flush">Flush</a> + </div> + </div> + <textarea id="output" class="disabled" readonly></textarea> + <div id="progress" class="row hide"> + <div class="small-12 columns left"> + <div class="inline left icon"></div> + <div class="text">Sending Request...</div> + </div> + </div> + </form> + </div> + </div> + </section> + + <!-- Modal dialog to show the client README --> + <div id="readme-modal" class="reveal-modal medium" data-reveal> + <div class="contents"> + <h4>Hello Demo Readme</h4> + <hr/> + <p>This demo illustrates how to invoke ordinary (twoway) operations, as + well as how to make oneway, secure, and batched invocations.</p> + + <p>To run the demo, first you need to start the Ice hello server. This + distribution includes server implementations in Python and C++, and + each one has its own README file with instructions for starting the + server. Please refer to the Python or C++ README in the appropriate + demo subdirectory for more information. + </p> + + <div class="panel callout radius"> + <ul> + <li>To use the Python server you'll need a Python installation that is + compatible with the Ice for Python module included in Ice 3.5.1.</li> + + <li>To use the C++ server you'll need a C++ compiler compatible with your + Ice for JavaScript distribution.</li> + </ul> + </div> + + <p>You can change the invocation mode using the <strong>"Mode"</strong> + select box; the default is "Twoway" invocation.</p> + + <p>To send an invocation, click the <strong>"Hello World!"</strong> button, + to shutdown the server click the <strong>"Shutdown"</strong> button, + and to flush batch requests click the <strong>"Flush"</strong> button + (this is only enabled when there are pending requests to flush).</p> + + <p>To test timeouts, you can use the timeout slider to set a timeout + on the client proxy and the delay slider to set a delayed response in + the server to cause a timeout.</p> + + <p>You will notice that two "Hello World!" messages will be printed by + the server in this case. This is because the <code>sayHello</code> method is marked + as idempotent in the Slice, meaning that Ice does not need to follow + the at-most-once retry semantics. See the + <a href="http://doc.zeroc.com/display/Ice/Automatic+Retries#AutomaticRetries-AutomaticRetriesforIdempotentOperations">manual</a> + for more information about retry behavior.</p> + </div> + <a class="close-reveal-modal">×</a> + </div> + <!-- Modal dialog to show the client source code --> + <div id="source-modal" class="reveal-modal" data-reveal> + <a class="close-reveal-modal">×</a> + <dl class="tabs" data-tab> + <dt></dt> + <dd class="active"><a href="#panel2-1">Slice</a></dd> + <dd><a href="#panel2-2">JavaScript</a></dd> + <dd><a href="#panel2-3">HTML</a></dd> + </dl> + <div class="tabs-content"> + <div class="content active" id="panel2-1"> + <h6>File: demo/Ice/hello/Hello.ice</h6> + <pre class="source language-c" data-code="Hello.ice"></pre> + </div> + <div class="content" id="panel2-2"> + <h6>File: demo/Ice/hello/browser/Client.js</h6> + <pre class="source" data-code="browser/Client.js"></pre> + </div> + <div class="content" id="panel2-3"> + <h6>File: demo/Ice/hello/index.html</h6> + <pre class="source" data-code="index.html"></pre> + </div> + </div> + </div> + <!-- Footer section --> + <section id="footer"> + <div class="logo"> + <h4><strong>ZeroC</strong></h4> + </div> + <div class="copyright"> + <h6>© 2014 ZeroC, Inc. All rights reserved.</h6> + </div> + </section> + <!-- Bundle with all the scripts used to build the user interface, + see assets/Makefile in your distribution for details --> + <script type="text/javascript" src="../../../assets/common.min.js"></script> + + <script type="text/javascript"> + + </script> + + <!-- Ice.js (Ice run-time library) --> + <script type="text/javascript" src="../../../lib/Ice.js"></script> + <!-- Hello.js (Demo generated code) --> + <script type="text/javascript" src="Hello.js"></script> + <!-- browser/Client.js (Hello Demo Application) --> + <script type="text/javascript" src="browser/Client.js"></script> + + <script type="text/javascript"> + if(["http:", "https:"].indexOf(document.location.protocol) !== -1) + { + checkGenerated(["Hello.js"]); + } + </script> + </body> +</html> |