The main entry point to the Ice run time is represented by the local interface Ice::Communicator. As for the client side, you must initialize the Ice run time by calling
Ice.Util.initialize before you can do anything else in your server.
Ice.Util.initialize returns a reference to an instance of an
Ice.Communicator:
using System;
public class Server
{
public static void Main(string[] args)
{
int status = 0;
Ice.Communicator communicator = null;
try {
communicator = Ice.Util.initialize(ref args);
// ...
} catch (Exception ex) {
Console.Error.WriteLine(ex);
status = 1;
}
// ...
}
}
Ice.Util.initialize accepts the argument vector that is passed to
Main by the operating system. The method scans the argument vector for any command-line options that are relevant to the Ice run time; any such options are removed from the argument vector so, when
Ice.Util.initialize returns, the only options and arguments remaining are those that concern your application. If anything goes wrong during initialization,
initialize throws an exception.
Before leaving your Main method, you
must call
Communicator::destroy. The
destroy operation is responsible for finalizing the Ice run time. In particular,
destroy waits for any operation invocations that may still be running to complete. In addition,
destroy ensures that any outstanding threads are joined with and reclaims a number of operating system resources, such as file descriptors and memory. Never allow your
Main method to terminate without calling
destroy first; doing so has undefined behavior.
The general shape of our server-side Main method is therefore as follows:
using System;
public class Server
{
public static void Main(string[] args)
{
int status = 0;
Ice.Communicator communicator = null;
try {
communicator = Ice.Util.initialize(ref args);
// ...
} catch (Exception ex) {
Console.Error.WriteLine(ex);
status = 1;
}
if (communicator != null) {
try {
communicator.destroy();
} catch (Exception ex) {
Console.Error.WriteLine(ex);
status = 1;
}
}
Environment.Exit(status);
}
}
Note that the code places the call to Ice.Util.initialize into a
try block and takes care to return the correct exit status to the operating system. Also note that an attempt to destroy the communicator is made only if the initialization succeeded.
16.3.1 The Ice.Application Class
The preceding structure for the Main method is so common that Ice offers a class,
Ice.Application, that encapsulates all the correct initialization and finalization activities. The synopsis of the class is as follows (with some detail omitted for now):
namespace Ice
{
public abstract class Application
{
public abstract int run(string[] args);
public Application();
public Application(SignalPolicy signalPolicy);
public int main(string[] args);
public int main(string[] args, string configFile);
public static string appName();
public static Communicator communicator();
}
}
The intent of this class is that you specialize Ice.Application and implement the abstract
run method in your derived class. Whatever code you would normally place in
Main goes into the
run method instead. Using
Ice.Application, our program looks as follows:
using System;
public class Server
{
class App : Ice.Application
{
public override int run(string[] args)
{
// Server code here...
return 0;
}
}
public static void Main(string[] args)
{
App app = new App();
Environment.Exit(app.main(args));
}
}
The Application.main method does the following:
1. It installs an exception handler for System.Exception. If your code fails to handle an exception,
Application.main prints the name of the exception and a stack trace on
Console.Error before returning with a non-zero return value.
2.
It initializes (by calling Ice.Util.initialize) and finalizes (by calling
Communicator.destroy) a communicator. You can get access to the communicator for your server by calling he static
communicator accessor.
6.
It installs a per-process logger (see Section 28.19.5) if the application has not already configured one. The per-process logger uses the value of the
Ice.ProgramName property (see
Section 26.7) as a prefix for its messages and sends its output to the standard error channel. An application can specify an alternate logger by including it in the
InitializationData structure.
Using Ice.Application ensures that your program properly finalizes the Ice run time, whether your server terminates normally or in response to an exception. We recommend that all your programs use this class; doing so makes your life easier. In addition
Ice.Application also provides features for signal handling and configuration that you do not have to implement yourself when you use this class.
Using Ice.Application on the Client Side
You can use Ice.Application for your clients as well: simply implement a class that derives from
Ice.Application and place the client code into its
run method. The advantage of this approach is the same as for the server side:
Ice.Application ensures that the communicator is destroyed correctly even in the presence of exceptions.
The simple server we developed in Chapter 3 had no way to shut down cleanly: we simply interrupted the server from the command line to force it to exit. Terminating a server in this fashion is unacceptable for many real-life server applications: typically, the server has to perform some cleanup work before terminating, such as flushing database buffers or closing network connections. This is particularly important on receipt of a signal or keyboard interrupt to prevent possible corruption of database files or other persistent data.
To make it easier to deal with signals, Ice.Application encapsulates the low-level signal handling tasks, allowing you to cleanly shut down on receipt of a signal.
namespace Ice
{
public abstract class Application
{
// ...
public static void destroyOnInterrupt();
public static void shutdownOnInterrupt();
public static void ignoreInterrupt();
public static void callbackOnInterrupt();
public static void holdInterrupt();
public static void releaseInterrupt();
public static bool interrupted();
public virtual void interruptCallback(int sig);
}
}
This method configures Ice.Application to invoke
interruptCallback when a signal occurs, thereby giving the subclass responsibility for handling the signal.
This method returns true if a signal caused the communicator to shut down,
false otherwise. This allows us to distinguish intentional shutdown from a forced shutdown that was caused by a signal. This is useful, for example, for logging purposes.
By default, Ice.Application behaves as if
destroyOnInterrupt was invoked, therefore our server
Main method requires no change to ensure that the program terminates cleanly on receipt of a signal. (You can disable the signal-handling functionality of
Ice.Application by passing the enumerator
NoSignalHandling to the constructor. In that case, signals retain their default behavior, that is, terminate the process.) However, we add a diagnostic to report the occurrence, so our
run method now looks like:
using System;
public class Server
{
class App : Ice.Application
{
public override int run(string[] args)
{
// Server code here...
if (interrupted())
Console.Error.WriteLine(
appName() + ": terminating");
return 0;
}
}
public static void Main(string[] args)
{
App app = new App();
Environment.Exit(app.main(args));
}
}
Apart from the functionality shown in this section, Ice.Application also takes care of initializing the Ice run time with property values. Properties allow you to configure the run time in various ways. For example, you can use properties to control things such as the thread pool size or port number for a server. The
main method of
Ice.Application is overloaded; the second version allows you to specify the name of a configuration file that will be processed during initialization. We discuss Ice properties in more detail in
Chapter 26.
Ice.Application is a singleton class that creates a single communicator. If you are using multiple communicators, you cannot use
Ice.Application. Instead, you must structure your code as we saw in
Chapter 3 (taking care to always destroy the communicator).