Commit 970ed795 authored by Elemer Lelik's avatar Elemer Lelik
Browse files

Titan Core Initial Contribution

parent f87b9e3a
doxygen
Install
Makefile.personal
.cproject
.TITAN*
*.o
*.exe
*.pdf
META-INF
build.properties
lib*.so
*.o
###############################################################################
# Copyright (c) 2000-2014 Ericsson Telecom AB
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# which accompanies this distribution, and is available at
# http://www.eclipse.org/legal/epl-v10.html
###############################################################################
#
# "org_eclipse_titan_executor_jni_JNIMiddleWare.h" is generated by javah (java header generator tool)
# DO NOT EDIT THAT FILE UNLESS YOU KNOW WHAT YOU ARE DOING
#
# CLI command for header generation:
# > javah -classpath . -jni org.eclipse.titan.executor.jni.JNIMiddleWare
# - must be issued in the /bin direcory within the project
TOP := ..
include $(TOP)/Makefile.cfg
CPPFLAGS += -I$(JDKDIR)/include
ifeq ($(PLATFORM), WIN32)
CPPFLAGS += -D__int64='long long'
endif
ifeq ($(PLATFORM), LINUX)
CPPFLAGS += -I$(JDKDIR)/include/linux
endif
ifeq ($(PLATFORM), SOLARIS)
CPPFLAGS += -I$(JDKDIR)/include/solaris
endif
ifeq ($(PLATFORM), SOLARIS8)
CPPFLAGS += -I$(JDKDIR)/include/solaris
endif
STATIC_SOURCES := jnimw.cc jninativelib.cc
HEADERS := jnimw.h org_eclipse_titan_executor_jni_JNIMiddleWare.h
SOURCES := $(STATIC_SOURCES)
OBJECTS := $(patsubst %.cc,%.o,$(patsubst %.c,%.o,$(SOURCES)))
OTHER_OBJECTS := $(TOP)/mctr2/mctr/MctrError.o $(TOP)/mctr2/mctr/MainController.o \
$(TOP)/mctr2/mctr/UserInterface.o $(TOP)/common/memory.o \
$(TOP)/common/new.o $(TOP)/core/Textbuf.o $(TOP)/core/RInt.o $(TOP)/common/NetworkHandler.o \
$(TOP)/mctr2/cli/config_read.lex.o $(TOP)/mctr2/cli/config_read.tab.o $(TOP)/mctr2/mctr/config_data.o \
$(TOP)/common/config_preproc_p.tab.o $(TOP)/common/config_preproc.o $(TOP)/common/config_preproc_la.o \
$(TOP)/common/path.o $(TOP)/common/memory.o $(TOP)/common/Path2.o
ifeq ($(LICENSING), yes)
OTHER_OBJECTS += $(TOP)/common/license.o
endif
TARGETS := libmctrjninative.so
SOLARIS_LIBS =
SOLARIS8_LIBS := -lsocket -lnsl
LINUX_LIBS :=
FREEBSD_LIBS =
WIN32_LIBS =
#
# Rules for building the executable...
#
all run: $(TARGETS);
libmctrjninative.so: $(OBJECTS) $(OTHER_OBJECTS)
$(LD) $(LDFLAGS) -o $@ $^ \
$($(PLATFORM)_LIBS) -lpthread $(LICENSE_LIBS) -L$(OPENSSL_DIR)/lib -lcrypto
install: $(TARGETS)
ifeq ($(DEBUG), no)
$(STRIP) $(TARGETS)
endif
mkdir -p $(LIBDIR)
cp $(TARGETS) $(LIBDIR)
include $(TOP)/Makefile.genrules
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2000-2014 Ericsson Telecom AB
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// which accompanies this distribution, and is available at
// http://www.eclipse.org/legal/epl-v10.html
///////////////////////////////////////////////////////////////////////////////
#include "jnimw.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <memory.h>
#include <string.h>
#include <errno.h>
#include "../core/Logger.hh"
using mctr::MainController;
using namespace jnimw;
Jnimw *Jnimw::userInterface;
bool Jnimw::has_status_message_pending;
int Jnimw::pipe_size;
/**
* The last MC state. It is needed by status_change(), as
* status change message is written to the pipe when any status (MC, TC, HC) was changed AND
* ( currently there is no status change message on the pipe (signalled by has_status_message_pending)
* OR MC state is changed )
*/
mctr::mc_state_enum last_mc_state;
pthread_mutex_t Jnimw::mutex = PTHREAD_MUTEX_INITIALIZER;
/**
* Config data, which was created by Java_org_eclipse_titan_executor_jni_JNIMiddleWare_set_1cfg_1file()
* by a JNI request, and the result will be used by
* Java_org_eclipse_titan_executor_jni_JNIMiddleWare_configure().
* This is done this way to use process_config_read_file() for processing the config file
* instead of processing it on the Java side.
*/
config_data Jnimw::mycfg;
void Jnimw::lock()
{
int result = pthread_mutex_lock(&mutex);
if (result > 0) {
fatal_error("Jni middleware::lock: "
"pthread_mutex_lock failed with code %d.", result);
}
}
void Jnimw::unlock()
{
int result = pthread_mutex_unlock(&mutex);
if (result > 0) {
fatal_error("Jni middleware:::unlock: "
"pthread_mutex_unlock failed with code %d.", result);
}
}
void Jnimw::fatal_error(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
if (errno != 0) fprintf(stderr, " (%s)", strerror(errno));
putc('\n', stderr);
exit(EXIT_FAILURE);
}
int Jnimw::enterLoop(int, char*[]) {
return EXIT_SUCCESS;
}
Jnimw::Jnimw()
{
pipe_buffer = NULL;
pipe_fd[0] = -1;
pipe_fd[1] = -1;
create_pipe();
FD_ZERO(&readfds);
FD_SET(pipe_fd[0], &readfds);
has_status_message_pending = false;
last_mc_state = mctr::MC_INACTIVE;
pipe_size = 0;
if (pthread_mutex_init(&mutex, NULL))
fatal_error("Jni middleware::constructor: pthread_mutex_init failed.");
}
Jnimw::~Jnimw()
{
destroy_pipe();
pthread_mutex_destroy(&mutex);
}
void strreverse(char* begin, char* end) {
char aux;
while(end>begin){
aux=*end, *end--=*begin, *begin++=aux;
}
}
/**
* Ansi C "itoa" based on Kernighan & Ritchie's "Ansi C":
*/
void itoa(int value, char* str) {
static char num[] = "0123456789";
char* wstr=str;
// Conversion. Number is reversed.
do *wstr++ = num[value%10]; while(value/=10);
*wstr='\0';
// Reverse string
strreverse(str,wstr-1);
}
void create_packet_header(const int source_length, char* dest, char method_id) {
char packet_size[6];
dest[0] = method_id;
itoa(source_length, packet_size);
int i;
for(i = 1; i < 6; i++) dest[i] = '0';
dest[6] = '\0';
int j = strlen(packet_size);
for(i = 0; i < j; i++) dest[5-i] = packet_size[j-i-1];
}
char* stuffer(const char* msg){
char* msg_stuffed = (char*) malloc(strlen(msg)*2);
int i = 0;
int j = 0;
while(msg[i] != '\0') {
if(msg[i] != '|' && msg[i] != '\\') {
msg_stuffed[j++] = msg[i];
} else {
msg_stuffed[j++] = '\\';
msg_stuffed[j++] = msg[i];
}
i++;
}
msg_stuffed[j] = '\0';
return msg_stuffed;
}
//----------------------------------------------------------------------------
// USERINTERFACE
void Jnimw::status_change()
{
lock();
mctr::mc_state_enum mc_state = MainController::get_state();
if(last_mc_state != mc_state || !has_status_message_pending){
char str[7];
sprintf( str,"S%02d000", mc_state );
write_pipe( str );
}
has_status_message_pending = true;
last_mc_state = mc_state;
unlock();
}
//----------------------------------------------------------------------------
// USERINTERFACE
void Jnimw::error(int severity, const char* msg)
{
char *msg_stuffed = stuffer(msg);
expstring_t pipe_s;
// creating packet header
char packet_header[7];
expstring_t tmp;
tmp = mprintf("%d|%s", severity, msg_stuffed);
create_packet_header(strlen(tmp), packet_header, 'E');
pipe_s = mprintf("%s%s", packet_header, tmp);
free(msg_stuffed);
write_pipe(pipe_s);
}
//----------------------------------------------------------------------------
// USERINTERFACE
void Jnimw::notify(const struct timeval* time, const char* source,
int severity, const char* msg)
{
char *source_stuffed = stuffer(source);
char *msg_stuffed = stuffer(msg);
expstring_t pipe_s;
// creating packet header
char packet_header[7];
expstring_t tmp;
tmp = mprintf("%ld|%ld|%s|%d|%s", time->tv_sec, time->tv_usec, source_stuffed, severity, msg_stuffed);
create_packet_header(strlen(tmp), packet_header, 'N');
pipe_s = mprintf("%s%s", packet_header, tmp);
write_pipe(pipe_s);
free(source_stuffed);
free(msg_stuffed);
Free(tmp);
Free(pipe_s);
}
void Jnimw::create_pipe()
{
if (pipe(pipe_fd)){
printf("Jnimw::create_pipes(): pipe system call failed.\n");
}
}
void Jnimw::destroy_pipe()
{
close(pipe_fd[0]);
pipe_fd[0] = -1;
close(pipe_fd[1]);
pipe_fd[1] = -1;
}
bool Jnimw::is_pipe_readable(){
// TODO maybe this could get faster
timeval time;
time.tv_sec = 0;
time.tv_usec = 0;
fd_set read_set;
FD_ZERO(&read_set);
FD_SET(pipe_fd[0], &read_set);
int ret = select(pipe_fd[0] + 1 , &read_set, NULL, NULL, &time);
return ret > 0;
}
char* Jnimw::read_pipe()
{
select(pipe_fd[0] + 1 , &readfds, NULL, NULL, NULL);
lock();
pipe_buffer = (char*)malloc(7);
int ret = read(pipe_fd[0], pipe_buffer, 6);
if(ret != 6){
printf("Malformed packet arrived!\n");
}
pipe_size-= ret;
if(pipe_buffer[0] == 'S'){
has_status_message_pending = false;
unlock();
return pipe_buffer;
}
int packet_size = (pipe_buffer[1]-48) * 10000 + (pipe_buffer[2]-48) * 1000 +
(pipe_buffer[3]-48) * 100 + (pipe_buffer[4]-48) * 10 + (pipe_buffer[5]-48);
pipe_buffer = (char*)realloc(pipe_buffer, packet_size + 7);
ret = read(pipe_fd[0],pipe_buffer + 6, packet_size);
if(ret != packet_size){
printf("Jnimw::read_pipe(): read system call failed\n");
}
pipe_buffer[packet_size + 6] = '\0';
pipe_size-=ret;
unlock();
return pipe_buffer;
}
void Jnimw::write_pipe(const char *buf)
{
if (write(pipe_fd[1], buf, strlen(buf)) < 0){
printf("Jnimw::write_pipe(): write system call failed\n");
}
pipe_size+=strlen(buf);
}
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2000-2014 Ericsson Telecom AB
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// which accompanies this distribution, and is available at
// http://www.eclipse.org/legal/epl-v10.html
///////////////////////////////////////////////////////////////////////////////
#ifndef JNIMW_JNIMW_H
#define JNIMW_JNIMW_H
//----------------------------------------------------------------------------
#include <stdio.h>
#include <pthread.h>
#include <jni.h>
#include "../core/Types.h"
#include "../mctr2/mctr/UserInterface.h"
#include "../mctr2/mctr/MainController.h"
#include "../common/memory.h"
#include "../mctr2/mctr/config_data.h"
//----------------------------------------------------------------------------
namespace jnimw {
//----------------------------------------------------------------------------
/**
* User interface jnimw implementation.
*/
class Jnimw : public mctr::UserInterface
{
public:
static Jnimw *userInterface;
int pipe_fd[2];
char *pipe_buffer;
fd_set readfds;
static bool has_status_message_pending;
static int pipe_size;
static pthread_mutex_t mutex;
/**
* Configuration data which is filled by calling set_cfg_file()
* Based on Cli::mycfg
*/
static config_data mycfg;
public:
/**
* Constructor, destructor.
*/
Jnimw();
~Jnimw();
virtual int enterLoop(int argc, char* argv[]);
/* Callback interface */
/**
* Status of MC has changed.
*/
virtual void status_change();
/**
* Error message from MC.
*/
virtual void error(int severity, const char* msg);
/**
* General notification from MC.
*/
virtual void notify(const struct timeval* time, const char* source,
int severity, const char* msg);
void create_pipe();
void destroy_pipe();
char* read_pipe();
void write_pipe(const char *buf);
bool is_pipe_readable();
static void lock();
static void unlock();
static void fatal_error(const char *fmt, ...)
__attribute__ ((__format__ (__printf__, 1, 2), __noreturn__));
};
//----------------------------------------------------------------------------
}
//----------------------------------------------------------------------------
#endif // JNIMW_JNIMW_H
This diff is collapsed.
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2000-2014 Ericsson Telecom AB
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// which accompanies this distribution, and is available at
// http://www.eclipse.org/legal/epl-v10.html
///////////////////////////////////////////////////////////////////////////////
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class org_eclipse_titan_executor_jni_JNIMiddleWare */
#ifndef _Included_org_eclipse_titan_executor_jni_JNIMiddleWare
#define _Included_org_eclipse_titan_executor_jni_JNIMiddleWare
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: org_eclipse_titan_executor_jni_JNIMiddleWare
* Method: init
* Signature: (I)J
*/
JNIEXPORT jlong JNICALL Java_org_eclipse_titan_executor_jni_JNIMiddleWare_init
(JNIEnv *, jobject, jint);
/*
* Class: org_eclipse_titan_executor_jni_JNIMiddleWare
* Method: terminate
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_org_eclipse_titan_executor_jni_JNIMiddleWare_terminate
(JNIEnv *, jobject);
/*
* Class: org_eclipse_titan_executor_jni_JNIMiddleWare
* Method: add_host
* Signature: (Ljava/lang/String;Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_org_eclipse_titan_executor_jni_JNIMiddleWare_add_1host
(JNIEnv *, jobject, jstring, jstring);
/*
* Class: org_eclipse_titan_executor_jni_JNIMiddleWare
* Method: assign_component
* Signature: (Ljava/lang/String;Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_org_eclipse_titan_executor_jni_JNIMiddleWare_assign_1component
(JNIEnv *, jobject, jstring, jstring);
/*
* Class: org_eclipse_titan_executor_jni_JNIMiddleWare
* Method: destroy_host_groups
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_org_eclipse_titan_executor_jni_JNIMiddleWare_destroy_1host_1groups
(JNIEnv *, jobject);
/*
* Class: org_eclipse_titan_executor_jni_JNIMiddleWare
* Method: set_kill_timer
* Signature: (D)V
*/
JNIEXPORT void JNICALL Java_org_eclipse_titan_executor_jni_JNIMiddleWare_set_1kill_1timer
(JNIEnv *, jobject, jdouble);
/*
* Class: org_eclipse_titan_executor_jni_JNIMiddleWare
* Method: start_session
* Signature: (Ljava/lang/String;IZ)I
*/
JNIEXPORT jint JNICALL Java_org_eclipse_titan_executor_jni_JNIMiddleWare_start_1session
(JNIEnv *, jobject, jstring, jint, jboolean);
/*
* Class: org_eclipse_titan_executor_jni_JNIMiddleWare
* Method: shutdown_session
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_org_eclipse_titan_executor_jni_JNIMiddleWare_shutdown_1session
(JNIEnv *, jobject);
/*
* Class: org_eclipse_titan_executor_jni_JNIMiddleWare
* Method: configure
* Signature: (Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_org_eclipse_titan_executor_jni_JNIMiddleWare_configure
(JNIEnv *, jobject, jstring);
/*
* Class: org_eclipse_titan_executor_jni_JNIMiddleWare
* Method: set_cfg_file
* Signature: (Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_org_eclipse_titan_executor_jni_JNIMiddleWare_set_1cfg_1file
(JNIEnv *, jobject, jstring);
/*
* Class: org_eclipse_titan_executor_jni_JNIMiddleWare
* Method: get_mc_host
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_org_eclipse_titan_executor_jni_JNIMiddleWare_get_1mc_1host
(JNIEnv *, jobject);
/*
* Class: org_eclipse_titan_executor_jni_JNIMiddleWare
* Method: get_port
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_org_eclipse_titan_executor_jni_JNIMiddleWare_get_1port
(JNIEnv *, jobject);
/*
* Class: org_eclipse_titan_executor_jni_JNIMiddleWare
* Method: create_mtc
* Signature: (I)V
*/
JNIEXPORT void JNICALL Java_org_eclipse_titan_executor_jni_JNIMiddleWare_create_1mtc
(JNIEnv *, jobject, jint);
/*
* Class: org_eclipse_titan_executor_jni_JNIMiddleWare
* Method: exit_mtc
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_org_eclipse_titan_executor_jni_JNIMiddleWare_exit_1mtc
(JNIEnv *, jobject);
/*
* Class: org_eclipse_titan_executor_jni_JNIMiddleWare
* Method: execute_control
* Signature: (Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_org_eclipse_titan_executor_jni_JNIMiddleWare_execute_1control
(JNIEnv *, jobject, jstring);
/*
* Class: org_eclipse_titan_executor_jni_JNIMiddleWare
* Method: execute_testcase
* Signature: (Ljava/lang/String;Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_org_eclipse_titan_executor_jni_JNIMiddleWare_execute_1testcase
(JNIEnv *, jobject, jstring, jstring);
/*
* Class: org_eclipse_titan_executor_jni_JNIMiddleWare
* Method: get_execute_cfg_len
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_org_eclipse_titan_executor_jni_JNIMiddleWare_get_1execute_1cfg_1len
(JNIEnv *, jobject);
/*
* Class: org_eclipse_titan_executor_jni_JNIMiddleWare
* Method: execute_cfg
* Signature: (I)V