-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmsdiskstorage.h
executable file
·190 lines (168 loc) · 4.89 KB
/
smsdiskstorage.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#ifndef SMS_A00B8199_324E_45A7_AEB4_54FDB4784FE6
#define SMS_A00B8199_324E_45A7_AEB4_54FDB4784FE6
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <linux/unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <string>
#include <cassert>
#include <ctime>
#include <fstream>
#include <vector>
#include <string.h>
#include <sstream>
#include "sms.h"
namespace SMS{
#define BACKUPDIR SMSHOME "error"
class CSMSDiskStorage: public CSMSStorage {
std::string m_OutgoingDirectory;
std::string m_IncomingDirectory;
std::string m_IncomingBackupDirectory;
std::string m_errorDirectory;
std::string m_currFilename;
DIR * m_pDIR;
std::vector<std::string> m_readedFileList;
char* m_pDataBuf;
unsigned int m_dataSize;
unsigned int m_bufSize;
int m_fp;
unsigned int m_count;
public:
CSMSDiskStorage(CSMSProtocol *pSMSPProtocol, const std::string & OutgoingDirectory, const std::string & IncomingDirectory, const std::string &IncomingBackupDirectory, const std::string &errorDirectory=BACKUPDIR);
~CSMSDiskStorage();
int set_notifier();
int writeSMStoStorage(const char* sourceNo, const char* TargetNo, char* buf, unsigned int buf_size){
if (CSMSStorage::writeSMStoStorage(sourceNo, TargetNo, buf, buf_size)!=SUCCESS) {
return ERROR;
}
int errCode;
std::time_t lt;
std::ofstream os;
lt=time(NULL);
std::stringstream filename;
m_count++;
filename<<m_OutgoingDirectory<<"/"<<sourceNo<<"."<<TargetNo<<"."<<lt<<"."<<m_count<<'\0';
os.exceptions(std::ios::badbit|std::ios::failbit|std::ios::eofbit);
syslog(LOG_ERR, "write new msg to %s", filename.str().c_str());
try{
os.open(filename.str().c_str());
os.write(buf,buf_size);
/*
os<<"from:"<<((SMSMessage*)(buf))->SenderNumber<<std::endl;
os<<"to:"<<((SMSMessage*)(buf))->TargetNumber<<std::endl;
os<<"BodyLength:"<<((SMSMessage*)(buf))->SMSBodyLength<<std::endl;
os<<"Body:";
os.write(((SMSMessage*)(buf))->SMSBody, ((SMSMessage*)(buf))->SMSBodyLength);
*/
os.close();
} catch (std::exception e){
syslog(LOG_ERR, "write file %s error : %s", filename.str().c_str(), e.what());
return ERROR;
}
return SUCCESS;
};
int getNextSMSFromStorage() {
dirent* pDirInfo;
struct stat statInfo;
for (;;) {
pDirInfo=readdir(m_pDIR);
if (pDirInfo==NULL)
{
return -1;
}
syslog(LOG_ERR,"check file:%s",pDirInfo->d_name);
std::string path=m_IncomingDirectory+"/"+pDirInfo->d_name;
if (stat(path.c_str(),&statInfo)!=0) {
syslog(LOG_ERR,"stat file %s error!", path.c_str());
continue;
}
if (S_ISREG(statInfo.st_mode))
{
break;
}
}
m_currFilename=pDirInfo->d_name;
syslog(LOG_ERR,"read msg: %s", pDirInfo->d_name);
if (m_bufSize<statInfo.st_size)
{
delete[] m_pDataBuf;
m_bufSize=statInfo.st_size;
m_pDataBuf=new char[m_bufSize];
if (m_pDataBuf==NULL){
syslog(LOG_ERR,"memory alloc in getNextSMSFromStorage failed: %s", pDirInfo->d_name);
}
}
m_dataSize=statInfo.st_size;
std::ifstream ifs;
ifs.exceptions(std::ios::badbit|std::ios::failbit);
try
{
std::string path=m_IncomingDirectory+"/"+pDirInfo->d_name;
ifs.open(path.c_str());
ifs.read(m_pDataBuf,m_dataSize);
ifs.close();
}
catch (std::exception e)
{
std::string errorMsg="read ";
errorMsg+=pDirInfo->d_name;
errorMsg+="error :" ;
errorMsg+=e.what();
throw SMS_Storage_error(errorMsg);
}
return 0;
};
int getFirstSMSFromStorage() {
m_pDIR=opendir(m_IncomingDirectory.c_str());
if (m_pDIR==NULL)
{
std::string errorMsg="open dir ";
errorMsg+=m_IncomingDirectory;
errorMsg+="error" ;
throw SMS_Storage_error(errorMsg);
}
syslog(LOG_ERR,"open dir successful: %s",m_IncomingDirectory.c_str());
return getNextSMSFromStorage();
}
int readGettedSMS(char* buf, unsigned int* buf_size) {
if (*buf_size < m_dataSize)
{
*buf_size=m_dataSize;
return 0;
}
memcpy(buf,m_pDataBuf,m_dataSize);
*buf_size=m_dataSize;
return 0;
}
int recordSended(){
m_readedFileList.push_back(m_currFilename);
}
int backupError() {
std::string oldpath(m_IncomingDirectory);
std::string newpath(m_errorDirectory);
oldpath+="/"+m_currFilename;
newpath+="/"+m_currFilename;
link(oldpath.c_str(),newpath.c_str());
unlink(oldpath.c_str());
return SUCCESS;
}
int clearStorage(){
std::vector<std::string>::const_iterator it;
for (it=m_readedFileList.begin();it!=m_readedFileList.end();it++){
std::string file=m_IncomingDirectory+"/"+(*it);
std::string backupfile=m_IncomingBackupDirectory+"/"+(*it);
link(file.c_str(),backupfile.c_str());
unlink(file.c_str());
syslog(LOG_ERR,"deleted file:%s", file.c_str());
}
m_readedFileList.clear();
closedir(m_pDIR);
m_pDIR=NULL;
return 0;
}
};
}
#endif