Getting start

ICE MINI GUIDE

                 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

         Client Application                                   Server Application




 Proxy                                                            Skeleton         Object
                                Ice API             Ice API
 Code                                                                              Adapter


              Client Ice Core                                  Server Ice Core
                                          Network




    Ice API

   Generated Code
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:Usersxylzworkspacesce-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());     101 => 101
          System.out.println((initValue+1)+" => "+counter.get());      101 => 101
          System.out.println((initValue)+" => "+counter.decr());
      } finally {
                                                                       100 => 100
          if (ic != null) ic.destroy();
      }
    }
}
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 git@github.com: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

Ice mini guide

  • 1.
    Getting start ICE MINIGUIDE Ady@SOHU 2012/02/28
  • 2.
    What is ICE?  The Internet Communications Engine  An object-oriented distributed middleware platform.
  • 3.
    ICE Content  object-orientedRPC 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
  • 4.
    Client and ServerStructure Client Application Server Application Proxy Skeleton Object Ice API Ice API Code Adapter Client Ice Core Server Ice Core Network Ice API Generated Code
  • 5.
    What is slice? language-independent types  a compiler creates language-specific source code  only type definitions without statements  define operations and exchange type defined
  • 6.
  • 7.
    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
  • 8.
    Enumerations enum CounterType { BLOG, ALBUM, FEED, GUEST }; Rules • Like Java Enum • Cannot specify the value • Empty enumerations are illegal
  • 9.
    Structures struct TimeOfDay { short hour; // 0-23 short minute; // 0-59 short second; // 0-59 };
  • 10.
    Sequences  sequences<AccountIndex> AccountIndexList;  Default Type(Arrays): AccountIndex[]  Define other types (ArrayList/LinkedList) ["java:type:java.util.ArrayList"] sequences<AccountIndex> AccountIndexList;
  • 11.
    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
  • 12.
    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); };
  • 13.
    Interfaces Operations AndParameters  rules  an operation name  a return type (or void if none)  zero or more parameters  an optional exception specification  operation cannot be overloaded
  • 14.
  • 15.
    Step 1: writea slice file module sce{ module demo{ interface CounterService{ int incr(); int decr(); int get(); void set(int num); }; }; };
  • 16.
    Step 2: slice2java slice2javademo.ice C:Usersxylzworkspacesce-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
  • 17.
    Step 3: writea 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); } }
  • 18.
    Step 4: startthe 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(); } } }
  • 19.
    Step 5: writea 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()); 101 => 101 System.out.println((initValue+1)+" => "+counter.get()); 101 => 101 System.out.println((initValue)+" => "+counter.decr()); } finally { 100 => 100 if (ic != null) ic.destroy(); } } }
  • 20.
    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
  • 21.
    Source Code  git  git clone git@github.com:adyliu/sce-demo.git  web  https://github.com/adyliu/sce-demo
  • 22.
  • 23.
    Next  SCE fullsamples with Protocol Buffers  ICE more advanced topic