Commit b732f33d authored by Ignacio Corderi's avatar Ignacio Corderi
Browse files

Refactored send() to use promise()

parent 871ad97d
Loading
Loading
Loading
Loading
+49 −49
Original line number Diff line number Diff line
@@ -91,6 +91,7 @@ public class KineticSession {
                    print("Oops: please code me...")
                }
            }
            print("Session closed, reader going away...")
        }
        
        // We only need the writer queue if connection was ok
@@ -108,31 +109,6 @@ public class KineticSession {
    }
    
    public func promise<C: ChannelCommand>(cmd: C) -> Future<C.ResponseType, PromiseErrors> {
        let promise = Promise<C.ResponseType, PromiseErrors>()
        
        Queue.global.async {
            do {
                try promise.success(self.send(cmd))
            } catch let err {
                promise.tryFailure(.SomeError(err))
            }
        }
        
        return promise.future
    }
    
    /// Sends a command to the target device and waits for a response
    ///
    /// The type of the result is determined by the command being sent.
    ///
    /// Example:
    /// ```swift
    /// let response = try session.send(cmd)
    /// ```
    ///
    /// - Parameter cmd: The command that will be sent.
    /// - Returns: The response from the device.
    public func send<C: ChannelCommand>(cmd: C) throws -> C.ResponseType {
        // Prepare command contents
        let builder = cmd.build(Builder())
        
@@ -144,24 +120,27 @@ public class KineticSession {
        
        let m = builder.message
        
        // Prepare promise
        let promise = Promise<C.ResponseType, PromiseErrors>()
        
        do {
            // Build command proto
            let cmdProto = try builder.command.build()
            m.commandBytes = cmdProto.data()
            
            self.credentials.authenticate(builder)
            
        // Prepare promise
        let promise = Promise<RawResponse, NoError>()
            
            self.pending[builder.command.header.sequence] = { r in
                do {
                try promise.success(r)
                    try promise.success(C.ResponseType.parse(r))
                } catch {
                    print("Mmm... when does this happen?")
                }
                self.pending[builder.command.header.sequence] = nil
            }
            
        // Send & Receive
            // Queue the command to be sent to the target device
            dispatch_async(self.writerQueue) {
                do {
                    print("Sending seq:\(builder.command.header.sequence)")
@@ -171,11 +150,32 @@ public class KineticSession {
                    print("Sending failed :/ what a bummer")
                }
            }
        } catch {
            print("More oops! FIX ME")
        }
        
        // TODO: all this !bangs are ugly, what can go wrong?
        let r = promise.future.forced()!.value!
        return promise.future
    }
    
    /// Sends a command to the target device and waits for a response
    ///
    /// The type of the result is determined by the command being sent.
    ///
    /// Example:
    /// ```swift
    /// let response = try session.send(cmd)
    /// ```
    ///
    /// - Parameter cmd: The command that will be sent.
    /// - Returns: The response from the device.
    public func send<C: ChannelCommand>(cmd: C) -> C.ResponseType {
        let future = self.promise(cmd)
        return future.forced()!.value!
    }
    
        return C.ResponseType.parse(r)
    public func send<C: ChannelCommand>(cmd: C, timeout:NSTimeInterval) -> C.ResponseType {
        let future = self.promise(cmd)
        return future.forced(timeout)!.value!
    }

}