File: jnimw.cc

package info (click to toggle)
eclipse-titan 7.2.0-1.1
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 103,144 kB
  • sloc: cpp: 264,784; ansic: 33,124; yacc: 23,073; makefile: 14,730; lex: 9,190; java: 4,849; perl: 3,783; sh: 2,298; xml: 1,378; javascript: 85; awk: 48; php: 32; python: 13
file content (291 lines) | stat: -rw-r--r-- 7,026 bytes parent folder | download
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
/******************************************************************************
 * Copyright (c) 2000-2020 Ericsson Telecom AB
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
 *
 * Contributors:
 *   Balasko, Jeno
 *   Lovassy, Arpad
 *
 ******************************************************************************/
#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);
}