1. Overview
Ice uses a configuration mechanism that allows us to control many behaviors of our ice applications at runtime, such as the maximum message size, number of threads, and whether to generate network tracking messages. This mechanism can be used not only to configure ice, but also to provide configuration parameters for your applications.
2. Attributes
Ice and its subsystems are configured through properties. An attribute is a name-value pair ). E. g.: Ice. UDP. sendsize = 65535;
Reserved prefix: Ice, icebox, icepack, icepatch, icessl, icestorm, freeze, and glacier. We cannot use the attributes starting with these prefixes to configure our own applications.
3. Configuration File
Attributes are usually set in the configuration file. The configuration file contains some name-value pairs, each of which is on a separate row. Blank lines and rows with completely blank characters are ignored. # The string following the character is a comment until the end of the current row.
Configuration File example:
# Example config file for ice
Ice. messagesizemax = 2048 # larger message size is 2 MB
Ice. Trace. Network = 3 # Highest Level of tracing for Network
Ice. Trace. Protocol = # disable protocol tracing
If you assign a vacant property, the property is cleared.
For C ++, ice reads the content of the configuration file when we create a communication device. The configuration file name is determined by the content of the ice_config environment variable. For example:
$ Ice_config =/opt/ICE/default_config
$ Export ice_config
$./Server
This allows the server to read its attribute settings from the configuration file/opt/ICE/default_config.
4. Set Properties in the command line
In addition to setting properties in the configuration file. We can also set properties on the command line, for example:
$./Server-ice. UDP. sendsize = 65535-icessl. Trace. Security = 2
Any command line option with a-starting header followed by a reserved prefix will be read and converted to attribute settings when the communicator is created. The attribute settings in the command line overwrite the settings in the configuration file.
For convenience, any attribute that is not explicitly set will be set to value 1.
For example:./Server-ice. Trace. Protocol is equivalent to./Server-ice. Trace. Protocol = 1 (only applicable to command line settings)
5. Ice. config attributes
For ice run time, the ice. config attribute has a special meaning: it determines the path name of the configuration file used to read the attribute settings. For example:
$./Server-ice. Config =/usr/local/filesystem/config
This allows the configuration settings to be read from the/usr/local/filesystem/comfig configuration file.
6. Command Line parsing and initialization
If we use the ice: Application Assistant class, the parameters received by the run method are also the cleaned parameter vectors.
7. Ice. programname attributes
In C ++, initialize sets the ice. programname attribute to the name of the current program (argv [0]). Ice uses the program name for log information.
8. Use properties in a program
The ice attribute mechanism can be used not only to configure ice, but also to configure its own applications. For example, we introduce an attribute to control the maximum file size of our file system application:
# Configuration file for File System Application
Filesystem. maxfilesize = 1024 # Max file size in KB
The ice run time stores the filesystem. maxfilesize attribute like any other attribute, and allows us to access it through the properties interface.
To access the property value in your own program, call getproperties to obtain the attributes of the communicator:
Module ice {
Localinterface properties; // Forward Declaration
Localinterface communicator {
Propertiesgetproperties ();
//...
};
};
The properties interface provides the following methods for setting read/write properties:
Module ice {
Local dictionary <string, string> propertydict;
Local interface properties {
Stringgetproperty (string key );
Stringgetpropertywithdefault (string key, string value );
Intgetpropertyasint (string key );
Intgetpropertyasintwithdefault (string key, int value );
Propertydictgetpropertiesforprefix (string prefix );
Voidsetproperty (string key, string value );
Stringseqgetcommandlineoptions ();
Stringseqparsecommandlineoptions (string prefix,
Stringseqoptions );
Stringseqparseicecommandlineoptions (stringseq options );
Voidload (string file );
Propertiesclone ();
};
};
8.1) read attributes
Getproperty: this operation returns the value of the specified property.
Getpropertywithdefault: If this property is not set, the default value provided by the user is returned.
Getpropertyasint: return the value of the specified attribute as an integer.
Getpropertyasintwithdefault: if the property is not set or contains a string that cannot be parsed as an integer, the default value provided by you is returned.
Getpropertyforprefix: returns all attributes starting with a specified prefix as a dictionary of the propertydict type.
For example:
//...
Ice: communicatorptric;
//...
Ic = ice: Initialize (argc, argv );
// Getthe maximum file size.
//
Ice: propertiesptr props = IC-> getproperties ();
Ice: intmaxsize
= Props-> getpropertyasintwithdefault ("filesystem. maxfilesize ",
1024 );
//...
Suppose we have created a configuration file for setting filesystem. maxfilesize attribute (and the ice_config variable or-ice is set accordingly. config option), our application will get the configured property value.
8.2) Set Properties
The setproperty operation sets an attribute to a specified value (you can clear it as long as the attribute is set to an empty string ). This operation is useful only when you call initialize. If you want to make sure a property is set to a specific value regardless of the setting in the configuration file, getdefaultproperties is useful. For example:
// Get the initialized property set.
//
Ice: propertiesptr props = ice: getdefaultproperties (argc, argv );
// Make sure that network and protocol tracing are off.
//
Props-> setproperty ("ice. Trace. Network", "0 ");
Props-> setproperty ("ice. Trace. Protocol", "0 ");
// Initialize a communicator with these properties.
//
Ice: communicatorptr Ic = ice: Initialize (argc, argv );
//...
8.3) parsing attributes
The properties interface provides three operations for conversion and resolution of attributes: getcommandlineoptions, parsecommandlineoptions, and parseicecommandlineoptions.
8.4) practical operations
The properties interface provides two practical operations: Clone -- this operation creates a copy of an existing property set.
Load -- the parameter of this operation is the path name of a configuration file, which will initialize the Attribute Set Based on this file.
8.5) process Multiple Attribute Sets
Sometimes multiple communication devices may be used in the program, so Multiple Attribute Sets must be used. In order to use independent property sets, ice provides a practical function for creating new property sets:
// Create a property set for the first communcator.
//
Ice: propertiesptr props1 = createproperties ();
// Make sure that network tracing is off.
//
Props1-> setproperty ("ice. Trace. Network", "0 ");
// Initialize a communicator with this property set.
//
Ice: communicatorptr IC1
= Ice: initializewithproperties (argc, argv, props1 );
9. Summary
The ice attribute Mechanism provides a simple way to configure the ice path. We can configure the attributes in the configuration file or on the command line. The API used to access attributes is small and simple, so it is easy to use it to obtain attribute sets at runtime. This is how ice is flexible.
Bytes --------------------------------------------------------------------------------------
The reference materials for the above notes are from ice1.3.0 (translated by Feng Weida) and ice3.4.2 (original version).