hamsterdb Embedded Database  2.1.7
server1.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2014 Christoph Rupp (chris@crupp.de).
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <ham/hamsterdb.h>
26 #include <ham/hamsterdb_srv.h>
27 
28 #ifdef WIN32
29 # define EXT ".exe"
30 #else
31 # define EXT ""
32 #endif
33 
34 int
36 {
37  ham_db_t *db;
38  ham_env_t *env;
39  ham_srv_t *srv;
40  ham_srv_config_t cfg;
41  ham_status_t st;
42  char input[1024];
43  int s;
44 
45  /* create a new Environment; this Environment will be attached to the
46  * server */
47  st = ham_env_create(&env, "env1.db", HAM_ENABLE_TRANSACTIONS, 0644, 0);
48  if (st) {
49  printf("ham_env_create: %d\n", st);
50  exit(-1);
51  }
52 
53  /* also create a Database in that Environment ... */
54  st = ham_env_create_db(env, &db, 12, HAM_ENABLE_DUPLICATE_KEYS, 0);
55  if (st) {
56  printf("ham_env_create_db: %d\n", st);
57  exit(-1);
58  }
59 
60  /* ... and close it again. It will be reopened remotely. */
61  ham_db_close(db, 0);
62 
63  /* Create a second database */
64  st = ham_env_create_db(env, &db, 13, HAM_ENABLE_DUPLICATE_KEYS, 0);
65  if (st) {
66  printf("ham_env_create_db: %d\n", st);
67  exit(-1);
68  }
69 
70  ham_db_close(db, 0);
71 
72  st = ham_env_create_db(env, &db, 33,
74  if (st) {
75  printf("ham_env_create_db: %d\n", st);
76  exit(-1);
77  }
78 
79  ham_db_close(db, 0);
80 
81  /* The ham_srv_config_t structure describes the settings of the server
82  * including the port, the Environment etc */
83  memset(&cfg, 0, sizeof(cfg));
84  cfg.port = 8080;
85  ham_srv_init(&cfg, &srv);
86  ham_srv_add_env(srv, env, "/env1.db");
87 
88  printf("server1%s started - please run sample 'client1%s' for a test\n",
89  EXT, EXT);
90  printf("type 'exit' to end the server\n");
91 
92  /* See client1.c for the corresponding client */
93  while (1) {
94  printf("> ");
95  s = scanf("%s", &input[0]);
96  if (s == EOF || !strcmp(input, "exit")) {
97  printf("exiting...\n");
98  break;
99  }
100  printf("unknown command\n");
101  }
102 
103  /* Close the server and the Environment */
104  ham_srv_close(srv);
106 
107  return (0);
108 }