-
Notifications
You must be signed in to change notification settings - Fork 3
[Refactor] NearbyNetwork 참여 요청, 수락 기능 리팩터링 #150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1ecd47e
0866dad
9e1cc6b
523b686
9c2bd93
c4b30e2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,31 +5,45 @@ | |
| // Created by 최정인 on 12/31/24. | ||
| // | ||
|
|
||
| import DataSource | ||
| import Foundation | ||
| import Network | ||
| import OSLog | ||
|
|
||
| public protocol NearbyNetworkBrowserDelegate: AnyObject { | ||
| func nearbyNetworkBrowserDidFindPeer( | ||
| _ sender: NearbyNetworkBrowser, | ||
| hostName: String, | ||
| connectedPeerInfo: [String]) | ||
| foundPeers: [RefactoredNetworkConnection]) | ||
| } | ||
|
|
||
| public final class NearbyNetworkBrowser { | ||
| private let nwBrowser: NWBrowser | ||
| private let browserQueue: DispatchQueue | ||
| private let serviceType: String | ||
| private let logger: Logger | ||
| private var foundPeers: [RefactoredNetworkConnection: NWEndpoint] { | ||
| didSet { | ||
| let foundPeers = foundPeers | ||
| .keys | ||
| .sorted(by: { $0.name < $1.name }) | ||
| delegate?.nearbyNetworkBrowserDidFindPeer(self, foundPeers: Array(foundPeers)) | ||
| } | ||
| } | ||
| weak var delegate: NearbyNetworkBrowserDelegate? | ||
|
|
||
| init(serviceType: String) { | ||
| let option = NWProtocolFramer.Options(definition: NearbyNetworkProtocol.definition) | ||
| let parameter = NWParameters.tcp | ||
| parameter.defaultProtocolStack | ||
| .applicationProtocols | ||
| .insert(option, at: 0) | ||
| nwBrowser = NWBrowser( | ||
| for: .bonjourWithTXTRecord(type: serviceType, domain: nil), | ||
| using: .tcp) | ||
| using: parameter) | ||
|
Comment on lines
+35
to
+42
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 기존에 .tcp로 넣던거를 parameter를 통해 옵션을 추가 해주는 방식으로 변경한 것으로 이해가 됩니다!
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 구현한 커스텀 프로토콜이 적용된 파라미터를 사용할 수 있게 됩니다. ! 그럴 경우 이제 데이터를 주고 받을 때 헤더를 포함하여 데이터를 송수신할 수 있고 바뀐 코드도 tcp를 사용중이지만 커스텀 프로토콜을 추가해 헤더를 사용할 수 있게 한 것이 다른 부분이라고 이해하시면 될 것 같습니다 ! let parameter = NWParameters.tcp |
||
| self.browserQueue = DispatchQueue.global() | ||
| self.serviceType = serviceType | ||
| self.logger = Logger() | ||
| self.foundPeers = [:] | ||
| configure() | ||
| } | ||
|
|
||
|
|
@@ -38,30 +52,51 @@ public final class NearbyNetworkBrowser { | |
| } | ||
|
|
||
| private func browserHandler(results: Set<NWBrowser.Result>, changes: Set<NWBrowser.Result.Change>) { | ||
| for result in results { | ||
| switch result.metadata { | ||
| case .bonjour(let foundedPeerData): | ||
| let dictionary = foundedPeerData.dictionary | ||
| guard | ||
| let hostName = dictionary[NearbyNetworkKey.host.rawValue], | ||
| let connectedPeerInfo = dictionary[NearbyNetworkKey.connectedPeerInfo.rawValue] | ||
| else { | ||
| logger.log(level: .error, "connection의 데이터 값이 유효하지 않습니다.") | ||
| return | ||
| } | ||
|
|
||
| delegate?.nearbyNetworkBrowserDidFindPeer( | ||
| self, | ||
| hostName: hostName, | ||
| connectedPeerInfo: connectedPeerInfo | ||
| .split(separator: ",") | ||
| .map { String($0) }) | ||
| for change in changes { | ||
| switch change { | ||
| case .added(let result): | ||
| guard let foundPeer = convertMetadata(metadata: result.metadata) else { return } | ||
| foundPeers[foundPeer] = result.endpoint | ||
| case .removed(let result): | ||
| guard let foundPeer = convertMetadata(metadata: result.metadata) else { return } | ||
| foundPeers[foundPeer] = nil | ||
| default: | ||
| logger.log(level: .error, "알 수 없는 피어가 발견되었습니다.") | ||
| break | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private func convertMetadata(metadata: NWBrowser.Result.Metadata) -> RefactoredNetworkConnection? { | ||
| switch metadata { | ||
| case .bonjour(let foundedPeerData): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 사용자(client)가 host와 connection을 맺기 위해서는 호스트의 ip와 port 번호를 알고 있어야 합니다. 하지만 저희가 구현하는 서비스에서는 당연히 그걸 알기 쉽지 않죠. 따라서 MPC의 advertiser처럼, host는 자신의 정보를 주변에 알려야 합니다. MPC의 advertiser의 역할을 Network 프레임워크에서는 NWListener가 수행합니다.
Listener가 봉주르 서비스를 이용하여 주변에게 advertising을 할 때, 본인의 정보를 한편, MPC의 browser의 역할은 Network framework에서 NWBrowser가 수행합니다. 브라우저가 특정 host의 정보를 찾으면. result라는 객체를 통해 host의 endpoint를 알 수 있습니다. 또한 만약 host가 위에서 말씀드린
따라서 metadata가 bonjour인 경우는 listner가 본인의 정보를 추가적으로 담아 광고하고 있는 경우라고 보시면 될 것 같습니다!
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 설명 감사합니다!! |
||
| let dictionary = foundedPeerData.dictionary | ||
| guard | ||
| let peerIDString = dictionary[NearbyNetworkKey.peerID.rawValue], | ||
| let peerID = UUID(uuidString: peerIDString), | ||
| let hostName = dictionary[NearbyNetworkKey.host.rawValue], | ||
| let connectedPeerInfo = dictionary[NearbyNetworkKey.connectedPeerInfo.rawValue] | ||
|
Comment on lines
+76
to
+77
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 앞 pr에서 정의 했던 내용들을 이런 방식으로 사용되는 군요.. |
||
| else { | ||
| logger.log(level: .error, "connection의 데이터 값이 유효하지 않습니다.") | ||
| return nil | ||
| } | ||
|
|
||
| let foundPeer = RefactoredNetworkConnection( | ||
| id: peerID, | ||
| name: hostName, | ||
| connectedPeerInfo: connectedPeerInfo | ||
| .split(separator: ",") | ||
| .map { String($0) }) | ||
| return foundPeer | ||
| default: | ||
| logger.log(level: .error, "알 수 없는 피어가 발견되었습니다.") | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| func fetchFoundConnection(networkConnection: RefactoredNetworkConnection) -> NWEndpoint? { | ||
| return foundPeers[networkConnection] | ||
| } | ||
|
|
||
| func startSearching() { | ||
| nwBrowser.start(queue: browserQueue) | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
| // | ||
|
|
||
| public enum NearbyNetworkKey: String { | ||
| case peerID | ||
| case host | ||
| case connectedPeerInfo | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이젠 연결 끊겼을 때도 관리 해주겠군요..! 좋습니다 점점 앱다워지는 것 같아요!