summaryrefslogtreecommitdiff
path: root/swift/src/Ice/Initialize.swift
blob: 2a6159e2a19af0c0a339fe349876fe867d2ff8cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//

import IceImpl

//
// Factories are registered once when `factoriesRegistered' is lazzy initialized,
// all Swift global variables are lazzy initialized.
//
// All code paths that require the use of the factories before `initialize' is call
// should check `factoriesRegistered' to ensure lazzy initinialization occurrs before
// the factories are used.
//
internal let factoriesRegistered: Bool = {
    ICEUtil.registerFactories(exception: ExceptionFactory.self,
                              connectionInfo: ConnectionInfoFactory.self,
                              endpointInfo: EndpointInfoFactory.self,
                              adminFacet: AdminFacetFactory.self)
    return true
}()

/// Creates a communicator.
///
/// - parameter _: `[String]` - A command-line argument vector. Any Ice-related options
///   in this vector are used to initialize the communicator.
///
/// - parameter initData: `Ice.InitializationData` - Additional intialization data. Property
///   settings in args override property settings in initData.
///
/// - returns: The initialized communicator.
public func initialize(_ args: [String], initData: InitializationData? = nil) throws -> Communicator {
    return try initializeImpl(args: args, initData: initData ?? InitializationData(), withConfigFile: true).0
}

/// Creates a communicator.
///
/// - parameter _: `[String]` - A command-line argument vector. Any Ice-related options
///   in this vector are used to initialize the communicator. This method modifies the
///   argument vector by removing any Ice-related options.
///
/// - parameter initData: `Ice.InitializationData` - Additional intialization data. Property
///   settings in args override property settings in initData.
///
/// - returns: `Ice.Communicator` - The initialized communicator.
public func initialize(_ args: inout [String], initData: InitializationData? = nil) throws -> Communicator {
    let result = try initializeImpl(args: args, initData: initData ?? InitializationData(), withConfigFile: true)
    args = result.1
    return result.0
}

/// Creates a communicator.
///
/// - parameter args: `[String]` - A command-line argument array. Any Ice-related options
///   in this array are used to initialize the communicator.
///
/// - parameter configFile: `String` - Path to a config file that sets the new communicator's
///   default properties.
///
/// - returns: `Ice.Communicator` - The initialized communicator.
public func initialize(args: [String], configFile: String) throws -> Communicator {
    var initData = InitializationData()
    let properties = createProperties()
    try properties.load(configFile)
    initData.properties = properties
    return try initialize(args, initData: initData)
}

/// Creates a communicator.
///
/// - parameter args: `[String]` - A command-line argument array. Any Ice-related options
///   in this array are used to initialize the communicator. This method modifies the
///   argument array by removing any Ice-related options.
///
/// - parameter configFile: `String` - Path to a config file that sets the new communicator's
///   default properties.
///
/// - returns: `Ice.Communicator` - The initialized communicator.
public func initialize(args: inout [String], configFile: String) throws -> Communicator {
    var initData = InitializationData()
    let properties = createProperties()
    try properties.load(configFile)
    initData.properties = properties
    return try initialize(&args, initData: initData)
}

/// Creates a communicator.
///
/// - parameter _: `Ice.InitializationData` - Additional intialization data.
///
/// - returns: `Ice.Communicator` - The initialized communicator.
public func initialize(_ initData: InitializationData? = nil) throws -> Communicator {
    // This is the no-configFile flavor: we never load config from ICE_CONFIG
    return try initializeImpl(args: [], initData: initData ?? InitializationData(), withConfigFile: false).0
}

/// Creates a communicator.
///
/// - parameter _: `String` - Path to a config file that sets the new communicator's default
///   properties.
///
/// - returns: `Ice.Communicator` - The initialized communicator.
public func initialize(_ configFile: String) throws -> Communicator {
    return try initialize(args: [], configFile: configFile)
}

private func initializeImpl(args: [String],
                            initData userInitData: InitializationData,
                            withConfigFile: Bool) throws -> (Communicator, [String]) {
    // Ensure factories are initialized
    guard factoriesRegistered else {
        fatalError("Unable to initialie Ice")
    }

    var initData = userInitData
    if initData.properties == nil {
        initData.properties = createProperties()
    }

    var loggerP: ICELoggerProtocol?
    if let l = initData.logger {
        loggerP = LoggerWrapper(handle: l)
    }

    let propsHandle = (initData.properties as! PropertiesI).handle

    return try autoreleasepool {
        var remArgs: NSArray?
        let handle = try ICEUtil.initialize(args,
                                            properties: propsHandle,
                                            withConfigFile: withConfigFile,
                                            logger: loggerP,
                                            remArgs: &remArgs)

        //
        // Update initData.properties reference to point to the properties object
        // created by Ice::initialize, in case it changed
        //
        let newPropsHandle = handle.getProperties()
        initData.properties = newPropsHandle.getSwiftObject(PropertiesI.self) {
            PropertiesI(handle: newPropsHandle)
        }

        //
        // Update initData.logger referecnce in case we are using a C++ logger (defined though a property) or
        //  a C++ logger plug-in installed a new logger
        //
        if let objcLogger = handle.getLogger() as? ICELogger {
            initData.logger = objcLogger.getSwiftObject(ObjcLoggerWrapper.self) {
                ObjcLoggerWrapper(handle: objcLogger)
            }
        }

        precondition(initData.logger != nil && initData.properties != nil)

        let communicator = CommunicatorI(handle: handle, initData: initData)
        if remArgs == nil {
            return (communicator, [])
        } else {
            // swiftlint:disable force_cast
            return (communicator, remArgs as! [String])
        }
    }
}

/// Creates a new empty property set.
///
/// - returns: `Properties` - A new empty property set.
public func createProperties() -> Properties {
    guard factoriesRegistered else {
        fatalError("Unable to initialie Ice")
    }
    return PropertiesI(handle: ICEUtil.createProperties())
}

/// Creates a property set initialized from an argument array.
///
/// - parameter _: `[String]` - A command-line argument array, possibly containing options to
///   set properties. If the command-line options include a `--Ice.Config` option, the
///   corresponding configuration files are parsed. If the same property is set in a configuration
///   file and in the argument array, the argument array takes precedence.
///
/// - parameter defaults: `Ice.Properties` - Optional default values for the property set. Settings in
///   configuration files and argument array override these defaults.
///
/// - returns: `Ice.Properties` - A new property set initialized with the property settings from the arguments
///   array and defaults.
public func createProperties(_ args: [String], defaults: Properties? = nil) throws -> Properties {
    guard factoriesRegistered else {
        fatalError("Unable to initialie Ice")
    }
    return try autoreleasepool {
        let propertiesHandle = try ICEUtil.createProperties(args,
                                                            defaults: (defaults as? PropertiesI)?.handle,
                                                            remArgs: nil)
        return PropertiesI(handle: propertiesHandle)
    }
}

/// Creates a property set initialized from an argument array.
///
/// - parameter _: `[String]` - A command-line argument array, possibly containing options to
///   set properties. If the command-line options include a `--Ice.Config` option, the
///   corresponding configuration files are parsed. If the same property is set in a configuration
///   file and in the argument array, the argument array takes precedence. This method modifies the
///   argument array by removing any Ice-related options.
///
/// - parameter defaults: `Ice.Properties` - Optional default values for the property set. Settings in
///   configuration files and argument array override these defaults.
///
/// - returns: `Ice.Properties` - A new property set initialized with the property settings from args
///   and defaults.
public func createProperties(_ args: inout [String], defaults: Properties? = nil) throws -> Properties {
    guard factoriesRegistered else {
        fatalError("Unable to initialie Ice")
    }
    return try autoreleasepool {
        var remArgs: NSArray?
        let propertiesHandle = try ICEUtil.createProperties(args,
                                                            defaults: (defaults as? PropertiesI)?.handle,
                                                            remArgs: &remArgs)

        // swiftlint:disable force_cast
        args = remArgs as! [String]
        return PropertiesI(handle: propertiesHandle)
    }
}

/// Returns the Ice version as an integer in the form A.BB.CC, where A
/// indicates the major version, BB indicates the minor version, and CC
/// indicates the patch level. For example, for Ice 3.3.1, the returned
/// value is 30301.
public let intVersion: Int = 30703

/// The Ice version in the form A.B.C, where A indicates the major version,
/// B indicates the minor version, and C indicates the patch level.
public let stringVersion: String = "3.7.3"

public let Encoding_1_0 = EncodingVersion(major: 1, minor: 0)
public let Encoding_1_1 = EncodingVersion(major: 1, minor: 1)

public let currentEncoding = Encoding_1_1

/// Converts a string to an object identity.
///
/// - parameter _: `String` - The string to convert.
///
/// - returns: `Ice.Identity` - The converted object identity.
public func stringToIdentity(_ string: String) throws -> Identity {
    guard factoriesRegistered else {
        fatalError("Unable to initialie Ice")
    }
    return try autoreleasepool {
        var name = NSString()
        var category = NSString()
        try ICEUtil.stringToIdentity(str: string, name: &name, category: &category)
        return Identity(name: name as String, category: category as String)
    }
}

/// Converts an object identity to a string.
///
/// - parameter id: `Ice.Identity` - The object identity to convert.
///
/// - parameter mode: `ToStringMode` - Specifies if and how non-printable ASCII characters are escaped
///   in the result.
///
/// - returns: `String` - The string representation of the object identity.
public func identityToString(id: Identity, mode: ToStringMode = ToStringMode.Unicode) -> String {
    return ICEUtil.identityToString(name: id.name, category: id.category, mode: mode.rawValue)
}

/// Converts an encoding version to a string.
///
/// - parameter _: `Ice.EncodingVersion` - The encoding version to convert.
///
/// - returns: `String` - The converted string.
public func encodingVersionToString(_ encoding: EncodingVersion) -> String {
    return ICEUtil.encodingVersionToString(major: encoding.major, minor: encoding.minor)
}