ISAAC
Overview :: Library Doc :: Server Doc :: JSON Commands

In Situ Animation of Accelerated Computations

SaveFileImageConnector.cpp
Go to the documentation of this file.
1 /* This file is part of ISAAC.
2  *
3  * ISAAC is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU Lesser General Public License as
5  * published by the Free Software Foundation, either version 3 of the
6  * License, or (at your option) any later version.
7  *
8  * ISAAC is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with ISAAC. If not, see <www.gnu.org/licenses/>. */
15 
17 #include <chrono>
18 #include <sys/stat.h>
19 #include <sys/types.h>
20 #include <boost/archive/iterators/binary_from_base64.hpp>
21 #include <boost/archive/iterators/transform_width.hpp>
22 
24 {
25  showClient = false;
26  this->dir = dir;
27 }
28 
30 {
31  return "SaveFileImageConnector";
32 }
33 
34 errorCode SaveFileImageConnector::init(int minport,int maxport)
35 {
36  return 0;
37 }
38 
40 {
41  int finish = 0;
42  while (finish == 0)
43  {
44  ImageBufferContainer* message;
45  while (message = clientGetMessage())
46  {
47  if (message->type == IMG_FORCE_EXIT)
48  finish = 1;
49  if (message->type == GROUP_ADDED)
50  {
51  std::chrono::seconds s = std::chrono::duration_cast< std::chrono::seconds >(std::chrono::system_clock::now().time_since_epoch());
52  std::string localDir = dir + std::string("/");
53  mkdir(localDir.c_str(),0777);
54  localDir += message->group->getName() + std::string("/");
55  mkdir(localDir.c_str(),0777);
56  localDir += std::to_string(s.count()) + std::string("/");
57  mkdir(localDir.c_str(),0777);
58  groupDir.insert(std::make_pair(message->group,localDir));
59  step.insert(std::make_pair(message->group,0));
60  }
61  if (message->type == GROUP_FINISHED)
62  {
63  groupDir.erase(message->group);
64  step.erase(message->group);
65  }
66  if (message->type == UPDATE_BUFFER)
67  {
68  pthread_mutex_lock (&(message->payload_mutex));
69  std::string payload( json_string_value( message->payload ) );
70  pthread_mutex_unlock (&(message->payload_mutex));
71  //Some copy and pasted black "base64 -> jpeg" magic from Broker.cpp
72  //Search for : in payload
73  const char* colon = strchr(payload.c_str(), ':');
74  if (colon != NULL)
75  {
76  colon++;
77  //Search for ; in payload
78  const char* semicolon = strchr(colon, ';');
79  if (semicolon != NULL)
80  {
81  //Search for , in payload
82  const char* comma = strchr(semicolon, ',');
83  if (comma != NULL)
84  {
85  //After the comma the base64 stream starts
86  comma++;
87  int whole_length = strlen(comma);
88 
89  uint8_t* temp_buffer = (uint8_t*)malloc(strlen(comma)+4); //Should always be enough
90  //base64 -> binary data
91  using namespace boost::archive::iterators;
92  typedef
93  transform_width
94  <
95  binary_from_base64
96  <
97  const uint8_t *
98  >,
99  8,
100  6
101  >
102  base64_dec;
103  int i = 0;
104  try
105  {
106  base64_dec src_it(comma);
107  for(; i < (whole_length+1)*3/4; ++i)
108  {
109  temp_buffer[i] = *src_it;
110  ++src_it;
111  }
112  }
113  catch (dataflow_exception&)
114  {
115  }
116  std::string filename = groupDir.at(message->group) + std::to_string(step.at(message->group)++) + std::string(".jpg");
117  FILE *fp = fopen(filename.c_str(),"wb");
118  fwrite (temp_buffer , sizeof(char), i, fp);
119  fclose (fp);
120  free(temp_buffer);
121  }
122  }
123  }
124  }
125  clientSendMessage( message );
126  }
127  usleep(1000);
128  }
129 }
ImageBufferContainer * clientGetMessage()
Definition: MessageAble.hpp:38
errorCode clientSendMessage(ImageBufferContainer *message)
Definition: MessageAble.hpp:34
InsituConnectorGroup * group
Definition: Common.hpp:196
std::string getName()
Definition: Broker.hpp:78
errorCode init(int minport, int maxport)
int errorCode
Definition: Common.hpp:24
SaveFileImageConnector(std::string dir)
ImageBufferType type
Definition: Common.hpp:195
pthread_mutex_t payload_mutex
Definition: Common.hpp:202