/[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 155 by torben, Mon Dec 8 17:14:21 2008 UTC
# 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                  out << recipient << "\n" << message;                  out << recipient << "\n" << message;
# Line 50  void Spooler::enqueue(std::string recipi Line 50  void Spooler::enqueue(std::string recipi
50                    
51          if (error)          if (error)
52                  throw std::runtime_error("Could not create spoolfile");                  throw std::runtime_error("Could not create spoolfile");
53            this->filename = file;
54  }  }
55    
56  std::string Spooler::dequeue()  std::string Spooler::dequeue()
# Line 86  std::string Spooler::dequeue() Line 87  std::string Spooler::dequeue()
87    
88          unlock();          unlock();
89    
90            this->filename = file;
91    
92          if (file != "")          if (file != "")
93                  return message;                  return message;
94          else          else
95                  throw filenotfoundexception();                  throw filenotfoundexception();
96  }  }
97    
98  std::string Spooler::getSpoolFilename()  std::string Spooler::findSpoolFilename()
99  {  {
100          std::string file;          std::string file;
101          std::stringstream ss;          std::stringstream ss;
# Line 113  std::string Spooler::getSpoolFilename() Line 116  std::string Spooler::getSpoolFilename()
116                          ::close(fd);                          ::close(fd);
117                          return file;                          return file;
118                  }                  }
119                    int err = errno;
120                    ::close(fd);
121    
122                  if (errno == EEXIST)                  if (err == EEXIST) {
123                          continue;                          continue;
124                    }
125    
126                  if (retrycount > 20)                  if (retrycount > 20)
127                          throw std::runtime_error("to many retry attempt at creating spool file");                          throw std::runtime_error("to many retry attempt at creating spool file");
# Line 127  std::string Spooler::getSpoolFilename() Line 133  std::string Spooler::getSpoolFilename()
133    
134  void Spooler::lock()  void Spooler::lock()
135  {  {
136          fd = open(spooldir.c_str() , O_RDONLY);          lockfd = open(spooldir.c_str() , O_RDONLY);
137    
138          if (fd == -1) {          if (lockfd == -1) {
139                  errnoException("Couldn open lockfile");                  errnoException("Couldn open lockfile");
140          }          }
141    
142          int status = flock(fd, LOCK_EX);          int status = flock(lockfd, LOCK_EX);
143    
144          if (status == -1) {          if (status == -1) {
145                  errnoException();                  errnoException();
# Line 142  void Spooler::lock() Line 148  void Spooler::lock()
148    
149  void Spooler::unlock()  void Spooler::unlock()
150  {  {
151          flock(fd, LOCK_UN);          flock(lockfd, LOCK_UN);
152            close(lockfd);
153    }
154    
155    string Spooler::getFilename()
156    {
157            return filename;
158  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.20