EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ImageServer Class Reference

Handles distribution of character and related game images. More...

#include "ImageServer.h"

Inheritance diagram for ImageServer:
Collaboration diagram for ImageServer:

Classes

class  Lock
 

Public Member Functions

 ImageServer ()
 
void Run ()
 
void Stop ()
 
std::string & url ()
 
void ReportNewImage (uint32 accountID, std::shared_ptr< std::vector< char > > imageData)
 
void ReportNewCharacter (uint32 creatorAccountID, uint32 characterID)
 
std::string GetFilePath (std::string &category, uint32 id, uint32 size)
 
std::shared_ptr< std::vector
< char > > 
GetImage (std::string &category, uint32 id, uint32 size)
 
- Public Member Functions inherited from Singleton< ImageServer >
 Singleton ()
 Primary constructor. More...
 

Static Public Attributes

static const char *const Categories []
 
static const uint32 CategoryCount = 5
 
static const char *const FallbackURL = "http://image.eveonline.com/"
 

Private Member Functions

void RunInternal ()
 
bool ValidateCategory (std::string &category)
 
bool ValidateSize (std::string &category, uint32 size)
 

Private Attributes

std::unordered_map< uint32,
std::shared_ptr< std::vector
< char > > > 
_limboImages
 
std::shared_ptr
< boost::asio::detail::thread > 
_ioThread
 
std::shared_ptr
< boost::asio::io_context > 
_io
 
std::shared_ptr
< ImageServerListener
_listener
 
std::string _url
 
std::string _basePath
 
boost::asio::detail::mutex _limboLock
 

Additional Inherited Members

- Static Public Member Functions inherited from Singleton< ImageServer >
static ImageServerget ()
 
- Static Protected Attributes inherited from Singleton< ImageServer >
static std::shared_ptr
< ImageServer
mInstance
 

Detailed Description

Handles distribution of character and related game images.

A very limited HTTP server that can efficiently deliver character and other images to clients Uses asio for efficient asynchronous network communication

Author
caytchen
Date
April 2011

Definition at line 48 of file ImageServer.h.

Constructor & Destructor Documentation

ImageServer::ImageServer ( )

Definition at line 42 of file ImageServer.cpp.

References _basePath, _url, Categories, CategoryCount, CreateDirectory(), sConfig, and sLog.

43 {
44  std::stringstream urlBuilder;
45  urlBuilder << "http://" << sConfig.net.imageServer << ":" << sConfig.net.imageServerPort << "/";
46  _url = urlBuilder.str();
47 
48  _basePath = sConfig.files.imageDir;
49  if (_basePath[_basePath.size() - 1] != '/')
50  _basePath += "/";
51 
52  sLog.Cyan(" ImageServer", "Image Server URL: %s", _url.c_str());
53  sLog.Cyan(" ImageServer", "Image Server path: %s", _basePath.c_str());
54 
55  if (CreateDirectory( _basePath.c_str(), NULL ) == 0) {
56  for (int i = 0; i < CategoryCount; i++) {
57  std::string subdir = _basePath;
58  subdir.append(Categories[i]);
59  CreateDirectory( subdir.c_str(), NULL );
60  }
61  } /* else directory probably exists */
62  sLog.Blue(" ImageServer", "Image Server Initalized.");
63 }
#define sConfig
A macro for easier access to the singleton.
static const char *const Categories[]
Definition: ImageServer.h:63
#define sLog
Evaluates to a NewLog instance.
Definition: LogNew.h:250
int CreateDirectory(const char *name, void *)
Definition: eve-compat.cpp:46
std::string _url
Definition: ImageServer.h:79
static const uint32 CategoryCount
Definition: ImageServer.h:64
std::string _basePath
Definition: ImageServer.h:80

Here is the call graph for this function:

Member Function Documentation

std::string ImageServer::GetFilePath ( std::string &  category,
uint32  id,
uint32  size 
)

Definition at line 151 of file ImageServer.cpp.

References _basePath.

Referenced by GetImage(), and ReportNewCharacter().

152 {
153  std::string extension = category == "Character" ? "jpg" : "png";
154 
155  // HACK: We don't have any other
156  size = 512;
157 
158  std::stringstream builder;
159  builder << _basePath << category << "/" << id << "_" << size << "." << extension;
160  return builder.str();
161 }
std::string _basePath
Definition: ImageServer.h:80

Here is the caller graph for this function:

std::shared_ptr< std::vector< char > > ImageServer::GetImage ( std::string &  category,
uint32  id,
uint32  size 
)

Definition at line 115 of file ImageServer.cpp.

References GetFilePath(), sLog, ValidateCategory(), and ValidateSize().

116 {
117  sLog.Cyan(" ImageServer"," GetImage() called. Cat: %s, id: %u, size:%u", category.c_str(), id, size);
118 
119  if (!ValidateCategory(category) || !ValidateSize(category, size))
120  return std::shared_ptr<std::vector<char> >();
121 
122  //std::ifstream stream;
123  std::string path(GetFilePath(category, id, size));
124  FILE * fp = fopen(path.c_str(), "rb");
125  if (fp == NULL)
126  return std::shared_ptr<std::vector<char> >();
127  fseek(fp, 0, SEEK_END);
128  size_t length = ftell(fp);
129  fseek(fp, 0, SEEK_SET);
130 
131  //stream.open(path, std::ios::binary | std::ios::in);
132  // not found or other error
133  //if (stream.fail())
134  // return std::shared_ptr<std::vector<char> >();
135 
136  // get length
137  //stream.seekg(0, std::ios::end);
138  //int length = stream.tellg();
139  //stream.seekg(0, std::ios::beg);
140 
141  std::shared_ptr<std::vector<char> > ret = std::shared_ptr<std::vector<char> >(new std::vector<char>());
142  ret->resize(length);
143 
144  // HACK
145  //stream.read(&((*ret)[0]), length);
146  fread(&((*ret)[0]), 1, length, fp);
147 
148  return ret;
149 }
#define sLog
Evaluates to a NewLog instance.
Definition: LogNew.h:250
bool ValidateSize(std::string &category, uint32 size)
std::string GetFilePath(std::string &category, uint32 id, uint32 size)
bool ValidateCategory(std::string &category)

Here is the call graph for this function:

void ImageServer::ReportNewCharacter ( uint32  creatorAccountID,
uint32  characterID 
)
Todo:
need to get client here, and send msg about emailing char pic and name to charP.nosp@m.ics@.nosp@m.eve.a.nosp@m.lasi.nosp@m.ya.ne.nosp@m.t for manual insertion
Todo:
we will need to make size 64 and size 40 images, and possibably 128/256 of char portaits

Definition at line 77 of file ImageServer.cpp.

References _limboImages, _limboLock, GetFilePath(), and sLog.

78 {
79  sLog.Warning(" ImageServer"," ReportNewCharacter() called.");
80  Lock lock(_limboLock);
81 
82  // check if we received an image from this account previously
83  if (_limboImages.find(creatorAccountID) == _limboImages.end()) {
84  sLog.Error(" ImageServer"," Image not received for characterID %u.", characterID);
86  return;
87  }
88 
89  // we have, so save it
90  //std::ofstream stream;
91  std::string dirName = "Character";
92  std::string path(GetFilePath(dirName, characterID, 512));
93  FILE * fp = fopen(path.c_str(), "wb");
94 
95  //stream.open(path, std::ios::binary | std::ios::trunc | std::ios::out);
96  std::shared_ptr<std::vector<char> > data = _limboImages[creatorAccountID];
97 
98  fwrite(&((*data)[0]), 1, data->size(), fp);
99  fclose(fp);
100 
101  //std::copy(data->begin(), data->end(), std::ostream_iterator<char>(stream));
102  //stream.flush();
103  //stream.close();
104 
106  // github.com/nothings/stb/blob/master/stb_image_resize.h
107  // github.com/nothings/stb/blob/master/stb_image.h
108 
109  // and delete it from our limbo map
110  _limboImages.erase(creatorAccountID);
111 
112  sLog.Green(" ImageServer", "Received image from %u and saved as %s", creatorAccountID, path.c_str());
113 }
A lock for a Lockable object.
Definition: Lock.h:70
boost::asio::detail::mutex _limboLock
Definition: ImageServer.h:81
#define sLog
Evaluates to a NewLog instance.
Definition: LogNew.h:250
std::string GetFilePath(std::string &category, uint32 id, uint32 size)
std::unordered_map< uint32, std::shared_ptr< std::vector< char > > > _limboImages
Definition: ImageServer.h:75

Here is the call graph for this function:

void ImageServer::ReportNewImage ( uint32  accountID,
std::shared_ptr< std::vector< char > >  imageData 
)

Definition at line 65 of file ImageServer.cpp.

References _limboImages, _limboLock, and sLog.

66 {
67  sLog.Warning(" ImageServer"," ReportNewImage() called.");
68  Lock lock(_limboLock);
69 
70  if (_limboImages.find(accountID) != _limboImages.end()) {
71  _limboImages.insert(std::pair<uint32,std::shared_ptr<std::vector<char> > >(accountID, imageData));
72  } else {
73  _limboImages[accountID] = imageData;
74  }
75 }
A lock for a Lockable object.
Definition: Lock.h:70
boost::asio::detail::mutex _limboLock
Definition: ImageServer.h:81
#define sLog
Evaluates to a NewLog instance.
Definition: LogNew.h:250
unsigned __int32 uint32
Definition: eve-compat.h:50
std::unordered_map< uint32, std::shared_ptr< std::vector< char > > > _limboImages
Definition: ImageServer.h:75
void ImageServer::Run ( )

Definition at line 191 of file ImageServer.cpp.

References _ioThread, and RunInternal().

192 {
193  _ioThread = std::shared_ptr<boost::asio::detail::thread>(new boost::asio::detail::thread(std::bind(&ImageServer::RunInternal, this)));
194 }
void RunInternal()
std::shared_ptr< boost::asio::detail::thread > _ioThread
Definition: ImageServer.h:76

Here is the call graph for this function:

void ImageServer::RunInternal ( )
private

Definition at line 202 of file ImageServer.cpp.

References _io, and _listener.

Referenced by Run().

203 {
204  _io = std::shared_ptr<boost::asio::io_context>(new boost::asio::io_context());
205  _listener = std::shared_ptr<ImageServerListener>(new ImageServerListener(*_io));
206  _io->run();
207 }
std::shared_ptr< ImageServerListener > _listener
Definition: ImageServer.h:78
Handles listening for new clients.
std::shared_ptr< boost::asio::io_context > _io
Definition: ImageServer.h:77

Here is the caller graph for this function:

void ImageServer::Stop ( )

Definition at line 196 of file ImageServer.cpp.

References _io, and _ioThread.

197 {
198  _io->stop();
199  _ioThread->join();
200 }
std::shared_ptr< boost::asio::detail::thread > _ioThread
Definition: ImageServer.h:76
std::shared_ptr< boost::asio::io_context > _io
Definition: ImageServer.h:77
std::string & ImageServer::url ( )

Definition at line 186 of file ImageServer.cpp.

References _url.

187 {
188  return _url;
189 }
std::string _url
Definition: ImageServer.h:79
bool ImageServer::ValidateCategory ( std::string &  category)
private

Definition at line 178 of file ImageServer.cpp.

References Categories.

Referenced by GetImage().

179 {
180  for (int i = 0; i < 5; i++)
181  if (category == Categories[i])
182  return true;
183  return false;
184 }
static const char *const Categories[]
Definition: ImageServer.h:63

Here is the caller graph for this function:

bool ImageServer::ValidateSize ( std::string &  category,
uint32  size 
)
private

Definition at line 163 of file ImageServer.cpp.

Referenced by GetImage().

164 {
165  if (category == "InventoryType")
166  return size == 64 || size == 32;
167 
168  if (category == "Alliance")
169  return size == 256 || size == 128 || size == 64 || size == 32;
170 
171  if (category == "Corporation")
172  return size == 256 || size == 128 || size == 64 || size == 32;
173 
174  // Render and Character
175  return size == 1024 || size == 512 || size == 256 || size == 128 || size == 64 || size == 40 || size == 32;
176 }

Here is the caller graph for this function:

Member Data Documentation

std::string ImageServer::_basePath
private

Definition at line 80 of file ImageServer.h.

Referenced by GetFilePath(), and ImageServer().

std::shared_ptr<boost::asio::io_context> ImageServer::_io
private

Definition at line 77 of file ImageServer.h.

Referenced by RunInternal(), and Stop().

std::shared_ptr<boost::asio::detail::thread> ImageServer::_ioThread
private

Definition at line 76 of file ImageServer.h.

Referenced by Run(), and Stop().

std::unordered_map<uint32 , std::shared_ptr<std::vector<char> > > ImageServer::_limboImages
private

Definition at line 75 of file ImageServer.h.

Referenced by ReportNewCharacter(), and ReportNewImage().

boost::asio::detail::mutex ImageServer::_limboLock
private

Definition at line 81 of file ImageServer.h.

Referenced by ReportNewCharacter(), and ReportNewImage().

std::shared_ptr<ImageServerListener> ImageServer::_listener
private

Definition at line 78 of file ImageServer.h.

Referenced by RunInternal().

std::string ImageServer::_url
private

Definition at line 79 of file ImageServer.h.

Referenced by ImageServer(), and url().

const char *const ImageServer::Categories
static
Initial value:
= {
"Alliance",
"Corporation",
"Character",
"InventoryType",
"Render" }

Definition at line 63 of file ImageServer.h.

Referenced by ImageServer(), ImageServerConnection::ProcessHeaders(), and ValidateCategory().

const uint32 ImageServer::CategoryCount = 5
static

Definition at line 64 of file ImageServer.h.

Referenced by ImageServer(), and ImageServerConnection::ProcessHeaders().

const char *const ImageServer::FallbackURL = "http://image.eveonline.com/"
static
Todo:
boost is the only system in this code that does NOT leak

Definition at line 68 of file ImageServer.h.

Referenced by ImageServerConnection::RedirectLocation().


The documentation for this class was generated from the following files: