Archive for February, 2008

The Cosmos

| February 19th, 2008
cosmic_gc3_new
A computer simulation depicting a large chunk of our universe
G. L. Bryan, M. L. Norman, UIUC, NCSA, GC3
Cosmology is the study of the overall structure of the universe. Just what is the Universe? It is everything that exists. However, from Earth we cannot observe everything in the Universe. Some things are dark (brown dwarf stars, planets, and Dark Matter) and we cannot see them. Additionally, there are parts of the universe whose light has not yet reached us in this part of the Universe.
 And because light travels at a set speed we actually look back in time when we look into the cosmos. Astronomers observe some interesting structure in the Current Universe. That structure can tell us much about the History of the Universe. It can also tell us what we can expect for the Future of the Universe and beyond…

check_cti.c

| February 10th, 2008

A “check_cti.c” code to check DN is alive/can be registered with Genesys SIP Server. Used for monitoring, specially if you don’t trust Genesys Alerts!

#ifndef lint
static char rcsid[] = "$Id: dial.c,v 0.2 2005/08/23 14:55:34 epi Exp $";
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "tlibrary.h"
#include "kvlist.h"
#include "convert.h"
#include "connection.h"

#define RESET		0
#define BRIGHT 		1
#define DIM		2
#define UNDERLINE 	3
#define BLINK		4
#define REVERSE		7
#define HIDDEN		8

#define BLACK 		0
#define RED		1
#define GREEN		2
#define YELLOW		3
#define BLUE		4
#define MAGENTA		5
#define CYAN		6
#define	WHITE		7

                /* Auto Dial parameters:                                     */
char *dest_dn;  /*   the destination DN (where the call should be placed to) */
char *check_dn;  /*   temporary DN used as origination of outgoing call       */
char *agent_dn; /*   agent DN where call should be transferred after dest_dn */
                /*   answers the call                                        */

int answer_timeout  = 30; /* how long to wait for answer (approx. 5 rings) */
int redial_delay    = 60; /* how long to wait before re-dial (one minute)  */
int redial_attempt  = 3; /* when to gave up (after 10 attempts)           */
int request_timeout =  5; /* timeout for requests sent to TServer          */
int contador=0, continuar=1, hangup=0;
int temp_user_data;
/*-------------------------------------------------------------------------*/

TKVList *user_data;

TConnectionID conn_id;    /* ConnID of the call (set in dispatch_function) */

enum call_status {
	stat_NotInitialized,
	stat_Idle,
	stat_Dialed,
	stat_Answered,
	stat_Released
}

current_status = stat_NotInitialized;

void textcolor(int attr, int fg, int bg);

static void dispatch_function      (TEvent *event);
static void wait_for_status_changed(TServer server, int timeout_secs);

int main (int argc, char **argv){

	char command_line_text[120];
	char *email = NULL;
	char *cmd = NULL;
	char *command = NULL;
	char *server_name = NULL;
	TServer server;  char *p; int i, y, rc, port;

	textcolor(BRIGHT, BLUE, BLACK);	
	printf("Ighor Toth <igtoth@gmail.com>\n");
	printf("Check_CTI\n");
	printf("v0.0.1 2008/07/04\n");
	textcolor(RESET, WHITE, BLACK);	

	if (argc < 6) {
                printf("CTI Check CTI\n");
                printf("Ighor Toth <igtoth@gmail.com>\n");
		printf("Usage: %s DN -h sip-server:port [options]\n", argv[0]);
		printf("options are:\n");
		printf("  -t seconds (timeout)\n");
		printf("  -e email (e-mail for alert)\n");
		printf("\n");	
		textcolor(BRIGHT, RED, BLACK);
		printf("-------------------------------------------------------------------------\n");
		textcolor(RESET, WHITE, BLACK);
		exit(1);
	}

	check_dn  = argv[1];
	
	if (!strcmp(argv[2], "-h")) server_name = argv[3];

	for (i = 4; i < argc; i++) {
		if (strcmp(argv[i], "-e") == 0){ 
			email = atoi(argv[1+i]); 
		} else if (strcmp(argv[i], "-t") == 0){ 
			request_timeout = atoi(argv[1+i]);
		} 
	}

	printf("\n");
	textcolor( BRIGHT, RED, BLACK);
	printf("-------------------------------------------------------------------------\n");
	textcolor(RESET, WHITE, BLACK);
	printf("@"); timestamp(stdout);
	printf(" [--] Using T-Server (host:port): %s | Checking SIP-Server now... \n",server_name);
	textcolor( BRIGHT, RED, BLACK);

	textcolor(RESET, WHITE, BLACK);

	conn_startup();       /* need to initialize socket layer at the beginning */
	atexit(conn_cleanup); /* and cleanup it before exit (mandatory for WinNT) */

	if (server_name && (p = strchr(server_name, ':')) != NULL) {
		*p++ = 0;
		port = atoi(p);
		
		/*
		 * Opening connection to TServer using hostname/port specification:
		 */


for (i = 1; i <= redial_attempt; i++) {

	printf("@"); timestamp(stdout);
	printf(" [<<] message: opening connection with SIP-Server %s \n",server_name);

		server = TOpenServerEx(server_name, /* hostname of TServer     */
                                  port, /* TServer port            */
                     dispatch_function, /* dispatch function       */
							"Check_CTI", /* application name        */
                                  NULL, /* app password (not used) */
                             SyncMode); /* using synchronous mode  */

  	} 

	if (server < 0) {
		printf("@"); timestamp(stdout);
		printf(" [!!] ERROR, cannot open connection with SIP-Server %s \n",server_name);
		sprintf(command_line_text,"[!!] ERROR, cannot open connection with SIP-Server %s ",server_name);
		if(i > 1) { system(command_line_text); }
		exit(1);
	} else {
		textcolor(BRIGHT, GREEN, BLACK);
		printf("@"); timestamp(stdout);
		printf(" [>>] message: connection with SIP-Server successful! \n");
		textcolor(RESET, WHITE, BLACK);

		wait_for_status_changed(server, request_timeout);

		if (current_status == stat_NotInitialized) {
			printf("@"); timestamp(stdout);
			printf(" [!!] ERROR, unable to register DN %s after %d seconds \n", check_dn, answer_timeout);
			cmd = "echo \"[!!] ERROR, unable to register DN %s after %d seconds \" | mail -s \"CTI Error\" ";
			command = strcat(cmd, email);
			sprintf(command_line_text, command, check_dn, answer_timeout);
			if(i > 1) { system(command_line_text); }

		} else {
			textcolor(BRIGHT, GREEN, BLACK);
			printf("@"); timestamp(stdout);
			printf(" [>>] message: DN %s registered! \n",check_dn);
			textcolor(RESET, WHITE, BLACK);

			printf("@"); timestamp(stdout);
			printf(" [<<] message: unregister DN: %s \n",check_dn);

			if(TUnregisterAddress(server,check_dn,RegisterDefault,NULL)< 0){
				printf("@"); timestamp(stdout);
				
			printf(" [!!] ERROR, unable to unregister DN: %s \n",check_dn);
				cmd = "echo \"[!!] ERROR, unable to unregister DN: %s \" | mail -s \"CTI Error\" ";
				command = strcat(cmd, email);
				sprintf(command_line_text, command, check_dn);
				if(i > 1) { system(command_line_text); }

			} else {
				wait_for_status_changed(server, request_timeout);
				textcolor(BRIGHT, GREEN, BLACK);
				printf("@"); timestamp(stdout);
				printf(" [>>] message: DN %s unregistered! \n",check_dn);
				textcolor(RESET, WHITE, BLACK);
			}


		}

		printf("@"); timestamp(stdout);
		printf(" [<<] message: closing connection to SIP-Server %s \n",server_name);

		if(TCloseServer(server) < 0){
			printf("@"); timestamp(stdout);
			printf(" [ERROR] unable to close connection to SIP-Server: %s \n",server_name);
			cmd = "echo \"[ERROR] unable to close connection to SIP-Server: %s \" | mail -s \"CTI Error\" ";
			command = strcat(cmd, email);
			sprintf(command_line_text, command, server_name);
			if(i > 1) { system(command_line_text); }
		} else {
			textcolor(BRIGHT, GREEN, BLACK);
			printf("@"); timestamp(stdout);
			printf(" [>>] message: closed connection to SIP-Server: %s \n",server_name);
			textcolor(RESET, WHITE, BLACK);

		}

		printf("@"); timestamp(stdout);
		printf(" [--] exit\n");

		return 0;

	}




} /* end_of_if_i */


} /* end_main_function */

/* ------------------------------------------- */
/* 
 * User-defined dispatch function is called by TLibrary whenever an event
 * comes from TServer:
 */
/* ------------------------------------------- */

static void dispatch_function (TEvent *event)
{


	/* printf("\nDISPATCH_FUNCTION CALLED"); */

	textcolor(BRIGHT, RED, BLACK);
	printf("\n--> %s", TGetMessageTypeName(event->Event));
	textcolor(RESET, WHITE, BLACK);	

	switch (event->Event){

		case EventError:
			printf("\nERROR: Cannot perform the request by SIP-Server");
			exit(1);
			break;

		case EventServerConnected:
			printf("\n*** Successful connection with SIP-Server established");
			break;

  		case EventServerDisconnected:
    			current_status = stat_NotInitialized;
    			break;

		case EventLinkConnected:


			printf("\n<-- Register DN: %s",check_dn);
			if (TRegisterAddress(event->Server, check_dn, ModeShare,
			    RegisterDefault, AddressTypeDN, NULL) < 0) {
      				printf("\nERROR: Cannot register DN %d\n", check_dn);
      				exit(1);
    			}
    			break;

		case EventRegistered:


			current_status = stat_Idle;
			break;

		case EventRinging:
			if(TAnswerCall(event->Server, check_dn, event->ConnID, NULL, NULL) < 0){
      				printf("\nERROR: Cannot answer call with ConnId %s\n", connid_to_str(event->ConnID));
      				exit(1);
			} else {
				printf("\n*** Call answered! ConnId: %s",connid_to_str(event->ConnID));
			}
			conn_id = event->ConnID;
			current_status = stat_Answered;
			break;

		case EventDialing:
			printf("\n*** ConnID = %s", connid_to_str(event->ConnID));
			printf("\n<-- Ringing! Waiting to be answered...");

			conn_id = event->ConnID;
			break;

		case EventDestinationBusy:	
			TReleaseCall(event->Server, event->ThisDN, event->ConnID, NULL, NULL);
			current_status = stat_Released;
			break;

		case EventReleased:
			current_status = stat_Idle;
			break;

		case EventEstablished:

			if (event->UserData){
				TKVListPrint(stdout, event->UserData, "\n*** UserData:");

				printf("*** TKV List (Userdata) Printed");
			}

			conn_id = event->ConnID;
			current_status = stat_Answered;
			break;
	}
}

/* ------------------------------------------- */

static void wait_for_status_changed(TServer server, int timeout_sec){

	enum call_status old_status = current_status;
	int current_time = time(NULL),
	time_started = current_time,
	time_to_exit = time_started + timeout_sec;

	do {
		if (TScanServer(server, time_to_exit - current_time) < 0) {
			printf("Error in TScanServer\n");
			exit(1);
		}
		if (current_status != old_status) return;
	}
	while ((current_time = time(NULL)) < time_to_exit);
}


/* ------------------------------------------- */

void textcolor(int attr, int fg, int bg){

	char command[13];
	/* Command is the control command to the terminal */
	sprintf(command, "%c[%d;%d;%dm", 0x1B, attr, fg + 30, bg + 40);
	printf("%s", command);
}

/* ------------------------------------------- */

void timestamp(FILE *out)
{
  struct tm *tmp;
  time_t t;
 
  t = time(NULL);
  tmp = localtime(&t); /* or gmtime, if you want GMT^H^H^HUTC */
  fprintf(out, “%02d.%02d.%04d %02d:%02d:%02d”,
	  tmp->tm_year+1900, tmp->tm_mon+1, tmp->tm_mday,
	  tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
}