/* * @map_get * Reads value associated with the key in map data structure pointed by map. * Retrievs just values not older than given timeout (in seconds). timeout = 0 * means no timeout. * retval: pointer to the data associated with the key * if NULL, no key was found or other error occurred * space for the returned value is freshly allocated */ void *map_get(struct map **map, char *key, time_t timeout) { struct map *tmp = *map; void *value = NULL; time_t now = time(NULL); if (!*map || !key) goto out; dprintf("Getting key %s from map %p.\n", key, *map); while (tmp) { if (!strcmp(key, tmp->key)) { if (timeout > 0 && now - tmp->timestamp > timeout) { /* * We invalidate the key, but someone * could try it with different timeout. * We don't bother, he'll wait. */ dprintf("Cache expired for key %s in map %p.\n", key, *map); map_del(map, key); goto out; } else value = tmp->data; break; } tmp = tmp->next; } out: return value; }
int network_close(struct network *net, const char *addr, uint16_t port) { endpoint_array_t *ep_array = map_get(&net->endpoints, addr); if (ep_array == NULL) { return kr_error(ENOENT); } /* Close endpoint in array. */ for (size_t i = ep_array->len; i--;) { struct endpoint *ep = ep_array->at[i]; if (ep->port == port) { close_endpoint(ep); array_del(*ep_array, i); break; } } /* Collapse key if it has no endpoint. */ if (ep_array->len == 0) { free(ep_array); map_del(&net->endpoints, addr); } return kr_ok(); }
int fclose2 (FILE *file) { #undef fclose fmode2_t fmode = get_fmode (file); map_del (fh_map, file); if (fmode == FM_NORMAL) return fclose (file); else if (fmode == FM_GZIP) return gzclose (file); else if (fmode == FM_ZIP) { unzCloseCurrentFile (file); return unzClose (file); } else return EOF; #define fclose fclose2 }
static void play(int skt, const char *file, int timeout) { char mode[] = { PLAY }; char *cp; char expect[MAX_CONCUR_NAME+1+MAX_CONCUR_MSG]; char actual[MAX_CONCUR_MSG]; MAP conh = map_create((map_diff*)strcmp, (map_hash*)fnv04, NULL); FILE *fp = fopen(file, "r"); fd_set fds; if (!fp) { fprintf(stderr, "error: cannot read %s\n", file); exit(2); } FD_ZERO(&fds); while (fgets(expect, sizeof expect, fp)) { int fd, ret; struct timeval to = { timeout, 0 }; for (cp = expect + strlen(expect); cp > expect && isspace(cp[-1]); *--cp = 0); if (!expect[0]) continue; if (expect[0] == '#') { if (expect[1] != ':') fputs(expect, stdout); continue; } if (!(cp = strchr(expect, ':'))) { fprintf(stderr, "concurs: invalid script line:\n%s\n", expect); continue; } *cp++ = 0; while (!(fd = (int)(uintptr_t)map_get(conh, expect))) { FD_SET(skt, &fds); if (timeout && 0 > select(skt+1, &fds, NULL, NULL, &to)) { fprintf(stderr, "concurs: timed out waiting for %s:connect\n", expect); exit(1); } FD_CLR(skt, &fds); fd = sock_accept(skt); FD_SET(fd, &open_fds); cp = playerv[fd] = strdup(expect); map_set(conh, playerv[fd], (void*)(uintptr_t)fd); } FD_SET(fd, &fds); if (timeout && 0 > select(skt+1, &fds, NULL, NULL, &to)) { fprintf(stderr, "concurs: error waiting for %s:%s\n", expect, cp); exit(1); } FD_CLR(fd, &fds); if (0 >= (ret = sock_recv(fd, actual, sizeof actual))) { close(fd); FD_CLR(fd, &open_fds); map_del(conh, playerv[fd]); free(playerv[fd]); playerv[fd] = NULL; } sock_send(fd, mode, sizeof(mode)); //REVISIT: select(nowait) on all open fds and report who was blocked. printf("%s:%s\n", expect, actual); if (strcmp(cp, actual)) { fprintf(stderr, "concurs: broken\n"); exit(1); } } }
double delete_vertex(struct graph* g, char* label){ return (double) map_del(g->vertices, label); }