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: 20385 $
00030  * $Author: rjongbloed $
00031  * $Date: 2008-06-04 10:40:38 +0000 (Wed, 04 Jun 2008) $
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 /*$MACRO PDECLARE_PROCESS(cls,ancestor,manuf,name,major,minor,status,build)
00088    This macro is used to declare the components necessary for a user PWLib
00089    process. This will declare the PProcess descendent class, eg PApplication,
00090    and create an instance of the class. See the #PCREATE_PROCESS# macro
00091    for more details.
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 /* This class defines a list of #PTimer# objects. It is primarily used
00105    internally by the library and the user should never create an instance of
00106    it. The #PProcess# instance for the application maintains an instance
00107    of all of the timers created so that it may decrements them at regular
00108    intervals.
00109  */
00110 {
00111   PCLASSINFO(PTimerList, PObject);
00112 
00113   public:
00114     // Create a new timer list
00115     PTimerList();
00116 
00117     /* Decrement all the created timers and dispatch to their callback
00118        functions if they have expired. The #PTimer::Tick()# function
00119        value is used to determine the time elapsed since the last call to
00120        Process().
00121 
00122        The return value is the number of milliseconds until the next timer
00123        needs to be despatched. The function need not be called again for this
00124        amount of time, though it can (and usually is).
00125        
00126        @return
00127        maximum time interval before function should be called again.
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     // map used to store timer information
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     // queue of timer action requests
00167     PMutex queueMutex;
00168     RequestQueueType requestQueue;
00169     RequestListType addQueue;
00170 
00171     // The last system timer tick value that was used to process timers.
00172     PTimeInterval lastSample;
00173 };
00174 
00175 
00177 // PProcess
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,     // Number of program arguments.
00555       char ** argv, // Array of strings for program arguments.
00556       char ** envp  // Array of string for the system environment
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   // Member variables
00625     static int p_argc;
00626     static char ** p_argv;
00627     static char ** p_envp;
00628     // main arguments
00629 
00630     int terminationValue;
00631     // Application return value
00632 
00633     PString manufacturer;
00634     // Application manufacturer name.
00635 
00636     PString productName;
00637     // Application executable base name from argv[0]
00638 
00639     WORD majorVersion;
00640     // Major version number of the product
00641     
00642     WORD minorVersion;
00643     // Minor version number of the product
00644     
00645     CodeStatus status;
00646     // Development status of the product
00647     
00648     WORD buildNumber;
00649     // Build number of the product
00650 
00651     PFilePath executableFile;
00652     // Application executable file from argv[0] (not open)
00653 
00654     PStringArray configurationPaths;
00655     // Explicit file or set of directories to find default PConfig
00656 
00657     PArgList arguments;
00658     // The list of arguments
00659 
00660     PTimerList timers;
00661     // List of active timers in system
00662 
00663     PTime programStartTime;
00664     // time at which process was intantiated, i.e. started
00665 
00666     int maxHandles;
00667     // Maximum number of file handles process can open.
00668 
00669 
00670   friend class PThread;
00671 
00672 
00673 // Include platform dependent part of class
00674 #ifdef _WIN32
00675 #include "msos/ptlib/pprocess.h"
00676 #else
00677 #include "unix/ptlib/pprocess.h"
00678 #endif
00679 };
00680 
00681 /*
00682  *  one instance of this class (or any descendants) will be instantiated
00683  *  via PGenericFactory<PProessStartup> one "main" has been started, and then
00684  *  the OnStartup() function will be called. The OnShutdown function will
00685  *  be called after main exits, and the instances will be destroyed if they
00686  *  are not singletons
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 // using an inline definition rather than a #define crashes gcc 2.95. Go figure
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 // End Of File ///////////////////////////////////////////////////////////////

Generated on Mon Sep 15 08:27:59 2008 for PTLib by  doxygen 1.5.1