2012-01-19 07:44:59 -06:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
2012-01-19 11:59:00 -06:00
|
|
|
#include <time.h>
|
2012-01-19 07:44:59 -06:00
|
|
|
#include "Config.h"
|
|
|
|
#include "Client.h"
|
|
|
|
#include "interface/Point.h"
|
|
|
|
#include "Graphics.h"
|
|
|
|
|
|
|
|
Client::Client()
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
http_init(NULL);
|
2012-01-19 11:59:00 -06:00
|
|
|
for(i = 0; i < THUMB_CACHE_SIZE; i++)
|
2012-01-19 07:44:59 -06:00
|
|
|
{
|
|
|
|
thumbnailCache[i] = NULL;
|
|
|
|
}
|
2012-01-19 11:59:00 -06:00
|
|
|
for(i = 0; i < IMGCONNS; i++)
|
2012-01-19 07:44:59 -06:00
|
|
|
{
|
|
|
|
activeThumbRequests[i] = NULL;
|
|
|
|
activeThumbRequestTimes[i] = 0;
|
2012-01-19 11:59:00 -06:00
|
|
|
activeThumbRequestCompleteTimes[i] = 0;
|
2012-01-19 07:44:59 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Client::~Client()
|
|
|
|
{
|
|
|
|
http_done();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Client::ClearThumbnailRequests()
|
|
|
|
{
|
2012-01-19 11:59:00 -06:00
|
|
|
for(int i = 0; i < IMGCONNS; i++)
|
2012-01-19 07:44:59 -06:00
|
|
|
{
|
|
|
|
if(activeThumbRequests[i])
|
|
|
|
{
|
|
|
|
http_async_req_close(activeThumbRequests[i]);
|
|
|
|
activeThumbRequests[i] = NULL;
|
2012-01-19 11:59:00 -06:00
|
|
|
activeThumbRequestTimes[i] = 0;
|
|
|
|
activeThumbRequestCompleteTimes[i] = 0;
|
2012-01-19 07:44:59 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Thumbnail * Client::GetThumbnail(int saveID, int saveDate)
|
|
|
|
{
|
|
|
|
std::stringstream urlStream;
|
|
|
|
std::stringstream idStream;
|
2012-01-19 11:59:00 -06:00
|
|
|
int i = 0, currentTime = time(NULL);
|
|
|
|
//Check active requests for any "forgotten" requests
|
|
|
|
for(i = 0; i < IMGCONNS; i++)
|
|
|
|
{
|
|
|
|
//If the request is active, and we've recieved a response
|
|
|
|
if(activeThumbRequests[i] && http_async_req_status(activeThumbRequests[i]))
|
|
|
|
{
|
|
|
|
//If we haven't already, mark the request as completed
|
|
|
|
if(!activeThumbRequestCompleteTimes[i])
|
|
|
|
{
|
|
|
|
activeThumbRequestCompleteTimes[i] = time(NULL);
|
|
|
|
}
|
|
|
|
else if(activeThumbRequestCompleteTimes[i] < (currentTime-20)) //Otherwise, if it completed more than 10 seconds ago, destroy it.
|
|
|
|
{
|
|
|
|
http_async_req_close(activeThumbRequests[i]);
|
|
|
|
activeThumbRequests[i] = NULL;
|
|
|
|
activeThumbRequestTimes[i] = 0;
|
|
|
|
activeThumbRequestCompleteTimes[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(i = 0; i < THUMB_CACHE_SIZE; i++)
|
2012-01-19 07:44:59 -06:00
|
|
|
{
|
|
|
|
if(thumbnailCache[i] && thumbnailCache[i]->ID == saveID && thumbnailCache[i]->Datestamp == saveDate)
|
|
|
|
return thumbnailCache[i];
|
|
|
|
}
|
|
|
|
urlStream << "http://" << SERVER << "/Get.api?Op=thumbsmall&ID=" << saveID;
|
|
|
|
if(saveDate)
|
|
|
|
{
|
|
|
|
urlStream << "&Date=" << saveDate;
|
|
|
|
}
|
|
|
|
idStream << saveID << ":" << saveDate;
|
|
|
|
std::string idString = idStream.str();
|
|
|
|
bool found = false;
|
2012-01-19 11:59:00 -06:00
|
|
|
for(i = 0; i < IMGCONNS; i++)
|
2012-01-19 07:44:59 -06:00
|
|
|
{
|
|
|
|
if(activeThumbRequests[i] && activeThumbRequestIDs[i] == idString)
|
|
|
|
{
|
|
|
|
found = true;
|
|
|
|
if(http_async_req_status(activeThumbRequests[i]))
|
|
|
|
{
|
|
|
|
pixel * thumbData;
|
|
|
|
char * data;
|
|
|
|
int status, data_size, imgw, imgh;
|
|
|
|
data = http_async_req_stop(activeThumbRequests[i], &status, &data_size);
|
|
|
|
activeThumbRequests[i] = NULL;
|
|
|
|
if (status == 200 && data)
|
|
|
|
{
|
|
|
|
thumbData = Graphics::ptif_unpack(data, data_size, &imgw, &imgh);
|
|
|
|
if(data)
|
|
|
|
{
|
|
|
|
free(data);
|
|
|
|
}
|
2012-01-19 11:59:00 -06:00
|
|
|
thumbnailCacheNextID %= THUMB_CACHE_SIZE;
|
2012-01-19 07:44:59 -06:00
|
|
|
if(thumbnailCache[thumbnailCacheNextID])
|
|
|
|
{
|
|
|
|
delete thumbnailCache[thumbnailCacheNextID];
|
|
|
|
}
|
|
|
|
if(thumbData)
|
|
|
|
{
|
|
|
|
thumbnailCache[thumbnailCacheNextID] = new Thumbnail(saveID, saveDate, thumbData, ui::Point(imgw, imgh));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
thumbnailCache[thumbnailCacheNextID] = new Thumbnail(saveID, saveDate, (pixel *)malloc((128*128) * PIXELSIZE), ui::Point(128, 128));
|
|
|
|
}
|
|
|
|
return thumbnailCache[thumbnailCacheNextID++];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(data)
|
|
|
|
{
|
|
|
|
free(data);
|
|
|
|
}
|
2012-01-19 11:59:00 -06:00
|
|
|
thumbnailCacheNextID %= THUMB_CACHE_SIZE;
|
2012-01-19 07:44:59 -06:00
|
|
|
if(thumbnailCache[thumbnailCacheNextID])
|
|
|
|
{
|
|
|
|
delete thumbnailCache[thumbnailCacheNextID];
|
|
|
|
}
|
|
|
|
thumbnailCache[thumbnailCacheNextID] = new Thumbnail(saveID, saveDate, (pixel *)malloc((128*128) * PIXELSIZE), ui::Point(128, 128));
|
|
|
|
return thumbnailCache[thumbnailCacheNextID++];
|
|
|
|
}
|
|
|
|
}
|
2012-01-19 11:59:00 -06:00
|
|
|
else if(activeThumbRequestTimes[i] < currentTime-HTTP_TIMEOUT)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
2012-01-19 07:44:59 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!found)
|
|
|
|
{
|
2012-01-19 11:59:00 -06:00
|
|
|
for(i = 0; i < IMGCONNS; i++)
|
2012-01-19 07:44:59 -06:00
|
|
|
{
|
|
|
|
if(!activeThumbRequests[i])
|
|
|
|
{
|
|
|
|
activeThumbRequests[i] = http_async_req_start(NULL, (char *)urlStream.str().c_str(), NULL, 0, 1);
|
2012-01-19 11:59:00 -06:00
|
|
|
activeThumbRequestTimes[i] = currentTime;
|
|
|
|
activeThumbRequestCompleteTimes[i] = 0;
|
2012-01-19 07:44:59 -06:00
|
|
|
std::cout << "ThumbCache: Requesting " << urlStream.str() << " : " << idString << std::endl;
|
|
|
|
activeThumbRequestIDs[i] = idString;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//http_async_req_start(http, urlStream.str().c_str(), NULL, 0, 1);
|
|
|
|
return NULL;
|
|
|
|
}
|