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 void QueueRequest(RequestType::Action action, PTimer * timer, bool _isSync = true);
00148
00149 private:
00150 mutable PAtomicInteger timerId;
00151
00152
00153 PMutex timerListMutex;
00154 struct TimerInfoType {
00155 TimerInfoType(PTimer * _timer) : timer(_timer) { removed = false; }
00156 PTimer * timer;
00157 bool removed;
00158 };
00159 typedef std::map<PTimer::IDType, TimerInfoType> TimerInfoMapType;
00160 TimerInfoMapType activeTimers;
00161 PThread * timerThread;
00162
00163
00164 PMutex queueMutex;
00165 typedef std::queue<RequestType> RequestQueueType;
00166 RequestQueueType requestQueue;
00167 RequestQueueType addQueue;
00168
00169
00170 PTimeInterval lastSample;
00171 };
00172
00173
00175
00176
00189 class PProcess : public PThread
00190 {
00191 PCLASSINFO(PProcess, PThread);
00192
00193 public:
00196
00197 enum CodeStatus {
00199 AlphaCode,
00201 BetaCode,
00203 ReleaseCode,
00204 NumCodeStatuses
00205 };
00206
00209 PProcess(
00210 const char * manuf = "",
00211 const char * name = "",
00212 WORD majorVersion = 1,
00213 WORD minorVersion = 0,
00214 CodeStatus status = ReleaseCode,
00215 WORD buildNumber = 1
00216 );
00218
00227 Comparison Compare(
00228 const PObject & obj
00229 ) const;
00231
00236 virtual void Terminate();
00237
00243 virtual PString GetThreadName() const;
00244
00250 virtual void SetThreadName(
00251 const PString & name
00252 );
00254
00263 static PProcess & Current();
00264
00268 virtual void OnThreadStart(
00269 PThread & thread
00270 );
00271
00275 virtual void OnThreadEnded(
00276 PThread & thread
00277 );
00278
00285 static PBoolean IsInitialised();
00286
00293 void SetTerminationValue(
00294 int value
00295 );
00296
00306 int GetTerminationValue() const;
00307
00314 PArgList & GetArguments();
00315
00325 virtual const PString & GetManufacturer() const;
00326
00336 virtual const PString & GetName() const;
00337
00352 virtual PString GetVersion(
00353 PBoolean full = PTrue
00354 ) const;
00355
00361 const PFilePath & GetFile() const;
00362
00370 DWORD GetProcessID() const;
00371
00374 PTime GetStartTime() const;
00375
00384 PString GetUserName() const;
00385
00408 PBoolean SetUserName(
00409 const PString & username,
00410 PBoolean permanent = PFalse
00411 );
00412
00421 PString GetGroupName() const;
00422
00447 PBoolean SetGroupName(
00448 const PString & groupname,
00449 PBoolean permanent = PFalse
00450 );
00451
00458 int GetMaxHandles() const;
00459
00469 PBoolean SetMaxHandles(
00470 int newLimit
00471 );
00472
00473 #ifdef P_CONFIG_FILE
00474
00476 virtual PString GetConfigurationFile();
00477 #endif
00478
00492 void SetConfigurationPath(
00493 const PString & path
00494 );
00496
00505 static PString GetOSClass();
00506
00513 static PString GetOSName();
00514
00520 static PString GetOSHardware();
00521
00528 static PString GetOSVersion();
00529
00537 static PDirectory GetOSConfigDir();
00539
00546 PTimerList * GetTimerList();
00547
00551 static void PreInitialise(
00552 int argc,
00553 char ** argv,
00554 char ** envp
00555 );
00556
00560 static void PreShutdown();
00561 static void PostShutdown();
00562
00564 virtual int _main(void * arg = NULL);
00565
00587 class HostSystemURLHandlerInfo
00588 {
00589 public:
00590 HostSystemURLHandlerInfo()
00591 { }
00592
00593 HostSystemURLHandlerInfo(const PString & _type)
00594 : type(_type)
00595 { }
00596
00597 static bool RegisterTypes(const PString & _types, bool force = true);
00598
00599 void SetIcon(const PString & icon);
00600 PString GetIcon() const;
00601
00602 void SetCommand(const PString & key, const PString & command);
00603 PString GetCommand(const PString & key) const;
00604
00605 bool GetFromSystem();
00606 bool CheckIfRegistered();
00607
00608 bool Register();
00609
00610 PString type;
00611
00612 #if _WIN32
00613 PString iconFileName;
00614 PStringToString cmds;
00615 #endif
00616 };
00618
00619 private:
00620 void Construct();
00621
00622
00623 static int p_argc;
00624 static char ** p_argv;
00625 static char ** p_envp;
00626
00627
00628 int terminationValue;
00629
00630
00631 PString manufacturer;
00632
00633
00634 PString productName;
00635
00636
00637 WORD majorVersion;
00638
00639
00640 WORD minorVersion;
00641
00642
00643 CodeStatus status;
00644
00645
00646 WORD buildNumber;
00647
00648
00649 PFilePath executableFile;
00650
00651
00652 PStringArray configurationPaths;
00653
00654
00655 PArgList arguments;
00656
00657
00658 PTimerList timers;
00659
00660
00661 PTime programStartTime;
00662
00663
00664 int maxHandles;
00665
00666
00667
00668 friend class PThread;
00669
00670
00671
00672 #ifdef _WIN32
00673 #include "msos/ptlib/pprocess.h"
00674 #else
00675 #include "unix/ptlib/pprocess.h"
00676 #endif
00677 };
00678
00679
00680
00681
00682
00683
00684
00685
00686 class PProcessStartup : public PObject
00687 {
00688 PCLASSINFO(PProcessStartup, PObject)
00689 public:
00690 virtual void OnStartup() { }
00691 virtual void OnShutdown() { }
00692 };
00693
00694 typedef PFactory<PProcessStartup> PProcessStartupFactory;
00695
00696 #if PTRACING
00697
00698
00699 #define P_DEFAULT_TRACE_OPTIONS ( PTrace::Blocks | PTrace::Timestamp | PTrace::Thread | PTrace::FileAndLine )
00700
00701 template <unsigned _level, unsigned _options = P_DEFAULT_TRACE_OPTIONS >
00702 class PTraceLevelSetStartup : public PProcessStartup
00703 {
00704 public:
00705 void OnStartup()
00706 { PTrace::Initialise(_level, NULL, _options); }
00707 };
00708
00709 #endif // PTRACING
00710
00711 #endif
00712
00713