Objective-C in the Cloud

OCFCloudApp Class Reference

Declared inOCFoundation/OCFCloudApp.h
Base classNSObject
GuidesRequest based Applications
Request based Applications in Detail

Overview

OCFCloudApp is a class which represents a cloud application. This class is never actually instantiated. All of its methods are class methods. By default a cloud application contains a subclass of OCFCloudApp.

A subclass of OCFCloudApp is called primary class, if it's name is stored in the info.plist under the key OCFCloudAppClass.

Typically you overwrite +finishLaunching in your subclass to get informed that the cloud application is launching. This is a good place to add routes and handlers to the cloud application using +handleRequestsWithMethod:matchingPath:withBlock:.

Please take into account that different instances of your cloud application can run on different servers. Therefore the content of global variables can be different for two requests, because the requests can be handled by instances of cloud applications on different servers.

Tasks

General Information

+finishLaunching
+additionalInformation

Request Handling

+handleRequestsWithMethod:matchingPath:withBlock:
+setServesStaticFiles:forBasePath:localPath:indexFilename:cacheAge:

Database

+defaultDatabaseConnection

Push Notifications

+pushNotificationsEnabled
+defaultPushNotificationCenter

Class Methods

additionalInformation

Returns additional global information.

+ (NSDictionary *)additionalInformation

Discussion

This method delivers additional informations global to the cloud app. This includes database access information, if a database is booked for the cloud application. However you should use +defaultDatabaseConnection instead of setting-up the connection yourself.

defaultDatabaseConnection

Returns the default connection of the cloud app's database.

+ (PGCConnection *)defaultDatabaseConnection

Discussion

Use this method to connect to your database. If not database is booked for the cloud application, nil is returned.

defaultPushNotificationCenter

Returns the default push notification center.

+ (OCFPushNotificationCenter *)defaultPushNotificationCenter

Discussion

Returns the default push notification center for the cloud app.

finishLaunching

Finishes the launching process.

+ (void)finishLaunching

Discussion

Never send this message to the receiver. It is done automatically by Objective-Cloud's runtime environment.

Typically you override this method to add routes and handlers or do other things that has to be done once after launching of your cloud app.

You cannot expect to run your cloud application continuously. It can crash or Objective-Cloud shuts it down because of high/low usage. If a new request, including invocations, is sent to your cloud application it will launch again.

handleRequestsWithMethod:matchingPath:withBlock:

Add handler for route.

+ (void)handleRequestsWithMethod:(NSString *)method matchingPath:(NSString *)path
withBlock:(void (^) (OCFRequest *request))requestHandler

Parameters

method
A regular expression the http method of the incoming request hast to match.
path
A regular expression which describes the path of the incoming request. Additionally you can use placeholders to put parts of the incoming string into variables.
requestHandler
The handler block that will be executed for requests that conform to the path pattern and method parameter values.
request
The incoming request that matched method and path.

Discussion

This method allows you to add routes and handlers to your cloud application. You can add as many handlers as you want to.

For each incoming request OCFCloudApp tries to match the HTTP method and path to the list of registered handlers. If multiple routes match then the route which was added first is executed.

Using placeholders for the HTTP method is possible, but a code smell. Usually request-based services implement a RESTful service, which should map different HTTP methods to different actions.

The path can contain wildcards and variables. Variables are parts of the URL string that that are not defined by the cloud application, but set by the client. For example the path /countries/:country/ would match every path starting with /countries/. You can read the values of the placeholders for the current request inside the handler by asking the request object passed to the handler for its parameters.

pushNotificationsEnabled

Returns YES, if push notifications are enabled

+ (BOOL)pushNotificationsEnabled

Discussion

Returns YES, if push notifications are enabled for the cloud app, otherwise NO.

setServesStaticFiles:forBasePath:localPath:indexFilename:cacheAge:

Enables static file delivery and sets the paths for it.

+ (void)setServesStaticFiles:(BOOL)servesStaticFiles forBasePath:(NSString *)basePath
localPath:(NSString *)localPath indexFilename:(NSString *)indexFilename cacheAge:(NSUInteger)cacheAge

Parameters

servesStaticFiles
If YES, static file delivery is turned on, otherwise off.
basePath
A base path, usually @"/".
localPath
The path of a folder inside the folder specified with base path.
indexFilename
Currently not used, set to nil
cacheAge
The maximum time in seconds a static file can be cached.

Discussion

You can use this method to tell your cloud app to serve static files. This is especially useful if you the response if for displaying in a web browser. Web sites usually have couple of static resources (images, CSS files, JavaScript files) as elements. To prevent you from implementing a appropriate handler for each resource, OCFCloadApp offers the ability to deliver static resources out of the box.

basePath is the prefix that specifies the URLs for which static contents should be delivered. For example @"/resources" as base path, a request with the path @"/resources/myphoto.jpeg" will search for a file with the name my photo.jpeg in the services static resources directory. The path component after the basePath can include subpaths.

localPath is the path of the the static resources folder and subfolders. Usually this is a path inside the cloud app's bundle. You can use appropriate methods of NSBundle to get the complete path.

indexFilename and cacheAge are currently unsupported.