#import "Example.h"
@implementation Example
@synthesize name;
@synthesize description;
@synthesize parameters;
@synthesize nullBridge_p;
- (id)initWithExampleInterface:(ExampleInterface *)exampleInterface {
self = [super init];
self.parameters = [[ParameterInterface alloc] init];
self.exampleInterface = exampleInterface;
self.nullBridge_p = malloc(sizeof(callbackBridge_t));
self.nullBridge_p->example_p = (__bridge void *)self;
self.nullBridge_p->user_p = NULL;
self.running = NO;
self.requestRun = NO;
[self addObserver:self forKeyPath:@"requestRun" options:0 context:NULL];
self.requestCancel = NO;
[self addObserver:self forKeyPath:@"requestCancel" options:0 context:NULL];
return self;
}
- (void)start {
self.running = YES;
self.exampleInterface.runningExample = self;
[self performSelectorInBackground:@selector(run) withObject:nil];
}
- (void)run {
[self handleErrorWithReturnCode:rc
errorString:"solClient_log_setCallback()"];
}
}
- (void)cancel {
self.requestCancel = YES;
}
- (void)cleanup {
[self printToTextView:@"Example complete."];
self.requestCancel = NO;
self.running = NO;
self.exampleInterface.runningExample = nil;
}
- (void)printToTextView:(NSString *)str {
[[NSNotificationCenter defaultCenter]
postNotificationName:@"output"
object:self
userInfo:[NSDictionary
dictionaryWithObjectsAndKeys:str, @"string",
nil]];
}
- (void)logCCSMPVersion {
"Unknown library version, solClient_version_get returns FAIL\n");
} else {
}
}
- (void)setLoggingLevel {
if ([[self.parameters parameterWithId:PARAMETER_LOGGING_LEVEL].value
isEqualToString:@"critical"]) {
return;
} else if ([[self.parameters parameterWithId:PARAMETER_LOGGING_LEVEL].value
isEqualToString:@"error"]) {
return;
} else if ([[self.parameters parameterWithId:PARAMETER_LOGGING_LEVEL].value
isEqualToString:@"warning"]) {
return;
} else if ([[self.parameters parameterWithId:PARAMETER_LOGGING_LEVEL].value
isEqualToString:@"notice"]) {
return;
} else if ([[self.parameters parameterWithId:PARAMETER_LOGGING_LEVEL].value
isEqualToString:@"info"]) {
return;
} else if ([[self.parameters parameterWithId:PARAMETER_LOGGING_LEVEL].value
isEqualToString:@"debug"]) {
return;
}
}
errorString:(const char *)errorStr {
"%s - ReturnCode=\"%s\", SubCode=\"%s\", ResponseCode=%d, Info=\"%s\"",
}
messageCallback:
eventCallback_p {
return [self createAndConnectSessionWithContext:context_p
session:session_p
messageCallback:msgCallback_p
eventCallback:eventCallback_p
userData:self.nullBridge_p];
}
messageCallback:
eventCallback_p
userData:(callbackBridge_t *)user_p {
const char *sessionProps[50];
int propIndex = 0;
propIndex = 0;
sessionProps[propIndex++] =
[[self.parameters parameterWithId:PARAMETER_HOST].value
cStringUsingEncoding:NSASCIIStringEncoding];
sessionProps[propIndex++] =
[self.parameters parameterWithId:PARAMETER_COMPRESSION].value.boolValue
? "9"
: "0";
sessionProps[propIndex++] = "3";
sessionProps[propIndex++] = "3";
if (![[self.parameters parameterWithId:PARAMETER_HOST].value
isEqualToString:@""]) {
sessionProps[propIndex++] =
[[self.parameters parameterWithId:PARAMETER_VPN].value
cStringUsingEncoding:NSASCIIStringEncoding];
}
sessionProps[propIndex++] =
[[self.parameters parameterWithId:PARAMETER_USERNAME].value
cStringUsingEncoding:NSASCIIStringEncoding];
sessionProps[propIndex++] =
[[self.parameters parameterWithId:PARAMETER_PASSWORD].value
cStringUsingEncoding:NSASCIIStringEncoding];
sessionProps[propIndex] = NULL;
(char **)sessionProps, context_p, session_p, &sessionFuncInfo,
[self handleErrorWithReturnCode:rc
errorString:"solClient_session_create()"];
return rc;
}
[self handleErrorWithReturnCode:rc
errorString:"solClient_session_connect()"];
return rc;
}
}
topic:(char *)topic_p
deliveryMode:(solClient_uint32_t)deliveryMode {
"publishMessageWithSession:topic:deliveryMode called.\n");
[self handleErrorWithReturnCode:rc errorString:"solClient_msg_alloc()"];
return rc;
}
[self handleErrorWithReturnCode:rc
errorString:"solClient_msg_setDeliveryMode()"];
goto freeMessage;
}
destination.
dest = topic_p;
msg_p, &destination,
sizeof(destination))) !=
SOLCLIENT_OK) {
[self handleErrorWithReturnCode:rc
errorString:"solClient_msg_setDestination()"];
goto freeMessage;
}
[self handleErrorWithReturnCode:rc
errorString:"solClient_session_sendMsg()"];
goto freeMessage;
}
freeMessage:
[self handleErrorWithReturnCode:rcFreeMsg
errorString:"solClient_msg_free()"];
}
return rc;
}
- (pthread_t)startThreadWithFP:(FP)fp argument:(void *)arg {
pthread_t th = _NULL_THREAD_ID;
if (pthread_create(&th, NULL, fp, arg)) {
}
if (th == _NULL_THREAD_ID) {
return _NULL_THREAD_ID;
} else {
return th;
}
}
- (void)waitOnThread:(pthread_t)handle {
void *value_p;
pthread_join(handle, &value_p);
}
threadRetType contextThread(void *user_p) {
@autoreleasepool {
callbackBridge_t *bridge = (callbackBridge_t *)user_p;
Example *example = (__bridge Example *)bridge->example_p;
contextThreadInfo_t *contextInfo_p =
(contextThreadInfo_t *)bridge->user_p;
free(bridge);
return [example contextThread:contextInfo_p];
}
}
- (threadRetType)contextThread:(void *)contextInfo_p {
contextThreadInfo_t *info_p = (contextThreadInfo_t *)contextInfo_p;
info_p->rc = 0;
while (!info_p->stopContextThread) {
[self handleErrorWithReturnCode:rc
errorString:"solClient_context_processEvents"];
break;
}
}
return NULL;
}
- (int)startContextThread:(contextThreadInfo_t *)info_p {
int rc = 1;
info_p->stopContextThread = 0;
callbackBridge_t *bridge = malloc(sizeof(callbackBridge_t));
bridge->user_p = info_p;
bridge->example_p = (__bridge void *)self;
if (pthread_create(&(info_p->handle), NULL, contextThread, (void *)bridge) <
0) {
rc = 0;
} else {
info_p->contextThreadStarted = 1;
}
return rc;
}
- (void)stopContextThread:(contextThreadInfo_t *)info_p {
info_p->stopContextThread = 1;
void *value_p;
pthread_join(info_p->handle, &value_p);
}
- (UInt64)getTimeInUs {
struct timespec tv;
mach_timespec_t mtv;
clock_serv_t cclock;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &mtv);
mach_port_deallocate(mach_task_self(), cclock);
tv.tv_sec = mtv.tv_sec;
tv.tv_nsec = mtv.tv_nsec;
return ((UInt64)tv.tv_sec * (UInt64)1000000) +
((UInt64)tv.tv_nsec / (UInt64)1000);
}
- (void)getUsageTimeWithUserTime:(long long *)userTime_p
systemTime:(long long *)systemTime_p {
struct rusage usage;
getrusage(RUSAGE_SELF, &usage);
*userTime_p = (long long)usage.ru_utime.tv_sec * (long long)1000000 +
(long long)usage.ru_utime.tv_usec;
*systemTime_p = (long long)usage.ru_stime.tv_sec * (long long)1000000 +
(long long)usage.ru_stime.tv_usec;
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
if ([keyPath isEqualToString:@"requestRun"]) {
if (self.requestRun) {
self.requestRun = NO;
[self start];
}
}
else if ([keyPath isEqualToString:@"requestCancel"]) {
if (self.requestCancel && !self.running) {
self.requestCancel = NO;
}
}
}
void *user_p) {
@autoreleasepool {
callbackBridge_t *bridge = (callbackBridge_t *)user_p;
id<EventCallbackProtocol> callbackProtocol =
(__bridge id<EventCallbackProtocol>)bridge->example_p;
[callbackProtocol eventCallbackWithSession:opaqueSession_p
eventInfo:eventInfo_p
userData:bridge->user_p];
}
}
eventInfo:
userData:(void *)user_p {
"eventCallbackWithSession:eventInfo: called - %s\n",
break;
"%s; subCode %s, responseCode %d, "
"reason %s\n",
break;
default:
"- %s. Unrecognized or deprecated "
"event.\n",
break;
}
}
void *user_p) {
@autoreleasepool {
callbackBridge_t *bridge = (callbackBridge_t *)user_p;
id<EventCallbackProtocol> callbackProtocol =
(__bridge id<EventCallbackProtocol>)bridge->example_p;
[callbackProtocol cacheEventCallbackWithSession:opaqueSession_p
eventInfo:eventInfo_p
userData:bridge->user_p];
}
}
- (void)cacheEventCallbackWithSession:
userData:(void *)user_p {
"cacheEventCallbackWithSession:eventInfo called - %s\n"
"topic: %s\n"
"responseCode: (%d) %s\n"
"subCode: (%d) %s\n"
"cacheRequestId: %llu\n\n",
eventInfo_p->
topic, eventInfo_p->
rc,
}
void *user_p) {
@autoreleasepool {
callbackBridge_t *bridge = (callbackBridge_t *)user_p;
id<EventCallbackProtocol> callbackProtocol =
(__bridge id<EventCallbackProtocol>)bridge->example_p;
[callbackProtocol eventPerfCallbackWithSession:opaqueSession_p
eventInfo:eventInfo_p
userData:bridge->user_p];
}
}
eventInfo_p
userData:(void *)user_p {
}
@autoreleasepool {
callbackBridge_t *bridge = (callbackBridge_t *)user_p;
id<LogCallbackProtocol> callbackProtocol =
(__bridge id<LogCallbackProtocol>)bridge->example_p;
[callbackProtocol logCallbackWithLogInfo:logInfo_p
userData:bridge->user_p];
}
}
userData:(void *)user_p {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"hh:mm:ss"];
NSString *str = [NSString
stringWithFormat:@"%s %s %@ : %s",
solClient_log_categoryToString(logInfo_p->category),
solClient_log_levelToString(logInfo_p->level),
[formatter stringFromDate:[NSDate date]],
logInfo_p->msg_p];
[self printToTextView:str];
}
@autoreleasepool {
callbackBridge_t *bridge = (callbackBridge_t *)user_p;
id<MessageReceiveCallbackProtocol> callbackProtocol =
(__bridge id<MessageReceiveCallbackProtocol>)bridge->example_p;
return
[callbackProtocol messageReceiveCallbackWithSession:opaqueSession_p
message:msg_p
userData:bridge->user_p];
}
}
messageReceiveCallbackWithSession:
userData:(void *)user_p {
const char *senderId_p;
rxSeqNum = 0;
} else {
[self
handleErrorWithReturnCode:rc
errorString:"solClient_msg_getSequenceNumber()"];
}
}
senderId_p = "";
} else {
[self handleErrorWithReturnCode:rc
errorString:"solClient_msg_getSenderId()"];
}
}
if (user_p != NULL) {
"%s received message from '%s' (seq# %llu)\n",
(char *)user_p, senderId_p, rxSeqNum);
} else {
"Received message from '%s' (seq# %llu)\n", senderId_p,
rxSeqNum);
}
}
@autoreleasepool {
callbackBridge_t *bridge = (callbackBridge_t *)user_p;
id<MessageReceiveCallbackProtocol> callbackProtocol =
(__bridge id<MessageReceiveCallbackProtocol>)bridge->example_p;
return [callbackProtocol
messageReceivePrintMsgCallbackWithSession:opaqueSession_p
message:msg_p
userData:bridge->user_p];
}
}
messageReceivePrintMsgCallbackWithSession:
userData:(void *)user_p {
if (user_p != NULL) {
(char *)user_p);
} else {
}
char buffer[5000];
[self handleErrorWithReturnCode:rc errorString:"solClient_msg_dump()"];
}
}
@autoreleasepool {
callbackBridge_t *bridge = (callbackBridge_t *)user_p;
id<MessageReceiveCallbackProtocol> callbackProtocol =
(__bridge id<MessageReceiveCallbackProtocol>)bridge->example_p;
return [callbackProtocol
messageReceivePerfCallbackWithSession:opaqueSession_p
message:msg_p
userData:bridge->user_p];
}
}
messageReceivePerfCallbackWithSession:
userData:(void *)user_p {
}
@end