Rev 73 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 73 | Rev 84 | ||
---|---|---|---|
1 | 1 | ||
2 | /// Application instance class header
|
2 | /// Application instance class header
|
3 | /**
|
3 | /**
|
4 | * \file appinst.h
|
4 | * \file appinst.h
|
5 | *
|
5 | *
|
6 | * Copyright (C) 2007 Lukas Jelinek, <lukas@aiken.cz>
|
6 | * Copyright (C) 2007 Lukas Jelinek, <lukas@aiken.cz>
|
7 | *
|
7 | *
|
8 | * This program is free software; you can redistribute it and/or
|
8 | * This program is free software; you can redistribute it and/or
|
9 | * modify it under the terms of one of the following licenses:
|
9 | * modify it under the terms of one of the following licenses:
|
10 | *
|
10 | *
|
11 | * \li 1. X11-style license (see LICENSE-X11)
|
11 | * \li 1. X11-style license (see LICENSE-X11)
|
12 | * \li 2. GNU Lesser General Public License, version 2.1 (see LICENSE-LGPL)
|
12 | * \li 2. GNU Lesser General Public License, version 2.1 (see LICENSE-LGPL)
|
13 | * \li 3. GNU General Public License, version 2 (see LICENSE-GPL)
|
13 | * \li 3. GNU General Public License, version 2 (see LICENSE-GPL)
|
14 | *
|
14 | *
|
15 | * If you want to help with choosing the best license for you,
|
15 | * If you want to help with choosing the best license for you,
|
16 | * please visit http://www.gnu.org/licenses/license-list.html.
|
16 | * please visit http://www.gnu.org/licenses/license-list.html.
|
17 | *
|
17 | *
|
18 | */
|
18 | */
|
19 | 19 | ||
20 | 20 | ||
21 | #ifndef APPINST_H_
|
21 | #ifndef APPINST_H_
|
22 | #define APPINST_H_
|
22 | #define APPINST_H_
|
23 | 23 | ||
24 | 24 | ||
25 | #include <string>
|
25 | #include <string>
|
26 | 26 | ||
27 | 27 | ||
28 | #define APPLOCK_BASEDIR "/var/run"
|
28 | #define APPLOCK_BASEDIR "/var/run"
|
29 | 29 | ||
30 | 30 | ||
31 | /// Exception class.
|
31 | /// Exception class.
|
32 | /**
|
32 | /**
|
33 | * This class provides information about occurred errors.
|
33 | * This class provides information about occurred errors.
|
34 | */
|
34 | */
|
35 | class AppInstException
|
35 | class AppInstException
|
36 | {
|
36 | {
|
37 | public: |
37 | public: |
38 | /// Constructor.
|
38 | /// Constructor.
|
39 | /**
|
39 | /**
|
40 | * \param[in] iErr error number
|
40 | * \param[in] iErr error number
|
41 | */
|
41 | */
|
42 | AppInstException(int iErr) : m_iErr(iErr) {} |
42 | AppInstException(int iErr) : m_iErr(iErr) {} |
43 | 43 | ||
44 | /// Returns the error number.
|
44 | /// Returns the error number.
|
45 | /**
|
45 | /**
|
46 | * \return error number
|
46 | * \return error number
|
47 | */
|
47 | */
|
48 | inline int GetErrorNumber() const |
48 | inline int GetErrorNumber() const |
49 | {
|
49 | {
|
50 | return m_iErr; |
50 | return m_iErr; |
51 | }
|
51 | }
|
52 | 52 | ||
53 | private: |
53 | private: |
54 | int m_iErr; ///< error number |
54 | int m_iErr; ///< error number |
55 | 55 | ||
56 | }; |
56 | }; |
57 | 57 | ||
58 | /// Application instance management class.
|
58 | /// Application instance management class.
|
59 | /**
|
59 | /**
|
60 | * This class is intended for application which require to
|
60 | * This class is intended for application which require to
|
61 | * be running only once (one instance only). It provides some
|
61 | * be running only once (one instance only). It provides some
|
62 | * methods for simple locking, signaling etc.
|
62 | * methods for simple locking, signaling etc.
|
63 | */
|
63 | */
|
64 | class AppInstance
|
64 | class AppInstance
|
65 | {
|
65 | {
|
66 | public: |
66 | public: |
67 | /// Constructor.
|
67 | /// Constructor.
|
68 | /**
|
68 | /**
|
69 | * \param[in] rName application name
|
69 | * \param[in] rName application name
|
70 | * \param[in] rBase lockfile base directory
|
70 | * \param[in] rBase lockfile base directory
|
71 | *
|
71 | *
|
72 | * \attention If an empty base directory is given it is replaced by
|
72 | * \attention If an empty base directory is given it is replaced by
|
73 | * the default value.
|
73 | * the default value.
|
74 | */
|
74 | */
|
75 | AppInstance(const std::string& rName, const std::string& rBase = APPLOCK_BASEDIR); |
75 | AppInstance(const std::string& rName, const std::string& rBase = APPLOCK_BASEDIR); |
76 | 76 | ||
77 | /// Destructor.
|
77 | /// Destructor.
|
78 | ~AppInstance(); |
78 | ~AppInstance(); |
79 | 79 | ||
80 | /// Attempts to lock the instance.
|
80 | /// Attempts to lock the instance.
|
81 | /**
|
81 | /**
|
82 | * This method attempts to create a lockfile. If the file
|
82 | * This method attempts to create a lockfile. If the file
|
83 | * already exists it checks whether its owner is still living.
|
83 | * already exists it checks whether its owner is still living.
|
84 | * If it does this method fails. Otherwise it unlinks this file
|
84 | * If it does this method fails. Otherwise it unlinks this file
|
85 | * and re-attempts to create it.
|
85 | * and re-attempts to create it.
|
86 | *
|
86 | *
|
87 | * \return true = instance locked, false = otherwise
|
87 | * \return true = instance locked, false = otherwise
|
88 | */
|
88 | */
|
89 | bool Lock(); |
89 | bool Lock(); |
90 | 90 | ||
91 | /// Unlocks the instance.
|
91 | /// Unlocks the instance.
|
92 | /**
|
92 | /**
|
93 | * This method removes (unlinks) the appropriate lockfile.
|
93 | * This method removes (unlinks) the appropriate lockfile.
|
94 | * If the instance hasn't been locked this method has no
|
94 | * If the instance hasn't been locked this method has no
|
95 | * effect.
|
95 | * effect.
|
96 | */
|
96 | */
|
97 | void Unlock(); |
97 | void Unlock(); |
98 | 98 | ||
99 | /// Checks whether an instance of this application exists.
|
99 | /// Checks whether an instance of this application exists.
|
100 | /**
|
100 | /**
|
101 | * If this instance has acquired the lockfile the call will
|
101 | * If this instance has acquired the lockfile the call will
|
102 | * be successful. Otherwise it checks for existence of
|
102 | * be successful. Otherwise it checks for existence of
|
103 | * another running instance.
|
103 | * another running instance.
|
104 | *
|
104 | *
|
105 | * \return true = instance exists, false = otherwise
|
105 | * \return true = instance exists, false = otherwise
|
106 | */
|
106 | */
|
107 | bool Exists() const; |
107 | bool Exists() const; |
108 | 108 | ||
109 | /// Sends a signal to an instance of this application.
|
109 | /// Sends a signal to an instance of this application.
|
110 | /**
|
110 | /**
|
111 | * This method doesn't signal the current instance.
|
111 | * This method doesn't signal the current instance.
|
112 | *
|
112 | *
|
113 | * \param[in] iSigNo signal number
|
113 | * \param[in] iSigNo signal number
|
114 | * \return true = success, false = otherwise
|
114 | * \return true = success, false = otherwise
|
115 | */
|
115 | */
|
116 | bool SendSignal(int iSigNo) const; |
116 | bool SendSignal(int iSigNo) const; |
117 | 117 | ||
118 | /// Terminates an instance of this application.
|
118 | /// Terminates an instance of this application.
|
119 | /**
|
119 | /**
|
120 | * This method doesn't terminate the current instance.
|
120 | * This method doesn't terminate the current instance.
|
121 | *
|
121 | *
|
122 | * \return true = success, false = otherwise
|
122 | * \return true = success, false = otherwise
|
123 | */
|
123 | */
|
124 | inline bool Terminate() const |
124 | inline bool Terminate() const |
125 | {
|
125 | {
|
126 | return SendSignal(SIGTERM); |
126 | return SendSignal(SIGTERM); |
127 | }
|
127 | }
|
128 | 128 | ||
129 | protected: |
129 | protected: |
130 | bool DoLock(); |
130 | bool DoLock(); |
131 | 131 | ||
132 | private: |
132 | private: |
133 | std::string m_path; ///< lock path |
133 | std::string m_path; ///< lock path |
134 | bool m_fLocked; ///< locked yes/no |
134 | bool m_fLocked; ///< locked yes/no |
135 | }; |
135 | }; |
136 | 136 | ||
137 | #endif /*APPINST_H_*/
|
137 | #endif /*APPINST_H_*/
|
138 | 138 |