Created
August 11, 2014 16:27
-
-
Save ansonl/9a98df84942e1f14b2cd to your computer and use it in GitHub Desktop.
multipeer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// NMPeerToPeer.m | |
// Next Meal | |
// | |
// Created by Anson Liu on 6/7/14. | |
// Copyright (c) 2014 Apparent Etch. All rights reserved. | |
// | |
#import "NMPeerToPeer.h" | |
@implementation NMPeerToPeer | |
- (NMPeerToPeer*)initWithTarget:(id)obj { | |
self = [super init]; | |
if (self) { | |
target = obj; | |
} | |
localPeerID = [[MCPeerID alloc] initWithDisplayName:[[UIDevice currentDevice] name]]; | |
//MCNearbyServiceAdvertiser *advertiser = [[MCNearbyServiceAdvertiser alloc] initWithPeer:localPeerID discoveryInfo:[NSDictionary dictionaryWithObject:[NSKeyedUnarchiver unarchiveObjectWithData:[ReadWriteData readFile:lastUpdatedFilename]] forKey:@"last-updated"] serviceType:XXServiceType]; | |
MCNearbyServiceAdvertiser *advertiser = [[MCNearbyServiceAdvertiser alloc] initWithPeer:localPeerID discoveryInfo:nil serviceType:XXServiceType]; | |
advertiser.delegate = self; | |
[advertiser startAdvertisingPeer]; | |
NSLog(@"advertser started"); | |
MCNearbyServiceBrowser* browser = [[MCNearbyServiceBrowser alloc] initWithPeer:localPeerID serviceType:XXServiceType]; | |
browser.delegate = self; | |
[browser startBrowsingForPeers]; | |
NSLog(@"browser started"); | |
return self; | |
} | |
#pragma mark - MCNearbyServerAdvertiserDelegate Methods | |
- (void)advertiser:(MCNearbyServiceAdvertiser *)advertiser didNotStartAdvertisingPeer:(NSError *)error { | |
NSLog(@"advertiser did not start advertising due to error %@", [error localizedDescription]); | |
} | |
- (void)advertiser:(MCNearbyServiceAdvertiser *)advertiser didReceiveInvitationFromPeer:(MCPeerID *)peerID withContext:(NSData *)context invitationHandler:(void (^)(BOOL, MCSession *))invitationHandler { | |
NSLog(@"advertiser recieved invitation from peer %@", peerID); | |
MCSession* session = [[MCSession alloc] initWithPeer:localPeerID securityIdentity:nil encryptionPreference:MCEncryptionNone]; | |
session.delegate = self; | |
invitationHandler(YES, session); | |
NSError* error; | |
[session sendData:[ReadWriteData readFile:menuFilename] toPeers:[NSArray arrayWithObject:localPeerID] withMode:MCSessionSendDataUnreliable error:&error]; | |
} | |
#pragma mark - MCNearbyServerBrowserDelegate Methods | |
- (void)browser:(MCNearbyServiceBrowser *)browser didNotStartBrowsingForPeers:(NSError *)error { | |
NSLog(@"browser did not start browsing due to error %@", [error localizedDescription]); | |
} | |
- (void)browser:(MCNearbyServiceBrowser *)browser foundPeer:(MCPeerID *)peerID withDiscoveryInfo:(NSDictionary *)info { | |
NSLog(@"browser found peer %@", peerID); | |
//if ([info objectForKey:@"last-updated"] && [[info objectForKey:@"last-updated"] timeIntervalSinceDate:[NSKeyedUnarchiver unarchiveObjectWithData:[ReadWriteData readFile:lastUpdatedFilename]]] > updateInterval) { //local menu is too old | |
NSLog(@"found peer has updated menus"); | |
MCSession* session = [[MCSession alloc] initWithPeer:localPeerID securityIdentity:nil encryptionPreference:MCEncryptionNone]; | |
session.delegate = self; | |
[browser invitePeer:peerID toSession:session withContext:nil timeout:15]; | |
//} | |
} | |
- (void)browser:(MCNearbyServiceBrowser *)browser lostPeer:(MCPeerID *)peerID { | |
NSLog(@"Peer %@ lost", peerID); | |
} | |
#pragma mark - MCSessionDelegate Methods | |
- (void)session:(MCSession *)session didReceiveData:(NSData *)data fromPeer:(MCPeerID *)peerID { | |
NSLog(@"recieved data from %@", peerID); | |
/* | |
[ReadWriteData saveFile:data withFilename:menuFilename]; | |
[ReadWriteData saveFile:[NSKeyedArchiver archivedDataWithRootObject:[NSDate date]] withFilename:lastUpdatedFilename]; | |
*/ | |
/* | |
if ([target respondsToSelector:@selector(loadLocalData:)]) | |
[target performSelector:@selector(loadLocalData:) withObject:NO]; | |
*/ | |
[session disconnect]; | |
} | |
- (void)session:(MCSession *)session didStartReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID withProgress:(NSProgress *)progress { | |
} | |
- (void)session:(MCSession *)session didFinishReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID atURL:(NSURL *)localURL withError:(NSError *)error { | |
} | |
- (void)session:(MCSession *)session didReceiveStream:(NSInputStream *)stream withName:(NSString *)streamName fromPeer:(MCPeerID *)peerID { | |
} | |
- (void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state { | |
NSLog(@"session state change with peer %@ and state %d", peerID, state); | |
if (state == MCSessionStateNotConnected) { | |
[session disconnect]; | |
} | |
} | |
- (void) session:(MCSession *)session didReceiveCertificate:(NSArray *)certificate fromPeer:(MCPeerID *)peerID certificateHandler:(void (^)(BOOL accept))certificateHandler | |
{ | |
certificateHandler(YES); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment