Loading presentation...

Present Remotely

Send the link below via email or IM

Copy

Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

DeleteCancel

Make your likes visible on Facebook?

Connect your Facebook account to Prezi and let your likes appear on your timeline.
You can change this under Settings & Account at any time.

No, thanks

Making Devices Talk with Peer to Peer Networking

Presentation for AUC's /dev/world conference: September 29-30, 2014, Melbourne, Australia. http://devworld.com.au
by

Judit Klein

on 23 March 2015

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Making Devices Talk with Peer to Peer Networking

Session Phase
Making Devices Talk
With Peer to Peer Networking

Super easy
MultipeerConnectivity - basic view controller

Easy-ish
MultipeerConnectivity - DIY
Protocols

Medium
MultipeerConnectivity - custom discovery

Hard
Roll your own custom framework! - Here be dragons
Agenda
Judit Klein
Facilitating the Connection
Interpreting the Information
Creative Technologist / AUT University / Prezi
Why?
SGVsbG8gaG93IGFyZSB5b3U/
V2hhdCdzIHlvdXIgbmFtZT8=
What about our green bubble friends?
Super - easy
Easy - ish
Medium
Hard
Multipeer Connectivity
Multipeer Connectivity
DEMO
Multipeer Connectivity
Roll Your Own
Using it out of the box
Custom discovery
Handling it yourself
JKPeerConnectivity
'" I'll write my own framework! With Blackjack! And.... "'
link to demo code:
COMING SOON
Thank you!
Discovery Phase
Discovery Phase
// set up peer ID
self.myPeerID = [[MCPeerID alloc] initWithDisplayName:[UIDevice currentDevice].name];

// set up session
self.mySession = [[MCSession alloc] initWithPeer:self.myPeerID];
self.mySession.delegate = self;


// set up BrowserViewController
self.browserVC = [[MCBrowserViewController alloc] initWithServiceType:@"myUniqueApp"
session:self.mySession];


// set up advertiser
self.advertiser = [[MCAdvertiserAssistant alloc] initWithServiceType:@"myUniqueApp"
discoveryInfo:nil
session:self.mySession];
[self.advertiser start];

self.browserVC.delegate = self;

[self presentViewController:self.browserVC animated:YES completion:nil];

//receive data
- (void)session:(MCSession *)session didReceiveData:(NSData *)data
fromPeer:(MCPeerID *)peerID






//receive byte stream
- (void)session:(MCSession *)session didReceiveStream:(NSInputStream *)stream
withName:(NSString *)streamName
fromPeer:(MCPeerID *)peerID

//receive resource
- (void)session:(MCSession *)session didStartReceivingResourceWithName:(NSString *)resourceName
fromPeer:(MCPeerID *)peerID
withProgress:(NSProgress *)progress

[self.mySession sendData:data
toPeers:[self.mySession connectedPeers]
withMode:MCSessionSendDataUnreliable
error:&error];
{
NSString *message = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
}
//send data
NSString *message = @"Foo";
NSData *data = [message dataUsingEncoding:NSUTF8StringEncoding];
Protocols
Hello!
My name is Judit.
//saying hello
NSDictionary *myDict = [[NSDictionary alloc]initWithObjectsAndKeys:@kIncomingMessageTypeHello,@"messageType",
peerID,@"peerName",
realName,@"realName",
displayAvatar,@"displayAvatar"
,nil];

NSData *myData = [NSKeyedArchiver archivedDataWithRootObject:myDict];

[session sendData:myData toPeers:peersToSayHelloTo withMode:MCSessionSendDataReliable error:nil];
#define kIncomingMessageTypeHello 1
#define kIncomingMessageTypeChatImage 3
#define kIncomingMessageTypeChatText 2
//Somebody Is Saying Hello
case kIncomingMessageTypeHello:
{
NSString *peerName = [incomingDict objectForKey:@"peerName"];
NSString *realName = [incomingDict objectForKey:@"realName"];
UIImage *displayAvatar = [incomingDict objectForKey:@"displayAvatar"];
}
// set up peer ID
self.myPeerID = [[MCPeerID alloc] initWithDisplayName:[UIDevice currentDevice].name];

// set up session
self.mySession = [[MCSession alloc] initWithPeer:self.myPeerID];
self.mySession.delegate = self;
[self.serviceAdvertiser startAdvertisingPeer];
[self.serviceBrowser startBrowsingForPeers];
- (void)browser:(MCNearbyServiceBrowser *)browser foundPeer:(MCPeerID *)peerID
withDiscoveryInfo:(NSDictionary *)info

if (shouldInvite)
{
[browser invitePeer:peerID
toSession:self.session
withContext:nil
timeout:10];
} else {

//wait for invitation


}
BOOL shouldInvite = ([self.myPeerID.displayName compare:peerID.displayName]==NSOrderedDescending);
- (void)advertiser:(MCNearbyServiceAdvertiser *)advertiser didReceiveInvitationFromPeer:(MCPeerID *)peerID
withContext:(NSData *)context
invitationHandler:(void(^)(BOOL accept, MCSession *session))invitationHandler
{


}

invitationHandler(YES, self.session);
Peer to Peer vs. Client Server
"Nearby Networking"
Bluetooth

Infrastructure Wi-Fi

Ad-hoc Wi-Fi
Networking 101
Making things talk
Browser
Advertiser
I am searching for nearby devices
Hello! I am here!
Facilitating the connection
Interpreting the information
Facilitating the connection

// set up nearbyServiceBrowser
self.serviceBrowser = [[MCNearbyServiceBrowser alloc] initWithPeer:myPeerID
serviceType:@"myUniqueApp"];
serviceBrowser.delegate = self;


// set up nearbyServiceAdvertiser
self.serviceAdvertiser = [[MCNearbyServiceAdvertiser alloc] initWithPeer:myPeerID
discoveryInfo:nil
serviceType:@"myUniqueApp"];
serviceAdvertiser.delegate = self;
Session Phase
//interpret the data
NSDictionary *incomingDict = (NSDictionary* )[NSKeyedUnarchiver unarchiveObjectWithData:incomingData];

NSInteger messageType = [[incomingDict valueForKey:@"messageType"]integerValue];

switch (messageType) {

case kIncomingMessageTypeChatText:
{
NSString *chatMessage = [incomingDict objectForKey:@"chatMessage"];
}
break;
}
//send data
NSDictionary *myDict = [[NSDictionary alloc]initWithObjectsAndKeys:@kIncomingMessageTypeChatText, @"messageType",
chatMessage,@"chatMessage"
,nil];

NSData *myData = [NSKeyedArchiver archivedDataWithRootObject:myDict];

[session sendData:myData toPeers:[session connectedPeers] withMode:MCSessionSendDataReliable error:nil];
Interpreting the information
//receive data
- (void)session:(MCSession *)session didReceiveData:(NSData *)data
fromPeer:(MCPeerID *)peerID
Limitations
- No interface control
- Not cross platform
- Inconsistent speed, slow
- Ghost Peers, Old Data
- Maximum connection limit (8)
Multipeer Connectivity is great, but...
WWDC 2014
session 709
WWDC 2013
session 708
By the way...
watch this space
JKPeerConnectivity
Multipeer Connectivity
So what?
Conclusion
Networking is not easy but is important
Multipeer Connectivity is easy but not highly customisable
Custom frameworks are more difficult but highly customisable
@juditk
jklein@aut.ac.nz
Facilitating the Connection
Interpreting the Information
Custom
// create new instance of netService
self.netService = [[NSNetService alloc]
initWithDomain:@"" type:@"_uniqueAppName._tcp."
name:deviceName port:self.port];
// Add the device discovery information
NSDictionary *txtRecordDataDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[[JKPeerConnectivitySetup sharedSetup]myUniqueID],@"DeviceID",
[[JKPeerConnectivitySetup sharedSetup]groupIdentifier],@"DeviceBeacon",
@"iOS",@"DeviceType",
@"1",@"AppVersion",
nil];

netServiceBrowser = [[NSNetServiceBrowser alloc] init];
netServiceBrowser.delegate = self;
[netServiceBrowser searchForServicesOfType:@"_heartchat._tcp." inDomain:@"local"];

[netServiceBrowser setIncludesPeerToPeer:YES];
- (void)netServiceBrowser:(NSNetServiceBrowser *)netServiceBrowser didFindService:(NSNetService *)netService
moreComing:(BOOL)moreServicesComing
[netService resolveWithTimeout:5.0];
- (void)netServiceDidResolveAddress:(NSNetService *)sender
public void discoverServices()
{

mNsdManager.discoverServices(

SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener);

}
public void registerService(int port)
{

NsdServiceInfo serviceInfo = new NsdServiceInfo();
serviceInfo.setPort(port);
serviceInfo.setServiceName(mServiceName);
serviceInfo.setServiceType(SERVICE_TYPE);

mNsdManager.registerService(serviceInfo, NsdManager.PROTOCOL_DNS_SD, mRegistrationListener);

}
- (void)sendNetworkPacket:(NSDictionary*)packet
{

// Encode packet
NSData* rawPacket = [NSKeyedArchiver archivedDataWithRootObject:packet];

// Write header: lengh of raw packet
int packetLength = [rawPacket length];
[outgoingDataBuffer appendBytes:&packetLength length:sizeof(int)];

// Write body: encoded NSDictionary
[outgoingDataBuffer appendData:rawPacket];

// Try to write to stream
[self writeOutgoingBufferToStream];
}

if ( [incomingDataBuffer length] >= sizeof(int) )
{
// extract length
memcpy(&packetBodySize, [incomingDataBuffer bytes], sizeof(int));

// remove that chunk from buffer
NSRange rangeToDelete = {0, sizeof(int)};
[incomingDataBuffer replaceBytesInRange:rangeToDelete withBytes:NULL length:0];
}

FireChat
UI-based
Programmatic
WWDC 2014
session 709
public void onServiceFound(NsdServiceInfo service)
{

Log.d(TAG, "Service discovery success" + service);

mNsdManager.resolveService(service,mResolveListener);

}
Full transcript