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 bool library = false
00224 );
00226
00235 Comparison Compare(
00236 const PObject & obj
00237 ) const;
00239
00244 virtual void Terminate();
00245
00251 virtual PString GetThreadName() const;
00252
00258 virtual void SetThreadName(
00259 const PString & name
00260 );
00262
00271 static PProcess & Current();
00272
00276 virtual void OnThreadStart(
00277 PThread & thread
00278 );
00279
00283 virtual void OnThreadEnded(
00284 PThread & thread
00285 );
00286
00299 virtual bool OnInterrupt(
00300 bool terminating
00301 );
00302
00309 static PBoolean IsInitialised();
00310
00317 void SetTerminationValue(
00318 int value
00319 );
00320
00330 int GetTerminationValue() const;
00331
00338 PArgList & GetArguments();
00339
00349 virtual const PString & GetManufacturer() const;
00350
00360 virtual const PString & GetName() const;
00361
00376 virtual PString GetVersion(
00377 PBoolean full = PTrue
00378 ) const;
00379
00385 const PFilePath & GetFile() const;
00386
00394 DWORD GetProcessID() const;
00395
00398 PTime GetStartTime() const;
00399
00408 PString GetUserName() const;
00409
00432 PBoolean SetUserName(
00433 const PString & username,
00434 PBoolean permanent = PFalse
00435 );
00436
00445 PString GetGroupName() const;
00446
00471 PBoolean SetGroupName(
00472 const PString & groupname,
00473 PBoolean permanent = PFalse
00474 );
00475
00482 int GetMaxHandles() const;
00483
00493 PBoolean SetMaxHandles(
00494 int newLimit
00495 );
00496
00497 #ifdef P_CONFIG_FILE
00498
00500 virtual PString GetConfigurationFile();
00501 #endif
00502
00516 void SetConfigurationPath(
00517 const PString & path
00518 );
00520
00529 static PString GetOSClass();
00530
00537 static PString GetOSName();
00538
00544 static PString GetOSHardware();
00545
00552 static PString GetOSVersion();
00553
00561 static PDirectory GetOSConfigDir();
00562
00569 static PString GetLibVersion();
00571
00578 PTimerList * GetTimerList();
00579
00583 void PreInitialise(
00584 int argc,
00585 char ** argv,
00586 char ** envp
00587 );
00588
00592 static void PreShutdown();
00593 static void PostShutdown();
00594
00596 virtual int InternalMain(void * arg = NULL);
00597
00619 class HostSystemURLHandlerInfo
00620 {
00621 public:
00622 HostSystemURLHandlerInfo()
00623 { }
00624
00625 HostSystemURLHandlerInfo(const PString & t)
00626 : type(t)
00627 { }
00628
00629 static bool RegisterTypes(const PString & types, bool force = true);
00630
00631 void SetIcon(const PString & icon);
00632 PString GetIcon() const;
00633
00634 void SetCommand(const PString & key, const PString & command);
00635 PString GetCommand(const PString & key) const;
00636
00637 bool GetFromSystem();
00638 bool CheckIfRegistered();
00639
00640 bool Register();
00641
00642 PString type;
00643
00644 #if _WIN32
00645 PString iconFileName;
00646 PStringToString cmds;
00647 #endif
00648 };
00650
00651 protected:
00652 void Construct();
00653
00654
00655 int terminationValue;
00656
00657
00658 PString manufacturer;
00659
00660
00661 PString productName;
00662
00663
00664 WORD majorVersion;
00665
00666
00667 WORD minorVersion;
00668
00669
00670 CodeStatus status;
00671
00672
00673 WORD buildNumber;
00674
00675
00676 PFilePath executableFile;
00677
00678
00679 PStringArray configurationPaths;
00680
00681
00682 PArgList arguments;
00683
00684
00685 PTimerList timers;
00686
00687
00688 PTime programStartTime;
00689
00690
00691 int maxHandles;
00692
00693
00694 bool m_library;
00695
00696 PDictionary<POrdinalKey, PThread> activeThreads;
00697 PMutex activeThreadMutex;
00698
00699 friend class PThread;
00700
00701
00702
00703 #ifdef _WIN32
00704 #include "msos/ptlib/pprocess.h"
00705 #else
00706 #include "unix/ptlib/pprocess.h"
00707 #endif
00708 };
00709
00710
00713 class PLibraryProcess : public PProcess
00714 {
00715 PCLASSINFO(PLibraryProcess, PProcess);
00716
00717 public:
00722 PLibraryProcess(
00723 const char * manuf = "",
00724 const char * name = "",
00725 WORD majorVersion = 1,
00726 WORD minorVersion = 0,
00727 CodeStatus status = ReleaseCode,
00728 WORD buildNumber = 1
00729 ) : PProcess(manuf, name, majorVersion, minorVersion, status, buildNumber, true) { }
00731
00733 virtual void Main() { }
00734 };
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744 class PProcessStartup : public PObject
00745 {
00746 PCLASSINFO(PProcessStartup, PObject)
00747 public:
00748 virtual void OnStartup() { }
00749 virtual void OnShutdown() { }
00750 };
00751
00752 typedef PFactory<PProcessStartup> PProcessStartupFactory;
00753
00754 #if PTRACING
00755
00756
00757 #define P_DEFAULT_TRACE_OPTIONS ( PTrace::Blocks | PTrace::Timestamp | PTrace::Thread | PTrace::FileAndLine )
00758
00759 template <unsigned level, unsigned options = P_DEFAULT_TRACE_OPTIONS >
00760 class PTraceLevelSetStartup : public PProcessStartup
00761 {
00762 public:
00763 void OnStartup()
00764 { PTrace::Initialise(level, NULL, options); }
00765 };
00766
00767 #endif // PTRACING
00768
00769
00770 #endif // PTLIB_PROCESS_H
00771
00772
00773