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

Annotation of /smsdaemon/Spooler.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 685 - (hide annotations) (download)
Wed Apr 28 09:02:30 2010 UTC (14 years ago) by torben
File size: 2564 byte(s)
correct callback function name
1 torben 132 #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 torben 158 #include "Util.h"
19 torben 132
20     #include "Exceptions.h"
21    
22 torben 685 const std::string spooldir = "/home/torben/smstool";
23 torben 132
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 torben 134 std::string file = findSpoolFilename();
42 torben 132 std::ofstream out(file.c_str());
43 torben 196 if (out)
44     {
45 torben 132 out << recipient << "\n" << message;
46     out.close();
47 torben 196 }
48     else
49     {
50 torben 132 error = true;
51     }
52     unlock();
53 torben 196
54 torben 132 if (error)
55     throw std::runtime_error("Could not create spoolfile");
56 torben 134 this->filename = file;
57 torben 132 }
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 torben 196 string message;
69 torben 132 dirent* entry;
70 torben 196 while ( (entry = readdir(dir)) != 0)
71 torben 132 {
72 torben 196 if (entry->d_name[0] == '.' )
73 torben 132 {
74     continue;
75     }
76     else
77     {
78     file = spooldir + string(entry->d_name);
79     break;
80     }
81    
82     }
83    
84     closedir(dir);
85    
86 torben 196 if (file != "")
87     {
88 torben 132 message = Util::readfile(file);
89     ::unlink(file.c_str());
90     }
91    
92     unlock();
93    
94 torben 134 this->filename = file;
95    
96 torben 132 if (file != "")
97     return message;
98     else
99     throw filenotfoundexception();
100     }
101    
102 torben 134 std::string Spooler::findSpoolFilename()
103 torben 132 {
104     std::string file;
105     std::stringstream ss;
106    
107     int retrycount = 0;
108    
109 torben 196 while (1)
110     {
111 torben 132 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 torben 196 if (fd != -1)
121     {
122 torben 132 ::close(fd);
123     return file;
124     }
125 torben 155 int err = errno;
126     ::close(fd);
127 torben 132
128 torben 196 if (err == EEXIST)
129     {
130 torben 132 continue;
131 torben 155 }
132 torben 132
133     if (retrycount > 20)
134     throw std::runtime_error("to many retry attempt at creating spool file");
135    
136 torben 196 throw std::runtime_error("no access to spool directory");
137 torben 132
138     }
139     }
140    
141     void Spooler::lock()
142     {
143 torben 155 lockfd = open(spooldir.c_str() , O_RDONLY);
144 torben 132
145 torben 196 if (lockfd == -1)
146     {
147 torben 132 errnoException("Couldn open lockfile");
148     }
149    
150 torben 155 int status = flock(lockfd, LOCK_EX);
151 torben 132
152 torben 196 if (status == -1)
153     {
154 torben 132 errnoException();
155     }
156     }
157    
158     void Spooler::unlock()
159     {
160 torben 155 flock(lockfd, LOCK_UN);
161     close(lockfd);
162 torben 132 }
163 torben 134
164     string Spooler::getFilename()
165     {
166     return filename;
167     }

  ViewVC Help
Powered by ViewVC 1.1.20