📜 ⬆️ ⬇️

Ice. First piece of ice

With this post, I begin a series of articles about the wonderful technology of Ice (Internet Communications Engine) , which I happened to use when writing research work at the university. In this article, I plan to do a brief overview of Ice technology, and show a small practical example. Immediately I would like to note that the beauty of Ice lies precisely in the simplicity of its use in practice.



Overview


')
Ice (The Internet Communications Engine) is an object-oriented middle layer platform (middleware platform) developed by ZeroC. Ice is equipped with tools, APIs and libraries for developing object-oriented client – ​​server applications. Ice applications can be written in various programming languages ​​(Java, C ++, Python, C #, Ruby), run under various operating systems (Windows NT, Linux, Mac OS) and hardware platforms, and can also communicate using various network technologies. In general, Ice is positioned as a tool RPC (Remote Procedure Call), which is quite transparent to use in practice. A large number of companies around the world, such as Skype, HP, Silicon Graphics, use Ice technology in their projects.

Slice - Specifications Language for Ice



Each Ice object has an interface with a finite set of operations. The interfaces, operations, and data types exchanged between the server and the client should be described in Slice. Slice allows you to describe the behavior of the server and the client, without relying on any programming language, thanks to this, the written code on slice is compiled into the code of any of the programming languages ​​supported by the platform.

List of supported languages


Ice supports the following client-server languages: C ++, Java, C #, Python, Objective-C, as well as Ruby and PHP in client mode.

Ice generates the slice2xxx compiler, where xxx = java | cpp | py ...

Practice



Let's write a primitive client-server application in which the server prints the message “Hello World!” To the standard output on the client's request. We will choose Java as the implementation language, although you could use any other as well. It is important to understand that Ice allows us to write the server part, say, Java, and the client part in Python, and for us it will be completely transparent. Ice encapsulates the entire process of marshaling / demarshaling and calling RPC from a programmer.
Well, let's get started. Write a slice module Hello.ice that describes the server behavior:
module Demo { interface Hello { void sayHello(); }; }; * This source code was highlighted with Source Code Highlighter .
  1. module Demo { interface Hello { void sayHello(); }; }; * This source code was highlighted with Source Code Highlighter .
  2. module Demo { interface Hello { void sayHello(); }; }; * This source code was highlighted with Source Code Highlighter .
  3. module Demo { interface Hello { void sayHello(); }; }; * This source code was highlighted with Source Code Highlighter .
  4. module Demo { interface Hello { void sayHello(); }; }; * This source code was highlighted with Source Code Highlighter .
  5. module Demo { interface Hello { void sayHello(); }; }; * This source code was highlighted with Source Code Highlighter .
  6. module Demo { interface Hello { void sayHello(); }; }; * This source code was highlighted with Source Code Highlighter .
  7. module Demo { interface Hello { void sayHello(); }; }; * This source code was highlighted with Source Code Highlighter .
module Demo { interface Hello { void sayHello(); }; }; * This source code was highlighted with Source Code Highlighter .
module Demo { interface Hello { void sayHello(); }; }; * This source code was highlighted with Source Code Highlighter .


This module says that the server is an interface with one single method seyHello (), and the method without parameters and without a return result (although it is possible and this, but this is already beyond the scope of this article).

Now we will generate the corresponding java-code for this slice, which will perform the entire routine for us. To do this, run in the console - sice2java Hello.ice.

The generated code is placed in the directory with the project, and we write the server code:

import Demo.*; public class Server extends Ice.Application { public int run( String [] args) { Ice.ObjectAdapter adapter = communicator().createObjectAdapterWithEndpors( "Hello" , "tcp -p 10000" ); adapter.add( new HelloI(), communicator().stringToIdentity( "hello" )); adapter.activate(); communicator().waitForShutdown(); return 0; } public static void main( String [] args) { Server app = new Server(); int status = app.main( "Server" , args); System.exit(status); } } * This source code was highlighted with Source Code Highlighter .
  1. import Demo.*; public class Server extends Ice.Application { public int run( String [] args) { Ice.ObjectAdapter adapter = communicator().createObjectAdapterWithEndpors( "Hello" , "tcp -p 10000" ); adapter.add( new HelloI(), communicator().stringToIdentity( "hello" )); adapter.activate(); communicator().waitForShutdown(); return 0; } public static void main( String [] args) { Server app = new Server(); int status = app.main( "Server" , args); System.exit(status); } } * This source code was highlighted with Source Code Highlighter .
  2. import Demo.*; public class Server extends Ice.Application { public int run( String [] args) { Ice.ObjectAdapter adapter = communicator().createObjectAdapterWithEndpors( "Hello" , "tcp -p 10000" ); adapter.add( new HelloI(), communicator().stringToIdentity( "hello" )); adapter.activate(); communicator().waitForShutdown(); return 0; } public static void main( String [] args) { Server app = new Server(); int status = app.main( "Server" , args); System.exit(status); } } * This source code was highlighted with Source Code Highlighter .
  3. import Demo.*; public class Server extends Ice.Application { public int run( String [] args) { Ice.ObjectAdapter adapter = communicator().createObjectAdapterWithEndpors( "Hello" , "tcp -p 10000" ); adapter.add( new HelloI(), communicator().stringToIdentity( "hello" )); adapter.activate(); communicator().waitForShutdown(); return 0; } public static void main( String [] args) { Server app = new Server(); int status = app.main( "Server" , args); System.exit(status); } } * This source code was highlighted with Source Code Highlighter .
  4. import Demo.*; public class Server extends Ice.Application { public int run( String [] args) { Ice.ObjectAdapter adapter = communicator().createObjectAdapterWithEndpors( "Hello" , "tcp -p 10000" ); adapter.add( new HelloI(), communicator().stringToIdentity( "hello" )); adapter.activate(); communicator().waitForShutdown(); return 0; } public static void main( String [] args) { Server app = new Server(); int status = app.main( "Server" , args); System.exit(status); } } * This source code was highlighted with Source Code Highlighter .
  5. import Demo.*; public class Server extends Ice.Application { public int run( String [] args) { Ice.ObjectAdapter adapter = communicator().createObjectAdapterWithEndpors( "Hello" , "tcp -p 10000" ); adapter.add( new HelloI(), communicator().stringToIdentity( "hello" )); adapter.activate(); communicator().waitForShutdown(); return 0; } public static void main( String [] args) { Server app = new Server(); int status = app.main( "Server" , args); System.exit(status); } } * This source code was highlighted with Source Code Highlighter .
  6. import Demo.*; public class Server extends Ice.Application { public int run( String [] args) { Ice.ObjectAdapter adapter = communicator().createObjectAdapterWithEndpors( "Hello" , "tcp -p 10000" ); adapter.add( new HelloI(), communicator().stringToIdentity( "hello" )); adapter.activate(); communicator().waitForShutdown(); return 0; } public static void main( String [] args) { Server app = new Server(); int status = app.main( "Server" , args); System.exit(status); } } * This source code was highlighted with Source Code Highlighter .
  7. import Demo.*; public class Server extends Ice.Application { public int run( String [] args) { Ice.ObjectAdapter adapter = communicator().createObjectAdapterWithEndpors( "Hello" , "tcp -p 10000" ); adapter.add( new HelloI(), communicator().stringToIdentity( "hello" )); adapter.activate(); communicator().waitForShutdown(); return 0; } public static void main( String [] args) { Server app = new Server(); int status = app.main( "Server" , args); System.exit(status); } } * This source code was highlighted with Source Code Highlighter .
  8. import Demo.*; public class Server extends Ice.Application { public int run( String [] args) { Ice.ObjectAdapter adapter = communicator().createObjectAdapterWithEndpors( "Hello" , "tcp -p 10000" ); adapter.add( new HelloI(), communicator().stringToIdentity( "hello" )); adapter.activate(); communicator().waitForShutdown(); return 0; } public static void main( String [] args) { Server app = new Server(); int status = app.main( "Server" , args); System.exit(status); } } * This source code was highlighted with Source Code Highlighter .
  9. import Demo.*; public class Server extends Ice.Application { public int run( String [] args) { Ice.ObjectAdapter adapter = communicator().createObjectAdapterWithEndpors( "Hello" , "tcp -p 10000" ); adapter.add( new HelloI(), communicator().stringToIdentity( "hello" )); adapter.activate(); communicator().waitForShutdown(); return 0; } public static void main( String [] args) { Server app = new Server(); int status = app.main( "Server" , args); System.exit(status); } } * This source code was highlighted with Source Code Highlighter .
  10. import Demo.*; public class Server extends Ice.Application { public int run( String [] args) { Ice.ObjectAdapter adapter = communicator().createObjectAdapterWithEndpors( "Hello" , "tcp -p 10000" ); adapter.add( new HelloI(), communicator().stringToIdentity( "hello" )); adapter.activate(); communicator().waitForShutdown(); return 0; } public static void main( String [] args) { Server app = new Server(); int status = app.main( "Server" , args); System.exit(status); } } * This source code was highlighted with Source Code Highlighter .
  11. import Demo.*; public class Server extends Ice.Application { public int run( String [] args) { Ice.ObjectAdapter adapter = communicator().createObjectAdapterWithEndpors( "Hello" , "tcp -p 10000" ); adapter.add( new HelloI(), communicator().stringToIdentity( "hello" )); adapter.activate(); communicator().waitForShutdown(); return 0; } public static void main( String [] args) { Server app = new Server(); int status = app.main( "Server" , args); System.exit(status); } } * This source code was highlighted with Source Code Highlighter .
  12. import Demo.*; public class Server extends Ice.Application { public int run( String [] args) { Ice.ObjectAdapter adapter = communicator().createObjectAdapterWithEndpors( "Hello" , "tcp -p 10000" ); adapter.add( new HelloI(), communicator().stringToIdentity( "hello" )); adapter.activate(); communicator().waitForShutdown(); return 0; } public static void main( String [] args) { Server app = new Server(); int status = app.main( "Server" , args); System.exit(status); } } * This source code was highlighted with Source Code Highlighter .
  13. import Demo.*; public class Server extends Ice.Application { public int run( String [] args) { Ice.ObjectAdapter adapter = communicator().createObjectAdapterWithEndpors( "Hello" , "tcp -p 10000" ); adapter.add( new HelloI(), communicator().stringToIdentity( "hello" )); adapter.activate(); communicator().waitForShutdown(); return 0; } public static void main( String [] args) { Server app = new Server(); int status = app.main( "Server" , args); System.exit(status); } } * This source code was highlighted with Source Code Highlighter .
  14. import Demo.*; public class Server extends Ice.Application { public int run( String [] args) { Ice.ObjectAdapter adapter = communicator().createObjectAdapterWithEndpors( "Hello" , "tcp -p 10000" ); adapter.add( new HelloI(), communicator().stringToIdentity( "hello" )); adapter.activate(); communicator().waitForShutdown(); return 0; } public static void main( String [] args) { Server app = new Server(); int status = app.main( "Server" , args); System.exit(status); } } * This source code was highlighted with Source Code Highlighter .
  15. import Demo.*; public class Server extends Ice.Application { public int run( String [] args) { Ice.ObjectAdapter adapter = communicator().createObjectAdapterWithEndpors( "Hello" , "tcp -p 10000" ); adapter.add( new HelloI(), communicator().stringToIdentity( "hello" )); adapter.activate(); communicator().waitForShutdown(); return 0; } public static void main( String [] args) { Server app = new Server(); int status = app.main( "Server" , args); System.exit(status); } } * This source code was highlighted with Source Code Highlighter .
  16. import Demo.*; public class Server extends Ice.Application { public int run( String [] args) { Ice.ObjectAdapter adapter = communicator().createObjectAdapterWithEndpors( "Hello" , "tcp -p 10000" ); adapter.add( new HelloI(), communicator().stringToIdentity( "hello" )); adapter.activate(); communicator().waitForShutdown(); return 0; } public static void main( String [] args) { Server app = new Server(); int status = app.main( "Server" , args); System.exit(status); } } * This source code was highlighted with Source Code Highlighter .
  17. import Demo.*; public class Server extends Ice.Application { public int run( String [] args) { Ice.ObjectAdapter adapter = communicator().createObjectAdapterWithEndpors( "Hello" , "tcp -p 10000" ); adapter.add( new HelloI(), communicator().stringToIdentity( "hello" )); adapter.activate(); communicator().waitForShutdown(); return 0; } public static void main( String [] args) { Server app = new Server(); int status = app.main( "Server" , args); System.exit(status); } } * This source code was highlighted with Source Code Highlighter .
  18. import Demo.*; public class Server extends Ice.Application { public int run( String [] args) { Ice.ObjectAdapter adapter = communicator().createObjectAdapterWithEndpors( "Hello" , "tcp -p 10000" ); adapter.add( new HelloI(), communicator().stringToIdentity( "hello" )); adapter.activate(); communicator().waitForShutdown(); return 0; } public static void main( String [] args) { Server app = new Server(); int status = app.main( "Server" , args); System.exit(status); } } * This source code was highlighted with Source Code Highlighter .
  19. import Demo.*; public class Server extends Ice.Application { public int run( String [] args) { Ice.ObjectAdapter adapter = communicator().createObjectAdapterWithEndpors( "Hello" , "tcp -p 10000" ); adapter.add( new HelloI(), communicator().stringToIdentity( "hello" )); adapter.activate(); communicator().waitForShutdown(); return 0; } public static void main( String [] args) { Server app = new Server(); int status = app.main( "Server" , args); System.exit(status); } } * This source code was highlighted with Source Code Highlighter .
  20. import Demo.*; public class Server extends Ice.Application { public int run( String [] args) { Ice.ObjectAdapter adapter = communicator().createObjectAdapterWithEndpors( "Hello" , "tcp -p 10000" ); adapter.add( new HelloI(), communicator().stringToIdentity( "hello" )); adapter.activate(); communicator().waitForShutdown(); return 0; } public static void main( String [] args) { Server app = new Server(); int status = app.main( "Server" , args); System.exit(status); } } * This source code was highlighted with Source Code Highlighter .
import Demo.*; public class Server extends Ice.Application { public int run( String [] args) { Ice.ObjectAdapter adapter = communicator().createObjectAdapterWithEndpors( "Hello" , "tcp -p 10000" ); adapter.add( new HelloI(), communicator().stringToIdentity( "hello" )); adapter.activate(); communicator().waitForShutdown(); return 0; } public static void main( String [] args) { Server app = new Server(); int status = app.main( "Server" , args); System.exit(status); } } * This source code was highlighted with Source Code Highlighter .
import Demo.*; public class Server extends Ice.Application { public int run( String [] args) { Ice.ObjectAdapter adapter = communicator().createObjectAdapterWithEndpors( "Hello" , "tcp -p 10000" ); adapter.add( new HelloI(), communicator().stringToIdentity( "hello" )); adapter.activate(); communicator().waitForShutdown(); return 0; } public static void main( String [] args) { Server app = new Server(); int status = app.main( "Server" , args); System.exit(status); } } * This source code was highlighted with Source Code Highlighter .


With this code, an adapter is created, on which the ice object described in Hello.ice is hung. The line communicator (). WaitForShutdown () - provides the hang of the server thread, in order to serve requests.

Now we will redefine the interface described in Hello.ice. To do this, we write the following code:
import Demo.*; public class HelloI extends _HelloDisp { public void sayHello(Ice.Current current) { System. out .println( "Hello World!" ); } } * This source code was highlighted with Source Code Highlighter .
  1. import Demo.*; public class HelloI extends _HelloDisp { public void sayHello(Ice.Current current) { System. out .println( "Hello World!" ); } } * This source code was highlighted with Source Code Highlighter .
  2. import Demo.*; public class HelloI extends _HelloDisp { public void sayHello(Ice.Current current) { System. out .println( "Hello World!" ); } } * This source code was highlighted with Source Code Highlighter .
  3. import Demo.*; public class HelloI extends _HelloDisp { public void sayHello(Ice.Current current) { System. out .println( "Hello World!" ); } } * This source code was highlighted with Source Code Highlighter .
  4. import Demo.*; public class HelloI extends _HelloDisp { public void sayHello(Ice.Current current) { System. out .println( "Hello World!" ); } } * This source code was highlighted with Source Code Highlighter .
  5. import Demo.*; public class HelloI extends _HelloDisp { public void sayHello(Ice.Current current) { System. out .println( "Hello World!" ); } } * This source code was highlighted with Source Code Highlighter .
  6. import Demo.*; public class HelloI extends _HelloDisp { public void sayHello(Ice.Current current) { System. out .println( "Hello World!" ); } } * This source code was highlighted with Source Code Highlighter .
  7. import Demo.*; public class HelloI extends _HelloDisp { public void sayHello(Ice.Current current) { System. out .println( "Hello World!" ); } } * This source code was highlighted with Source Code Highlighter .
  8. import Demo.*; public class HelloI extends _HelloDisp { public void sayHello(Ice.Current current) { System. out .println( "Hello World!" ); } } * This source code was highlighted with Source Code Highlighter .
  9. import Demo.*; public class HelloI extends _HelloDisp { public void sayHello(Ice.Current current) { System. out .println( "Hello World!" ); } } * This source code was highlighted with Source Code Highlighter .
import Demo.*; public class HelloI extends _HelloDisp { public void sayHello(Ice.Current current) { System. out .println( "Hello World!" ); } } * This source code was highlighted with Source Code Highlighter .
import Demo.*; public class HelloI extends _HelloDisp { public void sayHello(Ice.Current current) { System. out .println( "Hello World!" ); } } * This source code was highlighted with Source Code Highlighter .


The implementation of the server part ends there. We proceed to the client. It also requires the generated slice2java files from Hello.ice. We write client code:

public class Client extends Ice.Application { public int run( String args[]) { HelloPrx hello = HelloPrxHelper.uncheckedCast(communicator.stringToProxy( "hello:tcp -h 127.0.0.1 -p 10000" )); hello.sayHello(); } public static void main( String args[]) { Client client = new Client(); int status = client.main( "Client" , args); System.exit(status); } } * This source code was highlighted with Source Code Highlighter .
  1. public class Client extends Ice.Application { public int run( String args[]) { HelloPrx hello = HelloPrxHelper.uncheckedCast(communicator.stringToProxy( "hello:tcp -h 127.0.0.1 -p 10000" )); hello.sayHello(); } public static void main( String args[]) { Client client = new Client(); int status = client.main( "Client" , args); System.exit(status); } } * This source code was highlighted with Source Code Highlighter .
  2. public class Client extends Ice.Application { public int run( String args[]) { HelloPrx hello = HelloPrxHelper.uncheckedCast(communicator.stringToProxy( "hello:tcp -h 127.0.0.1 -p 10000" )); hello.sayHello(); } public static void main( String args[]) { Client client = new Client(); int status = client.main( "Client" , args); System.exit(status); } } * This source code was highlighted with Source Code Highlighter .
  3. public class Client extends Ice.Application { public int run( String args[]) { HelloPrx hello = HelloPrxHelper.uncheckedCast(communicator.stringToProxy( "hello:tcp -h 127.0.0.1 -p 10000" )); hello.sayHello(); } public static void main( String args[]) { Client client = new Client(); int status = client.main( "Client" , args); System.exit(status); } } * This source code was highlighted with Source Code Highlighter .
  4. public class Client extends Ice.Application { public int run( String args[]) { HelloPrx hello = HelloPrxHelper.uncheckedCast(communicator.stringToProxy( "hello:tcp -h 127.0.0.1 -p 10000" )); hello.sayHello(); } public static void main( String args[]) { Client client = new Client(); int status = client.main( "Client" , args); System.exit(status); } } * This source code was highlighted with Source Code Highlighter .
  5. public class Client extends Ice.Application { public int run( String args[]) { HelloPrx hello = HelloPrxHelper.uncheckedCast(communicator.stringToProxy( "hello:tcp -h 127.0.0.1 -p 10000" )); hello.sayHello(); } public static void main( String args[]) { Client client = new Client(); int status = client.main( "Client" , args); System.exit(status); } } * This source code was highlighted with Source Code Highlighter .
  6. public class Client extends Ice.Application { public int run( String args[]) { HelloPrx hello = HelloPrxHelper.uncheckedCast(communicator.stringToProxy( "hello:tcp -h 127.0.0.1 -p 10000" )); hello.sayHello(); } public static void main( String args[]) { Client client = new Client(); int status = client.main( "Client" , args); System.exit(status); } } * This source code was highlighted with Source Code Highlighter .
  7. public class Client extends Ice.Application { public int run( String args[]) { HelloPrx hello = HelloPrxHelper.uncheckedCast(communicator.stringToProxy( "hello:tcp -h 127.0.0.1 -p 10000" )); hello.sayHello(); } public static void main( String args[]) { Client client = new Client(); int status = client.main( "Client" , args); System.exit(status); } } * This source code was highlighted with Source Code Highlighter .
  8. public class Client extends Ice.Application { public int run( String args[]) { HelloPrx hello = HelloPrxHelper.uncheckedCast(communicator.stringToProxy( "hello:tcp -h 127.0.0.1 -p 10000" )); hello.sayHello(); } public static void main( String args[]) { Client client = new Client(); int status = client.main( "Client" , args); System.exit(status); } } * This source code was highlighted with Source Code Highlighter .
  9. public class Client extends Ice.Application { public int run( String args[]) { HelloPrx hello = HelloPrxHelper.uncheckedCast(communicator.stringToProxy( "hello:tcp -h 127.0.0.1 -p 10000" )); hello.sayHello(); } public static void main( String args[]) { Client client = new Client(); int status = client.main( "Client" , args); System.exit(status); } } * This source code was highlighted with Source Code Highlighter .
  10. public class Client extends Ice.Application { public int run( String args[]) { HelloPrx hello = HelloPrxHelper.uncheckedCast(communicator.stringToProxy( "hello:tcp -h 127.0.0.1 -p 10000" )); hello.sayHello(); } public static void main( String args[]) { Client client = new Client(); int status = client.main( "Client" , args); System.exit(status); } } * This source code was highlighted with Source Code Highlighter .
  11. public class Client extends Ice.Application { public int run( String args[]) { HelloPrx hello = HelloPrxHelper.uncheckedCast(communicator.stringToProxy( "hello:tcp -h 127.0.0.1 -p 10000" )); hello.sayHello(); } public static void main( String args[]) { Client client = new Client(); int status = client.main( "Client" , args); System.exit(status); } } * This source code was highlighted with Source Code Highlighter .
  12. public class Client extends Ice.Application { public int run( String args[]) { HelloPrx hello = HelloPrxHelper.uncheckedCast(communicator.stringToProxy( "hello:tcp -h 127.0.0.1 -p 10000" )); hello.sayHello(); } public static void main( String args[]) { Client client = new Client(); int status = client.main( "Client" , args); System.exit(status); } } * This source code was highlighted with Source Code Highlighter .
public class Client extends Ice.Application { public int run( String args[]) { HelloPrx hello = HelloPrxHelper.uncheckedCast(communicator.stringToProxy( "hello:tcp -h 127.0.0.1 -p 10000" )); hello.sayHello(); } public static void main( String args[]) { Client client = new Client(); int status = client.main( "Client" , args); System.exit(status); } } * This source code was highlighted with Source Code Highlighter .
public class Client extends Ice.Application { public int run( String args[]) { HelloPrx hello = HelloPrxHelper.uncheckedCast(communicator.stringToProxy( "hello:tcp -h 127.0.0.1 -p 10000" )); hello.sayHello(); } public static void main( String args[]) { Client client = new Client(); int status = client.main( "Client" , args); System.exit(status); } } * This source code was highlighted with Source Code Highlighter .


Here we create a proxy to an ice object in a remote address space, however, we work with this proxy as with a real object.

we start the java Server server;
we start the client java Client and we admire result.

It should be noted that replacing the host address from 127.0.0.1 to the real one in the proxy creation code, we get a client-server application.

Ps . In the following articles I will focus on the Ice platform architecture, the marshalling / demarshaling processes, the types of procedure calls (synchronous, asynchronous), the types of call dispatching, as well as the slice language (User data, exceptions, language-dependent constructions). If of course you are interested in habrayuzer)

Source: https://habr.com/ru/post/70111/


All Articles