Пример #1
0
Файл: som.c Проект: verzhak/fmll
fmll_som * fmll_som_load(const char * fname_prefix,
		double (* distance_w)(const double *, const double *, const unsigned), double (* distance)(const double *, const double *, const unsigned))
{
	char node_name[4096];
	int map_dim, dim;
	unsigned u, v, num, * N = NULL;
	double ** w;
	fmll_som * som = NULL;
	mxml_node_t * sub_sub_node, * sub_node, * node, * content_node, * main_node = NULL;

	fmll_try;

		fmll_throw_if(xml_load(fname_prefix, TYPE_SOM, & main_node, & content_node));

		fmll_throw_if(xml_get_int(content_node, "map_dim", & map_dim));
		fmll_throw_if(xml_get_int(content_node, "dim", & dim));

		fmll_throw_null(N = fmll_alloc(sizeof(unsigned), 1, map_dim));
		fmll_throw_null(node = mxmlFindElement(content_node, content_node, "N", NULL, NULL, MXML_DESCEND_FIRST));

		for(u = 0, sub_node = mxmlFindElement(node, node, NULL, NULL, NULL, MXML_DESCEND_FIRST); u < map_dim; u++)
		{
			fmll_throw_null(sub_node);
			N[u] = sub_node->child->value.integer;
			sub_node = mxmlFindElement(sub_node, node, NULL, NULL, NULL, MXML_DESCEND);
		}

		fmll_throw_null(som = fmll_som_init(N, map_dim, dim, distance_w, distance));
		fmll_throw_null(node = mxmlFindElement(content_node, content_node, "W", NULL, NULL, MXML_DESCEND_FIRST));

		w = som->w;
		num = som->num;

		for(u = 0; u < num; u++)
		{
			sprintf(node_name, "w_%u", u);
			fmll_throw_null(sub_node = mxmlFindElement(node, node, node_name, NULL, NULL, MXML_DESCEND_FIRST));

			for(v = 0, sub_sub_node = mxmlFindElement(sub_node, sub_node, NULL, NULL, NULL, MXML_DESCEND_FIRST); v < dim; v++)
			{
				w[u][v] = sub_sub_node->child->value.real;
				sub_sub_node = mxmlFindElement(sub_sub_node, sub_node, NULL, NULL, NULL, MXML_DESCEND);
			}
		}


	fmll_catch;

		fmll_som_destroy(som);
		som = NULL;

	fmll_finally;

		fmll_free(N);
		xml_destroy(main_node);

	return som;
}
Пример #2
0
/*
 * Poll all queues...
 * a thread, expected to be started from the TASKQ.  Note you must
 * re-register processors once this task exits.
 *
 * Note we expect sender_xml to have QueueInfo embedded!
 */
int qpoller_task (void *parm)
{
  int i,
      poll_interval,
      num_queues;
  QPOLLER *p;
  QPOLLERJOB *j;
  TASKQ *q;
  XML *xml = (XML *) parm;

  info ("Queue Poller starting\n");
  num_queues = xml_count (xml, QP_QUEUE);
  if ((poll_interval = xml_get_int (xml, QP_INFO".PollInterval")) < 1)
    poll_interval = 5;
  poll_interval *= 1000;
  if ((i = xml_get_int (xml, QP_INFO".MaxThreads")) < 1)
    i = 1;
  q = task_allocq (i, poll_interval);
  debug ("%d queues %d interval\n", num_queues, poll_interval);
  while (phineas_running ())
  {
    for (i = 0; i < num_queues; i++)
    {
      qpoller_poll (xml, i, q);
    }
    sleep (poll_interval);
  }
  debug ("Queue Poller shutting down...\n");
  task_stop (q);
  task_freeq (q);
  while ((j = QpollerJobs) != NULL)
  {
    QpollerJobs = j->next;
    free (j);
  }
  while ((p = Qpoller) != NULL)
  {
    Qpoller = p->next;
    free (p);
  }
  info ("Queue Poller exiting\n");
  return (0);
}
Пример #3
0
Файл: pca.c Проект: verzhak/fmll
fmll_pca * fmll_pca_load(const char * fname_prefix)
{
	int dim, num;
	char node_name[4096];
	unsigned u, v;
	double ** w;
	fmll_pca * pca = NULL;
	mxml_node_t * sub_sub_node, * sub_node, * node, * content_node, * main_node = NULL;

	fmll_try;

		fmll_throw_if(xml_load(fname_prefix, TYPE_PCA, & main_node, & content_node));

		fmll_throw_if(xml_get_int(content_node, "dim", & dim));
		fmll_throw_if(xml_get_int(content_node, "num", & num));

		fmll_throw_null(pca = fmll_pca_init(dim, num));
		fmll_throw_null(node = mxmlFindElement(content_node, content_node, "W", NULL, NULL, MXML_DESCEND_FIRST));

		for(u = 0, w = pca->w; u < num; u++)
		{
			sprintf(node_name, "w_%u", u);
			fmll_throw_null(sub_node = mxmlFindElement(node, node, node_name, NULL, NULL, MXML_DESCEND_FIRST));

			for(v = 0, sub_sub_node = mxmlFindElement(sub_node, sub_node, NULL, NULL, NULL, MXML_DESCEND_FIRST); v < dim; v++)
			{
				w[u][v] = sub_sub_node->child->value.real;
				sub_sub_node = mxmlFindElement(sub_sub_node, sub_node, NULL, NULL, NULL, MXML_DESCEND);
			}
		}

	fmll_catch;

		fmll_pca_destroy(pca);
		pca = NULL;

	fmll_finally;

		xml_destroy(main_node);

	return pca;
}
Пример #4
0
/*
 * A server thread (task)
 * assumes network is started and shutdown by parent thread
 */
server_task (XML *xml)
{
  SSL_CTX *ctx;
  NETCON *conn, *ssl;
  int port, threads, e;


  if ((threads = xml_get_int (xml, "Phineas.Server.NumThreads")) == 0)
    threads = 2;
  if (port = xml_get_int (xml, "Phineas.Server.Port"))
  {
    if ((conn = net_open ("ANY", port, threads, NULL)) == NULL)
    {
      return (phineas_fatal ("Failed to open port %d\n", port));
    }
  }
  if (port = xml_get_int (xml, "Phineas.Server.SSL.Port"))
  {
    ctx = server_ctx (xml);
    if ((ssl = net_open ("ANY", port, threads, ctx)) == NULL)
    {
      if (conn != NULL)
        net_close (conn);
      return (phineas_fatal ("Failed to open SSL port %d\n", port));
    }
    if (conn != NULL)
      threads *= 2;
  }
  e = server_listen (xml, conn, ssl, ctx, threads);
  if (conn != NULL)
    net_close (conn);
  if (ssl != NULL)
  {
    net_close (ssl);
    if (ctx != NULL)
      SSL_CTX_free (ctx);
  }
  if (e)
    phineas_fatal ("Failed to start PHINEAS server");
  return (0);
}
Пример #5
0
fmll_svm_net * fmll_svm_net_load(const char * fname_prefix, double (** K)(const double *, const double *, const unsigned))
{
	int num;
	unsigned u;
	char node_name[4096];
	fmll_svm ** svm = NULL;
	fmll_svm_net * svm_net = NULL;
	mxml_node_t * sub_node, * node, * content_node, * main_node = NULL;

	fmll_try;

		fmll_throw_null(svm_net = fmll_alloc(sizeof(fmll_svm_net), 1, 1));
		fmll_throw_if(xml_load(fname_prefix, TYPE_SVM_NET, & main_node, & content_node));
		fmll_throw_if(xml_get_int(content_node, "num", & num));

		svm_net->num = num;

		fmll_throw_null(svm = svm_net->svm = (fmll_svm **) fmll_alloc(sizeof(fmll_svm *), 1, num));
		fmll_throw_null(node = mxmlFindElement(content_node, content_node, "SVM", NULL, NULL, MXML_DESCEND_FIRST));

		for(u = 0; u < num; u++)
			svm[u] = NULL;

		for(u = 0; u < num; u++)
		{
			sprintf(node_name, "svm_%u", u);
			fmll_throw_null(sub_node = mxmlFindElement(node, node, node_name, NULL, NULL, MXML_DESCEND_FIRST));
			fmll_throw_null(svm[u] = fmll_svm_load_main(sub_node, K[u]));
		}
	
	fmll_catch;

		fmll_svm_net_destroy(svm_net);
		svm_net = NULL;

	fmll_finally;

		xml_destroy(main_node);

	return svm_net;
}
Пример #6
0
static gboolean dct3trace_get_packet(FILE_T fh, union wtap_pseudo_header *pseudo_header,
	unsigned char *buf, int *len, int *err, gchar **err_info)
{
	unsigned char line[1024];
	gboolean have_data = FALSE;

	while (file_gets(line, sizeof(line), fh) != NULL)
	{
		if( memcmp(dct3trace_magic_end, line, strlen(dct3trace_magic_end)) == 0 )
		{
			/* Return on end of file </dump> */
			*err = 0;
			return FALSE;
		}
		else if( memcmp(dct3trace_magic_record_end, line, strlen(dct3trace_magic_record_end)) == 0 )
		{
			/* Return on end of record </l1> */
			if( have_data )
			{
				*err = 0;
				return TRUE;
			}
			else
			{
				/* If not got any data return error */
				*err = WTAP_ERR_BAD_FILE;
				*err_info = g_strdup_printf("dct3trace: record without data");
				return FALSE;
			}
		}
		else if( memcmp(dct3trace_magic_record_start, line, strlen(dct3trace_magic_record_start)) == 0 )
		{
			/* Parse L1 header <l1 ...>*/
			int channel, tmp;
			char *ptr;

			pseudo_header->gsm_um.uplink = !strstr(line, "direction=\"down\"");
			if (xml_get_int(&channel, line, "logicalchannel") != 0)
				goto baddata;

			/* Parse downlink only fields */
			if( !pseudo_header->gsm_um.uplink )
			{
				if (xml_get_int(&tmp, line, "physicalchannel") != 0)
					goto baddata;
				pseudo_header->gsm_um.arfcn = tmp;
				if (xml_get_int(&tmp, line, "sequence") != 0)
					goto baddata;
				pseudo_header->gsm_um.tdma_frame = tmp;
				if (xml_get_int(&tmp, line, "bsic") != 0)
					goto baddata;
				pseudo_header->gsm_um.bsic = tmp;
				if (xml_get_int(&tmp, line, "error") != 0)
					goto baddata;
				pseudo_header->gsm_um.error = tmp;
				if (xml_get_int(&tmp, line, "timeshift") != 0)
					goto baddata;
				pseudo_header->gsm_um.timeshift = tmp;
			}

			switch( channel )
			{
				case 128: pseudo_header->gsm_um.channel = GSM_UM_CHANNEL_SDCCH; break;
				case 112: pseudo_header->gsm_um.channel = GSM_UM_CHANNEL_SACCH; break;
				case 176: pseudo_header->gsm_um.channel = GSM_UM_CHANNEL_FACCH; break;
				case 96: pseudo_header->gsm_um.channel = GSM_UM_CHANNEL_CCCH; break;
				case 80: pseudo_header->gsm_um.channel = GSM_UM_CHANNEL_BCCH; break;
				default: pseudo_header->gsm_um.channel = GSM_UM_CHANNEL_UNKNOWN; break;
			}

			/* Read data (if have it) into buf */
			ptr = strstr(line, "data=\"");
			if( ptr )
			{
				have_data = TRUE; /* If has data... */
				*len = hex2bin(buf, ptr+6);
			}
		}
		else if( !have_data && memcmp(dct3trace_magic_l2_start, line, strlen(dct3trace_magic_l2_start)) == 0 )
		{
			/* For uplink packets we might not get the raw L1, so have to recreate it from the L2 */
			/* Parse L2 header if didn't get data from L1 <l2 ...> */
			int data_len = 0;
			char *ptr = strstr(line, "data=\"");

			if( !ptr )
			{
				continue;
			}

			have_data = TRUE;

			if( pseudo_header->gsm_um.channel == GSM_UM_CHANNEL_SACCH || pseudo_header->gsm_um.channel == GSM_UM_CHANNEL_FACCH || pseudo_header->gsm_um.channel == GSM_UM_CHANNEL_SDCCH )
			{
				/* Add LAPDm B header */
				memset(buf, 0x1, 2);
				*len = 3;
			}
			else
			{
				/* Add LAPDm Bbis header */
				*len = 1;
			}
			buf += *len;

			data_len = hex2bin(buf, ptr+6);
			*len += data_len;

			/* Add LAPDm length byte */
			*(buf - 1) = data_len << 2 | 0x1;
		}
	}

	*err = file_error(fh, err_info);
	if (*err == 0)
	{
		*err = WTAP_ERR_SHORT_READ;
	}
	return FALSE;

baddata:
	*err = WTAP_ERR_BAD_FILE;
	*err_info = g_strdup_printf("dct3trace: record missing mandatory attributes");
	return FALSE;
}
Пример #7
0
static gboolean dct3trace_get_packet(FILE_T fh, struct wtap_pkthdr *phdr,
	Buffer *buf, int *err, gchar **err_info)
{
	char line[1024];
	guint8 databuf[MAX_PACKET_LEN], *bufp;
	gboolean have_data = FALSE;
	int len = 0;

	bufp = &databuf[0];
	while (file_gets(line, sizeof(line), fh) != NULL)
	{
		if( memcmp(dct3trace_magic_end, line, strlen(dct3trace_magic_end)) == 0 )
		{
			/* Return on end of file </dump> */
			*err = 0;
			return FALSE;
		}
		else if( memcmp(dct3trace_magic_record_end, line, strlen(dct3trace_magic_record_end)) == 0 )
		{
			/* Return on end of record </l1> */
			if( have_data )
			{
				/* We've got a full packet! */
				phdr->presence_flags = 0; /* no time stamp, no separate "on the wire" length */
				phdr->ts.secs = 0;
				phdr->ts.nsecs = 0;
				phdr->caplen = len;
				phdr->len = len;

				*err = 0;

				/* Make sure we have enough room for the packet */
				buffer_assure_space(buf, phdr->caplen);
				memcpy( buffer_start_ptr(buf), databuf, phdr->caplen );

				return TRUE;
			}
			else
			{
				/* If not got any data return error */
				*err = WTAP_ERR_BAD_FILE;
				*err_info = g_strdup_printf("dct3trace: record without data");
				return FALSE;
			}
		}
		else if( memcmp(dct3trace_magic_record_start, line, strlen(dct3trace_magic_record_start)) == 0 )
		{
			/* Parse L1 header <l1 ...>*/
			int channel, tmp;
			char *ptr;

			phdr->pseudo_header.gsm_um.uplink = !strstr(line, "direction=\"down\"");
			if (xml_get_int(&channel, line, "logicalchannel") != 0)
				goto baddata;

			/* Parse downlink only fields */
			if( !phdr->pseudo_header.gsm_um.uplink )
			{
				if (xml_get_int(&tmp, line, "physicalchannel") != 0)
					goto baddata;
				phdr->pseudo_header.gsm_um.arfcn = tmp;
				if (xml_get_int(&tmp, line, "sequence") != 0)
					goto baddata;
				phdr->pseudo_header.gsm_um.tdma_frame = tmp;
				if (xml_get_int(&tmp, line, "bsic") != 0)
					goto baddata;
				phdr->pseudo_header.gsm_um.bsic = tmp;
				if (xml_get_int(&tmp, line, "error") != 0)
					goto baddata;
				phdr->pseudo_header.gsm_um.error = tmp;
				if (xml_get_int(&tmp, line, "timeshift") != 0)
					goto baddata;
				phdr->pseudo_header.gsm_um.timeshift = tmp;
			}

			switch( channel )
			{
				case 128: phdr->pseudo_header.gsm_um.channel = GSM_UM_CHANNEL_SDCCH; break;
				case 112: phdr->pseudo_header.gsm_um.channel = GSM_UM_CHANNEL_SACCH; break;
				case 176: phdr->pseudo_header.gsm_um.channel = GSM_UM_CHANNEL_FACCH; break;
				case 96: phdr->pseudo_header.gsm_um.channel = GSM_UM_CHANNEL_CCCH; break;
				case 80: phdr->pseudo_header.gsm_um.channel = GSM_UM_CHANNEL_BCCH; break;
				default: phdr->pseudo_header.gsm_um.channel = GSM_UM_CHANNEL_UNKNOWN; break;
			}

			/* Read data (if have it) into databuf */
			ptr = strstr(line, "data=\"");
			if( ptr )
			{
				have_data = TRUE; /* If has data... */
				len = hex2bin(bufp, &databuf[MAX_PACKET_LEN], ptr+6);
				if (len == -1)
				{
					*err = WTAP_ERR_BAD_FILE;
					*err_info = g_strdup_printf("dct3trace: record length %d too long", phdr->caplen);
					return FALSE;
				}
			}
		}
		else if( !have_data && memcmp(dct3trace_magic_l2_start, line, strlen(dct3trace_magic_l2_start)) == 0 )
		{
			/* For uplink packets we might not get the raw L1, so have to recreate it from the L2 */
			/* Parse L2 header if didn't get data from L1 <l2 ...> */
			int data_len;
			char *ptr = strstr(line, "data=\"");

			if( !ptr )
			{
				continue;
			}

			have_data = TRUE;

			/*
			 * We know we have no data already, so we know
			 * we have enough room for the header.
			 */
			if( phdr->pseudo_header.gsm_um.channel == GSM_UM_CHANNEL_SACCH || phdr->pseudo_header.gsm_um.channel == GSM_UM_CHANNEL_FACCH || phdr->pseudo_header.gsm_um.channel == GSM_UM_CHANNEL_SDCCH )
			{
				/* Add LAPDm B header */
				memset(bufp, 0x1, 2);
				len = 3;
			}
			else
			{
				/* Add LAPDm Bbis header */
				len = 1;
			}
			bufp += len;

			data_len = hex2bin(bufp, &databuf[MAX_PACKET_LEN], ptr+6);
			if (data_len == -1)
			{
				*err = WTAP_ERR_BAD_FILE;
				*err_info = g_strdup_printf("dct3trace: record length %d too long", phdr->caplen);
				return FALSE;
			}
			len += data_len;

			/* Add LAPDm length byte */
			*(bufp - 1) = data_len << 2 | 0x1;
		}
	}

	*err = file_error(fh, err_info);
	if (*err == 0)
	{
		*err = WTAP_ERR_SHORT_READ;
	}
	return FALSE;

baddata:
	*err = WTAP_ERR_BAD_FILE;
	*err_info = g_strdup_printf("dct3trace: record missing mandatory attributes");
	return FALSE;
}