My Project
NPLRuntime.h
1 #pragma once
2 #include <set>
3 struct lua_State;
4 
5 // include NPLScriptingState
6 #include "ParaScripting.h"
7 #include "NPLRuntimeState.h"
8 #include "INPLRuntime.h"
9 /* internal data structure used by NPL runtime */
10 #include "NPLCommon.h"
11 #include "IAttributeFields.h"
12 
13 #include <boost/scoped_ptr.hpp>
14 #include <boost/thread/shared_mutex.hpp>
15 
16 namespace ParaEngine
17 {
19  class INPLJabberClient;
20  class CNPLNetClient;
21  class CURLRequestTask;
22 }
23 
28 namespace NPL
29 {
30  class CNPLNetServer;
31  using namespace ParaScripting;
32  class INPLStimulationPipe;
33  class CNPLNameSpaceBinding;
34  struct NPLPacket;
35 
36 
41  {
42  public:
43  int m_nPriority;
44  int m_nReliability;
48  ChannelProperty():m_nPriority(2),m_nReliability(3){};
49  ChannelProperty(int nPriority, int nReliability):m_nPriority(nPriority),m_nReliability(nReliability){};
50 
51  public:
53  void Set(int nPriority, int nReliability)
54  {
55  m_nPriority = nPriority;
56  m_nReliability = nReliability;
57  };
58  };
59 
65  {
66  public:
67  CNPLRuntime();
68  virtual ~CNPLRuntime(void);
69 
71  static CNPLRuntime* GetInstance();
73  static void SetDefaultNPLStateType(NPL::NPLRuntimeStateType val);
74 
76  virtual void Init();
77 
82  virtual void Run(bool bToEnd = true);
83 
85  virtual void Cleanup();
86 
88  // implementation of IAttributeFields
89 
91  virtual int GetAttributeClassID(){return ParaEngine::ATTRIBUTE_CLASSID_CNPLRuntime;}
93  virtual const char* GetAttributeClassName(){static const char name[] = "CNPLRuntime"; return name;}
95  virtual const char* GetAttributeClassDescription(){static const char desc[] = ""; return desc;}
97  virtual int InstallFields(ParaEngine::CAttributeClass* pClass, bool bOverride);
99  virtual int GetChildAttributeObjectCount(int nColumnIndex = 0);
100  virtual ParaEngine::IAttributeFields* GetChildAttributeObject(int nRowIndex, int nColumnIndex = 0);
101 
102  ATTRIBUTE_METHOD1(CNPLRuntime, IsServerStarted_s, bool*) { *p1 = cls->IsServerStarted(); return S_OK; }
103  ATTRIBUTE_METHOD1(CNPLRuntime, GetHostIP_s, const char**) { *p1 = cls->GetHostIP().c_str(); return S_OK; }
104  ATTRIBUTE_METHOD1(CNPLRuntime, GetHostPort_s, const char**) { *p1 = cls->GetHostPort().c_str(); return S_OK; }
105 
106  ATTRIBUTE_METHOD1(CNPLRuntime, IsTCPKeepAliveEnabled_s, bool*) {*p1 = cls->IsTCPKeepAliveEnabled(); return S_OK;}
107  ATTRIBUTE_METHOD1(CNPLRuntime, SetTCPKeepAlive_s, bool) {cls->SetTCPKeepAlive(p1); return S_OK;}
108 
109  ATTRIBUTE_METHOD1(CNPLRuntime, IsKeepAliveEnabled_s, bool*) {*p1 = cls->IsKeepAliveEnabled(); return S_OK;}
110  ATTRIBUTE_METHOD1(CNPLRuntime, SetKeepAlive_s, bool) {cls->SetKeepAlive(p1); return S_OK;}
111 
112  ATTRIBUTE_METHOD1(CNPLRuntime, IsIdleTimeoutEnabled_s, bool*) {*p1 = cls->IsIdleTimeoutEnabled(); return S_OK;}
113  ATTRIBUTE_METHOD1(CNPLRuntime, EnableIdleTimeout_s, bool) {cls->EnableIdleTimeout(p1); return S_OK;}
114 
115  ATTRIBUTE_METHOD1(CNPLRuntime, GetIdleTimeoutPeriod_s, int*) {*p1 = cls->GetIdleTimeoutPeriod(); return S_OK;}
116  ATTRIBUTE_METHOD1(CNPLRuntime, SetIdleTimeoutPeriod_s, int) {cls->SetIdleTimeoutPeriod(p1); return S_OK;}
117 
118  ATTRIBUTE_METHOD1(CNPLRuntime, GetCompressionLevel_s, int*) {*p1 = cls->GetCompressionLevel(); return S_OK;}
119  ATTRIBUTE_METHOD1(CNPLRuntime, SetCompressionLevel_s, int) {cls->SetCompressionLevel(p1); return S_OK;}
120 
121  ATTRIBUTE_METHOD1(CNPLRuntime, GetCompressionThreshold_s, int*) {*p1 = cls->GetCompressionThreshold(); return S_OK;}
122  ATTRIBUTE_METHOD1(CNPLRuntime, SetCompressionThreshold_s, int) {cls->SetCompressionThreshold(p1); return S_OK;}
123 
124  ATTRIBUTE_METHOD1(CNPLRuntime, IsAnsiMode_s, bool*) {*p1 = cls->IsAnsiMode(); return S_OK;}
125  ATTRIBUTE_METHOD1(CNPLRuntime, EnableAnsiMode_s, bool) {cls->EnableAnsiMode(p1); return S_OK;}
126 
127  ATTRIBUTE_METHOD1(CNPLRuntime, GetMaxPendingConnections_s, int*) { *p1 = cls->GetMaxPendingConnections(); return S_OK; }
128  ATTRIBUTE_METHOD1(CNPLRuntime, SetMaxPendingConnections_s, int) { cls->SetMaxPendingConnections(p1); return S_OK; }
129 
130  ATTRIBUTE_METHOD1(CNPLRuntime, GetLogLevel_s, int*) { *p1 = cls->GetLogLevel(); return S_OK; }
131  ATTRIBUTE_METHOD1(CNPLRuntime, SetLogLevel_s, int) { cls->SetLogLevel(p1); return S_OK; }
132 
133  public:
138  virtual void SetUseCompression(bool bCompressIncoming, bool bCompressOutgoing);
139 
152  virtual void SetCompressionKey(const byte* sKey=0, int nSize=0, int nUsePlainTextEncoding = 0);
153 
161  virtual void SetCompressionLevel(int nLevel);
162  virtual int GetCompressionLevel();
163 
168  virtual void SetCompressionThreshold(int nThreshold);
169  virtual int GetCompressionThreshold();
170 
185  virtual void SetTCPKeepAlive(bool bEnable);
186 
190  virtual bool IsTCPKeepAliveEnabled();
191 
196  virtual void SetKeepAlive(bool bEnable);
197  virtual bool IsKeepAliveEnabled();
198 
205  virtual void EnableIdleTimeout(bool bEnable);
206  virtual bool IsIdleTimeoutEnabled();
207 
209  virtual void SetIdleTimeoutPeriod(int nMilliseconds);
210  virtual int GetIdleTimeoutPeriod();
211 
215  virtual void EnableAnsiMode(bool bEnable);
216  virtual bool IsAnsiMode();
217 
219  virtual int GetMaxPendingConnections();
220  virtual void SetMaxPendingConnections(int val);
221 
222 
224  virtual const std::string& GetHostPort();
226  virtual const std::string& GetHostIP();
228  virtual bool IsServerStarted();
229 
230 
232  //
233  // NPL core functions
234  //
236 
253  void NPL_LoadFile(NPLRuntimeState_ptr runtime_state, const char* filePath, bool bReload);
254 
298  int NPL_Activate(NPLRuntimeState_ptr runtime_state, const char * sNeuronFile, const char * code = NULL,int nLength=0, int channel=0, int priority=2, int reliability=3);
299 
301  virtual int Activate(INPLRuntimeState* pRuntimeState, const char * sNeuronFile, const char * code = NULL,int nLength=0, int channel=0, int priority=2, int reliability=3);
302 
306  virtual int ActivateLocalNow(const char * sNeuronFile, const char * code = NULL, int nLength = 0);
307 
309  //
310  // NPL Net Server functions
311  //
313 
320  void NPL_StartNetServer(const char* server=NULL, const char* port=NULL);
321 
323  void NPL_StopNetServer();
324 
333  void NPL_AddPublicFile(const string& filename, int nID);
334 
338  void NPL_ClearPublicFiles();
339 
345  string NPL_GetIP(const char* nid);
346 
347 
353  void NPL_accept(const char* tid, const char* nid = NULL);
354 
356  void NPL_SetProtocol(const char* nid, int protocolType = 0);
357 
366  void NPL_reject(const char* nid, int nReason = 0);
367 
368  virtual void StartNetServer(const char* server=NULL, const char* port=NULL);
369  virtual void StopNetServer();
370  virtual void AddPublicFile(const string& filename, int nID);
371  virtual void ClearPublicFiles();
372  virtual void GetIP(const char* nid, char* pOutput);
373  virtual void accept(const char* tid, const char* nid = NULL);
374  virtual void reject(const char* nid, int nReason = 0);
375 
377  //
378  // jabber client functions
379  //
381 
387  virtual ParaEngine::INPLJabberClient* GetJabberClient(const char* sJID);
392  virtual ParaEngine::INPLJabberClient* CreateJabberClient(const char* sJID);
393 
399  virtual bool CloseJabberClient(const char* sJID);
400 
402  //
403  // new libcUrl interface.
404  //
406 
411  virtual bool AppendURLRequest(ParaEngine::CURLRequestTask* pUrlTask, const char* sPoolName = NULL);
412 
417  virtual bool ChangeRequestPoolSize(const char* sPoolName, int nCount);
418 
419 
421  //
422  // Downloader functions
423  //
425 
431  virtual void AsyncDownload(const char* url, const char* destFolder, const char* callbackScript, const char* DownloaderName);
432 
433 
438  virtual void CancelDownload(const char* DownloaderName);
439 
445  virtual int Download(const char* url, const char* destFolder, const char* callbackScript, const char* DownloaderName);
446 
447 
449  //
450  // Web Service functions
451  //
453 
466  void NPL_RegisterWSCallBack(const char * sWebServiceFile, const char * sCode);
471  void NPL_UnregisterWSCallBack(const char * sWebServiceFile);
472 
474  //
475  // Timer functions
476  //
478 
491  bool NPL_SetTimer(NPLRuntimeState_ptr runtime_state, int nIDEvent, float fElapse, const char* sNeuronFile);
492 
501  bool NPL_KillTimer(NPLRuntimeState_ptr runtime_state,int nIDEvent);
502 
514  bool NPL_ChangeTimer(NPLRuntimeState_ptr runtime_state,int nIDEvent, int dueTime, int period);
515 
517  //
518  // UDP networking functions (RakNet in NPL network layer, currently obsoleted)
519  //
521 
524  static INPLStimulationPipe* GetNetworkLayerPipe();
525 
535  virtual void NPL_AddDNSRecord(const char * sDNSName, const char* sAddress);
536 
541  virtual void NPL_SetDefaultChannel(int channel_ID);
546  virtual int NPL_GetDefaultChannel();
547 
566  virtual void NPL_SetChannelProperty(int channel_ID, int priority, int reliability);
580  virtual void NPL_ResetChannelProperties();
581 
588  virtual void NPL_GetChannelProperty(int channel_ID, int* priority, int* reliability);
589 
599  void NPL_EnableNetwork(bool bEnable, const char* CenterName, const char* password);
600 
603  const string& NPL_GetSourceName();
609  void NPL_SetSourceName(const char* sName);
610 
611  /* default to 1, set to 0 to silence some connection verbose log. */
612  int GetLogLevel() const;
613 
614  /* default to 1, set to 0 to silence some connection verbose log. */
615  void SetLogLevel(int val);
616 
617  private: // methods
619  bool LoadWebServicePlugin();
620 
626  void STI_DoWebServiceFile(const char* sWebserviceFile, const char * code);
627 
629  int TranslatePriorityValue(int priority);
630  public:
631  virtual INPLRuntimeState* CreateState(const char* name, NPLRuntimeStateType type_=NPLRuntimeStateType_NPL);
632  virtual INPLRuntimeState* GetState(const char* name);
633  virtual INPLRuntimeState* CreateGetState(const char* name, NPLRuntimeStateType type_=NPLRuntimeStateType_NPL);
634  virtual bool DeleteState(INPLRuntimeState* pRuntime_state);
635  virtual INPLRuntimeState* GetMainState();
636  virtual bool AddToMainThread(INPLRuntimeState* runtime_state);
637 
644  NPLRuntimeState_ptr CreateRuntimeState(const string& name, NPLRuntimeStateType type_=NPLRuntimeStateType_NPL);
645 
650  NPLRuntimeState_ptr GetRuntimeState(const string& name);
651 
653  NPLRuntimeState_ptr CreateGetRuntimeState(const string& name, NPLRuntimeStateType type_=NPLRuntimeStateType_NPL);
654 
657  bool DeleteRuntimeState(NPLRuntimeState_ptr runtime_state);
658 
660  NPLRuntimeState_ptr GetMainRuntimeState();
661 
665  bool AddStateToMainThread(NPLRuntimeState_ptr runtime_state);
666 
670  bool RemoveStateFromMainThread(NPLRuntimeState_ptr runtime_state);
671 
673  CNPLNetServer* GetNetServer() {return m_net_server.get();};
674 
676  ParaEngine::CNPLNetClient* GetNetClient();
677 
685  void SetHostMainStatesInFrameMove(bool bHostMainStatesInFrameMove);
686 
687  private:
689  stack <string> m_stackDNS;
690 
692  INPLStimulationPipe* m_pNetPipe;
693  CNPLNameSpaceBinding* m_pNPLNamespaceBinding;
694 
696  int m_nDefaultChannel;
698  std::vector<ChannelProperty> m_channelProperties;
699 
701  std::map <string, string> m_mapWebServiceCallBacks;
702 
704  ParaEngine::INPLWebServiceClient* m_pWebServiceClient;
705 
712  bool m_bHostMainStatesInFrameMove;
713 
714  /* default to 1, set to 0 to silence some connection verbose log. */
715  int m_nLogLevel;
716  private:
717  typedef std::set<NPLRuntimeState_ptr, NPLRuntimeState_PtrOps> NPLRuntime_Pool_Type;
718  typedef std::vector<NPLRuntimeState_ptr> NPLRuntime_Temp_Pool_Type;
719  typedef std::map<std::string, NPLRuntimeState_ptr> ActiveStateMap_Type;
720 
722  boost::scoped_ptr<CNPLNetServer> m_net_server;
723 
725  NPLRuntime_Pool_Type m_runtime_states;
726 
728  NPLRuntime_Pool_Type m_runtime_states_with_timers;
729 
731  ActiveStateMap_Type m_active_state_map;
732 
734  NPLRuntime_Pool_Type m_runtime_states_main_threaded;
736  NPLRuntime_Temp_Pool_Type m_temp_rts_pool;
737 
739  NPLRuntimeState_ptr m_runtime_state_main;
740 
742  ParaEngine::mutex m_mutex;
743 
744  static NPLRuntimeStateType m_defaultNPLStateType;
745  };
746 
747 }
an attribute class is a collection of attribute fields.
Definition: AttributeClass.h:10
define this to enable debugging of NPL code in visual studio
Definition: INPL.h:9
Definition: EmuCSParaEditGame.cpp:57
void Set(int nPriority, int nReliability)
set properties
Definition: NPLRuntime.h:53
INPLRuntimeState interface for DLL interface.
Definition: INPLRuntimeState.h:27
different physics engine has different winding order.
Definition: EventBinding.h:32
virtual const char * GetAttributeClassName()
a static string, describing the attribute class object&#39;s name
Definition: NPLRuntime.h:93
URL request can be a HTTP get/post request etc.
Definition: NPLNetClient.h:45
Option names.
Definition: NPLNetClient.h:252
ChannelProperty()
default to m_nPriority = NPL::MEDIUM_PRIORITY, m_nReliability = NPL::RELIABLE_ORDERED ...
Definition: NPLRuntime.h:48
CNPLRuntime is a wrapper of the NPL application programming interface (NPL API).
Definition: NPLRuntime.h:64
CNPLNetServer * GetNetServer()
get the NPL net server
Definition: NPLRuntime.h:673
Definition: minilua.c:461
NPL interface of a Jabber-XMPP client proxy.
Definition: IParaWebService.h:108
Current NPL configuration settings.
Definition: NPLNetServer.h:16
A common interface for all classes implementing IAttributeFields By implementing this class&#39;s virtual...
Definition: IAttributeFields.h:59
Definition: inftrees.h:24
virtual int GetAttributeClassID()
attribute class ID should be identical, unless one knows how overriding rules work.
Definition: NPLRuntime.h:91
virtual const char * GetAttributeClassDescription()
a static string, describing the attribute class object
Definition: NPLRuntime.h:95
for luabind, The main drawback of this approach is that the compilation time will increase for the fi...
Definition: luaSQLite.cpp:1971
interface of NPL web service client.
Definition: IParaWebService.h:466
cross platform mutex
Definition: mutex.h:95
NPL Runtime Environment interface.
Definition: INPLRuntime.h:30
NPLRuntimeStateType
The types of CNPLRuntimeState.
Definition: INPLRuntimeState.h:12