-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCache.cpp
More file actions
222 lines (200 loc) · 5.41 KB
/
Copy pathCache.cpp
File metadata and controls
222 lines (200 loc) · 5.41 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include "Cache.hpp"
#include <unistd.h>
#include <iostream>
#include <fcntl.h>
bool Cache::hostsubfolder=true;
/*uint32_t Cache::maxiumSizeToBeSmallFile=4096;
uint64_t Cache::maxiumSmallFileCacheSize=0;//diable by default (to be safe if on ram disk)
uint64_t Cache::smallFileCacheSize=0;*/
//use pread, pwrite
/*Format, insert/drop at middle with sequential scan:
* 64Bits: access time
* 64Bits: last modification time check (Modification based on ETag)
* 16Bits: http code
* 48Bits: frontend content Etag, Base64 Random bytes at Creation or Modification
* 8Bits: Etag backend size in Bytes
* XBytes: backend content (see Etag backend size)
* Http Headers ended with \n\n
* Http body
*/
Cache::Cache(const int &fd)
{
this->kind=EpollObject::Kind::Kind_Cache;
this->fd=fd;
/*
//while receive write to cache
//when finish
//unset curl to all future listener
//Close all listener
*/
}
Cache::~Cache()
{
close();
}
void Cache::parseEvent(const epoll_event &event)
{
(void)event;
/* if(!(event & EPOLLIN))
readyToRead();*/
}
void Cache::close()
{
if(fd!=-1)
{
epoll_ctl(epollfd,EPOLL_CTL_DEL, fd, NULL);
::close(fd);
fd=-1;
}
}
uint64_t Cache::access_time() const
{
uint64_t time=0;
if(::pread(fd,&time,sizeof(time),0)==sizeof(time))
return time;
return 0;
}
uint64_t Cache::last_modification_time_check() const
{
uint64_t time=0;
if(::pread(fd,&time,sizeof(time),sizeof(uint64_t))==sizeof(time))
return time;
return 0;
}
std::string Cache::ETagFrontend() const
{
char randomIndex[6];
if(::pread(fd,randomIndex,sizeof(randomIndex),2*sizeof(uint64_t)+sizeof(uint16_t))==sizeof(randomIndex))
return std::string(randomIndex,sizeof(randomIndex));
return 0;
}
std::string Cache::ETagBackend() const
{
uint8_t etagBackendSize=0;
if(::pread(fd,&etagBackendSize,sizeof(etagBackendSize),3*sizeof(uint64_t))==sizeof(etagBackendSize))
{
char buffer[etagBackendSize];
if(::pread(fd,buffer,etagBackendSize,3*sizeof(uint64_t)+sizeof(uint8_t))==etagBackendSize)
return std::string(buffer,etagBackendSize);
}
return std::string();
}
uint16_t Cache::http_code() const
{
uint64_t time=0;
if(::pread(fd,&time,sizeof(time),2*sizeof(uint64_t))==sizeof(time))
return time;
return 500;
}
void Cache::set_access_time(const uint64_t &time)
{
if(::pwrite(fd,&time,sizeof(time),0)!=sizeof(time))
{
std::cerr << "Unable to write last_modification_time_check" << std::endl;
return;
}
}
void Cache::set_last_modification_time_check(const uint64_t &time)
{
if(::pwrite(fd,&time,sizeof(time),sizeof(uint64_t))!=sizeof(time))
{
std::cerr << "Unable to write last_modification_time_check" << std::endl;
return;
}
}
void Cache::set_ETagFrontend(const std::string &etag)
{
if((size_t)::pwrite(fd,etag.data(),etag.size(),2*sizeof(uint64_t)+sizeof(uint16_t))!=etag.size())
{
std::cerr << "Unable to write last_modification_time_check" << std::endl;
return;
}
}
void Cache::set_ETagBackend(const std::string &etag)//at end seek to content pos
{
if(etag.size()>255)
{
char c=0x00;
if(::pwrite(fd,&c,sizeof(c),3*sizeof(uint64_t))!=sizeof(c))
{
std::cerr << "Unable to write last_modification_time_check" << std::endl;
return;
}
}
else
{
char c=etag.size();
if(::pwrite(fd,&c,sizeof(c),3*sizeof(uint64_t))!=sizeof(c))
{
std::cerr << "Unable to write last_modification_time_check" << std::endl;
return;
}
if(c>0)
{
if((size_t)::pwrite(fd,etag.data(),etag.size(),3*sizeof(uint64_t)+sizeof(uint8_t))!=etag.size())
{
std::cerr << "Unable to write last_modification_time_check" << std::endl;
return;
}
}
}
}
void Cache::set_http_code(const uint16_t &http_code)
{
if(::pwrite(fd,&http_code,sizeof(http_code),2*sizeof(uint64_t))!=sizeof(http_code))
{
std::cerr << "Unable to write last_modification_time_check" << std::endl;
return;
}
}
void Cache::setAsync()
{
int flags, s;
flags = fcntl(fd, F_GETFL, 0);
if(flags == -1)
std::cerr << "fcntl get flags error" << std::endl;
else
{
flags |= O_NONBLOCK;
s = fcntl(fd, F_SETFL, flags);
if(s == -1)
std::cerr << "fcntl set flags error" << std::endl;
}
}
bool Cache::seekToContentPos()
{
uint8_t etagBackendSize=0;
if(::pread(fd,&etagBackendSize,sizeof(etagBackendSize),3*sizeof(uint64_t))==sizeof(etagBackendSize))
{
const uint16_t &pos=3*sizeof(uint64_t)+sizeof(uint8_t)+etagBackendSize;
const off_t &s=lseek(fd,pos,SEEK_SET);
if(s==-1)
{
std::cerr << "Unable to seek setContentPos" << std::endl;
return false;
}
std::cout << "seek to:" << pos << std::endl;
return true;
}
return false;
}
ssize_t Cache::write(const char * const data,const size_t &size)
{
return ::write(fd,data,size);
}
ssize_t Cache::read(char * data,const size_t &size)
{
return ::read(fd,data,size);
}
uint32_t Cache::timeToCache(uint16_t http_code)
{
switch(http_code)
{
case 200:
return 24*3600;
break;
default:
return 60;
break;
}
}