00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #ifndef PTLIB_PROCESS_H
00035 #define PTLIB_PROCESS_H
00036
00037 #ifdef P_USE_PRAGMA
00038 #pragma interface
00039 #endif
00040
00041 #include <ptlib/mutex.h>
00042 #include <ptlib/syncpoint.h>
00043 #include <ptlib/thread.h>
00044 #include <ptlib/pfactory.h>
00045
00046 #include <queue>
00047
00054 #ifdef P_VXWORKS
00055 #define PCREATE_PROCESS(cls) \
00056 cls instance; \
00057 instance.InternalMain();
00058 #elif defined(P_RTEMS)
00059 #define PCREATE_PROCESS(cls) \
00060 extern "C" {\
00061 void* POSIX_Init( void* argument) \
00062 { \
00063 static cls instance; \
00064 exit( instance.InternalMain() ); \
00065 } \
00066 }
00067 #elif defined(_WIN32_WCE)
00068 #define PCREATE_PROCESS(cls) \
00069 int WinMain(HINSTANCE hInst, HINSTANCE, LPWSTR cmdLine, int) \
00070 { \
00071 cls *pInstance = new cls(); \
00072 pInstance->GetArguments().SetArgs(cmdLine); \
00073 int terminationValue = pInstance->InternalMain(hInst); \
00074 delete pInstance; \
00075 return terminationValue; \
00076 }
00077 #else
00078 #define PCREATE_PROCESS(cls) \
00079 int main(int argc, char ** argv, char ** envp) \
00080 { \
00081 cls *pInstance = new cls(); \
00082 pInstance->PreInitialise(argc, argv, envp); \
00083 int terminationValue = pInstance->InternalMain(); \
00084 delete pInstance; \
00085 return terminationValue; \
00086 }
00087 #endif // P_VXWORKS
00088
00089
00090
00091
00092
00093
00094
00095 #define PDECLARE_PROCESS(cls,ancestor,manuf,name,major,minor,status,build) \
00096 class cls : public ancestor { \
00097 PCLASSINFO(cls, ancestor); \
00098 public: \
00099 cls() : ancestor(manuf, name, major, minor, status, build) { } \
00100 private: \
00101 virtual void Main(); \
00102 };
00103
00104
00105 class PTimerList : public PObject
00106
00107
00108
00109
00110
00111
00112 {
00113 PCLASSINFO(PTimerList, PObject);
00114
00115 public:
00116
00117 PTimerList();
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131 PTimeInterval Process();
00132
00133 PTimer::IDType GetNewTimerId() const { return ++timerId; }
00134
00135 class RequestType {
00136 public:
00137 enum Action {
00138 Stop,
00139 Start
00140 } action;
00141
00142 RequestType(Action act, PTimer * t)
00143 : action(act)
00144 , timer(t)
00145 , id(timer->GetTimerId())
00146 , sync(NULL)
00147 { }
00148
00149 PTimer * timer;
00150 PTimer::IDType id;
00151 PSyncPoint * sync;
00152 };
00153
00154 void QueueRequest(RequestType::Action action, PTimer * timer, bool isSync = true);
00155
00156 private:
00157 mutable PAtomicInteger timerId;
00158
00159
00160 PMutex timerListMutex;
00161 struct TimerInfoType {
00162 TimerInfoType(PTimer * t) : timer(t) { removed = false; }
00163 PTimer * timer;
00164 bool removed;
00165 };
00166 typedef std::map<PTimer::IDType, TimerInfoType> TimerInfoMapType;
00167 TimerInfoMapType activeTimers;
00168 PThread * timerThread;
00169
00170
00171 PMutex queueMutex;
00172 typedef std::queue<RequestType> RequestQueueType;
00173 RequestQueueType requestQueue;
00174 RequestQueueType addQueue;
00175
00176
00177 PTimeInterval lastSample;
00178 };
00179
00180
00182
00183
00196 class PProcess : public PThread
00197 {
00198 PCLASSINFO(PProcess, PThread);
00199
00200 public:
00203
00204 enum CodeStatus {
00206 AlphaCode,
00208 BetaCode,
00210 ReleaseCode,
00211 NumCodeStatuses
00212 };
00213
00216 PProcess(
00217 const char * manuf = "",
00218 const char * name = "",
00219 WORD majorVersion = 1,
00220 WORD minorVersion = 0,
00221 CodeStatus status = ReleaseCode,
00222 WORD buildNumber = 1
00223 );
00225
00234 Comparison Compare(
00235 const PObject & obj
00236 ) const;
00238
00243 virtual void Terminate();
00244
00250 virtual PString GetThreadName() const;
00251
00257 virtual void SetThreadName(
00258 const PString & name
00259 );
00261
00270 static PProcess & Current();
00271
00275 virtual void OnThreadStart(
00276 PThread & thread
00277 );
00278
00282 virtual void OnThreadEnded(
00283 PThread & thread
00284 );
00285
00298 virtual bool OnInterrupt(
00299 bool terminating
00300 );
00301
00308 static PBoolean IsInitialised();
00309
00316 void SetTerminationValue(
00317 int value
00318 );
00319
00329 int GetTerminationValue() const;
00330
00337 PArgList & GetArguments();
00338
00348 virtual const PString & GetManufacturer() const;
00349
00359 virtual const PString & GetName() const;
00360
00375 virtual PString GetVersion(
00376 PBoolean full = PTrue
00377 ) const;
00378
00384 const PFilePath & GetFile() const;
00385
00393 DWORD GetProcessID() const;
00394
00397 PTime GetStartTime() const;
00398
00407 PString GetUserName() const;
00408
00431 PBoolean SetUserName(
00432 const PString & username,
00433 PBoolean permanent = PFalse
00434 );
00435
00444 PString GetGroupName() const;
00445
00470 PBoolean SetGroupName(
00471 const PString & groupname,
00472 PBoolean permanent = PFalse
00473 );
00474
00481 int GetMaxHandles() const;
00482
00492 PBoolean SetMaxHandles(
00493 int newLimit
00494 );
00495
00496 #ifdef P_CONFIG_FILE
00497
00499 virtual PString GetConfigurationFile();
00500 #endif
00501
00515 void SetConfigurationPath(
00516 const PString & path
00517 );
00519
00528 static PString GetOSClass();
00529
00536 static PString GetOSName();
00537
00543 static PString GetOSHardware();
00544
00551 static PString GetOSVersion();
00552
00560 static PDirectory GetOSConfigDir();
00561
00568 static PString GetLibVersion();
00570
00577 PTimerList * GetTimerList();
00578
00582 void PreInitialise(
00583 int argc,
00584 char ** argv,
00585 char ** envp
00586 );
00587
00591 static void PreShutdown();
00592 static void PostShutdown();
00593
00595 virtual int InternalMain(void * arg = NULL);
00596
00618 class HostSystemURLHandlerInfo
00619 {
00620 public:
00621 HostSystemURLHandlerInfo()
00622 { }
00623
00624 HostSystemURLHandlerInfo(const PString & t)
00625 : type(t)
00626 { }
00627
00628 static bool RegisterTypes(const PString & types, bool force = true);
00629
00630 void SetIcon(const PString & icon);
00631 PString GetIcon() const;
00632
00633 void SetCommand(const PString & key, const PString & command);
00634 PString GetCommand(const PString & key) const;
00635
00636 bool GetFromSystem();
00637 bool CheckIfRegistered();
00638
00639 bool Register();
00640
00641 PString type;
00642
00643 #if _WIN32
00644 PString iconFileName;
00645 PStringToString cmds;
00646 #endif
00647 };
00649
00650 protected:
00651 void Construct();
00652
00653
00654 int terminationValue;
00655
00656
00657 PString manufacturer;
00658
00659
00660 PString productName;
00661
00662
00663 WORD majorVersion;
00664
00665
00666 WORD minorVersion;
00667
00668
00669 CodeStatus status;
00670
00671
00672 WORD buildNumber;
00673
00674
00675 PFilePath executableFile;
00676
00677
00678 PStringArray configurationPaths;
00679
00680
00681 PArgList arguments;
00682
00683
00684 PTimerList timers;
00685
00686
00687 PTime programStartTime;
00688
00689
00690 int maxHandles;
00691
00692
00693 bool m_library;
00694
00695 PDictionary<POrdinalKey, PThread> activeThreads;
00696 PMutex activeThreadMutex;
00697
00698 friend class PThread;
00699
00700
00701
00702 #ifdef _WIN32
00703 #include "msos/ptlib/pprocess.h"
00704 #else
00705 #include "unix/ptlib/pprocess.h"
00706 #endif
00707 };
00708
00709
00712 class PLibraryProcess : public PProcess
00713 {
00714 PCLASSINFO(PLibraryProcess, PProcess);
00715
00716 public:
00721 PLibraryProcess(
00722 const char * manuf = "",
00723 const char * name = "",
00724 WORD majorVersion = 1,
00725 WORD minorVersion = 0,
00726 CodeStatus status = ReleaseCode,
00727 WORD buildNumber = 1
00728 ) : PProcess(manuf, name, majorVersion, minorVersion, status, buildNumber)
00729 {
00730 m_library = true;
00731 }
00733
00735 virtual void Main() { }
00736 };
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746 class PProcessStartup : public PObject
00747 {
00748 PCLASSINFO(PProcessStartup, PObject)
00749 public:
00750 virtual void OnStartup() { }
00751 virtual void OnShutdown() { }
00752 };
00753
00754 typedef PFactory<PProcessStartup> PProcessStartupFactory;
00755
00756 #if PTRACING
00757
00758
00759 #define P_DEFAULT_TRACE_OPTIONS ( PTrace::Blocks | PTrace::Timestamp | PTrace::Thread | PTrace::FileAndLine )
00760
00761 template <unsigned level, unsigned options = P_DEFAULT_TRACE_OPTIONS >
00762 class PTraceLevelSetStartup : public PProcessStartup
00763 {
00764 public:
00765 void OnStartup()
00766 { PTrace::Initialise(level, NULL, options); }
00767 };
00768
00769 #endif // PTRACING
00770
00771
00772 #endif // PTLIB_PROCESS_H
00773
00774
00775