Click here to Skip to main content
15,892,005 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.
/* ==============================================================================================================================
 * This notice must be untouched at all times.
 *
 * Copyright  IntelliWizard Inc. 
 * All rights reserved.
 * LICENSE: LGPL. 
 * Redistributions of source code modifications must send back to the Intelliwizard Project and republish them. 
 * Web: http://www.intelliwizard.com
 * eMail: info@intelliwizard.com
 * We provide technical supports for UML StateWizard users.
 * ==============================================================================================================================*/
/* A state machine sample running on Linux/Win32 platforms, refer to Readme.txt.
*/


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

#include "Player.h"

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

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

BOOL g_bQuit=FALSE;

extern CPlayer Player1; 
extern CPlayer Player2;

SME_THREAD_CONTEXT_T g_AppThreadContext1;
SME_THREAD_CONTEXT_T g_AppThreadContext2;

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

	// Install thread local storage data functions.
	XTlsAlloc();
	SmeSetTlsProc(XSetThreadContext, XGetThreadContext);

	////////////////////////////////////////////////////////////////
	// Engine initialization.
	SmeInitEngine(&g_AppThreadContext1); // Initialize at the thread-1
	// Save the thread context pointer to the TLS.
	// XSetThreadContext(&g_AppThreadContext1);
	XInitMsgBuf();

	// 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

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

	// Create the Player2 application thread 
	ret = XCreateThread(AppThread2Proc, NULL, &ThreadHandle);

	SmeActivateObj(&Player1,NULL);
	SmeRun();
	printf("Exit from SmeRun() at thread-1 \n");
	XFreeMsgBuf();
	XFreeThreadContext(&g_AppThreadContext1); /* 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;
}

#ifdef WIN32
	unsigned __stdcall AppThread2Proc(void *Param)
#else
	void* AppThread2Proc(void *Param)
#endif
{
	SmeInitEngine(&g_AppThreadContext2); // Initialize at the thread-2
	// Save the thread context pointer to the TLS.
	// XSetThreadContext(&g_AppThreadContext2);
	XInitMsgBuf();
	// Install event handler functions.
	SmeSetExtEventOprProc(XGetExtEvent, XDelExtEvent, XPostThreadExtIntEvent, XPostThreadExtPtrEvent, XInitMsgBuf, XFreeMsgBuf);

	SmeActivateObj(&Player2,NULL);
	SmeRun();
	printf("Exit from SmeRun() at thread-2 \n");

	XFreeMsgBuf();
	XFreeThreadContext(&g_AppThreadContext2); /* At last, free thread local storage resource. */
	return 0;
}

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

#ifdef 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();
	
	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_AppThreadContext1, SME_EVENT_EXIT_LOOP, 0, 0, NULL,0,SME_EVENT_CAT_OTHER);
				printf("Exiting thread-1 ... Please wait. \n");
				XPostThreadExtIntEvent(&g_AppThreadContext2, SME_EVENT_EXIT_LOOP, 0, 0, NULL,0,SME_EVENT_CAT_OTHER);
				printf("Exiting thread-2 ... Please wait. \n");
				return 0;
				break;
			case '?'://Help
				ConsoleProcUsage();
				break;
			case '1':
				XPostThreadExtIntEvent(&g_AppThreadContext1, EXT_EVENT_ID_POWER, 0, 0, NULL,0,SME_EVENT_CAT_OTHER);
				printf("Post a message: EXT_EVENT_ID_POWER to the Thread-1!\n");
				break;
			case '2':
				XPostThreadExtIntEvent(&g_AppThreadContext1, EXT_EVENT_ID_PAUSE_RESUME, 0, 0, NULL,0,SME_EVENT_CAT_OTHER);
				printf("Post a message: EXT_EVENT_ID_PAUSE_RESUME to the Thread-1!\n");
				break;

			case 'a':
				XPostThreadExtIntEvent(&g_AppThreadContext2, EXT_EVENT_ID_POWER, 0, 0, NULL,0,SME_EVENT_CAT_OTHER);
				printf("Post a message: EXT_EVENT_ID_POWER to the Thread-2!\n");
				break;
			case 'b':
				XPostThreadExtIntEvent(&g_AppThreadContext2, EXT_EVENT_ID_PAUSE_RESUME, 0, 0, NULL,0,SME_EVENT_CAT_OTHER);
				printf("Post a message: EXT_EVENT_ID_PAUSE_RESUME to the Thread-2!\n");
				break;

			case '\n':
			case '\r':
				break;
			default:
				break;
		}
	}

	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