/**
 * A function to destroy a storage snapshot.
 * @param[in] api The deltacloud_api structure representing the connection
 * @param[in] storage_snapshot The deltacloud_storage_snapstho structure
 *                             representing the storage snapshot
 * @returns 0 on success, -1 on error
 */
int deltacloud_storage_snapshot_destroy(struct deltacloud_api *api,
					struct deltacloud_storage_snapshot *storage_snapshot)
{
  if (!valid_api(api) || !valid_arg(storage_snapshot))
    return -1;

  return internal_destroy(storage_snapshot->href, api->user, api->password, api->driver, api->provider);
}
/**
 * A function to destroy a load balancer.
 * @param[in] api The deltacloud_api structure representing the connection
 * @param[in] balancer The deltacloud_loadbalancer structure representing the
 *                     load balancer
 * @returns 0 on success, -1 on error
 */
int deltacloud_loadbalancer_destroy(struct deltacloud_api *api,
				    struct deltacloud_loadbalancer *balancer)
{
  if (!valid_api(api) || !valid_arg(balancer))
    return -1;

  return internal_destroy(balancer->href, api->user, api->password, api->driver, api->provider);
}
/**
 * A function to create a new load balancer.
 * @param[in] api The deltacloud_api structure representing the connection
 * @param[in] name The name to give to the new load balancer
 * @param[in] realm_id The realm ID to put the new load balancer in
 * @param[in] protocol The protocol to load balance
 * @param[in] balancer_port The port the load balancer listens on
 * @param[in] instance_port The port the load balancer balances to
 * @param[in] params An array of deltacloud_create_parameter structures that
 *                   represent any optional parameters to pass into the
 *                   create call
 * @param[in] params_length An integer describing the length of the params
 *                          array
 * @returns 0 on success, -1 on error
 */
int deltacloud_create_loadbalancer(struct deltacloud_api *api, const char *name,
				   const char *realm_id, const char *protocol,
				   int balancer_port, int instance_port,
				   struct deltacloud_create_parameter *params,
				   int params_length)
{
  struct deltacloud_create_parameter *internal_params;
  int ret = -1;
  int pos;

  if (!valid_api(api) || !valid_arg(name) || !valid_arg(realm_id)
      || !valid_arg(protocol))
    return -1;
  if (balancer_port < 0 || balancer_port > 65536) {
    invalid_argument_error("balancer_port must be between 0 and 65536");
    return -1;
  }
  if (instance_port < 0 || instance_port > 65536) {
    invalid_argument_error("instance_port must be between 0 and 65536");
    return -1;
  }

  if (params_length < 0) {
    invalid_argument_error("params_length must be >= 0");
    return -1;
  }

  internal_params = calloc(params_length + 5,
			   sizeof(struct deltacloud_create_parameter));
  if (internal_params == NULL) {
    oom_error();
    return -1;
  }

  pos = copy_parameters(internal_params, params, params_length);
  if (pos < 0)
    /* copy_parameters already set the error */
    goto cleanup;

  if (deltacloud_prepare_parameter(&internal_params[pos++], "name", name) < 0 ||
      deltacloud_prepare_parameter(&internal_params[pos++], "realm_id", realm_id) < 0 ||
      deltacloud_prepare_parameter(&internal_params[pos++], "listener_protocol", protocol) < 0)
    /* deltacloud_prepare_parameter already set the error */
    goto cleanup;

  if (prepare_int_parameter(&internal_params[pos++], "listener_balancer_port",
			    balancer_port) < 0)
    /* prepare_int_parameter already set the error */
    goto cleanup;

  if (prepare_int_parameter(&internal_params[pos++], "listener_instance_port",
			    instance_port) < 0)
    /* prepare_int_parameter already set the error */
    goto cleanup;

  if (internal_create(api, "load_balancers", internal_params, pos, NULL,
		      NULL) < 0)
    /* internal_create already set the error */
    goto cleanup;

  ret = 0;

 cleanup:
  free_parameters(internal_params, pos);
  SAFE_FREE(internal_params);

  return ret;
}
static int lb_register_unregister(struct deltacloud_api *api,
				  struct deltacloud_loadbalancer *balancer,
				  const char *instance_id,
				  struct deltacloud_create_parameter *params,
				  int params_length,
				  const char *link)
{
  struct deltacloud_link *thislink;
  struct deltacloud_create_parameter *internal_params;
  char *href;
  int ret = -1;
  int pos;
  int rc;

  if (!valid_api(api) || !valid_arg(balancer) || !valid_arg(instance_id))
    return -1;

  if (params_length < 0) {
    invalid_argument_error("params_length must be >= 0");
    return -1;
  }

  thislink = api_find_link(api, "load_balancers");
  if (thislink == NULL)
    /* api_find_link set the error */
    return -1;

  internal_params = calloc(params_length + 1,
			   sizeof(struct deltacloud_create_parameter));
  if (internal_params == NULL) {
    oom_error();
    return -1;
  }

  pos = copy_parameters(internal_params, params, params_length);
  if (pos < 0)
    /* copy_parameters already set the error */
    goto cleanup;

  if (deltacloud_prepare_parameter(&internal_params[pos++], "instance_id",
				   instance_id) < 0)
    /* deltacloud_prepare_parameter already set the error */
    goto cleanup;

  if (asprintf(&href, "%s/%s/%s", thislink->href, balancer->id, link) < 0) {
    oom_error();
    goto cleanup;
  }

  rc = internal_post(api, href, internal_params, pos, NULL, NULL);
  SAFE_FREE(href);
  if (rc < 0)
    /* internal_post already set the error */
    goto cleanup;

  ret = 0;

 cleanup:
  free_parameters(internal_params, pos);
  SAFE_FREE(internal_params);

  return ret;
}
Exemplo n.º 5
0
int main(int argc, char **argv) {

  // Validacion y lectura de argumentos

  if(argc != 13){
    print_use();
    exit(EXIT_FAILURE);
  }

  char * nombre_centro;
  int suministro, puerto;

  read_arg(argv,argc,&nombre_centro,&capacidad,&inventario,&suministro,&puerto,&tiempo);

  if (!valid_arg(nombre_centro,capacidad,inventario,suministro,tiempo))
   exit(EXIT_FAILURE); 

  log_file_name = (char *) malloc(strlen(nombre_centro)+8);
  sprintf(log_file_name,"log_%s.txt",nombre_centro);
  
  log_file = fopen(log_file_name,"w");
  if(!log_file) {
    printf("Error: no se pudo crear el archivo log el archivo\n.");
    exit(EXIT_FAILURE);
  }

  fprintf(log_file,"Inventario inicial: %d litros.\n",inventario);

  pthread_attr_init(&attr1);
  pthread_attr_setdetachstate(&attr1,PTHREAD_CREATE_JOINABLE);
  if (pthread_create(&thread_inv, &attr1, inventario_suministro, (void *) suministro)) {
    printf("Error: no se pudo crear el hilo para controlar el inventario.");
    exit(EXIT_FAILURE);
  }

  pthread_attr_init(&attr2);
  pthread_attr_setdetachstate(&attr2,PTHREAD_CREATE_JOINABLE);
  if (pthread_create(&thread_func, &attr2, tiempo_funcionamiento,NULL)) {
    printf("Error: no se pudo crear el hilo para controlar el funcionamiento.");
    exit(EXIT_FAILURE);
  }

  signal(SIGUSR1,finish);
 
  pthread_attr_init(&attr3);
  pthread_attr_setdetachstate(&attr3,PTHREAD_CREATE_JOINABLE);
  if (pthread_create(&thread_exit, &attr3, tiempo_exit,NULL)) {
    printf("Error: no se pudo crear el hilo para salida.");
    exit(EXIT_FAILURE);
  }

  int newSocketID;
  socklen_t addrlen;
  struct sockaddr_in dirServ, dirCli;
  char buffer[256];
  pthread_t h;

  if (pthread_mutex_init(&mtx, NULL) !=0) {
    error("Error mutex");
  }
  socketID = socket(AF_INET,SOCK_STREAM,0);
  if (socketID == 0) {
    error("Error abriendo el socket");
  }

  bzero((char*)&dirServ,sizeof(dirServ));

  dirServ.sin_family = AF_INET;
  dirServ.sin_port = htons(puerto);
  dirServ.sin_addr.s_addr = htonl(INADDR_ANY);
  
  if (bind(socketID,(struct sockaddr *)&dirServ,sizeof(dirServ)) < 0 ) {
    error("Error en la conexion");
  }
  
  listen(socketID,5);
  addrlen = sizeof(dirCli);

  while (TRUE) {
    newSocketID = accept(socketID,(struct sockaddr *)&dirCli,&addrlen);
    if (newSocketID < 0) {
      error("Error aceptando la conexion");
    }
    pthread_create(&h,NULL,procesarPeticion, (void *)newSocketID);
  }
}
Exemplo n.º 6
0
int main(int argc, char *argv[])
{
    ApplicationsLib::LogogSetup logog_setup;

    TCLAP::CmdLine cmd(
        "Checks mesh properties.\n\n"
        "OpenGeoSys-6 software, version " +
            BaseLib::BuildInfo::ogs_version +
            ".\n"
            "Copyright (c) 2012-2019, OpenGeoSys Community "
            "(http://www.opengeosys.org)",
        ' ', BaseLib::BuildInfo::ogs_version);
    TCLAP::UnlabeledValueArg<std::string> mesh_arg("mesh-file","input mesh file",true,"","string");
    cmd.add( mesh_arg );
    TCLAP::SwitchArg valid_arg("v","validation","validate the mesh");
    cmd.add( valid_arg );
    TCLAP::SwitchArg print_properties_arg("p","print_properties","print properties stored in the mesh");
    cmd.add( print_properties_arg );

    cmd.parse( argc, argv );

    // read the mesh file
    BaseLib::MemWatch mem_watch;
    const unsigned long mem_without_mesh (mem_watch.getVirtMemUsage());
    BaseLib::RunTime run_time;
    run_time.start();
    std::unique_ptr<MeshLib::Mesh> mesh(
        MeshLib::IO::readMeshFromFile(mesh_arg.getValue()));
    if (!mesh)
    {
        return EXIT_FAILURE;
    }

    const unsigned long mem_with_mesh (mem_watch.getVirtMemUsage());
    if (mem_with_mesh>0)
    {
        INFO ("Memory size: %i MB", (mem_with_mesh - mem_without_mesh)/(1024*1024));
        (void)mem_with_mesh;
    }
    INFO ("Time for reading: %g s", run_time.elapsed());

    // Geometric information
    const GeoLib::AABB aabb(MeshLib::MeshInformation::getBoundingBox(*mesh));
    auto minPt(aabb.getMinPoint());
    auto maxPt(aabb.getMaxPoint());
    INFO("Node coordinates:");
    INFO("\tx [%g, %g] (extent %g)", minPt[0], maxPt[0], maxPt[0]-minPt[0]);
    INFO("\ty [%g, %g] (extent %g)", minPt[1], maxPt[1], maxPt[1]-minPt[1]);
    INFO("\tz [%g, %g] (extent %g)", minPt[2], maxPt[2], maxPt[2]-minPt[2]);

    INFO("Edge length: [%g, %g]", mesh->getMinEdgeLength(), mesh->getMaxEdgeLength());

    // Element information
    const std::array<unsigned, 7> nr_ele_types(MeshLib::MeshInformation::getNumberOfElementTypes(*mesh));
    INFO("Element types:");
    unsigned etype = 0;
    if (nr_ele_types[etype] > 0)
    {
        INFO("\t%d lines", nr_ele_types[etype]);
    }
    if (nr_ele_types[++etype] > 0)
    {
        INFO("\t%d triangles", nr_ele_types[etype]);
    }
    if (nr_ele_types[++etype] > 0)
    {
        INFO("\t%d quads", nr_ele_types[etype]);
    }
    if (nr_ele_types[++etype] > 0)
    {
        INFO("\t%d tetrahedra", nr_ele_types[etype]);
    }
    if (nr_ele_types[++etype] > 0)
    {
        INFO("\t%d hexahedra", nr_ele_types[etype]);
    }
    if (nr_ele_types[++etype] > 0)
    {
        INFO("\t%d pyramids", nr_ele_types[etype]);
    }
    if (nr_ele_types[++etype] > 0)
    {
        INFO("\t%d prisms", nr_ele_types[etype]);
    }

    std::vector<std::string> const& vec_names (mesh->getProperties().getPropertyVectorNames());
    INFO("There are %d properties in the mesh:", vec_names.size());
    for (const auto & vec_name : vec_names)
    {
        auto vec_bounds (MeshLib::MeshInformation::getValueBounds<int>(*mesh, vec_name));
        if (vec_bounds.second != std::numeric_limits<int>::max())
        {
            INFO("\t%s: [%d, %d]", vec_name.c_str(), vec_bounds.first,
                 vec_bounds.second);
        }
        else
        {
            auto vec_bounds (MeshLib::MeshInformation::getValueBounds<double>(*mesh, vec_name));
            if (vec_bounds.second != std::numeric_limits<double>::max())
            {
                INFO("\t%s: [%g, %g]", vec_name.c_str(), vec_bounds.first,
                     vec_bounds.second);
            }
            else
            {
                INFO("\t%s: Unknown properties", vec_name.c_str());
            }
        }
    }

    if (valid_arg.isSet()) {
        // MeshValidation outputs error messages
        // Remark: MeshValidation can modify the original mesh
        MeshLib::MeshValidation validation(*mesh);

        unsigned const n_holes (MeshLib::MeshValidation::detectHoles(*mesh));
        if (n_holes > 0)
        {
            INFO("%d hole(s) detected within the mesh", n_holes);
        }
        else
        {
            INFO ("No holes found within the mesh.");
        }
    }
}
/**
 * A function to create a new storage snapshot.
 * @param[in] api The deltacloud_api structure representing the connection
 * @param[in] volume The volume to take the snapshot from
 * @param[in] params An array of deltacloud_create_parameter structures that
 *                   represent any optional parameters to pass into the
 *                   create call
 * @param[in] params_length An integer describing the length of the params
 *                          array
 * @param[out] snap_id The snapshot_id returned by the create call
 * @returns 0 on success, -1 on error
 */
int deltacloud_create_storage_snapshot(struct deltacloud_api *api,
				       struct deltacloud_storage_volume *volume,
				       struct deltacloud_create_parameter *params,
				       int params_length,
				       char **snap_id)
{
  struct deltacloud_create_parameter *internal_params;
  struct deltacloud_storage_snapshot snap;
  char *data = NULL;
  int ret = -1;
  int pos;

  if (!valid_api(api) || !valid_arg(volume))
    return -1;

  if (params_length < 0) {
    invalid_argument_error("params_length must be >= 0");
    return -1;
  }

  internal_params = calloc(params_length + 1,
			   sizeof(struct deltacloud_create_parameter));
  if (internal_params == NULL) {
    oom_error();
    return -1;
  }

  pos = copy_parameters(internal_params, params, params_length);
  if (pos < 0)
    /* copy_parameters already set the error */
    goto cleanup;

  if (deltacloud_prepare_parameter(&internal_params[pos++], "volume_id",
				   volume->id) < 0)
    /* deltacloud_create_parameter already set the error */
    goto cleanup;

  if (internal_create(api, "storage_snapshots", internal_params, pos, &data,
		      NULL) < 0)
    /* internal_create already set the error */
    goto cleanup;

  if (snap_id != NULL) {
    if (internal_xml_parse(data, "storage_snapshot", parse_one_storage_snapshot,
			   1, &snap) < 0)
      /* internal_xml_parse set the error */
      goto cleanup;

    *snap_id = strdup(snap.id);
    deltacloud_free_storage_snapshot(&snap);
    if (*snap_id == NULL) {
      oom_error();
      goto cleanup;
    }
  }

  ret = 0;

 cleanup:
  free_parameters(internal_params, pos);
  SAFE_FREE(internal_params);
  SAFE_FREE(data);

  return ret;
}