My Project
Classes | Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
NPLInterface::CNPLMiniState Class Reference

One can usually implement one or more methods to make it a standalone runtime state. More...

#include <NPLMiniRuntime.hpp>

Inheritance diagram for NPLInterface::CNPLMiniState:
NPL::INPLRuntimeState

Classes

class  CCurrentMessage
 construct this to ensure matching calls to SetCurrentMessage(). More...
 

Public Types

typedef boost::function< void(int, void *) > NPLFileActivateHandler_t
 
typedef boost::function< void(int, void *) > NPLFileActivateHandlerCallback_t
 
typedef std::map< std::string, typename NPLFileActivateHandler_t > NPLFileHandlerMap_t
 
typedef std::queue< NPLMiniMessageNPLMiniMessageQueue_t
 

Public Member Functions

 CNPLMiniState (const char *name=NULL)
 
void Init ()
 
virtual const std::string & GetName () const
 return the name of this runtime state. More...
 
virtual int activate (const char *sNPLFilename, const char *sCode, int nCodeLength=0, int priority=2, int reliability=4)
 activate the specified file. More...
 
virtual NPL::NPLReturnCode Activate_async (const std::string &filepath, const char *code=NULL, int nLength=0, int priority=0)
 activate the specified file in this runtime state. More...
 
virtual NPL::NPLReturnCode ActivateLocal (const char *filepath, const char *code=NULL, int nLength=0, int priority=0)
 same as Activate_async, except that it is a short cut name. More...
 
virtual NPL::NPLReturnCode Activate_async (NPL::NPLMessage_ptr &msg, int priority=0)
 same as Activate_async. More...
 
virtual NPL::NPLReturnCode SendMessage (NPL::NPLMessage_ptr &msg, int priority=0)
 send a message to the current message queue. More...
 
virtual const char * GetCurrentMsg ()
 get a pointer to the current message
 
virtual int GetCurrentMsgLength ()
 get length of the current message
 
virtual NPL::INPLRuntimeGetNPLRuntime ()
 get the NPL runtime environment
 
virtual void WriteLog (const char *text, int nTextLen=0, int nLogType=0)
 write a log message More...
 
virtual bool SetTimer (int nIDEvent, float fElapse, const char *sNeuronFile)
 creates a timer with the specified time-out value [thread safe] More...
 
virtual bool KillTimer (int nIDEvent)
 Destroys the specified timer [thread safe]. More...
 
virtual bool ChangeTimer (int nIDEvent, int dueTime, int period)
 Changes the start time and the interval between method invocations for a timer, using 32-bit signed integers to measure time intervals. More...
 
virtual void RegisterFile (const char *sFilename, NPL::INPLActivationFile *pFileHandler=NULL)
 function to register the a file handler in the current NPL state, so that it is callable from NPL script or C++ More...
 
virtual void call (const char *sNPLFilename, const char *sCode, int nCodeLength=0)
 synchronous function call
 
virtual int Process ()
 process all queued message. More...
 
bool RegisterFileHandler (const char *filename, const NPLFileActivateHandlerCallback_t &fileCallback)
 if USE_BOOST_SIGNAL_FILE_HANDLER is defined, we will allow multiple file handlers to subscribe to the same filename target. More...
 
- Public Member Functions inherited from NPL::INPLRuntimeState
virtual NPLReturnCode Activate_async (NPLMessage_ptr &msg, int priority=0)=0
 same as Activate_async. More...
 
virtual NPLReturnCode SendMessage (NPLMessage_ptr &msg, int priority=0)=0
 send a message to the current message queue. More...
 

Protected Member Functions

void SetCurrentMessage (const char *msg, int nLength)
 
int ProcessMsg (const NPLMiniMessage &msg)
 process a single message. More...
 

Protected Attributes

const char * m_current_msg
 pointer to the current message. More...
 
int m_current_msg_length
 length of the current message. More...
 
std::string m_name
 the name of this runtime state. More...
 
ParaEngine::Mutex m_mutex
 
NPL::INPLRuntimem_pNPLRuntime
 
NPLMiniMessageQueue_t m_input_queue
 the input message queue
 
NPLFileHandlerMap_t m_file_handlers_map
 file handlers map. More...
 
int m_processed_msg_count
 for stats
 

Detailed Description

One can usually implement one or more methods to make it a standalone runtime state.

Depending on how you implement the activate() method, the message can be handled either in the calling thread or in the main thread. the main thread is the thread where NPLRuntime::Run() method is called at regular interval to process the message.

the default implementation can register message handler according to filename using callbacks. please note that filename is usually used as message target in traditional message system. Since we use std::string as filename internally, hence if the file name is less than 16 bytes, no memory allocation is needed. Note: this only applies to MiniState, for full featured NPL runtime, there is no such limit.

Member Function Documentation

§ activate()

virtual int NPLInterface::CNPLMiniState::activate ( const char *  sNPLFilename,
const char *  sCode,
int  nCodeLength = 0,
int  priority = 2,
int  reliability = 4 
)
inlinevirtual

activate the specified file.

It can either be local or remote file.

Implements NPL::INPLRuntimeState.

§ Activate_async() [1/2]

virtual NPL::NPLReturnCode NPLInterface::CNPLMiniState::Activate_async ( const std::string &  filepath,
const char *  code = NULL,
int  nLength = 0,
int  priority = 0 
)
inlinevirtual

activate the specified file in this runtime state.

the file can be script or DLL. The function will just insert the message into the message queue and return immediately.

Parameters
codeit is a chunk of code that should be executed in the destination neuron. this code usually set the values of POL global variables.
nLengththe code length. if this is 0, length is determined from code, however, it must not exceed 4096 bytes. if it is specified. it can be any code length
prioritybigger is higher. 0 is the default. if 1, it will be inserted to the front of the queue.
Returns
: NPLReturnCode

Implements NPL::INPLRuntimeState.

§ Activate_async() [2/2]

virtual NPL::NPLReturnCode NPLInterface::CNPLMiniState::Activate_async ( NPL::NPLMessage_ptr &  msg,
int  priority = 0 
)
inlinevirtual

same as Activate_async.

except that input are read from NPLMesage. e.g. NPLMessage_ptr msg(new NPLMessage()); return Activate_async(msg, priority);

Parameters
msgthe caller is should only new but never delete the NPLMessage_ptr. And that the message must be created in the same thread, usually just before calling this function

§ ActivateLocal()

virtual NPL::NPLReturnCode NPLInterface::CNPLMiniState::ActivateLocal ( const char *  filepath,
const char *  code = NULL,
int  nLength = 0,
int  priority = 0 
)
inlinevirtual

same as Activate_async, except that it is a short cut name.

and may be used by external dlls to activate a file on this local state asynchrounously.

Implements NPL::INPLRuntimeState.

§ ChangeTimer()

virtual bool NPLInterface::CNPLMiniState::ChangeTimer ( int  nIDEvent,
int  dueTime,
int  period 
)
inlinevirtual

Changes the start time and the interval between method invocations for a timer, using 32-bit signed integers to measure time intervals.

[thread safe]

Parameters
nIDEventSpecifies the timer to be destroyed.For nIDEvent<=0, they are reserved for internal uses can not be killed by this function. This value must be the same as the nIDEvent value passed to the SetTimer function that created the timer.
dueTimeThe amount of time to delay before the invoking the callback method specified when the Timer was constructed, in milliseconds. Specify zero (0) to restart the timer immediately. however, the current implementation does not accept dueTime that is larger than MAX_TIMER_DUE_TIME 10000000, which is 10000 seconds.
period:Thetime interval between invocations of the callback method specified when the Timer was constructed, in milliseconds.
Returns
: If the function succeeds, the return value is true

Implements NPL::INPLRuntimeState.

§ GetName()

virtual const std::string& NPLInterface::CNPLMiniState::GetName ( ) const
inlinevirtual

return the name of this runtime state.

if "", it is considered an anonymous name

Implements NPL::INPLRuntimeState.

§ KillTimer()

virtual bool NPLInterface::CNPLMiniState::KillTimer ( int  nIDEvent)
inlinevirtual

Destroys the specified timer [thread safe].

Parameters
nIDEventSpecifies the timer to be destroyed.For nIDEvent<=0, they are reserved for internal uses can not be killed by this function. This value must be the same as the nIDEvent value passed to the SetTimer function that created the timer.
Returns
: If the function succeeds, the return value is true

Implements NPL::INPLRuntimeState.

§ Process()

virtual int NPLInterface::CNPLMiniState::Process ( )
inlinevirtual

process all queued message.

This is usually called by the NPLMiniRuntime from the main thread.

§ ProcessMsg()

int NPLInterface::CNPLMiniState::ProcessMsg ( const NPLMiniMessage msg)
inlineprotected

process a single message.

§ RegisterFile()

virtual void NPLInterface::CNPLMiniState::RegisterFile ( const char *  sFilename,
NPL::INPLActivationFile pFileHandler = NULL 
)
inlinevirtual

function to register the a file handler in the current NPL state, so that it is callable from NPL script or C++

Parameters
sFilenameany name with cpp file extension can be used. usually it is "states.cpp". The name does not need to be same as the real cpp file.
pFileHandlerif NULL it will unregister. If not, it is the file handler pointer, the pointer must be always valid, it is usually a static singleton object.

Implements NPL::INPLRuntimeState.

§ RegisterFileHandler()

bool NPLInterface::CNPLMiniState::RegisterFileHandler ( const char *  filename,
const NPLFileActivateHandlerCallback_t &  fileCallback 
)
inline

if USE_BOOST_SIGNAL_FILE_HANDLER is defined, we will allow multiple file handlers to subscribe to the same filename target.

but the caller will need to link with the boost signal lib. if not defined, only one handler is allowed per file. The latest registered handler will overwrite the previous one.

§ SendMessage()

virtual NPL::NPLReturnCode NPLInterface::CNPLMiniState::SendMessage ( NPL::NPLMessage_ptr &  msg,
int  priority = 0 
)
inlinevirtual

send a message to the current message queue.

This function is rarely needed to call directly, use Activate_async instead. e.g. NPLMessage_ptr msg(new NPLMessage()); return SendMessage(msg, priority);

Parameters
msgthe message to send. Please note that when the function returns, the msg will be reset to null.
Returns
may fail if message queue is full.

§ SetTimer()

virtual bool NPLInterface::CNPLMiniState::SetTimer ( int  nIDEvent,
float  fElapse,
const char *  sNeuronFile 
)
inlinevirtual

creates a timer with the specified time-out value [thread safe]

Parameters
nIDEventSpecifies a positive timer identifier. For nIDEvent<=0, they are reserved for internal uses. If the NPL runtime already has a timer with the value nIDEvent, then the existing timer is replaced by the new timer. When SetTimer replaces a timer, the timer is reset.
fElapseSpecifies the time-out value, in seconds. Please note that a timer will not be repeatedly activated if its timeout is shorter than the frame rate of the NPL simulation pipeline .
sNeuronFileThe NPL file to be activated when the time-out value elapses. For more information about the file name See NPL.activate().
Returns
: true if succeeds.An application can pass the value of the nIDEvent parameter to the NPL.KillTimer function to destroy the timer.

Implements NPL::INPLRuntimeState.

§ WriteLog()

virtual void NPLInterface::CNPLMiniState::WriteLog ( const char *  text,
int  nTextLen = 0,
int  nLogType = 0 
)
inlinevirtual

write a log message

Parameters
textthe content of the log message.
nTextLenthe log text length in byte. if 0, text length will be determined automatically.
nLogTypeif this is 0, it is a normal log message. if this is 1, we will print current time, and runtime state name with the log message.

Implements NPL::INPLRuntimeState.

Member Data Documentation

§ m_current_msg

const char* NPLInterface::CNPLMiniState::m_current_msg
protected

pointer to the current message.

it is only valid during activation call. NULL will be returned

§ m_current_msg_length

int NPLInterface::CNPLMiniState::m_current_msg_length
protected

length of the current message.

it is only valid during activation call.

§ m_file_handlers_map

NPLFileHandlerMap_t NPLInterface::CNPLMiniState::m_file_handlers_map
protected

file handlers map.

§ m_name

std::string NPLInterface::CNPLMiniState::m_name
protected

the name of this runtime state.

if "", it is considered an anonymous name


The documentation for this class was generated from the following file: