hamsterdb Embedded Database  2.1.1
server1.c
Go to the documentation of this file.
00001 
00016 #include <stdio.h>
00017 #include <stdlib.h>
00018 #include <string.h>
00019 #include <ham/hamsterdb.h>
00020 #include <ham/hamsterdb_srv.h>
00021 
00022 #ifdef WIN32
00023 #   define EXT ".exe"
00024 #else
00025 #   define EXT ""
00026 #endif
00027 
00028 int
00029 main() {
00030     ham_db_t *db;
00031     ham_env_t *env;
00032     ham_srv_t *srv;
00033     ham_srv_config_t cfg;
00034     ham_status_t st;
00035     char input[1024];
00036 
00037     /* create a new Environment; this Environment will be attached to the
00038      * server */
00039     st = ham_env_create(&env, "env1.db", HAM_ENABLE_TRANSACTIONS, 0644, 0);
00040     if (st) {
00041         printf("ham_env_create: %d\n", st);
00042         exit(-1);
00043     }
00044 
00045     /* also create a Database in that Environment ... */
00046     st = ham_env_create_db(env, &db, 12, HAM_ENABLE_DUPLICATES, 0);
00047     if (st) {
00048         printf("ham_env_create_db: %d\n", st);
00049         exit(-1);
00050     }
00051 
00052     /* ... and close it again. It will be reopened remotely. */
00053     ham_db_close(db, 0);
00054 
00055     /* Create a second database */
00056     st = ham_env_create_db(env, &db, 13, HAM_ENABLE_DUPLICATES, 0);
00057     if (st) {
00058         printf("ham_env_create_db: %d\n", st);
00059         exit(-1);
00060     }
00061 
00062     ham_db_close(db, 0);
00063 
00064     st = ham_env_create_db(env, &db, 33,
00065                 HAM_RECORD_NUMBER | HAM_ENABLE_DUPLICATES, 0);
00066     if (st) {
00067         printf("ham_env_create_db: %d\n", st);
00068         exit(-1);
00069     }
00070 
00071     ham_db_close(db, 0);
00072 
00073     /* The ham_srv_config_t structure describes the settings of the server
00074      * including the port, the Environment etc */
00075     memset(&cfg, 0, sizeof(cfg));
00076     cfg.port = 8080;
00077     ham_srv_init(&cfg, &srv);
00078     ham_srv_add_env(srv, env, "/env1.db");
00079 
00080     printf("server1%s started - please run sample 'client1%s' for a test\n",
00081             EXT, EXT);
00082     printf("type 'exit' to end the server\n");
00083 
00084     /* See client1.c for the corresponding client */
00085     while (1) {
00086         printf("> ");
00087         scanf("%s", &input[0]);
00088         if (!strcmp(input, "exit")) {
00089             printf("exiting...\n");
00090             break;
00091         }
00092         printf("unknown command\n");
00093     }
00094 
00095     /* Close the server and the Environment */
00096     ham_srv_close(srv);
00097     ham_env_close(env, HAM_AUTO_CLEANUP);
00098 
00099     return (0);
00100 }