My Project
NPLCommon.h
1 #pragma once
2 #include "util/mutex.h"
3 #include "NPLMemPool.h"
4 #include "NPLTypes.h"
5 
6 #include <boost/noncopyable.hpp>
7 #include <boost/shared_ptr.hpp>
8 #include <boost/enable_shared_from_this.hpp>
9 
10 using namespace std;
11 
12 namespace NPL
13 {
19  typedef std::basic_string<char, std::char_traits<char>, ParaEngine::CNPLPool_Char_alloc<> > NPLString;
20 
26  struct NPLFileName
27  {
38  string sNID;
42  string sRelativePath;
48 
49  public:
50  NPLFileName();
51 
63  NPLFileName(const char * filename);
64 
69  void SetRelativePath(const char* sPath, int nCount=-1);
70 
71  public:
75  string ToString();
76  void ToString(string & output);
77 
81  void FromString(const char* filename);
82  };
83 
94  public boost::enable_shared_from_this<NPLRuntimeAddress>,
95  private boost::noncopyable
96  {
97  public:
98  NPLRuntimeAddress(const string& sHost, const string& sPort, const string& sNID)
99  :m_sHost(sHost), m_sPort(sPort), m_sNID(sNID){};
100 
101  inline const std::string& GetHost() const {return m_sHost;}
102  inline const std::string& GetPort() const {return m_sPort;}
103  inline const std::string& GetNID() const {return m_sNID;}
104  inline void SetNID(const char* nid) {m_sNID = nid;}
105  inline void SetNID(const std::string& nid) {m_sNID = nid;}
106 
107  private:
108  //
109  // ip address and port are const, because NPL runtime address are immutable.
110  //
111  const std::string m_sHost;
112  const std::string m_sPort;
113 
114  // nid may be changed during the course of authentication.
115  std::string m_sNID;
116  };
117 
118 
122  struct NPLAddress
123  {
124  string m_sAddress;
125  public:
126  NPLAddress(){m_sAddress = "";};
127  NPLAddress(const char* sAddress){m_sAddress = sAddress;};
128 
129  public:
131  bool IsLocal(){return m_sAddress.empty();}
133  string GetIP(){return "";};
135  string GetPort(){return "";};
137  string ToString(){return m_sAddress;}
138  };
139 
144  {
145  private:
146  list <NPLAddress> m_listReceivers;
147  public:
148  UIReceivers(){m_listReceivers.clear();}
149 
151  bool empty(){return m_listReceivers.empty();};
152 
154  void AddReceiver(const char* sAddress){
155  if(sAddress!=0)
156  m_listReceivers.push_back(NPLAddress(sAddress));
157  };
159  void Cleanup(){
160  m_listReceivers.clear();
161  };
164  string ToString(){
165  string sReceivers = "";
166  list<NPLAddress>::iterator itCurCP, itEndCP = m_listReceivers.end();
167  for( itCurCP = m_listReceivers.begin(); itCurCP != itEndCP; ++ itCurCP){
168  sReceivers += (*itCurCP).ToString() + ";";
169  }
170  return sReceivers;
171  }
174  void FromString(const char* sReceivers){
175  int nTo=0;
176  int nFrom = 0;
177  while( sReceivers[nTo]!='\0'){
178  if(sReceivers[nTo] == ';'){
179  string sAddress;
180  sAddress.assign(sReceivers+nFrom, nTo-nFrom);
181  AddReceiver(sAddress.c_str());
182  nFrom = nTo+1;
183  }
184  nTo++;
185  }
186  }
187  };
188 
190  struct NPLTimer : public boost::enable_shared_from_this<NPLTimer>,
191  private boost::noncopyable
192  {
193  const string m_nplFile;
194  const string m_sCode;
195 
196  private:
197  // interval in milliseconds
198  DWORD m_nInterval;
199  // value returned from last activation call of ::timeGetTime()
200  DWORD m_lastTick;
201  // mutex
202  ParaEngine::mutex m_mutex;
203 
204  public:
208  NPLTimer(const string& nplFile, const string& sCode, float fInterval);
209 
213  NPLTimer(const string& nplFile, const string& sCode, DWORD dwInterval);
214 
215 
224  void Change(int dueTime, int period);
225 
233  bool Tick(NPLRuntimeState_ptr runtime_state, DWORD nTickCount = 0);
234  };
235 
242  class NPLServerInfo : public boost::enable_shared_from_this<NPLServerInfo>
243  {
244  public:
245  NPLServerInfo(const string& sNID, const string& sIP, const string& sPort, bool bTrusted=true)
246  :m_sNID(sNID), m_sIP(sIP), m_sPort(sPort), m_bTrusted(bTrusted){};
247  NPLServerInfo():m_bTrusted(true){};
248  public:
250  string m_sNID;
252  string m_sIP;
254  string m_sPort;
255 
258  };
259 }
Information of the local or a remote server.
Definition: NPLCommon.h:242
string ToString()
convert to string.
Definition: NPLCommon.h:137
define this to enable debugging of NPL code in visual studio
Definition: INPL.h:9
void Cleanup()
clean up
Definition: NPLCommon.h:159
string sDNSServerName
the DNS file name.
Definition: NPLCommon.h:47
bool empty()
returns true if the receiver is empty.
Definition: NPLCommon.h:151
string sRuntimeStateName
the runtime state name that the file belongs to.
Definition: NPLCommon.h:32
string m_sPort
port of the server.
Definition: NPLCommon.h:254
string sRelativePath
the relative file path of the NPL file in the Runtime&#39;s file system.
Definition: NPLCommon.h:42
NOT USED YET: a list of NPL runtime address.
Definition: NPLCommon.h:143
std::basic_string< char, std::char_traits< char >, ParaEngine::CNPLPool_Char_alloc<> > NPLString
NPL String can be used instead of std::string, for strings which are created and deleted very regular...
Definition: NPLCommon.h:19
a globally unique name of a NPL file name instance.
Definition: NPLCommon.h:26
void AddReceiver(const char *sAddress)
add a new receiver from a string
Definition: NPLCommon.h:154
string GetPort()
TODO: get the port component in the address.
Definition: NPLCommon.h:135
string GetIP()
TODO: get the IP address component in the address.
Definition: NPLCommon.h:133
Obsoleted: the address of NPL runtime environment.
Definition: NPLCommon.h:122
bool IsLocal()
whether the address is the local address
Definition: NPLCommon.h:131
The globally unique address of NPL runtime.
Definition: NPLCommon.h:93
void FromString(const char *sReceivers)
append UI receivers from string.
Definition: NPLCommon.h:174
Check to see if it is a tick.
Definition: Tick.h:5
string m_sIP
this can be IP addresses or domain name, such as "localhost", "server.paraengine.com" ...
Definition: NPLCommon.h:252
string ToString()
convert to string.
Definition: NPLCommon.h:164
This class can be used as a custom allocator for stl containers, which constantly create and delete s...
Definition: NPLMemPool.h:11
bool m_bTrusted
whether we trust this server, if true, we will automatically connect to it when there is a NPL activa...
Definition: NPLCommon.h:257
cross platform mutex
Definition: mutex.h:95
string sNID
the NPL runtime server ID that the file belongs to.
Definition: NPLCommon.h:38
string m_sNID
NID of the NPL server.
Definition: NPLCommon.h:247
timer struct
Definition: NPLCommon.h:190