From 2d7340b24dc0ca09f41215cba4c6a8e99dfb19db Mon Sep 17 00:00:00 2001 From: badim Date: Wed, 16 Mar 2016 11:36:47 +0200 Subject: [PATCH] swift3 and c lib --- Package.swift | 2 +- Sources/CLibzmq/empty.c | 0 Sources/CLibzmq/include/module.modulemap | 5 +++ Sources/Info.plist | 28 -------------- Sources/{ => swiftzmq}/CURVE.swift | 8 ++-- Sources/{ => swiftzmq}/Context.swift | 2 +- Sources/{ => swiftzmq}/Error.swift | 6 +-- Sources/{ => swiftzmq}/Message.swift | 6 +-- Sources/{ => swiftzmq}/Poller.swift | 4 +- Sources/{ => swiftzmq}/Proxy.swift | 2 +- Sources/{ => swiftzmq}/Socket.swift | 48 ++++++++++++------------ 11 files changed, 44 insertions(+), 67 deletions(-) create mode 100644 Sources/CLibzmq/empty.c create mode 100644 Sources/CLibzmq/include/module.modulemap delete mode 100644 Sources/Info.plist rename Sources/{ => swiftzmq}/CURVE.swift (86%) rename Sources/{ => swiftzmq}/Context.swift (99%) rename Sources/{ => swiftzmq}/Error.swift (89%) rename Sources/{ => swiftzmq}/Message.swift (96%) rename Sources/{ => swiftzmq}/Poller.swift (97%) rename Sources/{ => swiftzmq}/Proxy.swift (98%) rename Sources/{ => swiftzmq}/Socket.swift (95%) diff --git a/Package.swift b/Package.swift index b4f2409..ef85f5f 100644 --- a/Package.swift +++ b/Package.swift @@ -2,8 +2,8 @@ import PackageDescription let package = Package( name: "SwiftZMQ", + targets: [Target(name: "swiftzmq", dependencies: ["CLibzmq"])], dependencies: [ - .Package(url: "https://github.com/Zewo/CZeroMQ.git", majorVersion: 1), .Package(url: "https://github.com/Zewo/Data.git", majorVersion: 0, minor: 2) ] ) diff --git a/Sources/CLibzmq/empty.c b/Sources/CLibzmq/empty.c new file mode 100644 index 0000000..e69de29 diff --git a/Sources/CLibzmq/include/module.modulemap b/Sources/CLibzmq/include/module.modulemap new file mode 100644 index 0000000..0ea2d85 --- /dev/null +++ b/Sources/CLibzmq/include/module.modulemap @@ -0,0 +1,5 @@ +module CLibzmq { + header "/usr/local/include/zmq.h" + link "zmq" + export * +} \ No newline at end of file diff --git a/Sources/Info.plist b/Sources/Info.plist deleted file mode 100644 index 61facdc..0000000 --- a/Sources/Info.plist +++ /dev/null @@ -1,28 +0,0 @@ - - - - - CFBundleDevelopmentRegion - en - CFBundleExecutable - $(EXECUTABLE_NAME) - CFBundleIdentifier - $(PRODUCT_BUNDLE_IDENTIFIER) - CFBundleInfoDictionaryVersion - 6.0 - CFBundleName - $(PRODUCT_NAME) - CFBundlePackageType - FMWK - CFBundleShortVersionString - 1.0 - CFBundleSignature - ???? - CFBundleVersion - $(CURRENT_PROJECT_VERSION) - NSHumanReadableCopyright - Copyright © 2015 Zewo. All rights reserved. - NSPrincipalClass - - - diff --git a/Sources/CURVE.swift b/Sources/swiftzmq/CURVE.swift similarity index 86% rename from Sources/CURVE.swift rename to Sources/swiftzmq/CURVE.swift index 47d333b..3b8bcac 100644 --- a/Sources/CURVE.swift +++ b/Sources/swiftzmq/CURVE.swift @@ -22,15 +22,15 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -import CZeroMQ +import CLibzmq public func CURVEKeyPair() throws -> (publicKey: String, secretKey: String) { - var publicKey = [Int8](count: 41, repeatedValue: 0) - var secretKey = [Int8](count: 41, repeatedValue: 0) + var publicKey = [Int8](repeating: 0, count: 41) + var secretKey = [Int8](repeating: 0, count: 41) if zmq_curve_keypair(&publicKey, &secretKey) == -1 { throw Error.lastError } - return (String.fromCString(publicKey)!, String.fromCString(secretKey)!) + return (String(validatingUTF8: publicKey)!, String(validatingUTF8: secretKey)!) } \ No newline at end of file diff --git a/Sources/Context.swift b/Sources/swiftzmq/Context.swift similarity index 99% rename from Sources/Context.swift rename to Sources/swiftzmq/Context.swift index bee8985..e657601 100644 --- a/Sources/Context.swift +++ b/Sources/swiftzmq/Context.swift @@ -22,7 +22,7 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -import CZeroMQ +import CLibzmq public enum SocketType { case Req diff --git a/Sources/Error.swift b/Sources/swiftzmq/Error.swift similarity index 89% rename from Sources/Error.swift rename to Sources/swiftzmq/Error.swift index 270eefb..730d634 100644 --- a/Sources/Error.swift +++ b/Sources/swiftzmq/Error.swift @@ -22,13 +22,13 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -import CZeroMQ +import CLibzmq -public struct Error : ErrorType, CustomStringConvertible { +public struct Error : ErrorProtocol, CustomStringConvertible { public let description: String static var lastError: Error { - let description = String.fromCString(zmq_strerror(zmq_errno()))! + let description = String(validatingUTF8: zmq_strerror(zmq_errno()))! return Error(description: description) } } \ No newline at end of file diff --git a/Sources/Message.swift b/Sources/swiftzmq/Message.swift similarity index 96% rename from Sources/Message.swift rename to Sources/swiftzmq/Message.swift index 291c5dd..59801b8 100644 --- a/Sources/Message.swift +++ b/Sources/swiftzmq/Message.swift @@ -22,7 +22,7 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -import CZeroMQ +import CLibzmq public final class Message { var message: zmq_msg_t @@ -78,7 +78,7 @@ public final class Message { throw Error.lastError } - return String.fromCString(result)! + return String(validatingUTF8: result)! } public func close() throws { @@ -109,7 +109,7 @@ public final class Message { return message } - public func move(inout message: Message) throws { + public func move(message: inout Message) throws { let message = try Message() if zmq_msg_move(&message.message, &self.message) == -1 { diff --git a/Sources/Poller.swift b/Sources/swiftzmq/Poller.swift similarity index 97% rename from Sources/Poller.swift rename to Sources/swiftzmq/Poller.swift index e1f6a25..5c8e49f 100644 --- a/Sources/Poller.swift +++ b/Sources/swiftzmq/Poller.swift @@ -22,9 +22,9 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -import CZeroMQ +import CLibzmq -public struct PollEvent : OptionSetType { +public struct PollEvent : OptionSet { public let rawValue: Int16 public init(rawValue: Int16) { diff --git a/Sources/Proxy.swift b/Sources/swiftzmq/Proxy.swift similarity index 98% rename from Sources/Proxy.swift rename to Sources/swiftzmq/Proxy.swift index 348490d..836aee5 100644 --- a/Sources/Proxy.swift +++ b/Sources/swiftzmq/Proxy.swift @@ -22,7 +22,7 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -import CZeroMQ +import CLibzmq public func proxy(frontend: Socket, backend: Socket, capture: Socket? = nil) { zmq_proxy(frontend.socket, backend.socket, capture?.socket ?? nil) diff --git a/Sources/Socket.swift b/Sources/swiftzmq/Socket.swift similarity index 95% rename from Sources/Socket.swift rename to Sources/swiftzmq/Socket.swift index 60ea352..3ac2637 100644 --- a/Sources/Socket.swift +++ b/Sources/swiftzmq/Socket.swift @@ -22,10 +22,10 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -import CZeroMQ +import CLibzmq import Data -public struct SendMode : OptionSetType { +public struct SendMode : OptionSet { public let rawValue: Int public init(rawValue: Int) { @@ -36,7 +36,7 @@ public struct SendMode : OptionSetType { public static let SendMore = SendMode(rawValue: Int(ZMQ_SNDMORE)) } -public struct ReceiveMode : OptionSetType { +public struct ReceiveMode : OptionSet { public let rawValue: Int public init(rawValue: Int) { @@ -169,7 +169,7 @@ public final class Socket { } } -public struct SocketEvent : OptionSetType { +public struct SocketEvent : OptionSet { public let rawValue: Int32 public init(rawValue: Int32) { @@ -541,24 +541,24 @@ extension Socket { } public func getCURVEPublicKey() throws -> String { - var value = [Int8](count: 41, repeatedValue: 0) + var value = [Int8](repeating: 0, count: 41) var length = value.count try getOption(ZMQ_CURVE_PUBLICKEY, value: &value, length: &length) - return String.fromCString(Array(value[0 ..< length]))! + return String(validatingUTF8: Array(value[0 ..< length]))! } public func getCURVESecretKey() throws -> String { - var value = [Int8](count: 41, repeatedValue: 0) + var value = [Int8](repeating: 0, count: 41) var length = value.count try getOption(ZMQ_CURVE_SECRETKEY, value: &value, length: &length) - return String.fromCString(Array(value[0 ..< length]))! + return String(validatingUTF8: Array(value[0 ..< length]))! } public func getCURVEServerKey() throws -> String { - var value = [Int8](count: 41, repeatedValue: 0) + var value = [Int8](repeating: 0, count: 41) var length = value.count try getOption(ZMQ_CURVE_SERVERKEY, value: &value, length: &length) - return String.fromCString(Array(value[0 ..< length]))! + return String(validatingUTF8: Array(value[0 ..< length]))! } public func getEvents() throws -> PollEvent? { @@ -583,10 +583,10 @@ extension Socket { } public func getGSSAPIPrincipal() throws -> String { - var value = [Int8](count: 256, repeatedValue: 0) + var value = [Int8](repeating: 0, count: 256) var length = value.count try getOption(ZMQ_GSSAPI_PRINCIPAL, value: &value, length: &length) - return String.fromCString(Array(value[0 ..< length]))! + return String(validatingUTF8: Array(value[0 ..< length]))! } public func getGSSAPIServer() throws -> Bool { @@ -597,10 +597,10 @@ extension Socket { } public func getGSSAPIServicePrincipal() throws -> String { - var value = [Int8](count: 256, repeatedValue: 0) + var value = [Int8](repeating: 0, count: 256) var length = value.count try getOption(ZMQ_GSSAPI_SERVICE_PRINCIPAL, value: &value, length: &length) - return String.fromCString(Array(value[0 ..< length]))! + return String(validatingUTF8: Array(value[0 ..< length]))! } public func getHandshakeInterval() throws -> Int32 { @@ -611,10 +611,10 @@ extension Socket { } public func getIdentity() throws -> String { - var value = [Int8](count: 256, repeatedValue: 0) + var value = [Int8](repeating: 0, count: 256) var length = value.count try getOption(ZMQ_IDENTITY, value: &value, length: &length) - return String.fromCString(Array(value[0 ..< length])) ?? "" + return String(validatingUTF8: Array(value[0 ..< length])) ?? "" } public func getImmediate() throws -> Bool { @@ -646,10 +646,10 @@ extension Socket { } public func getLastEndpoint() throws -> String { - var value = [Int8](count: 256, repeatedValue: 0) + var value = [Int8](repeating: 0, count: 256) var length = value.count try getOption(ZMQ_LAST_ENDPOINT, value: &value, length: &length) - return String.fromCString(Array(value[0 ..< length]))! + return String(validatingUTF8: Array(value[0 ..< length]))! } public func getLinger() throws -> Int32 { @@ -681,10 +681,10 @@ extension Socket { } public func getPlainPassword() throws -> String { - var value = [Int8](count: 256, repeatedValue: 0) + var value = [Int8](repeating: 0, count: 256) var length = value.count try getOption(ZMQ_PLAIN_PASSWORD, value: &value, length: &length) - return String.fromCString(Array(value[0 ..< length]))! + return String(validatingUTF8: Array(value[0 ..< length]))! } public func getPlainServer() throws -> Bool { @@ -695,10 +695,10 @@ extension Socket { } public func getPlainUsername() throws -> String { - var value = [Int8](count: 256, repeatedValue: 0) + var value = [Int8](repeating: 0, count: 256) var length = value.count try getOption(ZMQ_PLAIN_USERNAME, value: &value, length: &length) - return String.fromCString(Array(value[0 ..< length]))! + return String(validatingUTF8: Array(value[0 ..< length]))! } public func getRate() throws -> Int32 { @@ -835,10 +835,10 @@ extension Socket { } public func getZAPDomain() throws -> String { - var value = [Int8](count: 256, repeatedValue: 0) + var value = [Int8](repeating: 0, count: 256) var length = value.count try getOption(ZMQ_ZAP_DOMAIN, value: &value, length: &length) - return String.fromCString(Array(value[0 ..< length]))! + return String(validatingUTF8: Array(value[0 ..< length]))! } }