Distributed Programming
with Ice
Michi Henning
Mark Spruiell
With contributions by
Dwayne Boone, Brent Eagles, Benoit Foucher,
Marc Laukien, Matthew Newhook, Bernard Normier
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book and ZeroC was aware of the trademark claim,
the designations have been printed in initial caps or all caps.
The authors and publisher have taken care in the preparation of this book, but make no expressed or
implied warranty of any kind and assume no responsibility for errors or omissions. No liability is
assumed for incidental or consequential damages in connection with or arising out of the use of the
information or programs contained herein.
This documentation is licensed under the Creative Commons Attribution-NoDerivs 2.5 License. You
can find a copy of this license in Appendix J. The Ice software is licensed under different terms. See the
Ice distribution for details on that license.
Copyright © 2003-2009 by ZeroC, Inc.
mailto:info@zeroc.com
http://www.zeroc.com
Revision 3.3.1b, July 2009
This revision of the documentation describes Ice version 3.3.1, including Ice Touch 1.0.
The Ice source distribution makes use of a number of third-party products:
• Berkeley DB, developed by Oracle (http://www.oracle.com)
• bzip2/libbzip2, developed by Julian R. Seward (http://sources.redhat.com/bzip2)
• The OpenSSL Toolkit, developed by the OpenSSL Project (http://www.openssl.org)
• SSLeay, developed by Eric Young (mailto:eay@cryptsoft.com)
• Expat, developed by James Clark (http://www.libexpat.org)
• STLport, developed by the STLport Standard Library Project (http://www.stlport.org)
• mcpp, developed by Kiyoshi Matsui (http://mcpp.sourceforge.net)
See the Ice source distribution for the license agreements for each of these products.
iii
Contents
Chapter 1 Introduction 1
1.1 Introduction 1
1.2 The Internet Communications Engine (Ice) 4
1.3 Organization of this Book 4
1.4 Typographical Conventions 6
1.5 Source Code Examples 6
1.6 Contacting the Authors 6
1.7 Ice Support 7
Part I Ice Overview 9
Chapter 2 Ice Overview 11
2.1 Chapter Overview 11
2.2 The Ice Architecture 11
2.3 Ice Services 27
2.4 Architectural Benefits of Ice 30
2.5 A Comparison with CORBA 32
Chapter 3 A Hello World Application 37
3.1 Chapter Overview 37
3.2 Writing a Slice Definition 38
3.3 Writing an Ice Application with C++ 38
3.4 Writing an Ice Application with Java 47
3.5 Writing an Ice Application with C# 54
3.6 Writing an Ice Application with Visual Basic 61
3.7 Writing an Ice Application with Objective-C 69
3.8 Writing an Ice Application with Python 78
3.9 Writing an Ice Application with Ruby 83
3.10 Summary 86
iv
Part II Slice 89
Chapter 4 The Slice Language 91
4.1 Chapter Overview 91
4.2 Introduction 91
4.3 Compilation 92
4.4 Source Files 95
4.5 Lexical Rules 97
4.6 Modules 100
4.7 The Ice Module 101
4.8 Basic Slice Types 102
4.9 User-Defined Types 104
4.10 Interfaces, Operations, and Exceptions 111
4.11 Classes 136
4.12 Forward Declarations 152
4.13 Type IDs 153
4.14 Operations on
Object 154
4.15 Local Types 156
4.16 Names and Scoping 157
4.17 Metadata 164
4.18 Serializable Objects 165
4.19 Deprecating Slice Definitions 166
4.20 Using the Slice Compilers 167
4.21 Slice Checksums 169
4.22 A Comparison of Slice and CORBA IDL 170
4.23 Generating Slice Documentation 179
4.24 Summary 185
Chapter 5 Slice for a Simple File System 187
5.1 Chapter Overview 187
5.2 The File System Application 187
5.3 Slice Definitions for the File System 188
5.4 The Complete Definition 190
v
Part III Language Mappings 193
Part III.A C++ Mapping 195
Chapter 6 Client-Side Slice-to-C++ Mapping 197
6.1 Chapter Overview 197
6.2 Introduction 197
6.3 Mapping for Identifiers 198
6.4 Mapping for Modules 199
6.5 The Ice Namespace 200
6.6 Mapping for Simple Built-In Types 200
6.7 Mapping for User-Defined Types 202
6.8 Mapping for Constants 211
6.9 Mapping for Exceptions 212
6.10 Mapping for Run-Time Exceptions 216
6.11 Mapping for Interfaces 217
6.12 Mapping for Operations 227
6.13 Exception Handling 233
6.14 Mapping for Classes 235
6.15 slice2cpp Command-Line Options 259
6.16 Using Slice Checksums 266
6.17 A Comparison with the CORBA C++ Mapping 266
Chapter 7 Developing a File System Client in C++ 269
7.1 Chapter Overview 269
7.2 The C++ Client 269
7.3 Summary 274
Chapter 8 Server-Side Slice-to-C++ Mapping 275
8.1 Chapter Overview 275
8.2 Introduction 275
8.3 The Server-Side main Function 276
8.4 Mapping for Interfaces 291
8.5 Parameter Passing 294
8.6 Raising Exceptions 295
8.7 Object Incarnation 296
8.8 Summary 302
Chapter 9 Developing a File System Server in C++ 303
9.1 Chapter Overview 303
9.2 Implementing a File System Server 303
9.3 Summary 320