SwiftMQTT icon indicating copy to clipboard operation
SwiftMQTT copied to clipboard

Add Ubuntu support

Open luisdelarosa opened this issue 8 years ago • 6 comments

Swift is used on Ubuntu and MQTT is a good protocol for IoT purposes. This can be used both on the server-side and on Raspberry Pis.

Here is the current error log when built on Ubuntu MATE on Raspberry Pi:

/home/louie/projects/swift/SwiftMQTT/Sources/SwiftMQTT/MQTTSession.swift:90:89: error: '#selector' can only be used with the Objective-C runtime
        keepAliveTimer = Timer(timeInterval: Double(keepAlive), target: self, selector: #selector(MQTTSession.keepAliveTimerFired), userInfo: nil, repeats: true)
                                                                                        ^
/home/louie/projects/swift/SwiftMQTT/Sources/SwiftMQTT/MQTTSessionStreamDelegate.swift:65:19: error: method 'stream(_:handle:)' has different argument names from those required by protocol 'StreamDelegate' ('stream(_:handleEvent:)')
    internal func stream(_ aStream: Stream, handle eventCode: Stream.Event) {
                  ^                         ~~~~~~
                                            handleEvent
Foundation.StreamDelegate:2:17: note: requirement 'stream(_:handleEvent:)' declared here
    public func stream(_ aStream: Foundation.Stream, handleEvent eventCode: Foundation.Stream.Event)
                ^
/home/louie/projects/swift/SwiftMQTT/Sources/SwiftMQTT/MQTTSessionStreamDelegate.swift:34:9: error: type 'Stream' has no member 'getStreamsToHost'
        Stream.getStreamsToHost(withName: host, port: Int(port), inputStream: &inputStream, outputStream: &outputStream)
        ^~~~~~ ~~~~~~~~~~~~~~~~
/home/louie/projects/swift/SwiftMQTT/Sources/SwiftMQTT/MQTTSessionStreamDelegate.swift:43:38: error: cannot convert value of type 'String' to expected argument type 'AnyObject?'
            inputStream?.setProperty(securityLevel, forKey: Stream.PropertyKey.socketSecurityLevelKey)
                                     ^~~~~~~~~~~~~
                                                   as! AnyObject
/home/louie/projects/swift/SwiftMQTT/Sources/SwiftMQTT/MQTTSessionStreamDelegate.swift:44:39: error: cannot convert value of type 'String' to expected argument type 'AnyObject?'
            outputStream?.setProperty(securityLevel, forKey: Stream.PropertyKey.socketSecurityLevelKey)
                                      ^~~~~~~~~~~~~
                                                    as! AnyObject
/home/louie/projects/swift/SwiftMQTT/Sources/SwiftMQTT/MQTTSessionStreamDelegate.swift:95:26: warning: result of call to 'read(_:maxLength:)' is unused
            inputStream?.read(&readByte, maxLength: 1)
                         ^   ~~~~~~~~~~~~~~~~~~~~~~~~~

luisdelarosa avatar Oct 08 '17 00:10 luisdelarosa

For the #selector error, it looks like alternatives like Perfect's Threading framework is an alternative: https://stackoverflow.com/questions/41328989/swift-3-linux-with-perfect-add-a-scheduled-timer-with-interval-to-the-runloop

luisdelarosa avatar Oct 08 '17 00:10 luisdelarosa

For the steam(_:handleEvent:) error, I filed a bug with Swift at https://bugs.swift.org/browse/SR-6088 It could also be worked around with #if os() directives.

luisdelarosa avatar Oct 08 '17 00:10 luisdelarosa

Stream.getStreamsToHost is NSUnimplemented on Ubuntu, due to

// Discussion of this API is ongoing for its usage of AutoreleasingUnsafeMutablePointer

luisdelarosa avatar Oct 08 '17 00:10 luisdelarosa

Hi, Would like to ask you, if you have solved the problem with: Stream.getStreamsToHost(withName: host, port: Int(port), inputStream: &inputStream, outputStream: &outputStream) Thanks for your answer!

jiribohm avatar Oct 27 '19 11:10 jiribohm

The problem is stil not solved in 2021. I'm having the same issue on raspberry with swift 5.1. I wanted to rewrite my smart home logic from python to swift, thus mqtt connection is a must. Mqtt library uses Stream.getStreamsToHost under the hood, so it looks like it is a dead end to me.

tomieq avatar Mar 01 '21 10:03 tomieq

Hard to believe that the discussion over the usage of a pointer is holding back a very useful API for several years now. If that API is not available, is there a matching workaround that just returns a tuple?

Obviously it’s not just the autoreleased pointer, the whole CFSocketStream infrastructure is missing on Linux, hence someone needs to come up with a Stream pair that encapsulates POSIX sockets instead.

mickeyl avatar Apr 02 '21 09:04 mickeyl