/** * Helper function to lookup a role in a session. * */ static role *find_role_in_session(session *s, char *role_name) { int role_idx; #ifdef __DEBUG__ fprintf(stderr, "%s: { role: %s } in ", __FUNCTION__, role_name); session_dump(s); #endif for (role_idx=0; role_idx<s->nrole; ++role_idx) { switch (s->roles[role_idx]->type) { case SESSION_ROLE_P2P: if (strcmp(s->roles[role_idx]->p2p->name, role_name) == 0) { return s->roles[role_idx]; } break; case SESSION_ROLE_GRP: if (strcmp(s->roles[role_idx]->grp->name, role_name) == 0) { return s->roles[role_idx]; } break; case SESSION_ROLE_INDEXED: assert(0); // TODO handle indexed endpoint break; default: fprintf(stderr, "Unknown endpoint type: %d\n", s->roles[role_idx]->type); } } fprintf(stderr, "%s: Role %s not found in session.\n", __FUNCTION__, role_name); #ifdef __DEBUG__ session_dump(s); #endif return NULL; }
/** * 关闭 session 并将 session 数据写入临时文件并关闭 session * * @return int 成功返回0,失败返回非0值 */ int session_write_close() { FILE* fp = NULL; size_t i = 0; char* buffer = NULL; if (g_session_data == NULL) { fprintf(cgiOut, "Warning: You must make your calls to session_close()" " AFTER invoking session_start().<br />\n"); return -1; } #ifdef DEBUG fprintf(cgiOut, "session_write_close() <-- BEGIN\n"); #endif fp = fopen(g_session_data->session_filename, "w"); if (fp == NULL) { fprintf(cgiOut, "Warning: Can't create session file: %s.<br />\n", g_session_data->session_filename); return -1; } // 开始写入 session 数据 for (i = 0; i < g_session_data->max_index; i++) { if (g_session_data->items[i].name == NULL) { continue; } buffer = sess_serialize(g_session_data->items[i].name); fputs(buffer, fp); fputs("\n", fp); free(buffer); buffer = NULL; buffer = sess_serialize(g_session_data->items[i].string); fputs(buffer, fp); fputs("\n", fp); free(buffer); buffer = NULL; } fclose(fp); #ifdef DEBUG session_dump(cgiOut); fprintf(cgiOut, "session_write_close() END -->\n"); #endif // 释放 session 占用的资源 sess_free_resource(); return 0; }
int main(int argc, char *argv[]) { session *s; session_init(&argc, &argv, &s, "Protocol_R2.spr"); session_dump(s); long long barrier_start = sc_time(); barrier(s->r(s, "_Others"), "R0"); long long barrier_end = sc_time(); sc_print_version(); printf("%s: Barrier time: %f sec\n", s->name, sc_time_diff(barrier_start, barrier_end)); session_end(s); return EXIT_SUCCESS; }
/** * 销毁当前访问者的 session * * @return int 成功返回0,失败返回非0值 */ int session_destroy() { if (g_session_data == NULL) { fprintf(cgiOut, "Warning: You must make your calls to session_destroy()" " AFTER invoking session_start().<br />\n"); return -1; } #ifdef DEBUG fprintf(cgiOut, "session_destroy()\n"); session_dump(cgiOut); #endif // 删除 session 数据文件 remove(g_session_data->session_filename); // 释放 session 占用的资源 sess_free_resource(); return 0; }
/** * 载入现有的 session * * @param const char* session_id 要读取的 session_id * @param const char* datadir * * @return int 成功返回0,失败返回非0值 */ int sess_load(const char* session_id, const char* datadir) { FILE* fp = NULL; char* buffer = NULL; char* tmp = NULL; SESSION_ITEM_LIST tmp_list = NULL; size_t buffer_length = 2048; int index = 0; if (g_session_data == NULL) { return -1; } #ifdef DEBUG fprintf(cgiOut, "sess_load() BEGIN -->\n"); #endif // 构造 session_data g_session_data->session_id = (char*) malloc( (strlen(session_id) + 1) * sizeof(char)); strcpy(g_session_data->session_id, session_id); g_session_data->session_filename = sess_get_filename( g_session_data->session_datadir, g_session_data->session_id); #ifdef DEBUG fprintf(cgiOut, "sess_load(), session file:%s\n", g_session_data->session_filename); #endif fp = fopen(g_session_data->session_filename, "r"); if (fp == NULL) { // 无效的 session_filename sess_free_resource(); sess_init(datadir); return sess_create(); } buffer = (char*) malloc(buffer_length * sizeof(char)); tmp = buffer; while (!feof(fp)) { if (fgets(tmp, buffer_length, fp) == NULL) { break; } /* if (buffer[strlen(buffer) + 1] != '\n' && !feof(fp)) { // 缓冲区太小,无法容纳该行字符串 buffer_length += buffer_length; tmp = (char*)malloc(buffer_length * sizeof(char)); strcpy(tmp, buffer); free(buffer); buffer = tmp; tmp = buffer += strlen(buffer); continue; } */ // 如果 count >= max_index,则需要增加列表容量 if (g_session_data->count >= g_session_data->max_index) { g_session_data->max_index += 10; #ifdef DEBUG fprintf(cgiOut, "* items count: %ld, max_index grow up to: %ld\n", g_session_data->count, g_session_data->max_index); #endif tmp_list = (SESSION_ITEM_LIST) malloc( g_session_data->max_index * sizeof(SESSION_ITEM)); memset(tmp_list, 0, g_session_data->max_index * sizeof(SESSION_ITEM)); if (g_session_data->items != NULL) { memcpy(tmp_list, g_session_data->items, g_session_data->count * sizeof(SESSION_ITEM)); free(g_session_data->items); } g_session_data->items = tmp_list; tmp_list = NULL; } // 添加一个新项目 if ((index % 2) == 0) { tmp = g_session_data->items[g_session_data->count].name; #ifdef DEBUG fprintf(cgiOut, "read name:"); #endif } else { tmp = g_session_data->items[g_session_data->count].string; #ifdef DEBUG fprintf(cgiOut, "read string:"); #endif } if (buffer[strlen(buffer) - 1] == '\n') { buffer[strlen(buffer) - 1] = '\0'; } tmp = sess_unserialize(buffer); #ifdef DEBUG fprintf(cgiOut, "%s\n", tmp); #endif if ((index % 2) == 0) { g_session_data->items[g_session_data->count].name = tmp; } else { g_session_data->items[g_session_data->count].string = tmp; g_session_data->count++; } // 增加索引,准备读取下一行 tmp = buffer; index++; } free(buffer); fclose(fp); if ((index % 2) != 0) { free(g_session_data->items[g_session_data->count - 1].name); g_session_data->items[g_session_data->count - 1].name = NULL; } #ifdef DEBUG session_dump(cgiOut); #endif #ifdef DEBUG fprintf(cgiOut, "sess_load() END -->\n"); #endif return 0; }