pprocess.h

Go to the documentation of this file.
00001 /*
00002  * pprocess.h
00003  *
00004  * Operating System Process (running program executable) class.
00005  *
00006  * Portable Windows Library
00007  *
00008  * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
00009  *
00010  * The contents of this file are subject to the Mozilla Public License
00011  * Version 1.0 (the "License"); you may not use this file except in
00012  * compliance with the License. You may obtain a copy of the License at
00013  * http://www.mozilla.org/MPL/
00014  *
00015  * Software distributed under the License is distributed on an "AS IS"
00016  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
00017  * the License for the specific language governing rights and limitations
00018  * under the License.
00019  *
00020  * The Original Code is Portable Windows Library.
00021  *
00022  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
00023  *
00024  * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
00025  * All Rights Reserved.
00026  *
00027  * Contributor(s): ______________________________________.
00028  *
00029  * $Revision: 22356 $
00030  * $Author: rjongbloed $
00031  * $Date: 2009-03-31 18:57:19 -0500 (Tue, 31 Mar 2009) $
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 /*$MACRO PDECLARE_PROCESS(cls,ancestor,manuf,name,major,minor,status,build)
00090    This macro is used to declare the components necessary for a user PWLib
00091    process. This will declare the PProcess descendent class, eg PApplication,
00092    and create an instance of the class. See the #PCREATE_PROCESS# macro
00093    for more details.
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 /* This class defines a list of #PTimer# objects. It is primarily used
00107    internally by the library and the user should never create an instance of
00108    it. The #PProcess# instance for the application maintains an instance
00109    of all of the timers created so that it may decrements them at regular
00110    intervals.
00111  */
00112 {
00113   PCLASSINFO(PTimerList, PObject);
00114 
00115   public:
00116     // Create a new timer list
00117     PTimerList();
00118 
00119     /* Decrement all the created timers and dispatch to their callback
00120        functions if they have expired. The #PTimer::Tick()# function
00121        value is used to determine the time elapsed since the last call to
00122        Process().
00123 
00124        The return value is the number of milliseconds until the next timer
00125        needs to be despatched. The function need not be called again for this
00126        amount of time, though it can (and usually is).
00127        
00128        @return
00129        maximum time interval before function should be called again.
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     // map used to store timer information
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     // queue of timer action requests
00171     PMutex queueMutex;
00172     typedef std::queue<RequestType> RequestQueueType;
00173     RequestQueueType requestQueue;
00174     RequestQueueType addQueue;
00175 
00176     // The last system timer tick value that was used to process timers.
00177     PTimeInterval lastSample;
00178 };
00179 
00180 
00182 // PProcess
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,     // Number of program arguments.
00585       char ** argv, // Array of strings for program arguments.
00586       char ** envp  // Array of string for the system environment
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   // Member variables
00655     int terminationValue;
00656     // Application return value
00657 
00658     PString manufacturer;
00659     // Application manufacturer name.
00660 
00661     PString productName;
00662     // Application executable base name from argv[0]
00663 
00664     WORD majorVersion;
00665     // Major version number of the product
00666     
00667     WORD minorVersion;
00668     // Minor version number of the product
00669     
00670     CodeStatus status;
00671     // Development status of the product
00672     
00673     WORD buildNumber;
00674     // Build number of the product
00675 
00676     PFilePath executableFile;
00677     // Application executable file from argv[0] (not open)
00678 
00679     PStringArray configurationPaths;
00680     // Explicit file or set of directories to find default PConfig
00681 
00682     PArgList arguments;
00683     // The list of arguments
00684 
00685     PTimerList timers;
00686     // List of active timers in system
00687 
00688     PTime programStartTime;
00689     // time at which process was intantiated, i.e. started
00690 
00691     int maxHandles;
00692     // Maximum number of file handles process can open.
00693 
00694     bool m_library;
00695 
00696     PDictionary<POrdinalKey, PThread> activeThreads;
00697     PMutex                            activeThreadMutex;
00698     
00699   friend class PThread;
00700 
00701 
00702 // Include platform dependent part of class
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  *  one instance of this class (or any descendants) will be instantiated
00739  *  via PGenericFactory<PProessStartup> one "main" has been started, and then
00740  *  the OnStartup() function will be called. The OnShutdown function will
00741  *  be called after main exits, and the instances will be destroyed if they
00742  *  are not singletons
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 // using an inline definition rather than a #define crashes gcc 2.95. Go figure
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 // End Of File ///////////////////////////////////////////////////////////////

Generated on Thu May 27 01:36:48 2010 for PTLib by  doxygen 1.4.7