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 _PPROCESS
00035 #define _PPROCESS
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 PProcess::PreInitialise(0, NULL, NULL); \
00057 cls instance; \
00058 instance._main();
00059 #elif defined(P_RTEMS)
00060 #define PCREATE_PROCESS(cls) \
00061 extern "C" {\
00062 void* POSIX_Init( void* argument) \
00063 { PProcess::PreInitialise(0, 0, 0); \
00064 static cls instance; \
00065 exit( instance._main() ); \
00066 } \
00067 }
00068 #elif defined(_WIN32_WCE)
00069 #define PCREATE_PROCESS(cls) \
00070 int WinMain(HINSTANCE, HINSTANCE, LPWSTR, int) \
00071 { cls *pInstance = new cls(); \
00072 int terminationValue = pInstance->_main(); \
00073 delete pInstance; \
00074 return terminationValue; \
00075 }
00076 #else
00077 #define PCREATE_PROCESS(cls) \
00078 int main(int argc, char ** argv, char ** envp) \
00079 { PProcess::PreInitialise(argc, argv, envp); \
00080 cls *pInstance = new cls(); \
00081 int terminationValue = pInstance->_main(); \
00082 delete pInstance; \
00083 return terminationValue; \
00084 }
00085 #endif // P_VXWORKS
00086
00087
00088
00089
00090
00091
00092
00093 #define PDECLARE_PROCESS(cls,ancestor,manuf,name,major,minor,status,build) \
00094 class cls : public ancestor { \
00095 PCLASSINFO(cls, ancestor); \
00096 public: \
00097 cls() : ancestor(manuf, name, major, minor, status, build) { } \
00098 private: \
00099 virtual void Main(); \
00100 };
00101
00102
00103 class PTimerList : public PObject
00104
00105
00106
00107
00108
00109
00110 {
00111 PCLASSINFO(PTimerList, PObject);
00112
00113 public:
00114
00115 PTimerList();
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129 PTimeInterval Process();
00130
00131 PTimer::IDType GetNewTimerId() const { return ++timerId; }
00132
00133 class RequestType {
00134 public:
00135 enum Action {
00136 Stop,
00137 Start
00138 } action;
00139
00140 RequestType(Action _action, PTimer * _timer) : action(_action), timer(_timer), id(timer->GetTimerId()), sync(NULL) { }
00141
00142 PTimer * timer;
00143 PTimer::IDType id;
00144 PSyncPoint * sync;
00145 };
00146
00147 typedef std::queue<RequestType> RequestQueueType;
00148 typedef std::vector<RequestType> RequestListType;
00149
00150 void QueueRequest(RequestType::Action action, PTimer * timer, bool _isSync = true);
00151
00152 private:
00153 mutable PAtomicInteger timerId;
00154
00155
00156 PMutex timerListMutex;
00157 struct TimerInfoType {
00158 TimerInfoType(PTimer * _timer) : timer(_timer) { removed = false; }
00159 PTimer * timer;
00160 bool removed;
00161 };
00162 typedef std::map<PTimer::IDType, TimerInfoType> TimerInfoMapType;
00163 TimerInfoMapType activeTimers;
00164 PThread * timerThread;
00165
00166
00167 PMutex queueMutex;
00168 RequestQueueType requestQueue;
00169 RequestListType addQueue;
00170
00171
00172 PTimeInterval lastSample;
00173 };
00174
00175
00177
00178
00191 class PProcess : public PThread
00192 {
00193 PCLASSINFO(PProcess, PThread);
00194
00195 public:
00198
00199 enum CodeStatus {
00201 AlphaCode,
00203 BetaCode,
00205 ReleaseCode,
00206 NumCodeStatuses
00207 };
00208
00211 PProcess(
00212 const char * manuf = "",
00213 const char * name = "",
00214 WORD majorVersion = 1,
00215 WORD minorVersion = 0,
00216 CodeStatus status = ReleaseCode,
00217 WORD buildNumber = 1
00218 );
00220
00229 Comparison Compare(
00230 const PObject & obj
00231 ) const;
00233
00238 virtual void Terminate();
00239
00245 virtual PString GetThreadName() const;
00246
00252 virtual void SetThreadName(
00253 const PString & name
00254 );
00256
00265 static PProcess & Current();
00266
00270 virtual void OnThreadStart(
00271 PThread & thread
00272 );
00273
00277 virtual void OnThreadEnded(
00278 PThread & thread
00279 );
00280
00287 static PBoolean IsInitialised();
00288
00295 void SetTerminationValue(
00296 int value
00297 );
00298
00308 int GetTerminationValue() const;
00309
00316 PArgList & GetArguments();
00317
00327 virtual const PString & GetManufacturer() const;
00328
00338 virtual const PString & GetName() const;
00339
00354 virtual PString GetVersion(
00355 PBoolean full = PTrue
00356 ) const;
00357
00363 const PFilePath & GetFile() const;
00364
00372 DWORD GetProcessID() const;
00373
00376 PTime GetStartTime() const;
00377
00386 PString GetUserName() const;
00387
00410 PBoolean SetUserName(
00411 const PString & username,
00412 PBoolean permanent = PFalse
00413 );
00414
00423 PString GetGroupName() const;
00424
00449 PBoolean SetGroupName(
00450 const PString & groupname,
00451 PBoolean permanent = PFalse
00452 );
00453
00460 int GetMaxHandles() const;
00461
00471 PBoolean SetMaxHandles(
00472 int newLimit
00473 );
00474
00475 #ifdef P_CONFIG_FILE
00476
00478 virtual PString GetConfigurationFile();
00479 #endif
00480
00494 void SetConfigurationPath(
00495 const PString & path
00496 );
00498
00507 static PString GetOSClass();
00508
00515 static PString GetOSName();
00516
00522 static PString GetOSHardware();
00523
00530 static PString GetOSVersion();
00531
00539 static PDirectory GetOSConfigDir();
00541
00548 PTimerList * GetTimerList();
00549
00553 static void PreInitialise(
00554 int argc,
00555 char ** argv,
00556 char ** envp
00557 );
00558
00562 static void PreShutdown();
00563 static void PostShutdown();
00564
00566 virtual int _main(void * arg = NULL);
00567
00589 class HostSystemURLHandlerInfo
00590 {
00591 public:
00592 HostSystemURLHandlerInfo()
00593 { }
00594
00595 HostSystemURLHandlerInfo(const PString & _type)
00596 : type(_type)
00597 { }
00598
00599 static bool RegisterTypes(const PString & _types, bool force = true);
00600
00601 void SetIcon(const PString & icon);
00602 PString GetIcon() const;
00603
00604 void SetCommand(const PString & key, const PString & command);
00605 PString GetCommand(const PString & key) const;
00606
00607 bool GetFromSystem();
00608 bool CheckIfRegistered();
00609
00610 bool Register();
00611
00612 PString type;
00613
00614 #if _WIN32
00615 PString iconFileName;
00616 PStringToString cmds;
00617 #endif
00618 };
00620
00621 private:
00622 void Construct();
00623
00624
00625 static int p_argc;
00626 static char ** p_argv;
00627 static char ** p_envp;
00628
00629
00630 int terminationValue;
00631
00632
00633 PString manufacturer;
00634
00635
00636 PString productName;
00637
00638
00639 WORD majorVersion;
00640
00641
00642 WORD minorVersion;
00643
00644
00645 CodeStatus status;
00646
00647
00648 WORD buildNumber;
00649
00650
00651 PFilePath executableFile;
00652
00653
00654 PStringArray configurationPaths;
00655
00656
00657 PArgList arguments;
00658
00659
00660 PTimerList timers;
00661
00662
00663 PTime programStartTime;
00664
00665
00666 int maxHandles;
00667
00668
00669
00670 friend class PThread;
00671
00672
00673
00674 #ifdef _WIN32
00675 #include "msos/ptlib/pprocess.h"
00676 #else
00677 #include "unix/ptlib/pprocess.h"
00678 #endif
00679 };
00680
00681
00682
00683
00684
00685
00686
00687
00688 class PProcessStartup : public PObject
00689 {
00690 PCLASSINFO(PProcessStartup, PObject)
00691 public:
00692 virtual void OnStartup() { }
00693 virtual void OnShutdown() { }
00694 };
00695
00696 typedef PFactory<PProcessStartup> PProcessStartupFactory;
00697
00698 #if PTRACING
00699
00700
00701 #define P_DEFAULT_TRACE_OPTIONS ( PTrace::Blocks | PTrace::Timestamp | PTrace::Thread | PTrace::FileAndLine )
00702
00703 template <unsigned _level, unsigned _options = P_DEFAULT_TRACE_OPTIONS >
00704 class PTraceLevelSetStartup : public PProcessStartup
00705 {
00706 public:
00707 void OnStartup()
00708 { PTrace::Initialise(_level, NULL, _options); }
00709 };
00710
00711 #endif // PTRACING
00712
00713 #endif
00714
00715