esl_oop.cpp 9.77 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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
#include <esl.h>
#include <esl_oop.h>

#define connection_construct_common() memset(&handle, 0, sizeof(handle))
#define event_construct_common() event = NULL; serialized_string = NULL; mine = 0; hp = NULL

void eslSetLogLevel(int level)
{
	esl_global_set_default_logger(level);
}

ESLconnection::ESLconnection(const char *host, const char *port, const char *password)
{
	connection_construct_common();
	int x_port = atoi(port);

	esl_connect(&handle, host, x_port, NULL, password);
}

ESLconnection::ESLconnection(const char *host, const char *port, const char *user, const char *password)
{
	connection_construct_common();
	int x_port = atoi(port);

	esl_connect(&handle, host, x_port, user, password);
}


ESLconnection::ESLconnection(int socket)
{
	connection_construct_common();
	esl_attach_handle(&handle, (esl_socket_t)socket, NULL);
}

ESLconnection::~ESLconnection()
{
	if (handle.connected) {
		esl_disconnect(&handle);
	}

}

int ESLconnection::socketDescriptor()
{
	if (handle.connected) {
        return (int) handle.sock;
    }

	return -1;
}


int ESLconnection::disconnect()
{
	if (handle.connected) {
        return esl_disconnect(&handle);
    }

	return 0;
}

int ESLconnection::connected()
{
	return handle.connected;
}

int ESLconnection::send(const char *cmd)
{
	return esl_send(&handle, cmd);
}

ESLevent *ESLconnection::sendRecv(const char *cmd)
{
	if (esl_send_recv(&handle, cmd) == ESL_SUCCESS) {
		esl_event_t *event;
		esl_event_dup(&event, handle.last_sr_event);
		return new ESLevent(event, 1);
	}
	
	return NULL;
}

ESLevent *ESLconnection::api(const char *cmd, const char *arg)
{
	size_t len;
	char *cmd_buf;
	ESLevent *event;
	
	if (!cmd) {
		return NULL;
	}

	len = strlen(cmd) + (arg ? strlen(arg) : 0) + 10;

	cmd_buf = (char *) malloc(len + 1);
	assert(cmd_buf);

	snprintf(cmd_buf, len, "api %s %s", cmd, arg ? arg : "");
	*(cmd_buf + (len)) = '\0';


	event = sendRecv(cmd_buf);
	free(cmd_buf);

	return event;
}

ESLevent *ESLconnection::bgapi(const char *cmd, const char *arg, const char *job_uuid)
{
	size_t len;
	char *cmd_buf;
	ESLevent *event;
	
	if (!cmd) {
		return NULL;
	}

	len = strlen(cmd) + (arg ? strlen(arg) : 0) + (job_uuid ? strlen(job_uuid) + 12 : 0) + 10;

	cmd_buf = (char *) malloc(len + 1);
	assert(cmd_buf);
	
	if (job_uuid) {
		snprintf(cmd_buf, len, "bgapi %s%s%s\nJob-UUID: %s", cmd, arg ? " " : "", arg ? arg : "", job_uuid);
	} else {
		snprintf(cmd_buf, len, "bgapi %s%s%s", cmd, arg ? " " : "", arg ? arg : "");
	}

	*(cmd_buf + (len)) = '\0';

	event = sendRecv(cmd_buf);
	free(cmd_buf);
	
	return event;
}

ESLevent *ESLconnection::getInfo()
{
	if (handle.connected && handle.info_event) {
		esl_event_t *event;
		esl_event_dup(&event, handle.info_event);
		return new ESLevent(event, 1);
	}
	
	return NULL;
}

int ESLconnection::setAsyncExecute(const char *val)
{
	if (val) {
		handle.async_execute = esl_true(val);
	}
	return handle.async_execute;
}

int ESLconnection::setEventLock(const char *val)
{
	if (val) {
		handle.event_lock = esl_true(val);
	}
	return handle.event_lock;
}

ESLevent *ESLconnection::execute(const char *app, const char *arg, const char *uuid)
{
	if (esl_execute(&handle, app, arg, uuid) == ESL_SUCCESS) {
		esl_event_t *event;
		esl_event_dup(&event, handle.last_sr_event);
		return new ESLevent(event, 1);
	}

	return NULL;
}


ESLevent *ESLconnection::executeAsync(const char *app, const char *arg, const char *uuid)
{
	int async = handle.async_execute;
	int r;

	handle.async_execute = 1;
	r = esl_execute(&handle, app, arg, uuid);
	handle.async_execute = async;

	if (r == ESL_SUCCESS) {
		esl_event_t *event;
		esl_event_dup(&event, handle.last_sr_event);
		return new ESLevent(event, 1);
	}

	return NULL;
}

ESLevent *ESLconnection::sendEvent(ESLevent *send_me)
{
	if (esl_sendevent(&handle, send_me->event) == ESL_SUCCESS) {
		esl_event_t *e = handle.last_ievent ? handle.last_ievent : handle.last_event;
		if (e) {
			esl_event_t *event;
			esl_event_dup(&event, e);
			return new ESLevent(event, 1);
		}
	}

	return new ESLevent("server_disconnected");
}

ESLevent *ESLconnection::recvEvent()
{
	if (esl_recv_event(&handle, 1, NULL) == ESL_SUCCESS) {
		esl_event_t *e = handle.last_ievent ? handle.last_ievent : handle.last_event;
		if (e) {
			esl_event_t *event;
			esl_event_dup(&event, e);
			return new ESLevent(event, 1);
		}
	}

	return new ESLevent("server_disconnected");
}

ESLevent *ESLconnection::recvEventTimed(int ms)
{

	if (esl_recv_event_timed(&handle, ms, 1, NULL) == ESL_SUCCESS) {
		esl_event_t *e = handle.last_ievent ? handle.last_ievent : handle.last_event;
		if (e) {
			esl_event_t *event;
			esl_event_dup(&event, e);
			return new ESLevent(event, 1);
		}
    }
	
	return NULL;
}

ESLevent *ESLconnection::filter(const char *header, const char *value)
{
	esl_status_t status = esl_filter(&handle, header, value);

	if (status == ESL_SUCCESS && handle.last_sr_event) {
		esl_event_t *event;
		esl_event_dup(&event, handle.last_sr_event);
		return new ESLevent(event, 1);
	}

	return NULL;

}

int ESLconnection::events(const char *etype, const char *value)
{
	esl_event_type_t type_id = ESL_EVENT_TYPE_PLAIN;

	if (!strcmp(etype, "xml")) {
		type_id = ESL_EVENT_TYPE_XML;
	} else if (!strcmp(etype, "json")) {
        type_id = ESL_EVENT_TYPE_JSON;
	}

	return esl_events(&handle, type_id, value);
}

// ESLevent
///////////////////////////////////////////////////////////////////////

ESLevent::ESLevent(const char *type, const char *subclass_name)
{
	esl_event_types_t event_id;
	
	event_construct_common();

	if (!strcasecmp(type, "json") && !esl_strlen_zero(subclass_name)) {
		if (esl_event_create_json(&event, subclass_name) != ESL_SUCCESS) {
			return;
			
		}
		event_id = event->event_id;
	} else {

		if (esl_name_event(type, &event_id) != ESL_SUCCESS) {
			event_id = ESL_EVENT_MESSAGE;
		}

		if (!esl_strlen_zero(subclass_name) && event_id != ESL_EVENT_CUSTOM) {
			esl_log(ESL_LOG_WARNING, "Changing event type to custom because you specified a subclass name!\n");
			event_id = ESL_EVENT_CUSTOM;
		}

		if (esl_event_create_subclass(&event, event_id, subclass_name) != ESL_SUCCESS) {
			esl_log(ESL_LOG_ERROR, "Failed to create event!\n");
			event = NULL;
		}
	}
	
	serialized_string = NULL;
	mine = 1;
	
}

ESLevent::ESLevent(esl_event_t *wrap_me, int free_me)
{
	event_construct_common();
	event = wrap_me;
	mine = free_me;
	serialized_string = NULL;
}

ESLevent::ESLevent(ESLevent *me)
{
	/* workaround for silly php thing */
	event = me->event;
	mine = me->mine;
	serialized_string = NULL;
	me->event = NULL;
	me->mine = 0;
	esl_safe_free(me->serialized_string);
}

ESLevent::~ESLevent()
{
	
	if (serialized_string) {
		free(serialized_string);
	}

	if (event && mine) {
		esl_event_destroy(&event);
	}
}

const char *ESLevent::nextHeader(void)
{
	const char *name = NULL;

	if (hp) {
		name = hp->name;
		hp = hp->next;
	}

	return name;
}

const char *ESLevent::firstHeader(void)
{
	if (event) {
		hp = event->headers;
	}

	return nextHeader();
}

const char *ESLevent::serialize(const char *format)
{
	this_check("");

	esl_safe_free(serialized_string);
	
	if (format == NULL) {
		format = "text";
	}
	
	if (!event) {
		return "";
	}

	if (format && !strcasecmp(format, "json")) {
		esl_event_serialize_json(event, &serialized_string);
		return serialized_string;
	}

	if (esl_event_serialize(event, &serialized_string, ESL_TRUE) == ESL_SUCCESS) {
		return serialized_string;
	}

	return "";

}

bool ESLevent::setPriority(esl_priority_t priority)
{
	this_check(false);

	if (event) {
        esl_event_set_priority(event, priority);
		return true;
	} else {
		esl_log(ESL_LOG_ERROR, "Trying to setPriority an event that does not exist!\n");
    }
	return false;
}

const char *ESLevent::getHeader(const char *header_name, int idx)
{
	this_check("");

	if (event) {
		return esl_event_get_header_idx(event, header_name, idx);
	} else {
		esl_log(ESL_LOG_ERROR, "Trying to getHeader an event that does not exist!\n");
	}
	return NULL;
}

bool ESLevent::addHeader(const char *header_name, const char *value)
{
	this_check(false);

	if (event) {
		return esl_event_add_header_string(event, ESL_STACK_BOTTOM, header_name, value) == ESL_SUCCESS ? true : false;
	} else {
		esl_log(ESL_LOG_ERROR, "Trying to addHeader an event that does not exist!\n");
	}

	return false;
}

bool ESLevent::pushHeader(const char *header_name, const char *value)
{
	this_check(false);

	if (event) {
		return esl_event_add_header_string(event, ESL_STACK_PUSH, header_name, value) == ESL_SUCCESS ? true : false;
	} else {
		esl_log(ESL_LOG_ERROR, "Trying to addHeader an event that does not exist!\n");
	}

	return false;
}

bool ESLevent::unshiftHeader(const char *header_name, const char *value)
{
	this_check(false);

	if (event) {
		return esl_event_add_header_string(event, ESL_STACK_UNSHIFT, header_name, value) == ESL_SUCCESS ? true : false;
	} else {
		esl_log(ESL_LOG_ERROR, "Trying to addHeader an event that does not exist!\n");
	}

	return false;
}

bool ESLevent::delHeader(const char *header_name)
{
	this_check(false);

	if (event) {
		return esl_event_del_header(event, header_name) == ESL_SUCCESS ? true : false;
	} else {
		esl_log(ESL_LOG_ERROR, "Trying to delHeader an event that does not exist!\n");
	}

	return false;
}


bool ESLevent::addBody(const char *value)
{
	this_check(false);

	if (event) {
		return esl_event_add_body(event, "%s", value) == ESL_SUCCESS ? true : false;
	} else {
		esl_log(ESL_LOG_ERROR, "Trying to addBody an event that does not exist!\n");
	}
	
	return false;
}

char *ESLevent::getBody(void)
{
	
	this_check((char *)"");

	if (event) {
		return esl_event_get_body(event);
	} else {
		esl_log(ESL_LOG_ERROR, "Trying to getBody an event that does not exist!\n");
	}
	
	return NULL;
}

const char *ESLevent::getType(void)
{
	this_check("");

	if (event) {
		return esl_event_name(event->event_id);
	} else {
		esl_log(ESL_LOG_ERROR, "Trying to getType an event that does not exist!\n");
	}
	
	return (char *) "invalid";
}