Objective-C in the Cloud

OCCService Class Reference

Declared inOCClient/OCCService.h
Base classNSObject
GuidesClient Libraries
Client Libraries Conceptual Guide

Overview

Subclasses of OCCService builds the local counterpart of service classes inside the cloud application. Every used service of the cloud application has to be represented by a local stand-in subclass at the client application:

#import <Cocoa/Cocoa.h>
#import <OCClient/OCClient.h>                               // Import OCCService

// Subclass with messaging infrastructure
@interface Service : OCCService                             // Local subclass
@end

To import the API of the cloud application's service class you have to add a category interface, which promises to implement the exported cloud interface.

#import "ServiceCloudInterface.h"                           // Import the cloud interface

// Add service's API
@interface Service (CloudInterface) <ServiceCloudInterface> // Add cloud interface to subclass
@end

At runtime you have to link the local stand-in class to the remote service class:

[Service linkToCloudApp:@"clienttutorial" protocol:@protocol(ServiceCloudInterface) handler:
^(__unsafe_unretained Class serviceClass, NSError *error)
{

}

From this point you can send messages to that service directly …:

#import "Service.h"

[Service sayHello];

… or asynchronously using an invocation object:

#import "Service.h"

id<ServiceCloudInterface> invocation = [Service invokeWithHandler:
^(BOOL success, id response)
{

}];
[invocation sayHello];

Additionally you can group a bunch of invocations into invocation groups.

Tasks

Linking

+setLocalHost:
+setRemoteHost:
+setUsesRemoteHost:
+completeHostName
+linkToCloudApp:protocol:URLSession:handler:
+linkToCloudApp:protocol:handler:
OCCConnectionHandler

Invocation

+invokeWithHandler:
+invokeAndDispatchOnMainQueue:handler:
+invokeWithReturnHandler:errorHandler:

Class Methods

+completeHostName

Returns the composed URL as a string used for sending messages to the cloud application's service.

+ (NSString *)completeHostName

Return Value

The composed URL string.

Discussion

The result of the method depends on +setUsesRemoteHost:, +setRemoteHost: and +setLocalHost:. The content is an implementation detail and you should not rely on a concrete format. You can use the result to build an URL for sending messages with Cocoa's URL loading system.

+invokeAndDispatchOnMainQueue:handler:

Returns an invocation object.

+(id)invokeAndDispatchOnMainQueue:(BOOL)dispatchOnMainQueue handler:(OCCInvocationResponseHandler)handler;

Parameters

dispatchOnMainQueue
If YES, the completion handler is executed on the main queue otherwise on the queue on which the message is sent.
handler
A block called after a message is send using the invocation object.

Return Value

A proxy object you can sent the service's messages to.

Discussion

This method starts the asynchronous messaging mechanism. It returns an object that can receive all messages for the receiver class and sends them to the cloud. It then returns immediately. If the the response is received it calls the handler.

The handler is called with its parameter success being set to YES, if the message has been dispatched. In this case the responseValue parameter of the handler contains the return value of the method.

If the dispatching failed, success is set to NO and the response value contains an instance of NSError.

Before you can use this method the receiver class has to be linked to the cloud service class using +linkToCloudApp:protocol:handler: et al.

+invokeWithHandler:

Returns an invocation object.

+(id)invokeWithHandler:(OCCInvocationResponseHandler)handler

Parameters

handler
A block called after a message is send using the invocation object.

Return Value

A proxy object you can sent the service's messages to.

Discussion

Simply sends +invokeAndDispatchOnMainQueue:handler: with dispatchOnMainQueue set to YES.

See also

+invokeAndDispatchOnMainQueue:handler:

+invokeWithReturnHandler:errorHandler:

Returns an invocation object that calls separate handlers for success and error.

+(id)invokeWithReturnHandler:(OCCInvocationReturnHandler)returnHandler errorHandler:(OCCInvocationErrorHandler)errorHandler

Parameters

returnHandler
Handler that is called, when invocation dispatched.
errorHandler
Handler that is called, when dispatch failed.

Return Value

A proxy object you can sent the service's messages to.

Discussion

The returned invocation object calls it handlers on the main thread, but uses separate handlers for success and fail. This is useful, when a global standard error handling can be applied.

+linkToCloudApp:protocol:URLSession:handler:

Links the receiver to a remote class.

+(BOOL)linkToCloudApp:(NSString*)cloudApp protocol:(Protocol*)protocol URLSession:(NSURLSession *)URLSession
handler:(OCCConnectionHandler)handler

Parameters

cloudApp
The name of the cloud app
protocol
The protocol that defines the API
URLSession
URL session used to communicate
handler
Completion handler

Return Value

YES, if successfully linked, otherwise NO.

Discussion

Links the receiver against the eponymous class in the cloud application applying the protocol. Messages to the remote class are sent using the given URL session.

By now the linking is done only locally. That means that no communication is necessary. Consequently providing correct arguments the return value is YES even there is no connection to the server.

The method itself appends a block operation with the handler to the main operation queue and then returns. Therefore the handler is executed after returning from the method, if the message is sent on the main thread. Otherwise the order is undefined.

Future implementations may communicate with the server causing a longer delay between returning from the method and execution of the handler.

+linkToCloudApp:protocol:handler:

Links the receiver to a remote class

+ (BOOL)linkToCloudApp:(NSString*)cloudApp protocol:(Protocol*)protocol handler:(OCCConnectionHandler)handler

Parameters

cloudApp
The name of the cloud app
protocol
The protocol that defines the API
handler
Completion handler

Return Value

YES, if successfully linked, otherwise NO.

Discussion

Uses +linkToCloudApp:protocol:URSLSession:handler with a private URL session.

+setLocalHost:

Sets the domain and port of a local Terrasphere.

+ (void)setLocalHost:(NSString*)host;

Parameters

host
The domain and port of the local Terrapshere

Discussion

Sets the domain of the local host. A port number can be assigned. The default is @"localhost:10000". The local host is used for messages to the cloud application, when setUsesRemoteHost: hasn't been sent to the receiver with NO.

You can use this method to let the client application talk with a dedicated Terrasphere in your local area network or to use a Terrasphere on your Mac, when running the client application on an iOS device.

+setRemoteHost:

Sets the domain and port of a remote (production) Terrasphere.

+ (void)setRemoteHost:(NSString*)host;

Parameters

host
The domain and port of the remote Terrapshere

Discussion

Sets the domain of the remote host. A port number can be assigned. The default is @"obcl.io". The remote host is used for messages to the cloud application, when setUsesRemoteHost: has been sent to the receiver with YES.

You have to send this message only if you are self-hosting Objective-Cloud for production. If you want to set the host for development and debugging, use +setLocalHost:.

+setUsesRemoteHost:

Switches between local (development) and remote (production) Terrasphere.

+(void)setUsesRemoteHost:(BOOL)worksRemotly

Parameters

worksRemotly
If YES messages are sent to the remote Terrasphere, otherwise to the local instance.

Discussion

The client framework has to modes: Local mode and remote mode. The local mode is used for development and debugging, the remote mode for production.

There are some changes in the behavior of the client framework (and Terrasphere) depending on the mode. In local mode HTTP is used instead of HTTPS, no subdomains are applied to the host's domain and the local host domain is used instead of the remote host domain.

Types

OCCConnectionHandler

Block that is called after a class is linked to a cloud application's class.

typedef void (^OCCConnectionHandler)(Class serviceClass, NSError *error);

Parameters

serviceClass
The linked class
error
An instance of NSError

discussion

This block is called after a linkToCloudApp:… message successfully run. The first parameter is a class object of the linked class. The second parameter takes an instance of NSError, if something failed or nil.