Click here to Skip to main content
15,892,298 members
Articles / Desktop Programming / Win32

Visual Modeling of Complex Reactive Systems with Harel UML StateCharts

Rate me:
Please Sign up or sign in to vote.
5.00/5 (7 votes)
8 Sep 2009LGPL310 min read 43.7K   692   35  
This article presents a commercial-grade cross-platform Harel UML StateChart Open-Source application framework named StateWizard for concurrent, distributed, and real-time reactive system development with simplicity, efficiency, and scalability.

#include <stdio.h>
#include "sme_cross_platform.h"
#include "sme_ext_event.h"
#include "sme.h"
#include "sme_debug.h"

#include "Player_c.h"
#include "Player2_c.h"

#include "SingleState.h"
#include "EventId.h"

#ifdef SME_WIN32
	unsigned __stdcall ConsoleProc(void *Param);
#else
	void* ConsoleProc(void *Param);
#endif

BOOL g_bQuit=FALSE;

SME_DEC_EXT_OBJ_VAR(Player1)

SME_THREAD_CONTEXT_T g_AppThreadContext;

int main(int argc, char* argv[])
{
	XTHREADHANDLE ThreadHandle = 0;
	int ret;
	printf("Cross-Platform Pseudo State Sample: \n");


	// Install thread local storage data functions.
	XTlsAlloc();
	SmeSetTlsProc(XSetThreadContext, XGetThreadContext);
	////////////////////////////////////////////////////////////////
	// Engine initialization.
	SmeInitEngine(&g_AppThreadContext); // Initialize engine
	// Save the thread context pointer to the TLS.
	// XSetThreadContext(&g_AppThreadContext);

	// Install event handler functions.
	SmeSetExtEventOprProc(XGetExtEvent, XDelExtEvent, XPostThreadExtIntEvent, XPostThreadExtPtrEvent, XInitMsgBuf, XFreeMsgBuf);
	// Install memory operation functions.
	// SmeSetMemOprProc(SrvMAlloc, SrvMFree);???
	////////////////////////////////////////////////////////////////

	//SME_TURN_OFF_ALL_LOG_FIELDS(); // Test case

	XInitMsgBuf();
	////////////////////////////////////////////////////////////////
	// Create a thread to trigger external events.
	ret = XCreateThread(ConsoleProc, NULL, &ThreadHandle);

	// Make the Player state as a temporary root instead of the DummyRoot.
	SME_MAKE_TEMP_ROOT(&Player1, &SME_STATE_REF(Player));
	SmeActivateObj(&Player1,NULL);


	SmeRun();
	printf("Exit from SmeRun() \n");
	XFreeMsgBuf();
	XFreeThreadContext(&g_AppThreadContext); /* At last, free thread local storage resource. */

	/*
	do {
		SME_EVENT_T * ret = XGetExtEvent();

		if (ret == NULL)
			printf("Timeout! \n");
		else
			printf("Message Received (id=%d,p1=%d,p2=%d)\n", ret->nEventID, 0,0);
	} while (XIsThreadRunning(ThreadHandle));
	*/
	return 0;
}

void ConsoleProcUsage()
{
	printf("Console command usage:\n");
	printf("==============================================================\n");
	printf("? - Help\n");
	printf("1 - Press Power button \n");
	printf("2 - Press Pause/Resume button \n");
	printf("x/X- Quit\n");
	printf("Others- Post a message.\n");
	printf("==============================================================\n");
}

#ifdef SME_WIN32
	unsigned __stdcall ConsoleProc(void *Param)
#else
	void* ConsoleProc(void *Param)
#endif
{
	// On Linux platform, call the XInitTimer function at the time-out event trigger thread. 
	// On time-out, the external event trigger thread posts SME_EVENT_TIMER to the state machine application thread,
	// and then invokes the callback function installed by the XSetTimer function. 
	XInitTimer();

	printf("Enter the console procedure thread.\n");
	ConsoleProcUsage();
	
	SME_TRACE_ASC_FMT(0,"Trace Test");

	while(TRUE)
	{
		int nParam1 =0;
		if(g_bQuit) break;

		//OSRelated::Sleep(ISVW_LOOP_INTERVAL);

		nParam1 = fgetc(stdin);

		switch(nParam1)
		{
			case EOF:	return 0;//Cancel ConsoleProc in Daemon

			case 'x':
			case 'X'://Quit
				XPostThreadExtIntEvent(&g_AppThreadContext, SME_EVENT_EXIT_LOOP, 0, 0, NULL,0,SME_EVENT_CAT_OTHER);
				printf("Exiting... Please wait. \n");
				return 0;
				break;
			case '?'://Help
				ConsoleProcUsage();
				break;
			case '1':
				XPostThreadExtIntEvent(&g_AppThreadContext, EXT_EVENT_ID_POWER, 0, 0, NULL,0,SME_EVENT_CAT_OTHER);
				printf("Post a message: EXT_EVENT_ID_POWER!\n");
				break;
			case '2':
				XPostThreadExtIntEvent(&g_AppThreadContext, EXT_EVENT_ID_PAUSE_RESUME, 0, 0, NULL,0,SME_EVENT_CAT_OTHER);
				printf("Post a message: EXT_EVENT_ID_PAUSE_RESUME!\n");
				break;

			case '\n':
			case '\r':
				break;
			default:
				break;
		}
	}
	XDestroyTimer();
	return 0;
}


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions