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

Diff of /smsdaemon/Spooler.cpp

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

revision 158 by torben, Mon Dec 8 21:49:49 2008 UTC revision 196 by torben, Thu Dec 18 06:53:29 2008 UTC
# Line 40  void Spooler::enqueue(std::string recipi Line 40  void Spooler::enqueue(std::string recipi
40          lock();          lock();
41          std::string file = findSpoolFilename();          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;          this->filename = file;
# Line 62  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 80  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          }          }
# Line 102  std::string Spooler::findSpoolFilename() Line 106  std::string Spooler::findSpoolFilename()
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 112  std::string Spooler::findSpoolFilename() Line 117  std::string Spooler::findSpoolFilename()
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;                  int err = errno;
126                  ::close(fd);                  ::close(fd);
127    
128                  if (err == 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  }  }
# Line 135  void Spooler::lock() Line 142  void Spooler::lock()
142  {  {
143          lockfd = open(spooldir.c_str() , O_RDONLY);          lockfd = open(spooldir.c_str() , O_RDONLY);
144    
145          if (lockfd == -1) {          if (lockfd == -1)
146            {
147                  errnoException("Couldn open lockfile");                  errnoException("Couldn open lockfile");
148          }          }
149    
150          int status = flock(lockfd, LOCK_EX);          int status = flock(lockfd, LOCK_EX);
151    
152          if (status == -1) {          if (status == -1)
153            {
154                  errnoException();                  errnoException();
155          }          }
156  }  }

Legend:
Removed from v.158  
changed lines
  Added in v.196

  ViewVC Help
Powered by ViewVC 1.1.20