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: 21950 $
00030  * $Author: rjongbloed $
00031  * $Date: 2009-01-27 02:26:00 +0000 (Tue, 27 Jan 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     );
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,     // Number of program arguments.
00584       char ** argv, // Array of strings for program arguments.
00585       char ** envp  // Array of string for the system environment
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   // Member variables
00654     int terminationValue;
00655     // Application return value
00656 
00657     PString manufacturer;
00658     // Application manufacturer name.
00659 
00660     PString productName;
00661     // Application executable base name from argv[0]
00662 
00663     WORD majorVersion;
00664     // Major version number of the product
00665     
00666     WORD minorVersion;
00667     // Minor version number of the product
00668     
00669     CodeStatus status;
00670     // Development status of the product
00671     
00672     WORD buildNumber;
00673     // Build number of the product
00674 
00675     PFilePath executableFile;
00676     // Application executable file from argv[0] (not open)
00677 
00678     PStringArray configurationPaths;
00679     // Explicit file or set of directories to find default PConfig
00680 
00681     PArgList arguments;
00682     // The list of arguments
00683 
00684     PTimerList timers;
00685     // List of active timers in system
00686 
00687     PTime programStartTime;
00688     // time at which process was intantiated, i.e. started
00689 
00690     int maxHandles;
00691     // Maximum number of file handles process can open.
00692 
00693     bool m_library;
00694 
00695     PDictionary<POrdinalKey, PThread> activeThreads;
00696     PMutex                            activeThreadMutex;
00697     
00698   friend class PThread;
00699 
00700 
00701 // Include platform dependent part of class
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  *  one instance of this class (or any descendants) will be instantiated
00741  *  via PGenericFactory<PProessStartup> one "main" has been started, and then
00742  *  the OnStartup() function will be called. The OnShutdown function will
00743  *  be called after main exits, and the instances will be destroyed if they
00744  *  are not singletons
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 // using an inline definition rather than a #define crashes gcc 2.95. Go figure
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 // End Of File ///////////////////////////////////////////////////////////////

Generated on Fri Apr 10 22:33:48 2009 for PTLib by  doxygen 1.5.8