Skip to content

Commit

Permalink
Linux support
Browse files Browse the repository at this point in the history
  • Loading branch information
Dr. Brandon Wiley committed Sep 3, 2020
1 parent 0f7363e commit a78c630
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 1 deletion.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"object": {
"pins": [
{
"package": "Socket",
"repositoryURL": "https://github.com/OperatorFoundation/BlueSocket",
"state": {
"branch": null,
"revision": "90212ea8b66787c2d51d03527a04748ba6ee7679",
"version": "1.1.0"
}
},
{
"package": "Chord",
"repositoryURL": "https://github.com/OperatorFoundation/Chord",
"state": {
"branch": null,
"revision": "3d0189b4f6e71441d82c577c0f583fceace47684",
"version": "0.0.5"
}
},
{
"package": "Datable",
"repositoryURL": "https://github.com/OperatorFoundation/Datable",
"state": {
"branch": null,
"revision": "1c23f2b72d0d0cf4386fad05f8a5db4b91e8d304",
"version": "3.0.2"
}
},
{
"package": "SwiftQueue",
"repositoryURL": "https://github.com/OperatorFoundation/SwiftQueue",
"state": {
"branch": null,
"revision": "0e5979acbb104516a427bb2bb09debc50ca100b4",
"version": "0.0.3"
}
}
]
},
"version": 1
}
7 changes: 7 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,23 @@ let package = Package(
// .package(url: /* package url */, from: "1.0.0"),
.package(url: "https://github.com/OperatorFoundation/Chord", from: "0.0.5"),
.package(url: "https://github.com/OperatorFoundation/Datable", from: "3.0.2"),
.package(name: "Socket", url: "https://github.com/OperatorFoundation/BlueSocket", from: "1.1.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "Transmission",
dependencies: ["Chord"]),
.target(
name: "TransmissionLinux",
dependencies: ["Chord", "Socket"]),
.testTarget(
name: "TransmissionTests",
dependencies: ["Transmission", "Datable"]),
.testTarget(
name: "TransmissionLinuxTests",
dependencies: ["TransmissionLinux", "Datable"]),
],
swiftLanguageVersions: [.v5]
)
69 changes: 69 additions & 0 deletions Sources/TransmissionLinux/Connection.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import Foundation
import Socket
import Datable
import Chord

public class Connection
{
var connectLock = DispatchGroup()
var readLock = DispatchGroup()
var writeLock = DispatchGroup()

let socket: Socket

public init?(host: String, port: Int)
{
guard let socket = try? Socket.create() else {return nil}
self.socket = socket

do
{
try self.socket.connect(to: host, port: Int32(port))
}
catch
{
return nil
}
}

public init(socket: Socket)
{
self.socket = socket
}

public func read(size: Int) -> Data?
{
var data = Data()

do
{
let _ = try self.socket.read(into: &data)
}
catch
{
return nil
}

return data
}

public func write(string: String) -> Bool
{
let data = string.data
return write(data: data)
}

public func write(data: Data) -> Bool
{
do
{
try self.socket.write(from: data)
}
catch
{
return false
}

return true
}
}
36 changes: 36 additions & 0 deletions Sources/TransmissionLinux/Listener.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// Listener.swift
//
//
// Created by Dr. Brandon Wiley on 8/31/20.
//

import Foundation
import Socket
import Chord

public class Listener
{
var socket: Socket

public init?(port: Int)
{
guard let socket = try? Socket.create() else {return nil}
self.socket = socket

do
{
try socket.listen(on: port)
}
catch
{
return nil
}
}

public func accept() -> Connection?
{
guard let newConnection = try? self.socket.acceptClientConnection(invokeDelegate: false) else {return nil}
return Connection(socket: newConnection)
}
}
47 changes: 47 additions & 0 deletions Tests/TransmissionLinuxTests/TransmissionLinuxTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import XCTest
import Foundation
@testable import TransmissionLinux

final class TransmissionTests: XCTestCase
{
public func testConnection()
{
let lock = DispatchGroup()
let queue = DispatchQueue(label: "testing")

lock.enter()

queue.async
{
self.runServer(lock)
}

lock.wait()

runClient()
}

func runServer(_ lock: DispatchGroup)
{
guard let listener = Listener(port: 1234) else {return}
lock.leave()

guard let connection = listener.accept() else {return}
let _ = connection.read(size: 4)
let _ = connection.write(string: "back")
}

func runClient()
{
let connection = Connection(host: "127.0.0.1", port: 1234)
XCTAssertNotNil(connection)

let writeResult = connection!.write(string: "test")
XCTAssertTrue(writeResult)

let result = connection!.read(size: 4)
XCTAssertNotNil(result)

XCTAssertEqual(result!, "back")
}
}

0 comments on commit a78c630

Please sign in to comment.