pyclustering_package * silhouette_ksearch_algorithm(
    const pyclustering_package * const p_sample,
    const std::size_t p_kmin,
    const std::size_t p_kmax,
    const std::size_t p_algorithm)
{
    dataset data;
    p_sample->extract(data);

    auto allocator = get_silhouette_ksearch_allocator(static_cast<silhouette_ksearch_type>(p_algorithm));

    ccore::clst::silhouette_ksearch_data result;
    ccore::clst::silhouette_ksearch(p_kmin, p_kmax, allocator).process(data, result);

    pyclustering_package * package = new pyclustering_package(pyclustering_data_t::PYCLUSTERING_TYPE_LIST);
    package->size = SILHOUETTE_KSEARCH_PACKAGE_SIZE;
    package->data = new pyclustering_package * [SILHOUETTE_KSEARCH_PACKAGE_SIZE];

    std::vector<std::size_t> amount_cluters = { result.get_amount() };
    std::vector<double> score = { result.get_score() };

    ((pyclustering_package **) package->data)[SILHOUETTE_KSEARCH_PACKAGE_AMOUNT]  = create_package(&amount_cluters);
    ((pyclustering_package **) package->data)[SILHOUETTE_KSEARCH_PACKAGE_SCORE]   = create_package(&score);
    ((pyclustering_package **) package->data)[SILHOUETTE_KSEARCH_PACKAGE_SCORES]  = create_package(&result.scores());

    return package;
}
Пример #2
0
void discover_package_replier(IoT_Package *package_info, IoT_Command *cmd)
{
	const int max_device_list_buf = 4096;
	int data_len = 0,n=0;
	char allDeviceList[max_device_list_buf] = { '\0' };
	char send_buf[max_device_list_buf] = { '\0' };

	if (strcmp(cmd->ID, "Dis_All") == 0)
	{
		puts("Discover all device\n");
		data_len = encodeAllDevices_except_one(allDeviceList, package_info->sor_ip.ip);
	}
	else if (strcmp(cmd->ID, "Dis_Pxd")==0)
	{
		puts("Discover Proxied device\n");
		data_len = encodePxdDevices_except_one(allDeviceList, package_info->sor_ip.ip,1);
	}
	else if (strcmp(cmd->ID, "Dis_NPx")==0)
	{
		puts("Discover Non-Proxied device\n");
		data_len = encodePxdDevices_except_one(allDeviceList, package_info->sor_ip.ip, 0);
	}

	//int data_len=encodeAllDevices(allDeviceList);
	IoT_Package new_package = generate_iot_package();
	create_package(&new_package, ServerIP, package_info->sor_ip.ip, allDeviceList, data_len);
	n = encode_package(send_buf, &new_package);

	send(client_socket[package_info->belongSocketIdx], send_buf, n, 0);
	printf("Device info Data:%d sended\n", n);
	free_package(&new_package);
}
Пример #3
0
static void template_pack_unpack(const Container & container) {
    pyclustering_package * package = create_package(&container);

    Container unpack_container;
    package->extract(unpack_container);

    ASSERT_EQ(container, unpack_container);

    delete package;
}
Пример #4
0
pyclustering_package * rock_algorithm(const pyclustering_package * const p_sample, const double p_radius, const size_t p_number_clusters, const double p_threshold) {
    dataset input_dataset;
    p_sample->extract(input_dataset);

    ccore::clst::rock solver(p_radius, p_number_clusters, p_threshold);

    ccore::clst::rock_data output_result;
    solver.process(input_dataset, output_result);

    pyclustering_package * package = create_package(&output_result.clusters());
    return package;
}
Пример #5
0
static void template_pyclustering_package(const TypeContainer & container) {
    using container_data_t = typename TypeContainer::value_type;
    pyclustering_package * package = create_package(&container);

    ASSERT_EQ(container.size(), package->size);

    if (package->type != PYCLUSTERING_TYPE_LIST) {
        for (std::size_t index = 0; index < container.size(); index++) {
            ASSERT_EQ(container[index], package->at<container_data_t>(index));
        }
    }

    delete package;
}
Пример #6
0
ast_t* package_load(ast_t* from, const char* path, pass_opt_t* options)
{
  const char* magic = find_magic_package(path);
  const char* name = path;

  if(magic == NULL)
  {
    // Lookup (and hence normalise) path
    name = find_path(from, path);

    if(name == NULL)
      return NULL;
  }

  ast_t* program = ast_nearest(from, TK_PROGRAM);
  ast_t* package = ast_get(program, name, NULL);

  if(package != NULL) // Package already loaded
    return package;

  package = create_package(program, name);

  if(report_build)
    printf("Building %s\n", path);

  if(magic != NULL)
  {
    if(!parse_source_code(package, magic, options))
      return NULL;
  }
  else
  {
    if(!parse_files_in_dir(package, name, options))
      return NULL;
  }

  if(ast_child(package) == NULL)
  {
    ast_error(package, "no source files in package '%s'", path);
    return NULL;
  }

  if(!package_passes(package, options))
    return NULL;

  return package;
}
Пример #7
0
static void template_two_dimension_pyclustering_package(const TypeContainer & container) {
    using sub_container_t   = typename TypeContainer::value_type;
    using container_data_t  = typename sub_container_t::value_type;

    pyclustering_package * package = create_package(&container);

    ASSERT_EQ(container.size(), package->size);

    for (std::size_t i = 0; i < container.size(); i++) {
        pyclustering_package * sub_package = package->at<pyclustering_package *>(i);
        ASSERT_EQ(container[i].size(), sub_package->size);

        for (std::size_t j = 0; j < container[i].size(); j++) {
            ASSERT_EQ(container[i][j], package->at<container_data_t>(i, j));
        }
    }

    delete package;
}
Пример #8
0
	connection::dispatch_slot connection::create_dispatch_slot()
	{
		dispatch_slot slot = [this](std::unique_ptr<stream_buffer> buffer)
		{
			std::shared_ptr<package> pkg = create_package(std::move(buffer));

			std::lock_guard<std::mutex> locker(_mutex);

			if (_socket_locked)
			{
				_packages.push(pkg);
			}
			else
			{
				send_package(pkg);
			}
		};

		return slot;
	}
Пример #9
0
void handle_forwarding_package(IoT_Package *package_info)
{
	int n=0, idx=0,cmd_len=0;
	char sendBuffer[MAXPACKETSIZE];
	char cmdBuffer[MAXPACKETSIZE];
	printf("forwarding to:%s\n", package_info->des_ip.ip);
	//printAllChar(package_info->data, package_info->data_length);
	IoT_Device_Info *device_info = NULL;

	device_info = find_device_with_ip(package_info->des_ip);
	if (device_info == NULL)
	{
		puts("Cannot found target device ip");
		IoT_Command cmd = generate_iot_command();
		cmd.cmd_type = command_t_Management;
		strcpy(cmd.ID, "Del_Dev");
		strcpy(cmd.Value, package_info->des_ip.ip);
		memset(cmdBuffer,'\0', MAXPACKETSIZE);
		cmd_len =encode_iot_cmd(cmdBuffer, &cmd);

		IoT_Package send_package_info = generate_iot_package();
		create_package(&send_package_info, ServerIP, package_info->sor_ip.ip, cmdBuffer, cmd_len);
		n = encode_package(sendBuffer, &send_package_info);

		idx = package_info->belongSocketIdx;
		send(client_socket[idx], sendBuffer,n,0);
		free_package(&send_package_info);
	}
	else
	{
		n = encode_package(sendBuffer, package_info);
		//printf("Send to XBee agent:\n");
		//printAllChar(sendBuffer,n);
		idx = device_info->socket_index;
		send(client_socket[idx], sendBuffer, n, 0);

		//print_device_info(device_info);
	}
	//scan_all_device();
}
Пример #10
0
void handle_ip_request_package(IoT_Package *package_info)
{

	char suffix[3];
	char newIp[20];
	int offset = 0;
	sprintf(suffix, "%d", ip_count);
	int prefix_len = strlen(prefix);
	//int suffix_len = strlen(suffix);
	ip_count++;
	charcat(newIp, prefix, offset, prefix_len);
	offset += prefix_len;
	charcat(newIp, suffix, offset, prefix_len);

	char buffer[MAXRECV];
	IoT_Package new_package_info = generate_iot_package();
	create_package(&new_package_info, ServerIP, newIp, "0", 1);
	int packageLength = encode_package(buffer, &new_package_info);
	send(client_socket[package_info->belongSocketIdx], buffer, packageLength, 0);
	printf("Give ip:%s\n", new_package_info.des_ip.ip);
	free_package(&new_package_info);
}
Пример #11
0
pyclustering_package * silhouette_algorithm(
    const pyclustering_package * const p_sample,
    const pyclustering_package * const p_clusters,
    const void * const p_metric)
{
    dataset data;
    p_sample->extract(data);

    ccore::clst::cluster_sequence clusters;
    p_clusters->extract(clusters);

    distance_metric<point> * metric = ((distance_metric<point> *) p_metric);
    distance_metric<point> default_metric = distance_metric_factory<point>::euclidean_square();

    if (!metric)
        metric = &default_metric;

    ccore::clst::silhouette_data result;
    ccore::clst::silhouette(*metric).process(data, clusters, result);

    return create_package(&result.get_score());
}
Пример #12
0
ast_t* package_load(ast_t* from, const char* path, pass_opt_t* options)
{
  const char* magic = find_magic_package(path);
  const char* full_path = path;
  const char* qualified_name = path;
  ast_t* program = ast_nearest(from, TK_PROGRAM);

  if(magic == NULL)
  {
    // Lookup (and hence normalise) path
    bool is_relative = false;
    full_path = find_path(from, path, &is_relative);

    if(full_path == NULL)
      return NULL;

    if((from != NULL) && is_relative)
    {
      // Package to load is relative to from, build the qualified name
      // The qualified name should be relative to the program being built
      package_t* from_pkg = (package_t*)ast_data(ast_child(program));

      if(from_pkg != NULL)
      {
        const char* base_name = from_pkg->qualified_name;
        size_t base_name_len = strlen(base_name);
        size_t path_len = strlen(path);
        size_t len = base_name_len + path_len + 2;
        char* q_name = (char*)pool_alloc_size(len);
        memcpy(q_name, base_name, base_name_len);
        q_name[base_name_len] = '/';
        memcpy(q_name + base_name_len + 1, path, path_len);
        q_name[len - 1] = '\0';
        qualified_name = stringtab_consume(q_name, len);
      }
    }
  }

  ast_t* package = ast_get(program, full_path, NULL);

  // Package already loaded
  if(package != NULL)
    return package;

  package = create_package(program, full_path, qualified_name);

  if(report_build)
    printf("Building %s -> %s\n", path, full_path);

  if(magic != NULL)
  {
    if(!parse_source_code(package, magic, options))
      return NULL;
  }
  else
  {
    if(!parse_files_in_dir(package, full_path, options))
      return NULL;
  }

  if(ast_child(package) == NULL)
  {
    ast_error(package, "no source files in package '%s'", path);
    return NULL;
  }

  // We add new packages to the end of the program, so they will be reached by
  // the current pass processing. This means we need to catch up the new
  // package to the previous pass
  if(!ast_passes_subtree(&package, options,
    pass_prev(options->type_catchup_pass)))
    return NULL;

  return package;
}
Пример #13
0
ast_t* package_load(ast_t* from, const char* path, pass_opt_t* opt)
{
  pony_assert(from != NULL);

  magic_package_t* magic = find_magic_package(path, opt);
  const char* full_path = path;
  const char* qualified_name = path;
  ast_t* program = ast_nearest(from, TK_PROGRAM);

  if(magic == NULL)
  {
    // Lookup (and hence normalise) path
    bool is_relative = false;
    bool found_notdir = false;
    full_path = find_path(from, path, &is_relative, &found_notdir, opt);

    if(full_path == NULL)
    {
      errorf(opt->check.errors, path, "couldn't locate this path");

      if(found_notdir)
        errorf_continue(opt->check.errors, path, "note that a compiler "
          "invocation or a 'use' directive must refer to a directory");

      return NULL;
    }

    if((from != program) && is_relative)
    {
      // Package to load is relative to from, build the qualified name
      // The qualified name should be relative to the program being built
      package_t* from_pkg = (package_t*)ast_data(ast_child(program));

      if(from_pkg != NULL)
      {
        const char* base_name = from_pkg->qualified_name;
        size_t base_name_len = strlen(base_name);
        size_t path_len = strlen(path);
        size_t len = base_name_len + path_len + 2;
        char* q_name = (char*)ponyint_pool_alloc_size(len);
        memcpy(q_name, base_name, base_name_len);
        q_name[base_name_len] = '/';
        memcpy(q_name + base_name_len + 1, path, path_len);
        q_name[len - 1] = '\0';
        qualified_name = stringtab_consume(q_name, len);
      }
    }

    // we are loading the package specified as program dir
    if(from == program)
    {
      // construct the qualified name from the basename of the full path
      const char* basepath = strrchr(full_path, '/');
      if(basepath == NULL)
      {
        basepath = full_path;
      } else {
        basepath = basepath + 1;
      }
      qualified_name = basepath;
    }
  }

  ast_t* package = ast_get(program, full_path, NULL);

  // Package already loaded
  if(package != NULL)
    return package;

  package = create_package(program, full_path, qualified_name, opt);

  if(opt->verbosity >= VERBOSITY_INFO)
    fprintf(stderr, "Building %s -> %s\n", path, full_path);

  if(magic != NULL)
  {
    if(magic->src != NULL)
    {
      if(!parse_source_code(package, magic->src, opt))
        return NULL;
    } else if(magic->mapped_path != NULL) {
      if(!parse_files_in_dir(package, magic->mapped_path, opt))
        return NULL;
    } else {
      return NULL;
    }
  }
  else
  {
    if(!parse_files_in_dir(package, full_path, opt))
      return NULL;
  }

  if(ast_child(package) == NULL)
  {
    ast_error(opt->check.errors, package,
      "no source files in package '%s'", path);
    return NULL;
  }

  if(!ast_passes_subtree(&package, opt, opt->program_pass))
  {
    // If these passes failed, don't run future passes.
    ast_setflag(package, AST_FLAG_PRESERVE);
    return NULL;
  }

  return package;
}
struct sub_device_buffer * FUNCTION_ATTRIBUTE 
    create_command_package(uint16_t flags)
{
    return create_package(flags, PAYLOAD_TYPE_COMMAND);
}
struct sub_device_buffer * FUNCTION_ATTRIBUTE 
    create_event_package(uint16_t flags)
{
    return create_package(flags, PAYLOAD_TYPE_EVENT);
}
struct sub_device_buffer * FUNCTION_ATTRIBUTE 
    create_data_package(uint16_t flags)
{
    return create_package(flags, PAYLOAD_TYPE_DATA);
}
Пример #17
0
ast_t* package_load(ast_t* from, const char* path, pass_opt_t* options)
{
  assert(from != NULL);

  const char* magic = find_magic_package(path);
  const char* full_path = path;
  const char* qualified_name = path;
  ast_t* program = ast_nearest(from, TK_PROGRAM);

  if(magic == NULL)
  {
    // Lookup (and hence normalise) path
    bool is_relative = false;
    full_path = find_path(from, path, &is_relative);

    if(full_path == NULL)
      return NULL;

    if((from != program) && is_relative)
    {
      // Package to load is relative to from, build the qualified name
      // The qualified name should be relative to the program being built
      package_t* from_pkg = (package_t*)ast_data(ast_child(program));

      if(from_pkg != NULL)
      {
        const char* base_name = from_pkg->qualified_name;
        size_t base_name_len = strlen(base_name);
        size_t path_len = strlen(path);
        size_t len = base_name_len + path_len + 2;
        char* q_name = (char*)pool_alloc_size(len);
        memcpy(q_name, base_name, base_name_len);
        q_name[base_name_len] = '/';
        memcpy(q_name + base_name_len + 1, path, path_len);
        q_name[len - 1] = '\0';
        qualified_name = stringtab_consume(q_name, len);
      }
    }
  }

  ast_t* package = ast_get(program, full_path, NULL);

  // Package already loaded
  if(package != NULL)
    return package;

  package = create_package(program, full_path, qualified_name);

  if(report_build)
    printf("Building %s -> %s\n", path, full_path);

  if(magic != NULL)
  {
    if(!parse_source_code(package, magic, options))
      return NULL;
  }
  else
  {
    if(!parse_files_in_dir(package, full_path, options))
      return NULL;
  }

  if(ast_child(package) == NULL)
  {
    ast_error(package, "no source files in package '%s'", path);
    return NULL;
  }

  if(!ast_passes_subtree(&package, options, options->program_pass))
  {
    // If these passes failed, don't run future passes.
    ast_setflag(package, AST_FLAG_PRESERVE);
    return NULL;
  }

  return package;
}
Пример #18
0
// Threads handle chunks of the packages list. Since we don't know where
// package definitions are split in the list data, we'll usually toss some data
// from the front (it was handled as part of another chunk), and grab some
// extra data from the back. These will typically be small amounts, and the
// data is read-only, so rather than track splits (requiring synchronization),
// we just always locally discover the bounds (put another way, the overlapping
// areas at the front and back of chunks are lexed twice).
//
// Returns the number of packages parsed (possibly 0), or -1 on error. In the
// case of an error, packages already parsed *are not* freed, and enq *is
// not* reset.
//
// We're not treating the input as anything but ASCII text, though it's almost
// certainly UTF8. Need to ensure that newlines and pattern tags are all
// strictly ASCII or change how we handle things FIXME.
//
// Typical rules for DFA: If NULL, it's unused. If it points to a pointer to
// NULL, construct it from whatever we find. If it points to a non-NULL
// pointer, use that as a filter for construction of our list.
static int
lex_chunk(size_t offset,const char *start,const char *end,
		const char *veryend,pkgobj ***enq,
		struct pkgparse *pp){
	const char *expect,*pname,*pver,*pstatus,*c,*delim;
	size_t pnamelen,pverlen;
	int rewardstate,state;
	unsigned newp = 0;
	unsigned filter;
	dfactx dctx;
	pkgobj *po;

	// filter is 0 if we're building the dfa, 1 if we're being filtered by
	// the dfa, and undefined if dga == NULL
	if(pp->dfa){
		filter = *(pp->dfa) ? 1 : 0;
	}else{
		filter = 0; // FIXME get rid of this when gcc improves
	}
	// First, find the start of our chunk:
	//  - If we are offset 0, we are at the start of our chunk
	//  - Otherwise, if the previous two characters (those preceding our
	//     chunk) are newlines, we are at the start of our chunk,
	//  - Otherwise, if the first character is a newline, and the previous
	//     character is a newline, we are at the start of our chunk
	//  - Otherwise, our chunk starts at the first double newline
	if(offset){
		// We can be in one of two states: we know the previous
		// character to have been a newline, or we don't.
		state = STATE_PDATA;
		//assert(pp->csize > 2); // sanity check
		for(c = start - 2 ; c < end ; ++c){
			if(*c == '\n'){
				if(state == STATE_NLINE){
					++c;
					break;
				}
				state = STATE_NLINE;
			}else{
				state = STATE_PDATA;
			}
		}
	}else{
		c = start;
	}
	// We are at the beginning of our chunk, which might be 0 bytes. Any
	// partial record with which our map started has been skipped
	// Upon reaching the (optional, only one allowed) delimiter on each
	// line, delim will be updated to point one past that delimiter (which
	// might be outside the chunk!), and to chew whitespace.
	delim = NULL;
	expect = NULL;
	// We hand create_package our raw map bytes; it allocates the destbuf.
	// These are thus reset on each package.
	pname = NULL; pver = NULL; pstatus = NULL;
	state = STATE_RESET; // number of newlines we've seen, bounded by 2
	rewardstate = STATE_RESET;
	pverlen = pnamelen = 0;
	while(c < end || (state != STATE_RESET && c < veryend)){
		if(*c == '\n'){ // State machine is driven by newlines
			switch(state){
			case STATE_NLINE:{ // double newline
				if(pname == NULL || pnamelen == 0){
					return -1; // No package name
				}
				if(pver == NULL || pverlen == 0){
					if(!pp->statusfile){
						return -1; // No package version
					}
				}
				if(!pp->statusfile || pstatus){
					if(pp->dfa && filter){
						pkgobj *mpo;

						init_dfactx(&dctx,*pp->dfa);
						if( (mpo = match_dfactx_nstring(&dctx,pname,pnamelen)) ){
							if((po = create_package(pname,pnamelen,pver,pverlen,pp->sharedpcache)) == NULL){
								return -1;
							}
							pthread_mutex_lock(&mpo->lock);
							po->dfanext = mpo->dfanext;
							mpo->dfanext = po;
							pthread_mutex_unlock(&mpo->lock);
							po->haslock = 0;
						}else{
							po = NULL;
						}
					}else if((po = create_package(pname,pnamelen,pver,pverlen,pp->sharedpcache)) == NULL){
						return -1;
					}else if(pthread_mutex_init(&po->lock,NULL)){
						free_package(po);
					}else{
						po->haslock = 1;
					}
				}else{
					po = NULL;
				}
				// Package ended!
				if(po){
					++newp;
					**enq = po;
					*enq = &po->next;
				}
				pname = NULL;
				pver = NULL;
				pstatus = NULL;
				state = STATE_RESET;
				break;
			} // Else we processed a line of the current package
			case STATE_PACKAGE_DELIMITED:
// Don't allow a package to be named twice. Defined another way, require an
// empty line between every two instances of a Package: line.
				if(pname){
					return -1;
				}
				pnamelen = c - delim;
				pname = delim;
				break;
			case STATE_VERSION_DELIMITED:
				if(pver){
					return -1;
				}
				pverlen = c - delim;
				pver = delim;
				break;
			case STATE_STATUS_DELIMITED:
				if(pstatus){
					return -1;
				}
				pstatus = delim;
				break;
			}
			if(state != STATE_RESET){
				state = STATE_NLINE;
			}
		}else switch(state){ // not a newline
			case STATE_NLINE:
			case STATE_RESET:
				delim = NULL;
				start = c;
				if(*c == 'V'){
					state = STATE_EXPECT;
					expect = "ersion:";
					rewardstate = STATE_VERSION_DELIMITED;
				}else if(*c == 'P'){
					state = STATE_EXPECT;
					expect = "ackage:";
					rewardstate = STATE_PACKAGE_DELIMITED;
				}else if(*c == 'S'){
					state = STATE_EXPECT;
					expect = "tatus: install ";
					rewardstate = STATE_STATUS_DELIMITED;
				}else{
					state = STATE_PDATA;
				}
				break;
			case STATE_EXPECT:
				if(*c == *expect){
					if(!*++expect){
						state = STATE_DELIM;
						delim = c + 1;
					}
				}else{
					state = STATE_PDATA;
				}
				break;
			case STATE_DELIM:
				if(isspace(*c)){
					++delim;
				}else{
					state = rewardstate;
				}
				break;
		}
		++c;
	}
	if(state != STATE_RESET){
		return -1;
	}
	return newp;
}