void TrexRpcServerInterface::stop() {
    m_is_running = false;

    verbose_msg("Attempting To Stop RPC Server");

    /* call the dynamic type class stop */
    _stop_rpc_thread();
    
    /* hold until thread has joined */    
    m_thread->join();

    verbose_msg("Server Stopped");

    delete m_thread;
}
int get_datetime_val(const char *str, __u64 *tgt)
{
	struct tm t, t_old;
	char *ret;

	// strptime only sets
	memset(&t, 0, sizeof(struct tm));
	ret = strptime(str, "%Y-%m-%d %H:%M", &t);
	if (ret == NULL || *ret != '\0') {
		ret = strptime(str, "%Y-%m-%d %H:%M:%S", &t);
		if (ret == NULL || *ret != '\0') {
			fprintf(stderr, "%s: Could not parse date %s."
				" Please use format as specified in"
				" man-page\n", toolname, str);
			return -1;
		}
	}
	t_old = t;
	*tgt = mktime(&t);
	// if daylight savings time applies, 't' has been adjusted,
	// so we have to correct
	if (t_old.tm_hour != t.tm_hour)
		*tgt -= secs_since_1970(&t) - secs_since_1970(&t_old);
	verbose_msg("datetime value from user after translation: %s", ctime((const time_t *)tgt));

	return 0;
}
/**
 * starts a RPC specific server
 * 
 * @author imarom (17-Aug-15)
 */
void TrexRpcServerInterface::start() {
    m_is_running = true;

    verbose_msg("Starting RPC Server");

    /* prepare for run */
    _prepare();

    m_thread = new std::thread(&TrexRpcServerInterface::_rpc_thread_cb, this);
    if (!m_thread) {
        throw TrexRpcException("unable to create RPC thread");
    }
}
Ejemplo n.º 4
0
/**
 * Az NMEA-üzenetsztringeket értelmezõ függvény
 * @param s Az üzenetet tartalmazó karakterlánc
 * @param c Az aktuális visszahívási függvényeket tartalmazó struktúra
 */
static void nmea_parse_sentence(char *s, NMEA_CALLBACK c)
{
// Cancel if the callback doesn't want the sentence
	if(!(
		(nmea_get_talker_type(s) & c.talker_type_flags) &&
		(nmea_get_sentence_type(s) & c.sentence_type_flags)
	)) return;

	verbose_msg("%s", s);

// Do the callback
	c.mc
	(
		nmea_get_talker_type(s),
		nmea_get_sentence_type(s),
		nmea_get_sentence_args(s)
	);	
}
int print_report(FILE *fp, __u64 begin, __u64 end, __u32 interval,
				char *filename, __u64 topline,
				list<MsgTypes> *filter_types,
				DeviceFilter &dev_filter, Collapser &col,
				Printer &printer)
{
	int frames_printed = 0;
	bool first_time = true;
	time_t t;
	int rc = 0;
	Frameset frameset(&col);
	Framer framer(begin, end, interval,
		      filter_types, &dev_filter,
		      filename, &rc);

	if (rc)
		return -1;

	if (topline && printer.print_csv()) {
		fprintf(stderr, "%s: Warning: Cannot use '-t' with CSV mode,"
			" ignoring\n", toolname);
		topline = 0;
	}

	verbose_msg("print report for:\n");
	t = (time_t)begin;
	verbose_msg("    begin    : %s", ctime(&t));
	t = (time_t)end;
	verbose_msg("    end      : %s", (end == UINT64_MAX ? "-\n" : ctime(&t)));
	verbose_msg("    interval : %lu\n", (long unsigned int)interval);
	verbose_msg("    topline  : %llu\n", (long long unsigned int)topline);
	verbose_msg("    csv mode : %d\n", printer.print_csv());

	while ( (rc = framer.get_next_frameset(frameset, true)) == 0 ) {
		vverbose_msg("printing frameset %d\n", frames_printed);
		if (first_time || (topline && frames_printed % topline == 0)) {
			first_time = false;
			printer.print_topline(fp);
		}
		if (printer.print_frame(fp, frameset, dev_filter) < 0)
			return -1;
		++frames_printed;
	}

	if (rc > 0)
		return frames_printed;

	return rc;
}
int open_data_files(FILE **fp, const char *filename, struct file_header *f_hdr,
	      struct aggr_data **agg)
{
	struct message_preview msg_prev;
	struct message msg;
	int rc = 0;
	int i;
	__u64 end_of_agg;
	time_t t;

#ifndef NDEBUG
	assert(open_count == 0);
	open_count++;
#endif

	verbose_msg("open data\n");

	/*
	 * Open .agg file if exists
	 */

	*agg = (struct aggr_data*)malloc(sizeof(struct aggr_data));
	if ( (rc = open_agg_file(fp, filename, *agg)) < 0 ) {
		free(*agg);
		return -1;
	}
	if (rc == 0) {
		verbose_msg("  found .agg file\n");
		close_agg_file(*fp);
	}
	else {
		verbose_msg("  no .agg file found\n");
		free(*agg);
		*agg = NULL;
	}

	/*
	 * Open .log file
	 */
	if ( (rc = open_log_file(fp, filename, f_hdr)) )
		return -1;

	/*
	 * Eventually add messages from final frame of .agg file and adjust
	 * respective boundaries
	 */

	if (*agg) {
		conv_aggr_data_msg_data_from_BE(*agg);

		/* We use the first message that we have as the basis to
		   calculate when the final timeframe of the .agg data
		   would have ended. Note that we always add all messages
		   that are interval/2 after that timestamp! */
		end_of_agg = ((*agg)->end_time - (*agg)->begin_time -
			f_hdr->interval_length / 2) % f_hdr->interval_length;
		if (end_of_agg)
			end_of_agg = (*agg)->end_time
				+ (f_hdr->interval_length - end_of_agg);

		i = 0;
		while ( (rc = get_next_msg_preview(*fp, &msg_prev, f_hdr)) == 0
			 && msg_prev.timestamp <= end_of_agg) {
			if (get_complete_msg(*fp, &msg_prev, &msg))
			    return -1;
			rc = add_to_agg(*agg, &msg, f_hdr);
			discard_msg(&msg);
			if (rc)
				return -1;
			++i;
		}
		if (rc < 0) {
			fprintf(stderr, "%s: Could not read"
				" any messages in %s%s\n", toolname, filename,
				DACC_FILE_EXT_LOG);
			return -1;
		}
		// condition of the check impossible to fail, but still...
		if (msg_prev.timestamp > end_of_agg)
			rewind_to(*fp, &msg_prev);

		// finally, adjust boundaries
		(*agg)->end_time = end_of_agg - f_hdr->interval_length / 2;
		f_hdr->begin_time = (*agg)->end_time + f_hdr->interval_length;
		if (verbose > 0) {
			t = (*agg)->end_time;
			verbose_msg("  adjust agg end time to  : %s",
				    ctime(&t));
			t = (*agg)->begin_time;
			verbose_msg("  adjust log begin time to: %s",
				    ctime(&t));
		}

		conv_aggr_data_msg_data_to_BE(*agg);
		verbose_msg("  added %d messages to aggregated structure\n",
			    i);
	}

	verbose_msg("open data finished\n");

	return 0;
}
/**
 * Read all essential data from the files,
 * including the headers, timestamp of first message
 * and a DeviceFilter for all available devices.
 * Note that 'agg' is NULL if not available and must
 * be free()'d otherwise. */
static int get_initial_data(const char *filename, struct file_header *f_hdr,
			    struct aggr_data **agg,
			    DeviceFilter &dev_filt, ConfigReader &cfg)
{
	FILE *fp;
	int rc = 0;
	__u64 begin;

	if (open_data_files(&fp, filename, f_hdr, agg))
		return -1;
	close_data_files(fp);

	/*
	 * Retrieve first real frame
	 */
	MsgTypeFilter msgtype_filter;
	NoopCollapser	nop_col;
	Frameset frameset(&nop_col);
	list<MsgTypes> type_flt;
	type_flt.push_back(ioerr);

	// we retrieve the first interval only, and eventually the .agg data
	// as well
	if (*agg)
		begin = (*agg)->end_time;
	else
		begin = f_hdr->begin_time;

	Framer framer(begin,
		      begin + f_hdr->interval_length,
		      f_hdr->interval_length, &type_flt,
		      (DeviceFilter*)NULL, filename, &rc);
	vector <struct ioerr_cnt*> ioerrs;
	do {
		if ( framer.get_next_frameset(frameset) != 0 ) {
			fprintf(stderr, "%s: Could not read"
				" any frames in %s%s\n", toolname, filename,
				DACC_FILE_EXT_LOG);
			return -2;
		}

		/*
		 * Construct a DeviceFilter with all devices.
		 * NOTE: The very first ioerr msg might already have been moved to the .agg
		 * file - hence we have to consider the .agg data as well!
		 */
		ioerrs = frameset.get_ioerr_stats();
		rc = 0;
		for (vector<struct ioerr_cnt*>::const_iterator i = ioerrs.begin();
		      i != ioerrs.end(); ++i) {
			vverbose_msg("    add device: hctl=[%d:%d:%d:%d], mm=%d\n",
				    (*i)->identifier.host, (*i)->identifier.channel,
				    (*i)->identifier.target, (*i)->identifier.lun,
				    cfg.get_mm_by_ident(&(*i)->identifier, &rc));
			dev_filt.add_device(cfg.get_mm_by_ident(&(*i)->identifier, &rc), &(*i)->identifier);
			if (rc)
				return -1;
		}
	} while ( frameset.is_aggregated() && !ioerrs.size());

	if (dev_filt.get_host_id_list().size() == 0 || dev_filt.get_mm_list().size() == 0) {
		fprintf(stderr, "%s: Could not retrieve initial data"
			" - data files corrupted or broken, or the .agg file is missing.\n",
			toolname);
		return -1;
	}

	verbose_msg("retrieve initial data FINISHED\n");

	return 0;
}
/**
 * The times will be adjusted to respective exakt frame boundaries.
 * The end time is only ever used to determine whether we are done - we stop
 * in case the begin of the timeframe is later than the end date.
 * Note that in case we scratch into the .agg data, we
 * (a) take all of it
 * (b) synonymously associate the .agg data with the timestamp of its
 *     final frame. This is _utterly_ important, especially in context
 *     with the interval: If it is user adjusted, and the date range touches
 *     the .agg data, we adjust the begin to the .agg data's final frame's
 *     timestamp, and make sure that when we process the agg data, the next
 *     frame is 1 unit of the original interval length away. Only after we
 *     have shifted into the 'regular' .log data can we apply any user-set
 *     interval length.
 * */
int adjust_timeframe(const char *filename, __u64 *begin, __u64 *end,
		     __u32 *interval)
{
	struct file_header f_hdr;
	struct aggr_data *agg = NULL;
	FILE *fp;
	time_t t;
	int rc = 0;

	verbose_msg("adjust timeframe:\n");

	// check begin and end time
	if (*begin > *end) {
		fprintf(stderr, "%s: Start time must be"
			" prior to end time.\n", toolname);
		rc = -1;
		goto out;
	}

	if (open_data_files(&fp, filename, &f_hdr, &agg)) {
		rc = -1;
		goto out;
	}
	close_data_files(fp);

	// check if begin scratches into .agg data
	// if so, we use the _end_ time of the .agg frame
	if (*begin == 0 && agg) {
		verbose_msg("    begin time: take from .agg data\n");
		*begin = agg->end_time;
	}
	else if (agg && *begin < agg->begin_time) {
		fprintf(stderr, "%s: Warning: Begin of timeframe is before"
			" earliest available data, which is %s.\n", toolname,
			print_time_formatted(agg->begin_time));
		*begin = agg->end_time;
		verbose_msg("    begin time: adjust to begin from .agg data\n");
	}
	else if (agg && *begin <= agg->end_time) {
		*begin = agg->end_time;
		verbose_msg("    begin time: set to end of .agg data\n");
	}
	else if (*begin < f_hdr.begin_time) {
		*begin = f_hdr.begin_time;
		verbose_msg("    begin time: adjust to begin of .log data\n");
	}
	else if (*begin > f_hdr.end_time) {
		fprintf(stderr, "%s: Begin of timeframe is past the"
			" end of available data, which is %s.\n",
			toolname, print_time_formatted(f_hdr.end_time));
		rc = -1;
		goto out;
	}
	else {
		verbose_msg("    begin time: round to nearest boundary\n");
		*begin = round_lower_boundary(*begin, f_hdr.begin_time, f_hdr.interval_length);
	}
	t = *begin;
	verbose_msg("    begin time set to: %s", ctime(&t));

	if (*end == UINT64_MAX) {
		*end = f_hdr.end_time;
		verbose_msg("    end time  : take from .log data\n");
	}
	else if (agg && *end < agg->begin_time) {
		fprintf(stderr, "%s: End of timeframe is prior to earliest"
			" available data, which is %s.\n", toolname,
			print_time_formatted(agg->begin_time));
		rc = -1;
		goto out;
	}
	else if (agg && *end < agg->end_time) {
		*end = agg->end_time;
		verbose_msg("    end time  : take from .agg data\n");
	}
	else if (!agg && *end < f_hdr.begin_time) {
		fprintf(stderr, "%s: End of timeframe is prior to earliest"
			" available data, which is %s.\n", toolname,
			print_time_formatted(f_hdr.begin_time));
		rc = -1;
		goto out;
	}
	else if (*end > f_hdr.end_time) {
		fprintf(stderr, "%s: Warning: End of timeframe is after"
			" latest available data, which is %s.\n", toolname,
			print_time_formatted(f_hdr.end_time));
		*end = f_hdr.end_time;
		verbose_msg("    end time  : adjust to end of .log data\n");
	}
	else {
		*end = round_upper_boundary(*end, f_hdr.begin_time,
					       f_hdr.interval_length);
		verbose_msg("    end time  : round to nearest boundary\n");
	}
	t = *end;
	verbose_msg("    end time set to  : %s", ctime(&t));
	assert((*end - *begin) % f_hdr.interval_length == 0);

	if (*interval == UINT32_MAX) {
		*interval = f_hdr.interval_length;
		verbose_msg("using original interval length: %lus\n",
			    (long unsigned int)*interval);
	}
	/* the exact frame boundaries don't include the length of the very
	   first interval, so we have to add one more to our calculations */
	if (*interval && (*end - *begin + f_hdr.interval_length) % *interval != 0) {
		// cut off rest in case of user-set interval
		*end -= (*end - *begin) % *interval + f_hdr.interval_length;
		t = *end;
		verbose_msg("    cut off at : %s", ctime(&t));
	}

	// check if the interval is correct
	if (*interval % f_hdr.interval_length) {
		fprintf(stderr, "%s: Data aggregation interval %lu"
			" is incompatible with source data. Please use"
			" a multiple of %lu and try again.\n", toolname,
			(long unsigned int)*interval,
			(long unsigned int)(f_hdr.interval_length));
		rc = -1;
		goto out;
	}

out:
	if (agg) {
		discard_aggr_data_struct(agg);
		free(agg);
	}

	return rc;
}
Ejemplo n.º 9
0
/**
 * A socket-bõl érkezõ NMEA-üzenetek értelmezéséért felelõs függvény
 */
void nmea_parse()
{
	char rxbuf[129];
	char sentence[84]; /* 80 + "$" + <CR><LF> + '\0' */
	char *sp = sentence;
	char *t, *rp;
	
	fd_set fds;
	struct timeval tv;
	
	FD_ZERO(&fds);
	FD_SET(o.sockfd, &fds);
	time_t endrun = time(NULL) + o.runtime;

	verbose_msg("%d", o.runtime);

	verbose_msg("nmea_parse_socket() - runtime: %lu secs", o.runtime);

	while(time(NULL) < endrun)
	{
		tv.tv_sec = 5;
		tv.tv_usec = 0;
		
		select(o.sockfd + 1, &fds, NULL, NULL, &tv);
		
		if(FD_ISSET(o.sockfd, &fds))
		{		
			int status = recv(o.sockfd, rxbuf, 128, 0);
			
			if(status == -1)
			{
				verbose_msg("Sikertelen olvasás.");
			}
			else if(status == 0)
			{
				die("A kiszolgáló megszakította a kapcsolatot.");
			}
			else
			{
				rxbuf[status] = '\0';
				rp = rxbuf;
				
				/* Loop through end-of-sentence markers and parse sentences */
				while(NULL != (t = strstr(rp, "\r\n")))
				{
					/* Copy (portion of) the sentence into the temp. storage */
					memcpy(sp, rp, t - rp);
					sp[t - rp] = '\0';
					
					/* Parse the sentence and reset the pointer */
					if(*sentence == '$')
						nmea_parse_sentence
							(sentence, callbacks[o.display_mode]);
					memset(sentence, 0, 84);
					sp = sentence;
					
					rp = t + 2;
				}
				
				/* Store what remains */
				
				if(strlen(rp) > 0)
				{
					memcpy(sp, rp, strlen(rp));
					sp += strlen(rp);
				}
			}
		}
	}
}
Ejemplo n.º 10
0
int compare(const char *filepath)
{
    char *subpath = pathtrim(filepath);
    debug_msg("Comparing File: %s", filepath);
    file_count++;
    // ignore if no read permission
    if (check_privilege(filepath))
    {
        verbose_msg("Ignore file: %s due to privilege limitation: %s", subpath, strerror(errno));
        return -1;
    }

    struct stat buffer;
    int status;
    status = lstat(filepath, &buffer);
    if (status != 0)
    {
        verbose_msg("Fetch file status failed: $s: %s, Error code: %d", subpath, strerror(errno), errno);
        return -1;
    }

    // find the linked list with the same file size and the same file type
    // st_mode & S_IFMT extract the file type code from a mode value.
    list *ls_search = list_new(buffer.st_size, (unsigned int)buffer.st_mode & S_IFMT);
    debug_msg("Size: %d, type: %u,  status=%d", (int)ls_search->filesize, ls_search->filetype, status);
    // store the file info into the node
    list_node *newfile = node_new(filepath, buffer.st_size);
    // reference man page: http://man7.org/linux/man-pages/man3/tsearch.3.html
    // key points to the item to be searched for. rootp points to a variable which points to the root of the tree.
    list **lsp = (list **)tsearch((void *)ls_search, &tree_root, list_compare);
    // returns a pointer to the newly added item.
    list_node *finded_same; //0 true 1 false
    if (lsp == NULL)
    {
        debug_msg("Append child failed");
        exit(1);
    }
    else
    {
        // Find the same file
        list *rls = *lsp;
        debug_msg("Listinfo Size: %d, type: %d", rls->filesize, rls->filetype);
        if (rls != ls_search)
        {
            // list exists
            debug_msg("A list already exists! Search the item.");
            // wheather the same file is in the list
            finded_same = is_samefile_inlist(rls, newfile);

            // add the item to the existing list
            if (finded_same != NULL)
            {
                debug_msg("Same file found!");
                node_free(newfile);
                char *tmp_subpath = pathtrim(finded_same->filepath);
                printf("%s\t%s\n", tmp_subpath, subpath);
                num_du_files++;
                free(tmp_subpath);
                free(subpath);
            }
            else
            {
                list_additem(rls, newfile);
            }
            list_free(ls_search);
        }
        else
        {
            // list not exist
            list_additem(rls, newfile);
            debug_msg("Created a new list!");
        }
    }
    memLimitCheck();
    return 0;
}
void TrexRpcServerInterface::verbose_json(const std::string &msg, const std::string &json_str) {
    verbose_msg(msg + "\n\n" + TrexJsonRpcV2Parser::pretty_json_str(json_str));
}