예제 #1
0
파일: cache.c 프로젝트: Hexasoft/WebFS
/* create a new cache for given file.
   returns true if created, false else
   can destroy an other one if no place available */
Cache *cache_create(const char *file, unsigned int size) {
  int i;
  Cache *tmp=NULL;
  
  mylog("cache_create(%s, %u)\n", file, size);
  /* search a free cache */
  for(i=0; i<CACHE_MAX; i++) {
    if (caches[i].name == NULL) {
      tmp = &(caches[i]);
      break;
    }
  }
  if (tmp == NULL) {
    /* not found. destroy the oldest cache */
    tmp = cache_oldest();
    if (tmp == NULL) {
      /* should not occur */
      return(NULL);
    }
  }
  /* initialise common cache data */
  tmp->name = strdup(file);
  tmp->created = tmp->last_use = (unsigned int)time(NULL);
  tmp->firstblock = NULL;
  for(i=0; i<CACHE_MAX_CHUNK; i++)
    tmp->chunks[i] = NULL;
  tmp->size = size;
  
  /* alocate 'firstblock' to cache_chunksize or size
     if smaller (for dl at 'connect') */
  tmp->firstblock = malloc(MIN(cache_chunksize, size));
  if (tmp->firstblock != NULL) {
     tmp->firstblocksize = MIN(cache_chunksize, size);
  }
  /* just ignore if failed (NULL) as in this case
     fistblock will be ignored */
  
  /* creation connection for this file */
  
  mylog("cache_create: connextion cache %p (cnx=%p)\n", tmp, &(tmp->connection));
  if (!cache_connect(&(tmp->connection), file,
          url_path, tmp->firstblock, MIN(cache_chunksize, size))) {
    /* destroy this cache... */
    cache_free(tmp);
    return(NULL);
  }
  
  mylog("cache_create: %p->connection = { %s, %d, %p, %d}\n", tmp,
    tmp->connection.target, tmp->connection.type,
    tmp->connection.data, tmp->connection.idata);
  
  /* ok */
  return(tmp);
}
bool cps_api_get_handle(cps_api_key_t &key, cps_api_channel_t &handle) {
    if (cache_connect(key,handle)) {
        return true;
    }
    cps_api_object_owner_reg_t owner;
    if (!cps_api_find_owners(&key,owner)) return false;
    bool rc = (cps_api_connect_owner(&owner, handle))==cps_api_ret_code_OK;
    if (!rc) {
        char buff[SCRATCH_LOG_BUFF];
        EV_LOG(ERR,DSAPI,0,"NS","Could not connect with owner for %s (%s)",
                cps_api_key_print(&key,buff,sizeof(buff)-1),
                owner.addr.addr_type== e_std_socket_a_t_STRING ?
                        owner.addr.address.str: "unk");
    }
    if (rc) {
        fill_cache(&key,owner);
    }
    return rc;
}