1 / 28

CSE 486/586 Distributed Systems Recitation

CSE 486/586 Distributed Systems Recitation. Android Emulator Networking. Note: this is my (Steve’s) best guess; actual details might be different. But it should be good enough for your understanding. Each emulator instance is attached to a virtual router .

jed
Download Presentation

CSE 486/586 Distributed Systems Recitation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 486/586 Distributed SystemsRecitation

  2. Android Emulator Networking Note: this is my (Steve’s) best guess; actual details might be different. But it should be good enough for your understanding. Each emulator instance is attached to a virtual router. The virtual router runs on top of the host (probably as an application). The virtual router forwards connections originated from its emulator instance. However, the virtual router acts as a firewall; it blocks all connection attempts going into its emulator instance from somewhere else.

  3. Android Emulator Networking emu0 emu1 emu2 App connect() App connect() App connect() VR0 VR1 VR2 Host • What if we want to allow others (in general) to connect to an app running inside an emulator instance? • Android provides redirection as a solution.

  4. Android Emulator Networking • Redirection Basics • In order to allow connection attempts going into the emulator instance, we need to set up redirection from the virtual router to its emulator instance. • Since everything happens on top of the host, the only way to enable any kind of networking is to borrow the host’s IP and ports. • Redirection forces the virtual router to listen on a host port and forward all traffic coming to the host port to the emulator instance. • Process • Pick a host port to borrow (that others will use to connect to) • Set up redirection from that host port to the emulator port that the app is listening on • E.g., rediradd tcp:<host port>:<emulator port> • Meaning, forward all traffic from the host port to the emulator port

  5. Android Emulator Networking emu0 App may or may notlisten on 8080 (not required for redirection). App 8080 VR0 starts listening on 10000 & forwards all traffic to emu0’s 8080. VR0 10000 Host On emu0: redir add tcp:10000:8080

  6. Android Emulator Networking emu0 App may or may not listen on 8080 (not required for redirection). App 8080 VR0 starts listening on 10000 & forwards all traffic to emu0’s 8080. VR0 10000 Host For any connect() attempt to <host IP:10000> (either from outside or from an app running on the host), it will succeed. On emu0: redir add tcp:10000:8080

  7. Android Emulator Networking emu0 App may or may not listen on 8080 (not required for redirection). App This (port 8080) is not visible to anyone outside of VR0. All others can only see port 10000. This is why you can use the same server port number in your app. 8080 VR0 starts listening on 10000 & forwards all traffic to emu0’s 8080. VR0 10000 Host For any connect() attempt to <host IP:10000> (either from outside or from an app running on the host), it will succeed. On emu0: redir add tcp:10000:8080

  8. Android Emulator Networking emu0 emu1 App App Since we’re borrowing the host’s port numbers, this has to be different from VR0’s port number. 8080 8080 VR0 VR1 10000 20000 Host On emu0: redir add tcp:10000:8080 On emu1: redir add tcp:20000:8080

  9. Android Emulator Networking emu0 emu1 App App 8080 8080 VR0 VR1 10000 20000 Host Now all connect() should be to <host IP>:<redirected port> E.g., connect() to <host IP>:10000 will be forwarded to emu0:8080

  10. Android Emulator Networking • IP Addresses • A virtual router assigns an IP address to its emulator instance (10.0.2.15) and to the host (10.0.2.2) from a private IP range (10.0.0.0/8). • It assigns an IP address to the host mainly for convenience (I think…). • In the previous example, if emu1 wants to talk to emu0, it can use: • connect() to 10.0.2.2:10000 • Effectively, we are identifying different emulators using host port numbers we’re borrowing instead of their IP:port pairs.

  11. Content Providers A content provider provides a table view of data. If you write a content provider, any client application with the permission can enter/read/update/delete data items in your content provider. A client application (that uses your content provider) uses ContentResolver to interact with your content provider. You need to extend ContentProvider and implement necessary methods.

  12. How a Client Interacts • Table identification URI (android.net.Uri) • E.g., content://user_dictionary/words • Insert • public final Uri ContentResolver.insert (Uri url, ContentValues values) • Update • public final intContentResolver.update (Uri uri, ContentValues values, String where, String[] selectionArgs) • Query • public final Cursor ContentResolver.query (Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) • Delete • public final intContentResolver.delete (Uri url, String where, String[] selectionArgs)

  13. How to Write a Content Provider Declare in AndroidManifest.xml Define a URI that client apps will use Define permissions Implement necessary methods in ContentProvider When implementing ContentProvider, use either the Android file system or SQLite as the actual data storage.

  14. Declare in AndroidManifest.xml <manifest ... > ... <application ... > <provider android:name=".ExampleProvider" /> ... </application> </manifest>

  15. Defining a URI • Typical format • content://<authority>/<table name> • Authority: a global (Android-wide) name for the provider • E.g., edu.buffalo.cse.cse486.proj1.provider • Table name: the name of a table that the provider exposes • Note: a provider can expose more than one table. • Should be added to AndroidManifest.xml • E.g., <provider android:authorities=“edu.buffalo.cse.cse486.proj1.provider” …>…</provider>

  16. Define Permissions • Should define permissions (for others) in AndroidManifest.xml • android:permission: Single provider-wide read/write permission. • E.g., <provider android:permission=“edu.buffalo.cse.cse486.proj1.provider.permission.USE_PROJ1_PROVIDER” …>…</provider> • android:readPermission: Provider-wide read permission. • android:writePermission: Provider-wide write permission.

  17. Necessary Methods • query() • Retrieve data from your provider. • insert() • Insert a new row into your provider. • update() • Update existing rows in your provider. • delete() • Delete rows from your provider. • getType() • Return the MIME type corresponding to a content URI. • onCreate() • Initialize your provider. The Android system calls this method immediately after it creates your provider. Notice that your provider is not created until a ContentResolver object tries to access it. • These need to handle concurrent accesses (need to be thread-safe)

  18. Storage Options Internal storage: file system, private to the app External storage: file system, open to everybody SQLite: database, private to the app Read: http://developer.android.com/guide/topics/data/data-storage.html

  19. Internal Storage • Saving files directly on the device's internal storage. • User uninstallationfiles are removed. • To create and write a private file to the internal storage: • Call openFileOutput() with the name of the file and the operating mode. This returns a FileOutputStream. • Write to the file with write(). • Close the stream with close(). • E.g., String FILENAME = "hello_file"; String string = "hello world!”; FileOutputStreamfos = openFileOutput(FILENAME, Context.MODE_PRIVATE); fos.write(string.getBytes()); fos.close();

  20. Internal Storage • MODE_PRIVATEcreate the file (or replace a file of the same name) and make it private to your application. • Other modes available are: • MODE_APPEND, MODE_WORLD_READABLE, and MODE_WORLD_WRITEABLE. • To read a file from internal storage: • Call openFileInput() and pass it the name of the file to read. This returns a FileInputStream. • Read bytes from the file with read(). • Then close the stream with close().

  21. External Storage • Shared "external storage” • E.g., a removable storage media (such as an SD card) or an internal (non-removable) storage. • Files saved to the external storage are: • World-readable • Can be modified by the user when they enable USB mass storage to transfer files on a computer. • Checking media availability • Before you do any work with the external storage, you should always call getExternalStorageState() to check whether the media is available.

  22. External Storage • Accessing files on external storage (API Level 8 or greater) • Use getExternalFilesDir() to open a File that represents the external storage directory where you should save your files. • This method takes a type parameter that specifies the type of subdirectory you want, such as DIRECTORY_MUSIC and DIRECTORY_RINGTONES (pass null to receive the root of your application's file directory). This method will create the appropriate directory if necessary. • If the user uninstalls your application, this directory and all its contents will be deleted.

  23. External Storage • Saving files that should be shared • For files not specific to your application and that should not be deleted when your application is uninstalled • Save them to one of the public directories on the external storage. • These directories lay at the root of the external storage, such as Music/, Pictures/, Ringtones/, and others. • (API Level 8 or greater) • Use getExternalStoragePublicDirectory(), passing it the type of public directory you want, such as DIRECTORY_MUSIC, DIRECTORY_PICTURES, DIRECTORY_RINGTONES, or others. This method will create the appropriate directory if necessary.

  24. Services • A service runs in the background with no UI for long-running operations. • Playing music, sending/receiving network messages, … • Subclass of android.app.Service • Started service • A service is "started" when an application component (such as an activity) starts it by calling startService(). Once started, a service can run in the background indefinitely, even if the component that started it is destroyed. • Bound service • A service is "bound" when an application component binds to it by calling bindService(). A bound service offers a client-server interface that allows components to interact with the service, send requests, get results, and even do so across processes with interprocess communication (IPC).

  25. How to Write a Service Declare in AndroidManifest.xml Implement necessary methods in Service

  26. Declare in AndroidManifest.xml <manifest ... > ... <application ... > <service android:name=".ExampleService" /> ... </application> </manifest>

  27. Necessary Methods • onStartCommand() • The system calls this method when another component, such as an activity, requests that the service be started, by calling startService(). • onBind() • The system calls this method when another component wants to bind with the service (such as to perform RPC), by calling bindService(). • onCreate() • The system calls this method when the service is first created, to perform one-time setup procedures (before it calls either onStartCommand() or onBind()). • onDestroy() • The system calls this method when the service is no longer used and is being destroyed.

  28. Service Lifecycle

More Related