Upload
ady-liu
View
5.850
Download
2
Embed Size (px)
DESCRIPTION
A small guide for ICE (The Internet Communications Engine).The demo project at github(Java): https://github.com/adyliu/sce-demo
Citation preview
ICE MINI GUIDE
Getting start
Ady@SOHU 2012/02/28
What is ICE?
The Internet Communications Engine
An object-oriented distributed middleware platform.
ICE Content
object-oriented RPC mechanism
language-neutral specification language (Slice)
language mappings for various languages
support for different transports (TCP, SSL, UDP) with highly-efficient protocol
external services (server activation, firewall traversal, etc.)
integrated persistence (Freeze)
threading support
Client and Server Structure
Server ApplicationClient Application
Generated Code
Proxy
Code
Network
Ice API
Client Ice Core
Object
AdapterIce API
Server Ice Core
Skeleton
Ice API
What is slice?
language-independent types
a compiler creates language-specific source
code
only type definitions without statements
define operations and exchange type defined
Java Programming Model
Basic Slice Types
Type Range of Mapped Type Size of Mapped Type
bool false or true ≥ 1 bits
byte -128-127 or 0-255 ≥ 8 bits
short -215 to 215-1 ≥ 16 bits
int -231 to 231-1 ≥ 32 bits
long -263 to 263-1 ≥ 64 bits
float IEEE single-precision ≥ 32 bits
double IEEE double-precision ≥ 64 bits
string Unicode (UTF-8 for java) Variable-length
Enumerations
enum CounterType {
BLOG,
ALBUM,
FEED,
GUEST
};
Rules
• Like Java Enum
• Cannot specify the value
• Empty enumerations are illegal
Structures
struct TimeOfDay {
short hour; // 0-23
short minute; // 0-59
short second; // 0-59
};
Sequences
sequences<AccountIndex> AccountIndexList;
Default Type(Arrays): AccountIndex[]
Define other types (ArrayList/LinkedList)
["java:type:java.util.ArrayList"]
sequences<AccountIndex> AccountIndexList;
Dictionaries
dictionary<string,string> AccountCacheMap;
dictionary<string,AccountCacheMap> AccountCacheMapMap;
Default type: java.util.HashMap
The key rule:
An integral type (bool, byte, short, int, long, enum) or string
A structure containing only members of integral type or type string
Interfaces
dictionary<string,string> AccountCacheMap;
dictionary<string,AccountCacheMap> AccountCacheMapMap;
["java:type:java.util.ArrayList"] sequence<string> StringList;
interface AccountCacheService {
AccountCacheMap get(string passport);
AccountCacheMapMap gets(StringList passports);
};
Interfaces Operations And Parameters
rules
an operation name
a return type (or void if none)
zero or more parameters
an optional exception specification
operation cannot be overloaded
A SIMPLE DEMO
Step 1: write a slice file
module sce{
module demo{
interface CounterService{
int incr();
int decr();
int get();
void set(int num);
};
};
};
Step 2: slice2java
slice2java demo.ice
C:\Users\xylz\workspace\sce-demo>ll sce-demo
-rw-rw-rw- 1 user group 1089 Feb 28 16:34 Callback_CounterService_decr.java
-rw-rw-rw- 1 user group 1086 Feb 28 16:34 Callback_CounterService_get.java
-rw-rw-rw- 1 user group 1089 Feb 28 16:34 Callback_CounterService_incr.java
-rw-rw-rw- 1 user group 639 Feb 28 16:34 Callback_CounterService_set.java
-rw-rw-rw- 1 user group 697 Feb 28 16:34 CounterService.java
-rw-rw-rw- 1 user group 1149 Feb 28 16:34 CounterServiceHolder.java
-rw-rw-rw- 1 user group 3112 Feb 28 16:34 CounterServicePrx.java
-rw-rw-rw- 1 user group 17744 Feb 28 16:34 CounterServicePrxHelper.java
-rw-rw-rw- 1 user group 808 Feb 28 16:34 CounterServicePrxHolder.java
-rw-rw-rw- 1 user group 1034 Feb 28 16:34 _CounterServiceDel.java
-rw-rw-rw- 1 user group 7998 Feb 28 16:34 _CounterServiceDelD.java
-rw-rw-rw- 1 user group 5642 Feb 28 16:34 _CounterServiceDelM.java
-rw-rw-rw- 1 user group 6210 Feb 28 16:34 _CounterServiceDisp.java
-rw-rw-rw- 1 user group 769 Feb 28 16:34 _CounterServiceOperations.java
-rw-rw-rw- 1 user group 687 Feb 28 16:34 _CounterServiceOperationsNC.java
Step 3: write a server
package sce.demo.server;
import java.util.concurrent.atomic.AtomicInteger;
import Ice.Current;
import sce.demo._CounterServiceDisp;
public class CounterServiceI extends _CounterServiceDisp {
final AtomicInteger counter = new AtomicInteger(0);
public int incr(Current __current) { return counter.incrementAndGet(); }
public int decr(Current __current) {return counter.decrementAndGet();}
public int get(Current __current) {return counter.get();}
public void set(int num, Current __current) {counter.set(num); }
}
Step 4: start the server package sce.demo.server;
public class Server {
public static void main(String[] args) throws Exception {
Ice.Communicator ic = null;
try {
ic = Ice.Util.initialize(args);
Ice.ObjectAdapter adapter = ic.createObjectAdapterWithEndpoints("Demo", "default -p 2012");
adapter.add(new CounterServiceI(), ic.stringToIdentity("Counter"));
adapter.activate();
ic.waitForShutdown();
} finally {
if (ic != null) ic.destroy();
}
}
}
Step 5: write a client package sce.demo.client;
import sce.demo.CounterServicePrx;
import sce.demo.CounterServicePrxHelper;
public class Client {
public static void main(String[] args) {
Ice.Communicator ic = null;
try {
ic = Ice.Util.initialize(args);
Ice.ObjectPrx base = ic.stringToProxy("Counter:default -p 2012");
CounterServicePrx counter = CounterServicePrxHelper.checkedCast(base);
//
final int initValue = 100;
counter.set(initValue);
System.out.println((initValue+1)+" => "+counter.incr());
System.out.println((initValue+1)+" => "+counter.get());
System.out.println((initValue)+" => "+counter.decr());
} finally {
if (ic != null) ic.destroy();
}
}
}
101 => 101
101 => 101
100 => 100
Common Steps
1. write the slice files
2. convert slice to java source files (slice2java)
3. write the server (active the object)
4. write the client
Source Code
git
git clone [email protected]:adyliu/sce-demo.git
web
https://github.com/adyliu/sce-demo
Multi-language supported
Next
SCE full samples with Protocol Buffers
ICE more advanced topic