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

In Situ Animation of Accelerated Computations

Common.hpp
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 
16 #pragma once
17 
18 #include <string>
19 #include <string.h>
20 #include <jansson.h>
21 
22 typedef int ClientRef;
23 typedef int ObserverRef;
24 typedef int errorCode;
25 
26 #define ISAAC_MAX_RECEIVE 4194304 //4 MB
27 
28 typedef enum
29 {
30  FORCE_EXIT = -1,
31  FEEDBACK = 0,
44 } MessageType;
45 
47 {
48  public:
49  static pthread_mutex_t deep_copy_mutex;
50  MessageContainer(MessageType type = NONE,json_t *json_root = NULL, bool keep_json = false, bool drop_able = false)
52  {
53  if (keep_json && json_root)
54  {
55  pthread_mutex_lock(&deep_copy_mutex);
56  //json_root = json_deep_copy( json_root );
57  json_incref( json_root );
58  pthread_mutex_unlock(&deep_copy_mutex);
59  }
60  this->json_root = json_root;
61  json_t *json_type;
62  if (type == NONE && (json_type = json_object_get(json_root, "type")) && json_is_string(json_type))
63  {
64  const char* str = json_string_value(json_type);
65  if (strcmp(str,"feedback") == 0)
66  this->type = FEEDBACK;
67  else
68  if (strcmp(str,"hello") == 0)
69  this->type = MASTER_HELLO;
70  else
71  if (strcmp(str,"register") == 0)
72  this->type = REGISTER;
73  else
74  if (strcmp(str,"register video") == 0)
75  this->type = REGISTER_VIDEO;
76  else
77  if (strcmp(str,"exit") == 0)
78  this->type = EXIT_PLUGIN;
79  else
80  if (strcmp(str,"period video") == 0)
81  this->type = PERIOD_VIDEO;
82  else
83  if (strcmp(str,"period") == 0)
84  this->type = PERIOD;
85  else
86  if (strcmp(str,"observe") == 0)
87  this->type = OBSERVE;
88  else
89  if (strcmp(str,"stop") == 0)
90  this->type = STOP;
91  else
92  if (strcmp(str,"closed") == 0)
93  this->type = CLOSED;
94  else
95  if (strcmp(str,"update") == 0)
96  this->type = UPDATE;
97  else
98  this->type = UNKNOWN;
99  }
100  else
101  this->type = type;
102  }
103  void suicide()
104  {
105  delete this;
106  }
108  {
109  if (json_root)
110  json_decref( json_root );
111  }
113  json_t *json_root;
114  bool drop_able;
115 };
116 
117 typedef enum
118 {
127 
129 
131 {
132  public:
133  ImageBuffer(uint8_t* buffer, int ref_count) :
134  buffer( buffer ),
135  ref_count (ref_count )
136  {
137  pthread_mutex_init (&ref_mutex, NULL);
138  }
140  {
141  pthread_mutex_destroy(&ref_mutex);
142  }
143  void incref()
144  {
145  pthread_mutex_lock (&ref_mutex);
146  ref_count++;
147  pthread_mutex_unlock (&ref_mutex);
148  }
149  void suicide()
150  {
151  pthread_mutex_lock (&ref_mutex);
152  ref_count--;
153  if (ref_count <= 0)
154  {
155  pthread_mutex_unlock (&ref_mutex);
156  free(buffer);
157  delete this;
158  }
159  else
160  pthread_mutex_unlock (&ref_mutex);
161  }
162  uint8_t* buffer;
164  pthread_mutex_t ref_mutex;
165 };
166 
168 {
169  public:
170  ImageBufferContainer(ImageBufferType type,uint8_t* buffer,InsituConnectorGroup* group,int ref_count,std::string target = "",void* reference = NULL,json_t* json = NULL,json_t* payload = NULL,int insitu_id = 0) :
171  type( type ),
172  group( group ),
173  target( target ),
174  reference( reference ),
175  ref_count( ref_count ),
176  json( json ),
177  payload( payload ),
178  insitu_id( insitu_id )
179  {
180  image = new ImageBuffer( buffer, ref_count );
181  pthread_mutex_init (&json_mutex, NULL);
182  pthread_mutex_init (&payload_mutex, NULL);
183  }
184  void suicide()
185  {
186  ref_count--;
187  if (ref_count <= 0)
188  delete this;
189  }
191  {
192  pthread_mutex_destroy(&json_mutex);
193  pthread_mutex_destroy(&payload_mutex);
194  }
197  std::string target;
198  void* reference;
201  pthread_mutex_t json_mutex;
202  pthread_mutex_t payload_mutex;
203  json_t* json;
204  json_t* payload;
206 };
207 
Definition: Common.hpp:42
~ImageBuffer()
Definition: Common.hpp:139
InsituConnectorGroup * group
Definition: Common.hpp:196
Definition: Common.hpp:39
MessageType type
Definition: Common.hpp:112
pthread_mutex_t json_mutex
Definition: Common.hpp:201
int ClientRef
Definition: Common.hpp:22
void suicide()
Definition: Common.hpp:149
pthread_mutex_t ref_mutex
Definition: Common.hpp:164
int errorCode
Definition: Common.hpp:24
MessageType
Definition: Common.hpp:28
static pthread_mutex_t deep_copy_mutex
Definition: Common.hpp:49
json_t * json_root
Definition: Common.hpp:113
void incref()
Definition: Common.hpp:143
ImageBufferType
Definition: Common.hpp:117
ImageBufferType type
Definition: Common.hpp:195
ImageBuffer(uint8_t *buffer, int ref_count)
Definition: Common.hpp:133
MessageContainer(MessageType type=NONE, json_t *json_root=NULL, bool keep_json=false, bool drop_able=false)
Definition: Common.hpp:50
int ObserverRef
Definition: Common.hpp:23
int ref_count
Definition: Common.hpp:163
std::string target
Definition: Common.hpp:197
uint8_t * buffer
Definition: Common.hpp:162
ImageBufferContainer(ImageBufferType type, uint8_t *buffer, InsituConnectorGroup *group, int ref_count, std::string target="", void *reference=NULL, json_t *json=NULL, json_t *payload=NULL, int insitu_id=0)
Definition: Common.hpp:170
pthread_mutex_t payload_mutex
Definition: Common.hpp:202
void suicide()
Definition: Common.hpp:103
ImageBuffer * image
Definition: Common.hpp:199