Skip to content

Commit

Permalink
Added RPC capability
Browse files Browse the repository at this point in the history
  • Loading branch information
Dr. Brandon Wiley committed Aug 19, 2019
1 parent 941fceb commit 47a31b9
Show file tree
Hide file tree
Showing 18 changed files with 1,217 additions and 44 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/.build
/Packages
/*.xcodeproj
query
12 changes: 6 additions & 6 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
"object": {
"pins": [
{
"package": "bocho",
"repositoryURL": "https://github.com/yanagiba/bocho",
"package": "Datable",
"repositoryURL": "https://github.com/OperatorFoundation/Datable",
"state": {
"branch": null,
"revision": "a02d4faf11539b121cc007210a0141478fa3d924",
"version": "0.1.0"
"revision": "eccbedd3c89007691034b951a1fc0780e54701ea",
"version": "1.1.1"
}
},
{
"package": "swift-ast",
"repositoryURL": "https://github.com/yanagiba/swift-ast",
"state": {
"branch": null,
"revision": "87888a78bc264a57536e6c628f4ad4ad02eadd06",
"version": "0.4.1"
"revision": "6656a3dde2e5876818a83c978292687e43620cd1",
"version": "0.19.6"
}
}
]
Expand Down
35 changes: 33 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,52 @@ let package = Package(
.library(
name: "Song",
targets: ["Song"]),
.library(
name: "Composition",
targets: ["Composition"]),
.library(
name: "Chorus",
targets: ["Chorus"]),
.library(
name: "Package",
targets: ["Package"]),
.executable(
name: "compose",
targets: ["compose"]),
.executable(
name: "choir",
targets: ["choir"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
.package(url: "https://github.com/yanagiba/swift-ast", from: "0.4.1")
.package(url: "https://github.com/yanagiba/swift-ast", from: "0.4.2"),
.package(url: "https://github.com/OperatorFoundation/Datable", from: "1.1.1"),
],
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: "Song",
dependencies: ["SwiftAST"]),
.target(
name: "Composition",
dependencies: ["Datable"]),
.target(
name: "Chorus",
dependencies: []),
.target(
name: "Package",
dependencies: []),
.target(
name: "compose",
dependencies: ["Composition", "SwiftAST"]),
.target(
name: "choir",
dependencies: ["Chorus", "Datable", "Package", "SwiftAST"]),
.testTarget(
name: "SongTests",
dependencies: ["Song"]),
dependencies: ["Song", "Composition"]),
],
swiftLanguageVersions: [4]
)
11 changes: 11 additions & 0 deletions Resources/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="11134" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="11134"/>
</dependencies>
<objects>
<customObject id="-2" userLabel="File's Owner"/>
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
<customObject id="-3" userLabel="Application" customClass="NSObject"/>
</objects>
</document>
Empty file added Sources/Chorus/Chorus.swift
Empty file.
48 changes: 48 additions & 0 deletions Sources/Composition/Composition.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import Foundation
import Network
import Datable

public func remoteCall(code: String, callback: @escaping (Data) -> Void )
{
let queue = DispatchQueue(label: "network")

let conn = NWConnection(host: NWEndpoint.Host("127.0.0.1"), port: NWEndpoint.Port(integerLiteral: 1234), using: .tcp)
conn.start(queue: queue)

let payload = code.data
let length = payload.count
let length16 = UInt16(length)
let lengthBytes = length16.data
let data = lengthBytes + payload

conn.send(content: data, completion: NWConnection.SendCompletion.contentProcessed(
{
maybeError in

guard maybeError == nil else { return }

conn.receive(minimumIncompleteLength: 2, maximumLength: 2)
{
(maybeData, maybeContext, isComplete, maybeError) in

print("received \(maybeData)")

guard maybeError == nil else { return }
guard let data = maybeData else { return }

let length = Int(data.uint16)

conn.receive(minimumIncompleteLength: length, maximumLength: length)
{
(maybeData, maybeContext, isComplete, maybeError) in

print("received.. \(maybeData)")

guard maybeError == nil else { return }
guard let data = maybeData else { return }

callback(data)
}
}
}))
}
78 changes: 78 additions & 0 deletions Sources/Package/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import Foundation

public struct Package
{
public let name: String
public let type: PackageType

public init(name: String, type: PackageType = .library)
{
self.name = name
self.type = type

guard let _ = try? FileManager.default.createDirectory(atPath: self.name, withIntermediateDirectories: false, attributes: nil) else { return }

let process = Process()
process.currentDirectoryPath = self.name
process.launchPath = "/usr/bin/swift"

switch self.type
{
case .library:
process.arguments = ["package", "init", "--type", "library"]
case .executable:
process.arguments = ["package", "init", "--type", "executable"]
}

process.launch()
process.waitUntilExit()
}

public func update()
{
let process = Process()
process.currentDirectoryPath = self.name
process.launchPath = "/usr/bin/swift"

process.arguments = ["package", "update"]

process.launch()
process.waitUntilExit()
}

public func build()
{
let process = Process()
process.currentDirectoryPath = self.name
process.launchPath = "/usr/bin/swift"

process.arguments = ["build", "-Xswiftc", "-target", "-Xswiftc", "x86_64-apple-macosx10.14"]

process.launch()
process.waitUntilExit()
}

public func run() -> Data
{
let process = Process()
process.currentDirectoryPath = self.name
process.launchPath = "/usr/bin/swift"

process.arguments = ["run"]
let output = Pipe()
process.standardOutput = output
let handle = output.fileHandleForReading

process.launch()
process.waitUntilExit()

let data = handle.readDataToEndOfFile()
return data
}
}

public enum PackageType
{
case library
case executable
}
4 changes: 2 additions & 2 deletions Sources/Song/Decoding/SongDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class SongDecoder {

func decode<T>(_ type: [T].Type, from data: Data) throws -> [T] where T : Decodable {
NSLog("ARRAY DECODE CHOSEN")
NSLog("decode<T> data: \(data as! NSData)")
NSLog("decode<T> data: \(data as NSData)")
self.data=data
let container = self._unkeyedContainer()
do {
Expand Down Expand Up @@ -187,7 +187,7 @@ public class SongDecoder {

func decode<T>(_ type: T.Type, from data: Data) throws -> T where T : Decodable
{
NSLog("decode<T> data: \(data as! NSData)")
NSLog("decode<T> data: \(data as NSData)")
self.data=data
if singleType(type) {
let single = self._singleValueContainer()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ public class SongSingleValueDecodingContainer: SingleValueDecodingContainer {

switch ini {
case is PatternInitializer:
let pat = ini as! PatternInitializer
let pat = ini
let maybeEx = pat.initializerExpression
guard let ex = maybeEx else {
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: codingPath, debugDescription: "Missing initializer expression"))
Expand Down
12 changes: 6 additions & 6 deletions Sources/Song/Encoding/SongEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class SongEncoder {
if let ints = value as? [Int] {
let list = self._unkeyedContainer()
do {
list.type=TypeIdentifier(names: [TypeIdentifier.TypeName(name: "Int")])
list.type=TypeIdentifier(names: [TypeIdentifier.TypeName(name: Identifier.name("Int"))])
for int in ints {
try list.encode(int)
}
Expand All @@ -53,7 +53,7 @@ public class SongEncoder {
} else if let ints = value as? [Int64] {
let list = self._unkeyedContainer()
do {
list.type=TypeIdentifier(names: [TypeIdentifier.TypeName(name: "Int64")])
list.type=TypeIdentifier(names: [TypeIdentifier.TypeName(name: Identifier.name("Int64"))])
for int in ints {
try list.encode(int)
}
Expand All @@ -65,7 +65,7 @@ public class SongEncoder {
} else if let ints = value as? [Int32] {
let list = self._unkeyedContainer()
do {
list.type=TypeIdentifier(names: [TypeIdentifier.TypeName(name: "Int32")])
list.type=TypeIdentifier(names: [TypeIdentifier.TypeName(name: Identifier.name("Int32"))])
for int in ints {
try list.encode(int)
}
Expand All @@ -77,7 +77,7 @@ public class SongEncoder {
} else if let ints = value as? [Int16] {
let list = self._unkeyedContainer()
do {
list.type=TypeIdentifier(names: [TypeIdentifier.TypeName(name: "Int16")])
list.type=TypeIdentifier(names: [TypeIdentifier.TypeName(name: Identifier.name("Int16"))])
for int in ints {
try list.encode(int)
}
Expand All @@ -89,7 +89,7 @@ public class SongEncoder {
} else if let ints = value as? [Int8] {
let list = self._unkeyedContainer()
do {
list.type=TypeIdentifier(names: [TypeIdentifier.TypeName(name: "Int8")])
list.type=TypeIdentifier(names: [TypeIdentifier.TypeName(name: Identifier.name("Int8"))])
for int in ints {
try list.encode(int)
}
Expand All @@ -101,7 +101,7 @@ public class SongEncoder {
} else {
let list = self._unkeyedContainer()
do {
list.type=TypeIdentifier(names: [TypeIdentifier.TypeName(name: String(describing: Value.self))])
list.type=TypeIdentifier(names: [TypeIdentifier.TypeName(name: Identifier.name(String(describing: Value.self)))])
for val in value {
try list.encode(val)
}
Expand Down
Loading

0 comments on commit 47a31b9

Please sign in to comment.