Explorar o código

Ready for swift-2.2.1-RELEASE-ubuntu15.10.

kolakowski %!s(int64=10) %!d(string=hai) anos
pai
achega
e945be3c2f

+ 5 - 1
Sources/HttpRequest.swift

@@ -5,7 +5,11 @@
 //  Copyright (c) 2014-2016 Damian Kołakowski. All rights reserved.
 //
 
-import Foundation
+#if os(Linux)
+    import Glibc
+#else
+    import Foundation
+#endif
 
 public class HttpRequest {
     

+ 20 - 9
Sources/HttpResponse.swift

@@ -5,7 +5,11 @@
 //  Copyright (c) 2014-2016 Damian Kołakowski. All rights reserved.
 //
 
-import Foundation
+#if os(Linux)
+    import Glibc
+#else
+    import Foundation
+#endif
 
 public enum SerializationError: ErrorType {
     case InvalidObject
@@ -28,14 +32,21 @@ public enum HttpResponseBody {
         do {
             switch self {
             case .Json(let object):
-                guard NSJSONSerialization.isValidJSONObject(object) else {
-                    throw SerializationError.InvalidObject
-                }
-                let json = try NSJSONSerialization.dataWithJSONObject(object, options: NSJSONWritingOptions.PrettyPrinted)
-                let data = Array(UnsafeBufferPointer(start: UnsafePointer<UInt8>(json.bytes), count: json.length))
-                return (data.count, {
-                    $0.write(data)
-                })
+                #if os(Linux)
+                    let data = [UInt8]("Not ready for Linux.".utf8)
+                    return (data.count, {
+                        $0.write(data)
+                    })
+                #else
+                    guard JSONSerialization.isValidJSONObject(object) else {
+                        throw SerializationError.InvalidObject
+                    }
+                    let json = try NSJSONSerialization.dataWithJSONObject(object, options: NSJSONWritingOptions.PrettyPrinted)
+                    let data = Array(UnsafeBufferPointer(start: UnsafePointer<UInt8>(json.bytes), count: json.length))
+                    return (data.count, {
+                        $0.write(data)
+                    })
+                #endif
             case .Text(let body):
                 let data = [UInt8](body.utf8)
                 return (data.count, {

+ 5 - 1
Sources/HttpRouter.swift

@@ -5,7 +5,11 @@
 //  Copyright (c) 2014-2016 Damian Kołakowski. All rights reserved.
 //
 
-import Foundation
+#if os(Linux)
+    import Glibc
+#else
+    import Foundation
+#endif
 
 public class HttpRouter {
     

+ 6 - 2
Sources/HttpServer.swift

@@ -5,7 +5,11 @@
 //  Copyright (c) 2014-2016 Damian Kołakowski. All rights reserved.
 //
 
-import Foundation
+#if os(Linux)
+    import Glibc
+#else
+    import Foundation
+#endif
 
 public class HttpServer: HttpServerIO {
     
@@ -65,4 +69,4 @@ public class HttpServer: HttpServerIO {
             get { return nil }
         }
     }
-}
+}

+ 45 - 3
Sources/HttpServerIO.swift

@@ -7,7 +7,6 @@
 
 #if os(Linux)
     import Glibc
-    import NSLinux
 #else
     import Foundation
 #endif
@@ -16,7 +15,7 @@ public class HttpServerIO {
     
     private var listenSocket: Socket = Socket(socketFileDescriptor: -1)
     private var clientSockets: Set<Socket> = []
-    private let clientSocketsLock = NSLock()
+    private let clientSocketsLock = Lock()
     
     public func start(listenPort: in_port_t = 8080, forceIPv4: Bool = false) throws {
         stop()
@@ -76,7 +75,7 @@ public class HttpServerIO {
         socket.release()
     }
     
-    private func lock(handle: NSLock, closure: () -> ()) {
+    private func lock(handle: Lock, closure: () -> ()) {
         handle.lock()
         closure()
         handle.unlock();
@@ -123,3 +122,46 @@ public class HttpServerIO {
         return keepAlive && content.length != -1;
     }
 }
+
+#if os(Linux)
+    
+    import Glibc
+
+    public class Lock {
+    
+        private var mutex = pthread_mutex_t()
+	    
+        init() { pthread_mutex_init(&mutex, nil) }
+	    
+        public func lock() { pthread_mutex_lock(&mutex) }
+	    
+        public func unlock() { pthread_mutex_unlock(&mutex) }
+	    
+        deinit { pthread_mutex_destroy(&mutex) }
+    }
+
+    
+    let DISPATCH_QUEUE_PRIORITY_BACKGROUND = 0
+    
+    private class dispatch_context {
+        let block: ((Void) -> Void)
+        init(_ block: ((Void) -> Void)) {
+            self.block = block
+        }
+    }
+    
+    func dispatch_get_global_queue(queueId: Int, _ arg: Int) -> Int { return 0 }
+    
+    func dispatch_async(queueId: Int, _ block: ((Void) -> Void)) {
+        let unmanagedDispatchContext = Unmanaged.passRetained(dispatch_context(block))
+        let context = UnsafeMutablePointer<Void>(unmanagedDispatchContext.toOpaque())
+        var pthread: pthread_t = 0
+        pthread_create(&pthread, nil, { (context: UnsafeMutablePointer<Void>) -> UnsafeMutablePointer<Void> in
+            let unmanaged = Unmanaged<dispatch_context>.fromOpaque(COpaquePointer(context))
+            unmanaged.takeUnretainedValue().block()
+            unmanaged.release()
+            return context
+        }, context)
+    }
+    
+#endif