Beispiel #1
0
FskErr KprZeroconfServiceInfoNew(KprZeroconfServiceInfo *it, const char* type, const char* name, const char* host, const char* ip, UInt32 port, const UInt32 interfaceIndex, char* txt)
{
    FskErr err = kFskErrNone;
    KprZeroconfServiceInfo self = NULL;

    bailIfError(FskMemPtrNewClear(sizeof(KprZeroconfServiceInfoRecord), it));
    self = *it;
    self->type = FskStrDoCopy(type);
    bailIfNULL(self->type);
    self->name = FskStrDoCopy(name);
    bailIfNULL(self->name);
    if (host) {
        self->host = FskStrDoCopy(host);
        bailIfNULL(self->host);
    }
    if (ip) {
        self->ip = FskStrDoCopy(ip);
        bailIfNULL(self->ip);
    }
    self->port = port;
    self->interfaceIndex = interfaceIndex;
    if (txt) {
        self->txt = FskStrDoCopy(txt);
        bailIfNULL(self->txt);
    }
    FskInstrumentedItemNew(self, NULL, &KprZeroconfServiceInfoInstrumentation);
bail:
    if (err)
        KprZeroconfServiceInfoDispose(self);
    return err;
}
FskErr KprWebSocketEndpointConnect(KprWebSocketEndpoint self, char *url, char *origin)
{
	FskErr err = kFskErrNone;
	
	self->url = FskStrDoCopy(url);
	bailIfNULL(self->url);
	bailIfError(FskStrParseUrl(self->url, &self->parts));
	if (!KprWebSocketEndpointCheckURL(self)) {
		bailIfError(kFskErrInvalidParameter);
	}
	
	self->isSecure = (FskStrCompare(self->parts->scheme, "wss") == 0);
	
	if (origin) {
		self->origin = FskStrDoCopy(origin);
		bailIfNULL(self->origin);
	}
	
	self->state = kKprWebSocketStateConnecting;
	
	INVOKE_AFTER1(KprWebSocketEndpointStartConnect, self);
	
bail:
	return err;
}
Beispiel #3
0
FskErr KprMQTTClientNew(KprMQTTClient* it, char *clientId, Boolean cleanSession, void *refcon)
{
	FskErr err = kFskErrNone;
	KprMQTTClient self = NULL;

	bailIfError(FskMemPtrNewClear(sizeof(KprMQTTClientRecord), &self));

	bailIfError(KprMQTTQueueNew(&self->queue, 15, KprMQTTClientSendMessageViaDelivery, self));

	FskTimeCallbackNew(&self->pingRequestCallaback);
	bailIfNULL(self->pingRequestCallaback);

	FskTimeCallbackNew(&self->pingResponseCallaback);
	bailIfNULL(self->pingResponseCallaback);

	self->clientIdentifier = FskStrDoCopy(clientId);
	bailIfNULL(self->clientIdentifier);

	self->cleanSession = cleanSession;
	self->refcon = refcon;
	self->state = kKprMQTTStateDisconnected;

	KprMQTTQueueStart(self->queue);

	*it = self;
bail:
	if (err) KprMQTTClientDispose(self);
	return err;
}
Beispiel #4
0
FskErr KprDBStatementNew(KprDBStatement* it, KprDB db, const char* text, char* bindFormat, char* rowFormat)
{
	FskErr err = kFskErrNone;
	KprDBStatement self = NULL;
	SInt32 count, size, i;
	bailIfError(KprMemPtrNewClear(sizeof(KprDBStatementRecord), it));
	self = *it;
	FskInstrumentedItemNew(self, NULL, &KprDBStatementInstrumentation);
	bailIfError(sqlite3_prepare_v2(db->data, text, FskStrLen(text), &self->data, NULL));
	count = sqlite3_bind_parameter_count(self->data);
	if (count && !bindFormat) BAIL(kFskErrInvalidParameter);
	if (!count && bindFormat) BAIL(kFskErrInvalidParameter);
	if (count) {
		if ((SInt32)FskStrLen(bindFormat) != count) BAIL(kFskErrInvalidParameter);
		self->bindFormat = FskStrDoCopy(bindFormat);
		bailIfNULL(self->bindFormat);
	}
	count = sqlite3_column_count(self->data);
	if (count && !rowFormat) BAIL(kFskErrInvalidParameter);
	if (!count && rowFormat) BAIL(kFskErrInvalidParameter);
	if (count) {
		if ((SInt32)FskStrLen(rowFormat) != count) BAIL(kFskErrInvalidParameter);
		self->rowFormat = FskStrDoCopy(rowFormat);
		bailIfNULL(self->rowFormat);
		bailIfError(KprMemPtrNewClear(count * sizeof(char*), &self->keys));
		for (size = 0, i = 0; rowFormat[i]; i++) {
			self->keys[i] = sqlite3_column_name(self->data, i);
			switch (rowFormat[i]) {
				case 'b':
					size += sizeof(void*) + sizeof(UInt32);
					break;
				case 'd':
					size += sizeof(double);
					break;
				case 'i':
					size += sizeof(SInt32);
					break;
				case 'I':
					size += sizeof(FskInt64);
					break;
				case 'r':
					size += sizeof(SInt32);
					break;
				case 't':
					size += sizeof(char*);
					break;
				default:
					BAIL(kFskErrInvalidParameter);
					break;
			}
		}
		bailIfError(KprMemPtrNewClear(size, &self->values));
	}
bail:
	if (err)
		KprDBStatementDispose(self);
	return err;
}
Beispiel #5
0
FskErr KprMessageSetCredentials(KprMessage self, char* user, char* password)
{
	FskErr err = kFskErrNone;
	FskMemPtrDisposeAt(&self->user);
	FskMemPtrDisposeAt(&self->password);
	if (user && password) {
		self->user = FskStrDoCopy(user);
		bailIfNULL(self->user);
		self->password = FskStrDoCopy(password);
		bailIfNULL(self->password);
	}
bail:
	return err;
}
Beispiel #6
0
void KprSoundSetup()
{
	FskErr err = kFskErrNone;
	FskMediaPropertyValueRecord ambientAudio = {kFskMediaPropertyTypeInteger, {kFskAudioOutCategoryAmbient}};
#if FSK_EXTENSION_EMBED
#else
	char* applicationPath = NULL;
	char* libraryPath = NULL;
#if TARGET_OS_WIN32
	char* libraryName = "mediareader.dll";
#else
	char* libraryName = "mediareader.so";
#endif
	FskLibrary library = NULL;
#endif
	
	if (gSoundChannel) goto bail;
	
#if FSK_EXTENSION_EMBED
	FskMediaReaderNewProc = FskMediaReaderNew;
	FskMediaReaderDisposeProc = FskMediaReaderDispose;
	FskMediaReaderExtractProc = FskMediaReaderExtract;
	FskMediaReaderGetPropertyProc = FskMediaReaderGetProperty;
	FskMediaReaderGetTrackProc = FskMediaReaderGetTrack;
	FskMediaReaderStartProc = FskMediaReaderStart;
	FskMediaReaderStopProc = FskMediaReaderStop;
	FskMediaReaderTrackGetPropertyProc = FskMediaReaderTrackGetProperty;
#else
	applicationPath = FskGetApplicationPath();
	bailIfNULL(applicationPath);
	bailIfError(FskMemPtrNewClear(FskStrLen(applicationPath) + FskStrLen(libraryName) + 1, &libraryPath));
	FskStrCopy(libraryPath, applicationPath);
	FskStrCat(libraryPath, libraryName);
	bailIfError(FskLibraryLoad(&library, libraryPath));
	bailIfError(FskLibraryGetSymbolAddress(library, "FskMediaReaderNew", &FskMediaReaderNewProc));
	bailIfError(FskLibraryGetSymbolAddress(library, "FskMediaReaderDispose", &FskMediaReaderDisposeProc));
	bailIfError(FskLibraryGetSymbolAddress(library, "FskMediaReaderExtract", &FskMediaReaderExtractProc));
	bailIfError(FskLibraryGetSymbolAddress(library, "FskMediaReaderGetProperty", &FskMediaReaderGetPropertyProc));
	bailIfError(FskLibraryGetSymbolAddress(library, "FskMediaReaderGetTrack", &FskMediaReaderGetTrackProc));
	bailIfError(FskLibraryGetSymbolAddress(library, "FskMediaReaderStart", &FskMediaReaderStartProc));
	bailIfError(FskLibraryGetSymbolAddress(library, "FskMediaReaderStop", &FskMediaReaderStopProc));
	bailIfError(FskLibraryGetSymbolAddress(library, "FskMediaReaderTrackGetProperty", &FskMediaReaderTrackGetPropertyProc));
#endif
	bailIfError(FskSndChannelNew(&gSoundChannel, 0, kFskAudioFormatUndefined, 0));
	FskTimeCallbackNew(&gSoundTimer);

	FskSndChannelSetProperty(gSoundChannel, kFskMediaPropertyAudioCategory, &ambientAudio);

bail:
#if FSK_EXTENSION_EMBED
#else
	if (err)
		FskLibraryUnload(library);
	else
		FskMemPtrDispose(library);
	FskMemPtrDispose(libraryPath);
	FskMemPtrDispose(applicationPath);
#endif
	return;
}
Beispiel #7
0
FskErr KprSoundPlay(KprSound self)
{
	FskErr err = kFskErrNone;
	FskSndChannel soundChannel = gSoundChannel;
    float volume;
	UInt32 c;
	KprSoundFrame frame;
	
	bailIfNULL(soundChannel);
	bailIfError(KprSoundLoad(self));
	FskTimeCallbackRemove(gSoundTimer);
	FskSndChannelSetMoreCallback(soundChannel, NULL, NULL);
	FskSndChannelGetVolume(soundChannel, &volume);
    FskSndChannelStop(soundChannel);
	bailIfError(FskSndChannelSetFormat(soundChannel, kFskAudioFormatUndefined, self->audioFormat, self->numChannels, self->sampleRate, self->formatInfo, self->formatInfoSize));
	c = self->frameCount;
	frame = &self->frames[0];
	while (c) {
		bailIfError(FskSndChannelEnqueue(soundChannel, self->data + frame->frameOffset, frame->frameSize * frame->count, frame->count, frame->samplesPerFrame, NULL, NULL));
		c--;
		frame++;
	}
    self->playing = true;
	FskSndChannelSetVolume(soundChannel, volume);
	FskSndChannelSetMoreCallback(soundChannel, KprSoundPlayMore, self);
	bailIfError(FskSndChannelStart(soundChannel, 0));
bail:	
	return err;
}
FskErr KprCoAPClientResolverNew(KprCoAPClientResolver *it, KprCoAPClient client, const char *host, UInt16 port, KprCoAPMessage message)
{
	FskErr err = kFskErrNone;
	KprCoAPClientResolver self = NULL;

	bailIfError(KprMemPtrNewClear(sizeof(KprCoAPClientResolverRecord), &self));

	self->host = FskStrDoCopy(host);
	bailIfNULL(self->host);

	bailIfError(KprCoAPMessageQueueAppend(&self->waiting, message));

	self->client = client;
	self->port = port;

	bailIfError(FskNetHostnameResolveQTAsync((char *) host, 0, KprCoAPClientResolverResolved, self, &self->resolver));

	bailIfError(self->err);

	self->constructed = true;
	*it = self;

bail:
	if (err) {
		KprCoAPClientResolverDispose(self);
	}
	return err;
}
Beispiel #9
0
FskErr KprSocketConnect(KprSocket self, const char *host, int port)
{
	FskErr err = kFskErrNone;
	long flags = 0;

	if (self->mode != kKprSocketModeUndefined) return kFskErrBadState;

	self->host = FskStrDoCopy(host);
	bailIfNULL(self->host);

	self->port = port;
	self->mode = kKprSocketModeTCP;
	if (self->secure) {
		FskSSLOption ssl;

		flags |= kConnectFlagsSSLConnection;

		FskNetSSLOptionInitialize(self->host, self->port, flags, kFskNetSocketDefaultPriority, &ssl);

		ssl.protocolVersion = self->tlsProtocolVersion;
		ssl.applicationProtocols = self->applicationProtocols;
		bailIfError(FskNetConnectToSecureHost(&ssl, self->certs, KprSocketOnConnect, self));
	} else {
		bailIfError(FskNetConnectToHost((char *) self->host, self->port, false, KprSocketOnConnect, self, flags, NULL, "KprSocketTCP"));
	}

bail:
	if (err) {
		KprSocketCleanup(self);
	}
	return err;
}
Beispiel #10
0
static FskErr KprWebSocketServerRequestNew(KprWebSocketServerRequest *it, KprWebSocketServer server, FskSocket skt, const char *interfaceName, int ip) {
	FskErr err = kFskErrNone;
	KprWebSocketServerRequest request = NULL;

	bailIfError(FskMemPtrNewClear(sizeof(KprWebSocketServerRequestRecord), &request));

	request->server = server;
	request->skt = skt;
	skt = NULL;

	request->interfaceName = FskStrDoCopy(interfaceName);
	bailIfNULL(request->interfaceName);
	request->ip = ip;

	FskNetSocketGetRemoteAddress(request->skt, (UInt32 *)&request->requesterAddress, &request->requesterPort);
	FskNetSocketMakeNonblocking(request->skt);
	bailIfError(FskHeaderStructNew(&request->requestHeaders));
	bailIfError(FskHeaderStructNew(&request->responseHeaders));

	request->out.size = 512;
	bailIfError(FskMemPtrNew(request->out.size, &request->out.buffer));

	FskThreadAddDataHandler(&request->dataHandler, (FskThreadDataSource)request->skt, (FskThreadDataReadyCallback)KprWebSocketServerRequestDoRead, true, false, request);
	//	request->state = kHTTPNewSession;
	
	*it = request;

bail:
	if (err) {
		KprWebSocketServerRequestDispose(request);
		FskNetSocketClose(skt);
	}

	return err;
}
Beispiel #11
0
FskErr KprMQTTClientConnect(KprMQTTClient self, char *host, UInt16 port, KprMQTTClientConnectOptions *options)
{
	FskErr err = kFskErrNone;

	if (self->state != kKprMQTTStateDisconnected) {
		return kFskErrBadState;
	}

	FskMemPtrDisposeAt(&self->host);
	self->host = FskStrDoCopy(host);
	bailIfNULL(self->host);
	self->port = port;
	self->cancelConnection = false;

	KprMQTTMessageDispose(self->connectMessage);
	self->connectMessage = NULL;
	bailIfError(KprMQTTClientCreateConnectMessage(self, options, &self->connectMessage));

	self->keepAlive = options->keepAlive;

	bailIfError(KprMQTTClientReconnect(self));

bail:
	return err;
}
Beispiel #12
0
static FskErr KprMQTTClientCreatePublishMessage(KprMQTTClient self, char *topic, void *payload, UInt32 payloadLength, UInt8 qos, Boolean retain, KprMQTTMessage *it)
{
	FskErr err;
	KprMQTTMessage message = NULL;

	bailIfError(KprMQTTMessageNewWithType(&message, kKprMQTTMessageTypePUBLISH));

	message->qualityOfService = qos;
	message->isRetained = retain;

	if (qos > 0) {
		message->messageId = KprMQTTQueueNextId(self->queue);
	}

	message->t.publish.topic = FskStrDoCopy(topic);
	bailIfNULL(message->t.publish.topic);

	bailIfError(KprMemoryBufferNewFromData(payloadLength, payload, &message->t.publish.payload));

	*it = message;
	message = NULL;

bail:
	KprMQTTMessageDispose(message);
	return err;
}
Beispiel #13
0
static FskErr KprSocketNewFromFskSocket(FskSocket skt, KprSocketFlags flags, KprSocket *it)
{
	FskErr err = kFskErrNone;
	KprSocket self = NULL;
	UInt32 ipaddr;
	char ipaddrStr[22];

	bailIfError(FskMemPtrNewClear(sizeof(KprSocketRecord), &self));

	bailIfError(FskNetSocketGetLocalAddress(skt, &ipaddr, &self->port));
	FskNetIPandPortToString(ipaddr, 0, ipaddrStr);
	self->host = FskStrDoCopy(ipaddrStr);
	bailIfNULL(self->host);

	self->mode = kKprSocketModeTCP;
	self->proto = kKprSocketProtoTCP;
	self->secure = (Boolean)(flags & kKprSocketFlagsSecure);

	bailIfError(KprSocketOnConnect(skt, self));

	*it = self;

	FskListAppend(&gSockets, self);

bail:
	if (err) {
		KprSocketDispose(self);
	}

	return err;
}
Beispiel #14
0
FskErr KprZeroconfServiceNewAuthority(char* type, char** it)
{
#define kKPRZeroconfKinomaServe "_%s._tcp."
	FskErr err = kFskErrNone;
	char* authority = type + 1;
	char* dot = FskStrChr(type, '.');
	char* ptr;
	bailIfNULL(dot);
	*dot = 0;
	authority = FskStrDoCopy(type + 1);
	*dot = '.';
	bailIfNULL(authority);
	for (ptr = FskStrChr(authority, '_'); ptr; ptr = FskStrChr(ptr, '_')) 
		*ptr = '.';
	*it = authority;
bail:
	return err;
}
Beispiel #15
0
FskErr KprSSDPClientRemove(KprSSDPClient self, const char* uuid)
{
	FskErr err = kFskErrNone;
	char* it = FskStrDoCopy(uuid);
	bailIfNULL(it);
	FskThreadPostCallback(KprHTTPGetThread(), (FskThreadCallback)KprSSDPRemoveDiscoveryByUUID, it, NULL, NULL, NULL);
bail:
	return err;
}
Beispiel #16
0
FskErr KprSocketSetTLSApplicationProtocols(KprSocket self, const char *protocols)
{
	FskErr err = kFskErrNone;

	if (self->applicationProtocols) FskMemPtrDispose((void *) self->applicationProtocols);
	self->applicationProtocols = FskStrDoCopy(protocols);
	bailIfNULL(self->applicationProtocols);

bail:
	return err;
}
Beispiel #17
0
FskErr KprMessageSetMethod(KprMessage self, char* method)
{
	FskErr err = kFskErrNone;
	FskMemPtrDisposeAt(&self->method);
	if (method) {
		self->method = FskStrDoCopy(method);
		bailIfNULL(self->method);
	}
bail:
	return err;
}
Beispiel #18
0
FskErr KprApplicationNew(KprApplication* it, char* url, char* id, Boolean breakOnStart, Boolean breakOnExceptions)
{
	KprCoordinatesRecord coordinates = { kprLeftRight, kprTopBottom, 0, 0, 0, 0, 0, 0 };
	xsAllocation allocation = {
		2 * 1024 * 1024,
		1024 * 1024,
		64 * 1024,
		8 * 1024,
		2048,
		16000,
		1993
	};
	FskErr err = kFskErrNone;
	KprApplication self;
	
	bailIfError(FskMemPtrNewClear(sizeof(KprApplicationRecord), it));
	self = *it;
	FskInstrumentedItemNew(self, NULL, &KprApplicationInstrumentation);
	self->dispatch = &KprApplicationDispatchRecord;
	self->flags = kprContainer | kprClip | kprVisible;
	KprContentInitialize((KprContent)self, &coordinates, NULL, NULL);
	bailIfError(KprURLMerge(gShell->url, url, &self->url));	
	if (id) {
		self->id = FskStrDoCopy(id);	
		bailIfNULL(self->id);
	}
	self->the = xsAliasMachine(&allocation, gShell->root, self->url, self);
	if (!self->the) 
		BAIL(kFskErrMemFull);
	FskInstrumentedItemSendMessageNormal(self, kprInstrumentedContentCreateMachine, self);
	xsBeginHost(self->the);
	xsResult = xsNewHostFunction(KPR_include, 1);
	xsSet(xsResult, xsID("uri"), xsString(self->url));
	xsNewHostProperty(xsGlobal, xsID("include"), xsResult, xsDontDelete | xsDontSet, xsDontScript | xsDontDelete | xsDontSet);
	xsResult = xsNewHostFunction(KPR_require, 1);
	xsSet(xsResult, xsID("uri"), xsString(self->url));
	xsNewHostProperty(xsGlobal, xsID("require"), xsResult, xsDontDelete | xsDontSet, xsDontScript | xsDontDelete | xsDontSet);
	xsResult = xsNewInstanceOf(xsGet(xsGet(xsGlobal, xsID("KPR")), xsID("application")));
	self->slot = xsResult;
	xsSetHostData(xsResult, self);
	(void)xsCall1(xsGet(xsGlobal, xsID("Object")), xsID("seal"), xsResult);
	xsNewHostProperty(xsGlobal, xsID("application"), xsResult, xsDontDelete | xsDontSet, xsDontScript | xsDontDelete | xsDontSet);
	xsNewHostProperty(xsGlobal, xsID("shell"), xsNull, xsDontDelete | xsDontSet, xsDontScript | xsDontDelete | xsDontSet);
	if (breakOnStart)
		xsDebugger();
    if (breakOnExceptions)
		(void)xsCall1(xsGet(xsGet(xsGlobal, xsID("xs")), xsID("debug")), xsID("setBreakOnException"), xsBoolean(breakOnExceptions));
	(void)xsCall1(xsGlobal, xsID("include"), xsString(self->url));
	xsEndHost(self->the);
	KprContentChainPrepend(&gShell->applicationChain, self, 0, NULL);
bail:
	return err;
}
Beispiel #19
0
FskErr KprMessageNew(KprMessage *it, char* url)
{
	FskErr err = kFskErrNone;
	KprMessage self;
	bailIfError(FskMemPtrNewClear(sizeof(KprMessageRecord), it));
	self = *it;
	FskInstrumentedItemNew(self, NULL, &KprMessageInstrumentation);
	self->url = FskStrDoCopy(url);
	bailIfNULL(self->url);
	KprURLSplit(self->url, &self->parts);
	self->priority = 256;
bail:
	return err;
}
Beispiel #20
0
FskErr KprSSDPClientNew(KprSSDPClient *it, const char* type)
{
	FskErr err = kFskErrNone;
	KprSSDPClient self = NULL;

	bailIfError(FskMemPtrNewClear(sizeof(KprSSDPClientRecord), it));
	self = *it;
	if (type) {
		self->type = FskStrDoCopy(type);
		bailIfNULL(self->type);
	}
	FskInstrumentedItemNew(self, NULL, &KprSSDPClientInstrumentation);
bail:
	if (err)
		KprSSDPClientDispose(self);
	return err;
}
Beispiel #21
0
FskErr KprSSDPCommonAddService(KprSSDPCommon self, char* service)
{
    FskErr err = kFskErrNone;
    UInt32 i = 0;
    char* tmp = FskStrDoCopy(service);
    bailIfNULL(tmp);
    if (self->services)
        for (i = 0; self->services[i]; i++);
    bailIfError(FskMemPtrRealloc(sizeof(char**) * (i + 2), &self->services));
    self->services[i] = tmp;
    self->services[i + 1] = NULL;
    return err;
bail:
    if (tmp)
        FskMemPtrDispose(tmp);
    return err;
}
Beispiel #22
0
void KPR_system_set_backGesture(xsMachine *the)
{
	FskErr err = kFskErrNone;
	char* path = NULL;
	char* pathName = NULL;
	FskFile file = NULL;
	char* data = xsToString(xsArg(0));
	xsIntegerValue size = FskStrLen(data);
#if MINITV
	pathName = TOUCHD_CONF;
	// system("/etc/init.d/touchd.sh stop");
#else
	bailIfError(FskDirectoryGetSpecialPath(kFskDirectorySpecialTypeApplicationPreference, true, NULL, &path));
	pathName = FskStrDoCat(path, "touchd.conf");
	bailIfNULL(pathName);
#endif
	err = FskFileOpen(pathName, kFskFilePermissionReadWrite, &file);
	if (kFskErrFileNotFound == err) {
		bailIfError(FskFileCreate(pathName));
		err = FskFileOpen(pathName, kFskFilePermissionReadWrite, &file);
	}
	else {
		FskInt64 zero = 0;
		err = FskFileSetSize(file, &zero);
	}
	bailIfError(err);
	bailIfError(FskFileWrite(file, size, data, NULL));
bail:
	FskFileClose(file);
#if MINITV
    int pipe = open(TOUCH_PIPE, O_WRONLY | O_NONBLOCK);
    if (pipe >= 0) {
    	char data = 1;
    	write(pipe, &data, 1);
        close(pipe);
    }
	// system("/etc/init.d/touchd.sh start");
#endif
	if (path) {
		FskMemPtrDispose(path);
		FskMemPtrDispose(pathName);
	}
	xsThrowIfFskErr(err);
}
Beispiel #23
0
static void KprServicesLoop(void* theParameter)
{
	xsAllocation allocation = {
		1100000,		// 700 * 1024,
		32 * 1024,
		70000,			// 62000,
		4096,
		2048,
		16000,
		1993
	};
	FskErr err = kFskErrNone;
	KprService service = gServices;
	FskThread thread = FskThreadGetCurrent();
	gServiceMachine = xsAliasMachine(&allocation, gShell->root, "services", gShell);
	bailIfNULL(gServiceMachine);

	while (service) {
		if (service->flags & kprServicesThread) {
			FskInstrumentedTypePrintfNormal(&KprServiceInstrumentation, "Starting %s", service->id);
			(*service->start)(service, thread, gServiceMachine);
		}
		service = service->next;
	}
	FskThreadInitializationComplete(thread);
	while (!gServicesThreadQuitting)
		FskThreadRunloopCycle(-1);
	service = gServices;
	while (service) {
		if (service->flags & kprServicesThread) {
			FskInstrumentedTypePrintfNormal(&KprServiceInstrumentation, "Stopping %s", service->id);
			(*service->stop)(service);
		}
		service = service->next;
	}
bail:
	if (gServiceMachine) {
		xsDeleteMachine(gServiceMachine);
		gServiceMachine = NULL;
	}
	return;
}
FskErr KprNetworkInterfaceMark(int ip, char *name)
{
	FskErr err = kFskErrNone;
	KprNetworkInterface* address = &gNetworkInterface;
	KprNetworkInterface self;
	while ((self = *address)) {
		if ((self->ip == ip) && !FskStrCompare(self->name, name)) {
			self->flag = 1;
			return err;
		}
        address = &self->next;
	}
	bailIfError(FskMemPtrNewClear(sizeof(KprNetworkInterfaceRecord), address));
	self = *address;
	self->ip = ip;
	self->name = FskStrDoCopy(name);
	bailIfNULL(self->name);
bail:
	return err;
}
Beispiel #25
0
FskErr KprSoundNew(KprSound *it, KprContext context, char* base, char* url, char* mime)
{
	FskErr err = kFskErrNone;
	KprSound self;
	KprSoundSetup();
	bailIfError(KprAssetNew((KprAsset *)it, sizeof(KprSoundRecord), context, &context->firstSound, KprSoundDispose));
	self = *it;	
	FskInstrumentedItemNew(self, NULL, &KprSoundInstrumentation);
#if SUPPORT_INSTRUMENTATION
	FskInstrumentedItemSetOwner(self, context);
#endif
	if (base && url)
		bailIfError(KprURLMerge(base, url, &self->url));	
	if (mime) {
		self->mime = FskStrDoCopy(mime);
		bailIfNULL(self->mime);
	}
bail:
	return err;
}
Beispiel #26
0
FskErr KprMessageURL(KprContext context, char* url, char** result)
{
	FskErr err = kFskErrNone;
	KprURLPartsRecord parts;
	KprURLSplit(url, &parts);
	if (!parts.scheme) {
		parts.scheme = "xkpr";
		parts.schemeLength = 4;
		if (!parts.authority) {
			parts.authority = context->id;
			parts.authorityLength = FskStrLen(parts.authority);
		}
		bailIfError(KprURLJoin(&parts, result));
	}
	else {
		*result = FskStrDoCopy(url);
		bailIfNULL(*result);
	}
bail:
	return err;
}
Beispiel #27
0
FskErr KprZeroconfBrowserNew(KprZeroconfBrowser *it, const char* serviceType)
{
	FskErr err = kFskErrNone;
	KprZeroconfBrowser self = NULL;

	if (KprZeroconfBrowserFind(NULL, serviceType)) {
		bailIfError(kFskErrDuplicateElement); // only one browser per serviceType
	}
	bailIfError(FskMemPtrNewClear(sizeof(KprZeroconfBrowserRecord), it));
	self = *it;
	if (serviceType) {
		self->serviceType = FskStrDoCopy(serviceType);
		bailIfNULL(self->serviceType);
	}
	FskInstrumentedItemNew(self, NULL, &KprZeroconfBrowserInstrumentation);
	bailIfError(KprZeroconfPlatformBrowserNew(self));
bail:
	if (err)
		KprZeroconfBrowserDispose(self);
	return err;
}
Beispiel #28
0
FskErr KprZeroconfPlatformServiceNew(KprZeroconfPlatformService* it, KprZeroconfPlatformService owner, DNSServiceRef serviceRef, const char* name, UInt32 port)
{
	FskErr err = kFskErrNone;
	KprZeroconfPlatformService self = NULL;
	SInt32 fd = DNSServiceRefSockFD(serviceRef);
	bailIfError(FskMemPtrNewClear(sizeof(KprZeroconfPlatformServiceRecord), &self));
	FskInstrumentedItemNew(self, NULL, &KprZeroconfPlatformServiceInstrumentation);
	self->owner = owner;
	self->serviceRef = serviceRef;
	if (name) {
		self->name = FskStrDoCopy(name);
		bailIfNULL(self->name);
	}
	self->port = port;
#if KPR_ZEROCONF_EMBEDDED
	if (!gEmbeddedServiceCount) {
		FskTimeCallbackNew(&gEmbeddedServiceTimer);
		FskTimeCallbackScheduleFuture(gEmbeddedServiceTimer, 0, 0, KprZeroconfEmbeddedCallback, NULL);
	}
	gEmbeddedServiceCount++;
#elif TARGET_OS_MAC
	CFSocketContext context;
	FskMemSet(&context, 0, sizeof(context));
	context.info = (void*)serviceRef;
	self->socket = CFSocketCreateWithNative(kCFAllocatorDefault, fd, kCFSocketReadCallBack, KprZeroconfPlatformCallBack, &context);
	self->source = CFSocketCreateRunLoopSource(NULL, self->socket, 0);
	CFRunLoopAddSource(CFRunLoopGetCurrent(), self->source, kCFRunLoopCommonModes);
#else
	self->source = FskThreadCreateDataSource(fd);
	FskSocketActivate(self->source, true);
	FskThreadAddDataHandler(&self->handler, self->source, KprZeroconfPlatformCallBack, true, false, serviceRef);
#endif
	*it = self;
	return err;
bail:
	if (err) {
		KprZeroconfPlatformServiceDispose(self);
	}
	return err;
}
Beispiel #29
0
FskErr KprMQTTQueueNew(KprMQTTQueue *it, UInt16 resendInterval, KprMQTTQueueCallback callback, void *refcon)
{
	FskErr err = kFskErrNone;
	KprMQTTQueue self;

	bailIfError(FskMemPtrNewClear(sizeof(KprMQTTQueueRecord), &self));

	FskTimeCallbackNew(&self->resendCallback);
	bailIfNULL(self->resendCallback);

	self->refcon = refcon;
	self->callback = callback;
	self->resendInterval = resendInterval;
	self->messageId = 1;
	self->pause = true;

	*it = self;
	self = NULL;

bail:
	KprMQTTQueueDispose(self);
	return err;
}
Beispiel #30
0
static FskErr KprMQTTClientCreateUnsubscribeMessage(KprMQTTClient self, char *topic, KprMQTTMessage *it)
{
	FskErr err;
	KprMQTTMessage message = NULL;
	KprMQTTSubscribeTopic st;

	bailIfError(KprMQTTMessageNewWithType(&message, kKprMQTTMessageTypeUNSUBSCRIBE));

	message->messageId = KprMQTTQueueNextId(self->queue);

	bailIfError(KprMQTTMessageAddSubscribeTopic(message));
	st = KprMQTTMessageLastSubscribeTopic(message);

	st->topic = FskStrDoCopy(topic);
	bailIfNULL(st->topic);

	*it = message;
	message = NULL;

bail:
	KprMQTTMessageDispose(message);
	return err;
}