-
Notifications
You must be signed in to change notification settings - Fork 1
/
SharedKeys.swift
144 lines (118 loc) · 4.01 KB
/
SharedKeys.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//
// SharedKeys.swift
// tvOSGame
//
// Created by Paul Jones on 26/10/2015.
// Copyright © 2015 Fluid Pixel. All rights reserved.
//
import Foundation
import UIKit
let SERVICE_NAME = "_probonjore._tcp."
let NET_SERVICE_NAME = "com.fpstudios.iPhone-controller Lauren"
let CURRENT_DEVICE_VENDOR_ID:String = UIDevice.currentDevice().identifierForVendor!.UUIDString
enum MessageDirection : CustomStringConvertible {
case Incoming
case Outgoing
var description: String {
switch self {
case .Incoming: return "Incoming"
case .Outgoing: return "Outgoing"
}
}
}
enum MessageType : String {
static let cases = [Message, Broadcast, Reply, RequestDeviceID, SendingDeviceID]
case Message = "kMessage"
case Broadcast = "kBroadcast"
case Reply = "kReply"
case RequestDeviceID = "kRequestDeviceID"
case SendingDeviceID = "kSendingDeviceID"
}
extension MessageType : CustomStringConvertible {
var description: String {
switch self {
case .Message: return "Message"
case .Broadcast: return "Broadcast"
case .Reply: return "Reply"
case .RequestDeviceID: return "RequestDeviceID"
case .SendingDeviceID: return "SendingDeviceID"
// default: return self.rawValue.substringFromIndex(self.rawValue.startIndex.successor())
}
}
}
struct Message {
let direction:MessageDirection
let type:MessageType
let senderDeviceID:String
let targetDeviceID:String?
let replyID:Int?
let contents:[String:AnyObject]?
var isForThisDevice:Bool {
if let targetDeviceID = self.targetDeviceID {
return targetDeviceID == CURRENT_DEVICE_VENDOR_ID
}
return true
}
init(type: MessageType, replyID: Int? = nil, contents: [String:AnyObject]? = nil, targetDeviceID: String? = nil) {
self.type = type
self.senderDeviceID = CURRENT_DEVICE_VENDOR_ID
self.targetDeviceID = targetDeviceID
self.replyID = replyID
self.contents = contents
self.direction = .Outgoing
}
var dictionary:[String:AnyObject] {
var rv:[String:AnyObject] = ["senderDeviceID":senderDeviceID]
if let contents = self.contents {
rv[type.rawValue] = contents
}
else {
rv[type.rawValue] = type.rawValue
}
if let targetDeviceID = self.targetDeviceID {
rv["targetDeviceID"] = targetDeviceID
}
if let replyID = self.replyID {
rv["replyID"] = replyID
}
return rv
}
var data:NSData {
return NSKeyedArchiver.archivedDataWithRootObject(self.dictionary)
}
init?(dictionary:[String:AnyObject]) {
self.direction = .Incoming
self.senderDeviceID = dictionary["senderDeviceID"] as! String
self.targetDeviceID = dictionary["targetDeviceID"] as? String
self.replyID = dictionary["replyID"] as? Int
for type in MessageType.cases {
if let object = dictionary[type.rawValue] {
self.type = MessageType(rawValue: type.rawValue)!
if let text = object as? String where text == type.rawValue {
self.contents = nil
return
}
else if let message = object as? [String:AnyObject] {
self.contents = message
return
}
break
}
}
return nil
}
init?(data:NSData) {
if let object = try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data) {
if let dictionary = object as? [String:AnyObject] {
self.init(dictionary: dictionary)
return
}
}
return nil
}
}
extension GCDAsyncSocket {
func sendMessageObject(message:Message, withTimeout: NSTimeInterval = -1.0) {
self.writeData(message.data, withTimeout: withTimeout, tag: 0)
}
}