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

Contents of /smsdaemon/Spooler.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 688 - (show annotations) (download)
Wed Apr 28 09:38:09 2010 UTC (14 years ago) by torben
File size: 2565 byte(s)
go back to correct spooldir
1 #include "Spooler.h"
2 #include <iostream>
3 #include <string>
4 #include <stdexcept>
5 #include <cstring>
6 #include <fstream>
7 #include <sstream>
8 #include <iomanip>
9 #include <errno.h>
10
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <sys/file.h>
14 #include <dirent.h>
15 #include <fcntl.h>
16 #include <stdlib.h>
17
18 #include "Util.h"
19
20 #include "Exceptions.h"
21
22 const std::string spooldir = "/var/spool/smsdaemon/";
23
24 using namespace std;
25
26
27 void errnoException(std::string msg = "")
28 {
29 if (msg != "")
30 msg += ": ";
31 msg += std::string(strerror(errno));
32 throw std::runtime_error(msg);
33 }
34
35
36
37 void Spooler::enqueue(std::string recipient, std::string message)
38 {
39 bool error = false;
40 lock();
41 std::string file = findSpoolFilename();
42 std::ofstream out(file.c_str());
43 if (out)
44 {
45 out << recipient << "\n" << message;
46 out.close();
47 }
48 else
49 {
50 error = true;
51 }
52 unlock();
53
54 if (error)
55 throw std::runtime_error("Could not create spoolfile");
56 this->filename = file;
57 }
58
59 std::string Spooler::dequeue()
60 {
61 lock();
62
63 DIR* dir = opendir( spooldir.c_str() );
64 if (dir == 0)
65 errnoException();
66
67 string file;
68 string message;
69 dirent* entry;
70 while ( (entry = readdir(dir)) != 0)
71 {
72 if (entry->d_name[0] == '.' )
73 {
74 continue;
75 }
76 else
77 {
78 file = spooldir + string(entry->d_name);
79 break;
80 }
81
82 }
83
84 closedir(dir);
85
86 if (file != "")
87 {
88 message = Util::readfile(file);
89 ::unlink(file.c_str());
90 }
91
92 unlock();
93
94 this->filename = file;
95
96 if (file != "")
97 return message;
98 else
99 throw filenotfoundexception();
100 }
101
102 std::string Spooler::findSpoolFilename()
103 {
104 std::string file;
105 std::stringstream ss;
106
107 int retrycount = 0;
108
109 while (1)
110 {
111 ss.str(std::string()); //clear
112 ss << std::setw(8) << std::setfill('0') << std::uppercase << std::hex << rand();
113
114 string file = spooldir;
115 file += ss.str().c_str();
116
117 int fd = ::open(file.c_str(), O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH);
118 retrycount ++;
119
120 if (fd != -1)
121 {
122 ::close(fd);
123 return file;
124 }
125 int err = errno;
126 ::close(fd);
127
128 if (err == EEXIST)
129 {
130 continue;
131 }
132
133 if (retrycount > 20)
134 throw std::runtime_error("to many retry attempt at creating spool file");
135
136 throw std::runtime_error("no access to spool directory");
137
138 }
139 }
140
141 void Spooler::lock()
142 {
143 lockfd = open(spooldir.c_str() , O_RDONLY);
144
145 if (lockfd == -1)
146 {
147 errnoException("Couldn open lockfile");
148 }
149
150 int status = flock(lockfd, LOCK_EX);
151
152 if (status == -1)
153 {
154 errnoException();
155 }
156 }
157
158 void Spooler::unlock()
159 {
160 flock(lockfd, LOCK_UN);
161 close(lockfd);
162 }
163
164 string Spooler::getFilename()
165 {
166 return filename;
167 }

  ViewVC Help
Powered by ViewVC 1.1.20