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: 21002 $
00030  * $Author: rjongbloed $
00031  * $Date: 2008-09-16 04:08:58 +0000 (Tue, 16 Sep 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     void QueueRequest(RequestType::Action action, PTimer * timer, bool _isSync = true);
00148 
00149   private:
00150     mutable PAtomicInteger timerId; 
00151 
00152     // map used to store timer information
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     // queue of timer action requests
00164     PMutex queueMutex;
00165     typedef std::queue<RequestType> RequestQueueType;
00166     RequestQueueType requestQueue;
00167     RequestQueueType addQueue;
00168 
00169     // The last system timer tick value that was used to process timers.
00170     PTimeInterval lastSample;
00171 };
00172 
00173 
00175 // PProcess
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,     // Number of program arguments.
00553       char ** argv, // Array of strings for program arguments.
00554       char ** envp  // Array of string for the system environment
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   // Member variables
00623     static int p_argc;
00624     static char ** p_argv;
00625     static char ** p_envp;
00626     // main arguments
00627 
00628     int terminationValue;
00629     // Application return value
00630 
00631     PString manufacturer;
00632     // Application manufacturer name.
00633 
00634     PString productName;
00635     // Application executable base name from argv[0]
00636 
00637     WORD majorVersion;
00638     // Major version number of the product
00639     
00640     WORD minorVersion;
00641     // Minor version number of the product
00642     
00643     CodeStatus status;
00644     // Development status of the product
00645     
00646     WORD buildNumber;
00647     // Build number of the product
00648 
00649     PFilePath executableFile;
00650     // Application executable file from argv[0] (not open)
00651 
00652     PStringArray configurationPaths;
00653     // Explicit file or set of directories to find default PConfig
00654 
00655     PArgList arguments;
00656     // The list of arguments
00657 
00658     PTimerList timers;
00659     // List of active timers in system
00660 
00661     PTime programStartTime;
00662     // time at which process was intantiated, i.e. started
00663 
00664     int maxHandles;
00665     // Maximum number of file handles process can open.
00666 
00667 
00668   friend class PThread;
00669 
00670 
00671 // Include platform dependent part of class
00672 #ifdef _WIN32
00673 #include "msos/ptlib/pprocess.h"
00674 #else
00675 #include "unix/ptlib/pprocess.h"
00676 #endif
00677 };
00678 
00679 /*
00680  *  one instance of this class (or any descendants) will be instantiated
00681  *  via PGenericFactory<PProessStartup> one "main" has been started, and then
00682  *  the OnStartup() function will be called. The OnShutdown function will
00683  *  be called after main exits, and the instances will be destroyed if they
00684  *  are not singletons
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 // using an inline definition rather than a #define crashes gcc 2.95. Go figure
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 // End Of File ///////////////////////////////////////////////////////////////

Generated on Mon Feb 23 01:57:54 2009 for PTLib by  doxygen 1.5.1