/[projects]/smsdaemon/Spooler.cpp
ViewVC logotype

Diff of /smsdaemon/Spooler.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 132 by torben, Sun Dec 7 00:59:05 2008 UTC revision 685 by torben, Wed Apr 28 09:02:30 2010 UTC
# Line 15  Line 15 
15  #include <fcntl.h>  #include <fcntl.h>
16  #include <stdlib.h>  #include <stdlib.h>
17    
18  #include "util.h"  #include "Util.h"
19    
20  #include "Exceptions.h"  #include "Exceptions.h"
21    
22  const std::string spooldir = "/var/spool/smsdaemon/";  const std::string spooldir = "/home/torben/smstool";
23    
24  using namespace std;  using namespace std;
25    
# Line 38  void Spooler::enqueue(std::string recipi Line 38  void Spooler::enqueue(std::string recipi
38  {  {
39          bool error = false;          bool error = false;
40          lock();          lock();
41          std::string file = getSpoolFilename();          std::string file = findSpoolFilename();
42          std::ofstream out(file.c_str());          std::ofstream out(file.c_str());
43          if (out) {          if (out)
44            {
45                  out << recipient << "\n" << message;                  out << recipient << "\n" << message;
46                  out.close();                  out.close();
47          } else {          }
48            else
49            {
50                  error = true;                  error = true;
51          }          }
52          unlock();          unlock();
53            
54          if (error)          if (error)
55                  throw std::runtime_error("Could not create spoolfile");                  throw std::runtime_error("Could not create spoolfile");
56            this->filename = file;
57  }  }
58    
59  std::string Spooler::dequeue()  std::string Spooler::dequeue()
# Line 61  std::string Spooler::dequeue() Line 65  std::string Spooler::dequeue()
65                  errnoException();                  errnoException();
66    
67          string file;          string file;
68          string message;          string message;
69          dirent* entry;          dirent* entry;
70          while ( (entry = readdir(dir)) != 0)          while ( (entry = readdir(dir)) != 0)
71          {          {
72                  if (entry->d_name[0] == '.' )                  if (entry->d_name[0] == '.' )
73                  {                  {
74                          continue;                          continue;
75                  }                  }
# Line 79  std::string Spooler::dequeue() Line 83  std::string Spooler::dequeue()
83    
84          closedir(dir);          closedir(dir);
85    
86          if (file != "") {          if (file != "")
87            {
88                  message = Util::readfile(file);                  message = Util::readfile(file);
89                  ::unlink(file.c_str());                  ::unlink(file.c_str());
90          }          }
91    
92          unlock();          unlock();
93    
94            this->filename = file;
95    
96          if (file != "")          if (file != "")
97                  return message;                  return message;
98          else          else
99                  throw filenotfoundexception();                  throw filenotfoundexception();
100  }  }
101    
102  std::string Spooler::getSpoolFilename()  std::string Spooler::findSpoolFilename()
103  {  {
104          std::string file;          std::string file;
105          std::stringstream ss;          std::stringstream ss;
106    
107          int retrycount = 0;          int retrycount = 0;
108    
109          while(1) {          while (1)
110            {
111                  ss.str(std::string()); //clear                  ss.str(std::string()); //clear
112                  ss << std::setw(8) << std::setfill('0') << std::uppercase  << std::hex << rand();                  ss << std::setw(8) << std::setfill('0') << std::uppercase  << std::hex << rand();
113    
# Line 109  std::string Spooler::getSpoolFilename() Line 117  std::string Spooler::getSpoolFilename()
117                  int fd = ::open(file.c_str(), O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH);                  int fd = ::open(file.c_str(), O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH);
118                  retrycount ++;                  retrycount ++;
119    
120                  if (fd != -1) {                  if (fd != -1)
121                    {
122                          ::close(fd);                          ::close(fd);
123                          return file;                          return file;
124                  }                  }
125                    int err = errno;
126                    ::close(fd);
127    
128                  if (errno == EEXIST)                  if (err == EEXIST)
129                    {
130                          continue;                          continue;
131                    }
132    
133                  if (retrycount > 20)                  if (retrycount > 20)
134                          throw std::runtime_error("to many retry attempt at creating spool file");                          throw std::runtime_error("to many retry attempt at creating spool file");
135    
136                  throw std::runtime_error("no access to spool directory");                        throw std::runtime_error("no access to spool directory");
137    
138          }          }
139  }  }
140    
141  void Spooler::lock()  void Spooler::lock()
142  {  {
143          fd = open(spooldir.c_str() , O_RDONLY);          lockfd = open(spooldir.c_str() , O_RDONLY);
144    
145          if (fd == -1) {          if (lockfd == -1)
146            {
147                  errnoException("Couldn open lockfile");                  errnoException("Couldn open lockfile");
148          }          }
149    
150          int status = flock(fd, LOCK_EX);          int status = flock(lockfd, LOCK_EX);
151    
152          if (status == -1) {          if (status == -1)
153            {
154                  errnoException();                  errnoException();
155          }          }
156  }  }
157    
158  void Spooler::unlock()  void Spooler::unlock()
159  {  {
160          flock(fd, LOCK_UN);          flock(lockfd, LOCK_UN);
161            close(lockfd);
162    }
163    
164    string Spooler::getFilename()
165    {
166            return filename;
167  }  }

Legend:
Removed from v.132  
changed lines
  Added in v.685

  ViewVC Help
Powered by ViewVC 1.1.20