Subversion Repositories public

Rev

Rev 71 | Go to most recent revision | Details | Last modification | View Log | RSS feed

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