Outline Mobile Software Develoment Framework: Mobile-Cloud Services (Push/Track) Admin Mobile cloud service Push notification service Storage service Track service Slit service 10/18/2012 Y. Richard Yang 1 2 Admin. HW3 osted Reca: Accessing Data in Cloud A tyical design attern is that a device udates/receives data in the cloud Cloud as a rendezvous oint Challenge: How do you kee data on a device fresh? 3 4 Reca: Solution Sace Shared Push Service Mobile oll Cloud ush Each a ush Shared (infrastructure) ush A single ersistent connection from device to a cloud ush service rovider Multile alication roviders ush to the service rovider Service rovider ushes to a device using the ersistent connection Two examles Ale Push Notification Service (APNS) Google Cloud Messaging (GCM) 5 6 1
Design Requirements of a Shared Push Service Security/Authorization Do not allow arbitrary a to ush to a device Design Point: Authorization Design 1: A does not know registered devices. Broadcast to all. Scalability A large scale system may have millions of clients A Design 2: A query registered devices; Multicast Registration(DEV_ID, A_ID) Device Fault tolerance Client/device, ush servers all can fail Generality Design 3: Device notifies registration ID to its server; Can be used by diverse alications 7 8 Design Point: What to Push? Design Point: Reliability (What Can Go Wrong) Otion 1: Just ush signal (data available) to devices and then devices fetch from a servers Otion 2: ush a data A A sends to regids RegID=Registration(DEV_ID, A_ID) Device A Device Device notifies regid to its server; 9 10 Soft State Design State at the third arty is soft state if the the entity who sets u the state does not refresh it, the state will be runed at the 3 rd arty Ale Push Notification Service ios device maintains a ersistent TCP connection to an Ale Push Notification Server(APNS) A ush notification from a rovider to a client alication Multi-roviders to multile devices 11 12 2
! APNS Authorization: Device Token Device token Contains information that enables APNs to locate the device Client a needs to rovide the token to its a rovider Device token should be requested and assed to roviders every time your alication launches Ale Push Notification Data Each ush notification carries a ayload 256 bytes maximum Best effort delivery A rovider rovides a JSON dictionary object, which contains another dictionary identified by the key as as secifies the following actions An alert message to dislay to the user A number to badge the alication icon with A sound to lay 13 14 APNS Examle: Client 1. - (BOOL)alication:(UIAlication *)alication didfinishlaunchingwithotions:(nsdictionary *)launchotions! 2. {! 3. // Let the device know we want to receive ush notifications! 4. [[UIAlication sharedalication] registerforremotenotificationtyes:! 5. "(UIRemoteNotificationTyeBadge "UIRemoteNotificationTyeSound "UIRemoteNotificationTyeAlert)];! 6.! 7. return YES;! 8. }! 9. - (void)alication:(uialication*)alication didreceiveremotenotification:(nsdictionary*)userinfo! 10. {//userinfo contains the notification! 11. NSLog(@"Received notification: %@", userinfo);! 12. }! 13. - (void)alication:(uialication*)alication didregisterforremotenotificationswithdevicetoken:(nsdata*)devicetoken! 14. {! 15. " NSLog(@"My token is: %@", devicetoken);! 16. }! APNS Examle: Server 1. $devicetoken ='f05571e4be60a4e11524d76e4366862128f430522fb470c46fc6810fffb07af7 ;! 2. // Put your rivate key's asshrase here:! 3. $asshrase = 'PushChat';! 4. // Put your alert message here:! 5. $message = CS434: my first ush notification!';! 6. $ctx = stream_context_create();! 7. Stream_context_set_otion($ctx, 'ssl', 'local_cert', 'ck.em');! 8. stream_context_set_otion($ctx, 'ssl', 'asshrase', $asshrase);! 9. // Oen a connection to the APNS server! 10. $f = stream_socket_client(! 11. " 'ssl://gateway.sandbox.ush.ale.com:2195', $err,! 12. " $errstr, 60, STREAM_CLIENT_CONNECT STREAM_CLIENT_PERSISTENT, $ctx);! 13. if (!$f)! 14. " exit("failed to connect: $err $errstr". PHP_EOL);! 15 15. echo 'Connected to APNS'. PHP_EOL;! 16 APNS Examle: Server (cont ) Google Cloud Messaging 16. // Create the ayload body! 17. $body['as'] = array(! 18. " 'alert' => $message,! 19. " 'sound' => 'default'! 20. " );! Very similar to APNS 21. // Encode the ayload as JSON! 22. $ayload = json_encode($body);! 23. // Build the binary notification! 24. $msg = chr(0). ack('n', 32). ack('h*', $devicetoken). ack('n', strlen($ayload)). $ayload;! GCM Servers 25. // Send it to the server! 26. $result = fwrite($f, $msg, strlen($msg));! 27. if (!$result)! 28. " echo 'Message not delivered'. PHP_EOL;! 29. else! 30. " echo 'Message successfully delivered'. PHP_EOL;! 31. // Close the connection to the server! 32. fclose($f); 17 See htt://develoer.android.com/guide/google/gcm/gs.html for detailed stes 18 3
GCM Flow: A Develoer Registration A develoer registers a roject at Google Oen API console: htts://code.google.com/ais/console/ GCM Flow: Device A Registration Enable cloud to device messaging in your a Add ermissions in Manifest A (on device) registers with Google to get registration ID ublic class MyActivity extends Activity { After Create roject Project ID; Sender ID ublic void oncreate(bundle savedinstancestate) { suer.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } GCMRegistrar.checkDevice(this); GCMRegistrar.checkManifest(this); final String regid = GCMRegistrar.getRegistrationId(this); if (regid.equals("")) { GCMRegistrar.register(this, SENDER_ID); } else { Log.v(TAG, "Already registered"); } 19 20 Device A Handle Events <receiver android:name="com.google.android.gcm.gcmbroadcastreceiver" android:ermission="com.google.android.c2dm.ermission.send" > <intent-filter> <action android:name="com.google.android.c2dm.intent.receive" /> <action android:name="com.google.android.c2dm.intent.registration" /> <category android:name="my_a_ackage" /> </intent-filter> </receiver> The GCMBroadcastReceiver (defined in GCM library) handles the broadcast messages, and calls methods defined in.gcmintentservice, if you define this service GCMIntentService // called after GCM library finishes registration // you need to send regid to your server onregistered(context context, String regid); onunregistered(context context, String regid); // called after your server sends a msg to GCM, and // GCM sends to this device onmessage(context context, Intent intent); onerror(context context, String errorid); onrecoverableerror(context context, String errorid) 21 22 A Server If you use GCM server library imort com.google.android.gcm.server.*; Sender sender = new Sender(myAiKey); Message message = new Message.Builder().build(); MulticastResult result = sender.send(message, devices, 5); Summary: GCM Flow Enabling cloud to device messaging A (on device) registers with Google to get registration ID A sends registration ID to its A Server Per message A Server sends (authenticated) message to Google Google sends message to device, which sends to a Disabling cloud to device messaging A can unregister ID, e.g., when user no longer wants ush 23 24 4
Additional Details Discussion: Mobile Cloud Services See Thialfi: A Client Notification Service for We have discussed ush notification service. Internet-Scale Alications, by Atul Adya, Gregory Cooer, Daniel Myers, Michael Piatek, ACM SOSP 2011. What other services can you think of? 25 26 Outline Examle Mobile Cloud Services Push notification service Admin Location based service Track service (suorting location based services) Mobile cloud service Push notification service Track service Storage and sync Syncing and storage service (icloud) StarTrack Next Generation: A Scalable Infrastructure for Track-Based Alications, by Maya Haridasan, Iqbal Mohomed, Doug Terry, Chandramohan A. Thekkath, and Li Zhang, in OSDI 2010. Proxy service (Kindle Slit Browser) Recognition services Seech to text/text to seech service language rocessing service (oen Siri API for 3rd arty alications in the future) Natural 27 Location-Based Alications Many hones already have the ability to determine their own location 28 A Common Abstraction: Track Time-ordered sequence of location readings GPS, cell tower triangulation, or roximity to WiFi hotsots Many mobile alications use location information La#tude: 37.4013 Longitude: - 122.0730 Time: 07/08/10 08:46:45.125 Courtesy: Maya et al. 5
32 A Taxonomy of Alications Alication: Personalized Driving Directions Goal: Find directions to new gym Personal Social Current loca=on Driving direc#ons, Nearby restaurants Friend finder, Crowd scenes Past loca=ons Personal travel journal, Geocoded hotos Post- it notes, Recommenda#ons Tracks Personalized Driving Direc#ons, Ride sharing, Discovery, Track- Based Search Urban sensing Class of alica=ons enabled by StarTrack 31 Courtesy: Maya et al. StarTrack System Inser#on Alica#on Loca#on ST Client Manager System Challenges 1. Handling error-rone tracks Inser#on ST Server 2. Flexible rogramming interface ST Server ST Client 3. Efficient imlementation of oerations on tracks Alica#on ST Server 4. Scalability and fault tolerance Retrieval Maniula#on Comarison 34 33 Challenges of Using Raw Tracks StarTrack API: Track Collections Crea#on TC MakeCollec#on(GrouCriteria criteria, bool removedulicates) Maniula#on Advantages of Canonicalization: efficient retrieval and comarison oerations Enables StarTrack to maintain a list of non-dulicate tracks TC JoinTrackCollec#ons (TC tcs[], bool removedulicates) TC SortTracks (TC tc, SortASribute asr) TC TakeTracks(TC tc, int count) TC GetSimilarTracks (TC tc, Track reftrack, float simthreshold) TC GetPassByTracks (TC tc, Area[] areas) TC GetCommonSegments(TC tc, float freqthreshold) More 35 Pre- filter tracks Maniulate tracks Fetch tracks 36 6
API Usage: Ride-Sharing Alication Track Similarity // get user s most oular track in the morning TC mytc = MakeCollec#on( name = Maya, [0800 1000], true); TC mypotc = SortTracks(myTC, FREQ); Track track = GetTracks(myPoTC, 0, 1); // find tracks of all fellow emloyees TC mstc = MakeCollec#on( name.emloyer = MS, [0800 1000], true); S5 Tracks A, B Track C s7 s6 s5 s4 S6-7 s8 // ick tracks from the community most similar to user s oular track TC similartc = GetSimilarTracks(msTC, track, 0.8); Track[] similartracks = GetTracks(similarTC, 0, 20); s3 s2 s9 Track D // Verify if each track is frequently traveled by its resec#ve owner User[] result = FindOwnersOfFrequentTracks(similarTracks); s1 S1-4 Summary The Track abstraction is simle but quite interesting Think about abstractions in your roject 39 7