return 0;
}
+/*
+ hash function for mapping data to a VNN - taken from tdb
+*/
+uint32_t ctdb_hash(const TDB_DATA *key)
+{
+ uint32_t value; /* Used to compute the hash value. */
+ uint32_t i; /* Used to cycle through random values. */
+
+ /* Set the initial value from the key size. */
+ for (value = 0x238F13AF * key->dsize, i=0; i < key->dsize; i++)
+ value = (value + (key->dptr[i] << (i*5 % 24)));
+
+ return (1103515243 * value + 12345);
+}
+
+void fetch_lock(int fd, uint32_t db_id, TDB_DATA key)
+{
+ struct ctdb_req_fetch_lock *req;
+ struct ctdb_reply_fetch_lock *rep;
+ uint32_t length;
+ int len, cnt, tot;
+
+ len = offsetof(struct ctdb_req_fetch_lock, key) + key.dsize;
+ req = malloc(len);
+
+ req->hdr.length = len;
+ req->hdr.ctdb_magic = CTDB_MAGIC;
+ req->hdr.ctdb_version = CTDB_VERSION;
+ req->hdr.operation = CTDB_REQ_FETCH_LOCK;
+ req->hdr.reqid = 1;
+ req->db_id = db_id;
+ req->keylen = key.dsize;
+ memcpy(&req->key[0], key.dptr, key.dsize);
+
+ cnt=write(fd, req, len);
+
+
+ /* wait fot the reply */
+ /* read the 4 bytes of length for the pdu */
+ cnt=0;
+ tot=4;
+ while(cnt!=tot){
+ int numread;
+ numread=read(fd, ((char *)&length)+cnt, tot-cnt);
+ if(numread>0){
+ cnt+=numread;
+ }
+ }
+ /* read the rest of the pdu */
+ rep = malloc(length);
+ tot=length;
+ while(cnt!=tot){
+ int numread;
+ numread=read(fd, ((char *)rep)+cnt, tot-cnt);
+ if(numread>0){
+ cnt+=numread;
+ }
+ }
+ printf("fetch lock reply: state:%d datalen:%d\n",rep->state,rep->datalen);
+ if(!rep->datalen){
+ printf("no data\n");
+ } else {
+ printf("data:[%s]\n",rep->data);
+ }
+
+}
+
+void store_unlock(int fd, uint32_t db_id, TDB_DATA key, TDB_DATA data)
+{
+ struct ctdb_req_store_unlock *req;
+ struct ctdb_reply_store_unlock *rep;
+ uint32_t length;
+ int len, cnt, tot;
+
+ len = offsetof(struct ctdb_req_store_unlock, data) + key.dsize + data.dsize;
+ req = malloc(len);
+
+ req->hdr.length = len;
+ req->hdr.ctdb_magic = CTDB_MAGIC;
+ req->hdr.ctdb_version = CTDB_VERSION;
+ req->hdr.operation = CTDB_REQ_STORE_UNLOCK;
+ req->hdr.reqid = 1;
+ req->db_id = db_id;
+ req->keylen = key.dsize;
+ req->datalen = data.dsize;
+ memcpy(&req->data[0], key.dptr, key.dsize);
+ memcpy(&req->data[key.dsize], data.dptr, data.dsize);
+
+ cnt=write(fd, req, len);
+
+
+ /* wait fot the reply */
+ /* read the 4 bytes of length for the pdu */
+ cnt=0;
+ tot=4;
+ while(cnt!=tot){
+ int numread;
+ numread=read(fd, ((char *)&length)+cnt, tot-cnt);
+ if(numread>0){
+ cnt+=numread;
+ }
+ }
+ /* read the rest of the pdu */
+ rep = malloc(length);
+ tot=length;
+ while(cnt!=tot){
+ int numread;
+ numread=read(fd, ((char *)rep)+cnt, tot-cnt);
+ if(numread>0){
+ cnt+=numread;
+ }
+ }
+ printf("store unlock reply: state:%d\n",rep->state);
+}
+
int main(int argc, const char *argv[])
{
int fd, pid, vnn, dstvnn, dstpid;
TDB_DATA message;
struct ctdb_req_message *reply;
+ TDB_DATA dbname;
+ uint32_t db_id;
+ TDB_DATA key, data;
+ char str[256];
/* open the socket to talk to the local ctdb daemon */
fd=ux_socket_connect(CTDB_SOCKET);
/* send a message to ourself */
dstvnn=vnn;
dstpid=pid;
- message.dptr="Test message";
- message.dsize=strlen(message.dptr)+1;
+ message.dptr=discard_const("Test message");
+ message.dsize=strlen((const char *)message.dptr)+1;
+ printf("sending test message [%s] to ourself\n", message.dptr);
send_a_message(fd, vnn, dstvnn, dstpid, message);
- receive_a_message(fd, &reply);
-
/* wait for the message to come back */
+ receive_a_message(fd, &reply);
+ printf("received message: [%s]\n",&reply->data[0]);
+
+ /* create the db id for "test.tdb" */
+ dbname.dptr = discard_const("test.tdb");
+ dbname.dsize = strlen((const char *)(dbname.dptr));
+ db_id = ctdb_hash(&dbname);
+ printf("the has for the database id is 0x%08x\n",db_id);
+ printf("\n");
+
+ /* send a fetch lock */
+ key.dptr=discard_const("TestKey");
+ key.dsize=strlen((const char *)(key.dptr));
+ printf("fetch the test key:[%s]\n",key.dptr);
+ fetch_lock(fd, db_id, key);
+ printf("\n");
+
+
+ /* send a store unlock */
+ sprintf(str,"TestData_%d",getpid());
+ data.dptr=discard_const(str);
+ data.dsize=strlen((const char *)(data.dptr));
+ printf("store new data==[%s] for this record\n",data.dptr);
+ store_unlock(fd, db_id, key, data);
+ printf("\n");
+
+ /* send a fetch lock */
+ printf("fetch the test key:[%s]\n",key.dptr);
+ fetch_lock(fd, db_id, key);
+ printf("\n");
return 0;