Skip to content

Commit

Permalink
Broadcast station #63: Renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
filimo committed Dec 13, 2019
1 parent 21610c8 commit 236ea06
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 39 deletions.
16 changes: 8 additions & 8 deletions ReaderTranslator.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
F02B04AF23A2844700F93B84 /* NWParameters.swift in Sources */ = {isa = PBXBuildFile; fileRef = F02B04AE23A2844700F93B84 /* NWParameters.swift */; };
F02B04B023A2844700F93B84 /* NWParameters.swift in Sources */ = {isa = PBXBuildFile; fileRef = F02B04AE23A2844700F93B84 /* NWParameters.swift */; };
F02B04B123A2844700F93B84 /* NWParameters.swift in Sources */ = {isa = PBXBuildFile; fileRef = F02B04AE23A2844700F93B84 /* NWParameters.swift */; };
F02B04B323A2847700F93B84 /* GameProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = F02B04B223A2847700F93B84 /* GameProtocol.swift */; };
F02B04B423A2847700F93B84 /* GameProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = F02B04B223A2847700F93B84 /* GameProtocol.swift */; };
F02B04B523A2847700F93B84 /* GameProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = F02B04B223A2847700F93B84 /* GameProtocol.swift */; };
F02B04B323A2847700F93B84 /* ReaderTranslatorProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = F02B04B223A2847700F93B84 /* ReaderTranslatorProtocol.swift */; };
F02B04B423A2847700F93B84 /* ReaderTranslatorProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = F02B04B223A2847700F93B84 /* ReaderTranslatorProtocol.swift */; };
F02B04B523A2847700F93B84 /* ReaderTranslatorProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = F02B04B223A2847700F93B84 /* ReaderTranslatorProtocol.swift */; };
F02B04B723A2894700F93B84 /* PeerBrowser.swift in Sources */ = {isa = PBXBuildFile; fileRef = F02B04B623A2894700F93B84 /* PeerBrowser.swift */; };
F02B04B823A2894700F93B84 /* PeerBrowser.swift in Sources */ = {isa = PBXBuildFile; fileRef = F02B04B623A2894700F93B84 /* PeerBrowser.swift */; };
F02B04B923A2894700F93B84 /* PeerBrowser.swift in Sources */ = {isa = PBXBuildFile; fileRef = F02B04B623A2894700F93B84 /* PeerBrowser.swift */; };
Expand Down Expand Up @@ -290,7 +290,7 @@
F026B1E72344830200E032B9 /* SafariView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SafariView.swift; sourceTree = "<group>"; };
F02989EB2360488800DF722E /* OpenPanel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OpenPanel.swift; sourceTree = "<group>"; };
F02B04AE23A2844700F93B84 /* NWParameters.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NWParameters.swift; sourceTree = "<group>"; };
F02B04B223A2847700F93B84 /* GameProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GameProtocol.swift; sourceTree = "<group>"; };
F02B04B223A2847700F93B84 /* ReaderTranslatorProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReaderTranslatorProtocol.swift; sourceTree = "<group>"; };
F02B04B623A2894700F93B84 /* PeerBrowser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PeerBrowser.swift; sourceTree = "<group>"; };
F02B04BA23A2896900F93B84 /* PeerListener.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PeerListener.swift; sourceTree = "<group>"; };
F02B04BE23A2898300F93B84 /* PeerConnection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PeerConnection.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -624,7 +624,7 @@
F02B04B623A2894700F93B84 /* PeerBrowser.swift */,
F02B04BA23A2896900F93B84 /* PeerListener.swift */,
F02B04BE23A2898300F93B84 /* PeerConnection.swift */,
F02B04B223A2847700F93B84 /* GameProtocol.swift */,
F02B04B223A2847700F93B84 /* ReaderTranslatorProtocol.swift */,
F04D5D3E23A3677800424479 /* ConnectionServerStatus.swift */,
F046182D23A3A4AF0041E726 /* ConnectionClientStatus.swift */,
);
Expand Down Expand Up @@ -1144,7 +1144,7 @@
F058C7F72397FA56002C84F0 /* WKCoordinator.swift in Sources */,
F058C7FC2397FAD0002C84F0 /* Safari.swift in Sources */,
F058C7F52397FA23002C84F0 /* WKCoordinatorNavigationDelegate.swift in Sources */,
F02B04B523A2847700F93B84 /* GameProtocol.swift in Sources */,
F02B04B523A2847700F93B84 /* ReaderTranslatorProtocol.swift in Sources */,
F08EBB09239564F3009025D3 /* AudioRateView.swift in Sources */,
F08EBB0723956495009025D3 /* RewindButtonsView.swift in Sources */,
F08EBB0B239565CC009025D3 /* PlayerContolsView.swift in Sources */,
Expand Down Expand Up @@ -1249,7 +1249,7 @@
F0505C552360D475004F2D50 /* LongmanRepresenter.swift in Sources */,
F0D2E335234BA49000D95994 /* Safari.swift in Sources */,
F0505C572360D485004F2D50 /* LongmanView.swift in Sources */,
F02B04B423A2847700F93B84 /* GameProtocol.swift in Sources */,
F02B04B423A2847700F93B84 /* ReaderTranslatorProtocol.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -1328,7 +1328,7 @@
F0FCDD482361A80C0016F23F /* CollinsRepresenter.swift in Sources */,
F0FCDD462361A8060016F23F /* CollinsView.swift in Sources */,
F0AA69DE232E97B0007CC07B /* SpeechSynthesizer.swift in Sources */,
F02B04B323A2847700F93B84 /* GameProtocol.swift in Sources */,
F02B04B323A2847700F93B84 /* ReaderTranslatorProtocol.swift in Sources */,
F0C4EDA92349271C00CCD97A /* ViewRepresentable.swift in Sources */,
F0C53178234B3EE1003174B5 /* WKCoordinator.swift in Sources */,
F0AA69E1232E97B0007CC07B /* PDFKitView.swift in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion ReaderTranslator/Extentions/NWParameters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ extension NWParameters {
self.includePeerToPeer = true

// Add your custom game protocol to support game messages.
let gameOptions = NWProtocolFramer.Options(definition: GameProtocol.definition)
let gameOptions = NWProtocolFramer.Options(definition: ReaderTranslatorProtocol.definition)
self.defaultProtocolStack.applicationProtocols.insert(gameOptions, at: 0)
}

Expand Down
10 changes: 5 additions & 5 deletions ReaderTranslator/Networking/PeerConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class PeerConnection {
}

// Create a message object to hold the command type.
let message = NWProtocolFramer.Message(gameMessageType: .selectedCharacter)
let message = NWProtocolFramer.Message(readerTranslatorMessageType: .selectedCharacter)
let context = NWConnection.ContentContext(identifier: "SelectCharacter",
metadata: [message])

Expand All @@ -114,7 +114,7 @@ class PeerConnection {
}

// Create a message object to hold the command type.
let message = NWProtocolFramer.Message(gameMessageType: .move)
let message = NWProtocolFramer.Message(readerTranslatorMessageType: .move)
let context = NWConnection.ContentContext(identifier: "Move",
metadata: [message])

Expand All @@ -134,9 +134,9 @@ class PeerConnection {

connection.receiveMessage { (content, context, _, error) in
// Extract your message type from the received context.
let gameMessage = context?.protocolMetadata(definition: GameProtocol.definition)
if let gameMessage = gameMessage as? NWProtocolFramer.Message {
self.delegate?.receivedMessage(content: content, message: gameMessage)
let message = context?.protocolMetadata(definition: ReaderTranslatorProtocol.definition)
if let message = message as? NWProtocolFramer.Message {
self.delegate?.receivedMessage(content: content, message: message)
}
if error == nil {
// Continue to receive more messages until you receive and error.
Expand Down
3 changes: 1 addition & 2 deletions ReaderTranslator/Networking/PeerListener.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ class PeerListener {
sharedConnection = PeerConnection(connection: newConnection, delegate: delegate)
} else {
// If a game is already in progress, reject it.
// newConnection.cancel()
sharedConnection = PeerConnection(connection: newConnection, delegate: delegate)
newConnection.cancel()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// GameProtocol.swift
// ReaderTranslatorProtocol.swift
// ReaderTranslator
//
// Created by Viktor Kushnerov on 12/12/19.
Expand All @@ -10,20 +10,20 @@ import Foundation
import Network

// Define the types of commands your game will use.
enum GameMessageType: UInt32 {
enum ReaderTranslatorMessageType: UInt32 {
case invalid = 0
case selectedCharacter = 1
case move = 2
}

// Create a class that implements a framing protocol.
class GameProtocol: NWProtocolFramerImplementation {
class ReaderTranslatorProtocol: NWProtocolFramerImplementation {

// Create a global definition of your game protocol to add to connections.
static let definition = NWProtocolFramer.Definition(implementation: GameProtocol.self)
static let definition = NWProtocolFramer.Definition(implementation: ReaderTranslatorProtocol.self)

// Set a name for your protocol for use in debugging.
static var label: String { return "TicTacToe" }
static var label: String { return "ReaderTranslator" }

// Set the default behavior for most framing protocol functions.
required init(framer: NWProtocolFramer.Instance) { }
Expand All @@ -39,10 +39,10 @@ class GameProtocol: NWProtocolFramerImplementation {
messageLength: Int,
isComplete: Bool) {
// Extract the type of message.
let type = message.gameMessageType
let type = message.readerTranslatorMessageType

// Create a header using the type and length.
let header = GameProtocolHeader(type: type.rawValue, length: UInt32(messageLength))
let header = ReaderTranslatorProtocolHeader(type: type.rawValue, length: UInt32(messageLength))

// Write the header.
framer.writeOutput(data: header.encodedData)
Expand All @@ -59,8 +59,8 @@ class GameProtocol: NWProtocolFramerImplementation {
func handleInput(framer: NWProtocolFramer.Instance) -> Int {
while true {
// Try to read out a single header.
var tempHeader: GameProtocolHeader?
let headerSize = GameProtocolHeader.encodedSize
var tempHeader: ReaderTranslatorProtocolHeader?
let headerSize = ReaderTranslatorProtocolHeader.encodedSize
let parsed = framer.parseInput(minimumIncompleteLength: headerSize,
maximumLength: headerSize) { (buffer, _) -> Int in
guard let buffer = buffer else {
Expand All @@ -69,7 +69,7 @@ class GameProtocol: NWProtocolFramerImplementation {
if buffer.count < headerSize {
return 0
}
tempHeader = GameProtocolHeader(buffer)
tempHeader = ReaderTranslatorProtocolHeader(buffer)
return headerSize
}

Expand All @@ -79,11 +79,11 @@ class GameProtocol: NWProtocolFramerImplementation {
}

// Create an object to deliver the message.
var messageType = GameMessageType.invalid
if let parsedMessageType = GameMessageType(rawValue: header.type) {
var messageType = ReaderTranslatorMessageType.invalid
if let parsedMessageType = ReaderTranslatorMessageType(rawValue: header.type) {
messageType = parsedMessageType
}
let message = NWProtocolFramer.Message(gameMessageType: messageType)
let message = NWProtocolFramer.Message(readerTranslatorMessageType: messageType)

// Deliver the body of the message, along with the message object.
if !framer.deliverInputNoCopy(length: Int(header.length), message: message, isComplete: true) {
Expand All @@ -95,27 +95,27 @@ class GameProtocol: NWProtocolFramerImplementation {

// Extend framer messages to handle storing your command types in the message metadata.
extension NWProtocolFramer.Message {
convenience init(gameMessageType: GameMessageType) {
self.init(definition: GameProtocol.definition)
self.gameMessageType = gameMessageType
convenience init(readerTranslatorMessageType: ReaderTranslatorMessageType) {
self.init(definition: ReaderTranslatorProtocol.definition)
self.readerTranslatorMessageType = readerTranslatorMessageType
}

var gameMessageType: GameMessageType {
var readerTranslatorMessageType: ReaderTranslatorMessageType {
get {
if let type = self["GameMessageType"] as? GameMessageType {
if let type = self["ReaderTranslatorMessageType"] as? ReaderTranslatorMessageType {
return type
} else {
return .invalid
}
}
set {
self["GameMessageType"] = newValue
self["ReaderTranslatorMessageType"] = newValue
}
}
}

// Define a protocol header struct to help encode and decode bytes.
struct GameProtocolHeader: Codable {
struct ReaderTranslatorProtocolHeader: Codable {
let type: UInt32
let length: UInt32

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,14 @@ extension StatusBarView_Listener.Coordinator: PeerConnectionDelegate {
status = .failed(error: .connection(text: "connection failed"))
}
func receivedMessage(content: Data?, message: NWProtocolFramer.Message) {
print(2)
guard let content = content else { return }
print(content)
logMessage(content)
}
func logMessage(_ content: Data) {
if let text = String(data: content, encoding: .unicode) {
print(text)
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion ReaderTranslatorMac/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<key>CFBundleShortVersionString</key>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>1150</string>
<string>1169</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.education</string>
<key>LSMinimumSystemVersion</key>
Expand Down
1 change: 1 addition & 0 deletions ReaderTranslatorPlayer/Views/HostsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ extension HostsView.Coordinator: PeerBrowserDelegate {
extension HostsView.Coordinator: PeerConnectionDelegate {
func connectionReady() {
status = .connected
sharedConnection?.sendMove("Let's go")
}

func connectionFailed() {
Expand Down
2 changes: 1 addition & 1 deletion ReaderTranslatorSafari/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<key>CFBundleShortVersionString</key>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>1848</string>
<string>1870</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>NSAppleEventsUsageDescription</key>
Expand Down

0 comments on commit 236ea06

Please sign in to comment.