/* * Create an Database */ int RemoteDBCreate(const char * filename,char * addr,int port) { if(filename == NULL) { fprintf(stderr,"filename Error,%s:%d\n", __FILE__,__LINE__); filename = "nezha.hdb"; } /* connect server */ tServiceHandler dbhandler = OpenRemoteService(addr,port); if(dbhandler == -1) { return -1; } /* open database file */ char Buf[MAX_BUF_LEN] = "\0"; int BufSize = MAX_BUF_LEN; FormatData1(Buf,&BufSize,OPEN_CMD,(char*)filename,strlen(filename)); SendData(dbhandler,Buf,BufSize); BufSize = MAX_BUF_LEN; RecvData(dbhandler,Buf,&BufSize); int cmd = -1; int DataNum = -1; char Data1[MAX_BUF_LEN] = "\0"; int Data1Size = MAX_BUF_LEN; char Data2[MAX_BUF_LEN] = "\0"; int Data2Size = MAX_BUF_LEN; ParseData(Buf,MAX_BUF_LEN,&cmd,&DataNum,Data1,&Data1Size,Data2,&Data2Size); if(cmd != OPEN_RSP || DataNum != 0) { fprintf(stderr,"Remote DBCreate Error,%s:%d\n", __FILE__,__LINE__); return -1; } return dbhandler; }
/* * Create an Database */ int CreateRemoteDB(char *addr, int port, const char * dbName) { if(dbName == NULL) { fprintf(stderr, "filename Error,%s:%d\n", __FILE__, __LINE__); dbName = "default.hdb"; } /* connect server */ tServiceHandler h = OpenRemoteService(addr, port); if(h == -1) { return -1; } /* open database file */ char Buf[MAX_BUF_LEN] = "\0"; int BufSize = MAX_BUF_LEN; FormatData1(Buf, &BufSize, OPEN_CMD, (char*)dbName, strlen(dbName)); SendData(h, Buf, BufSize); BufSize = MAX_BUF_LEN; RecvData(h, Buf, &BufSize); int cmd = -1; int DataNum = -1; char Data1[MAX_BUF_LEN] = "\0"; int Data1Size = MAX_BUF_LEN; char Data2[MAX_BUF_LEN] = "\0"; int Data2Size = MAX_BUF_LEN; ParseData(Buf, MAX_BUF_LEN, &cmd, &DataNum, Data1, &Data1Size, Data2, &Data2Size); if(cmd != OPEN_RSP || DataNum != 0) { fprintf(stderr, "CreateRemoteDB Error,%s:%d\n", __FILE__, __LINE__); return -1; } return h; }
/* * delete key/value */ int RemoteDBDelKeyValue(int db,tKey key) { if(db == -1) { return -1; } char Buf[MAX_BUF_LEN] = "\0"; int BufSize = MAX_BUF_LEN; int ret = FormatData1(Buf,&BufSize,DELETE_CMD,(char*)key.str,key.len); if(ret == -1) { return -1; } SendData(db,Buf,BufSize); BufSize = MAX_BUF_LEN; if(RecvData(db,Buf,&BufSize) == 0) { fprintf(stderr,"Connection Error,%s:%d\n",__FILE__,__LINE__); return -1; } int cmd = -1; int DataNum = -1; char Data1[MAX_BUF_LEN] = "\0"; int Data1Size = MAX_BUF_LEN; char Data2[MAX_BUF_LEN] = "\0"; int Data2Size = MAX_BUF_LEN; ParseData(Buf,BufSize,&cmd,&DataNum,Data1,&Data1Size,Data2,&Data2Size); if(cmd == ERROR_RSP && DataNum == 1) { fprintf(stderr,"%s,%s:%d\n",Data1, __FILE__,__LINE__); return -1; } return 0; }
/* * Create an Database */ tDatabase DBCreate(const char * filename) { if(filename == NULL) { fprintf(stderr,"filename Error,%s:%d\n", __FILE__,__LINE__); filename = "nezha.hdb"; } /* connect server */ h = OpenRemoteService(IP_ADDR,PORT); if(h == -1) { exit(-1); } /* open database file */ char Buf[MAX_BUF_LEN] = "\0"; int BufSize = MAX_BUF_LEN; FormatData1(Buf,&BufSize,OPEN_CMD,(char*)filename,strlen(filename)); SendData(h,Buf,BufSize); BufSize = MAX_BUF_LEN; RecvData(h,Buf,&BufSize); int cmd = -1; int DataNum = -1; char Data1[MAX_BUF_LEN] = "\0"; int Data1Size = MAX_BUF_LEN; char Data2[MAX_BUF_LEN] = "\0"; int Data2Size = MAX_BUF_LEN; ParseData(Buf,MAX_BUF_LEN,&cmd,&DataNum,Data1,&Data1Size,Data2,&Data2Size); if(cmd != OPEN_RSP || DataNum != 0) { fprintf(stderr,"Remote DBCreate Error,%s:%d\n", __FILE__,__LINE__); } return (tDatabase)&h; }
/* * delete key/value */ int DBDelKeyValue(tDatabase db,tKey key) { if(db == NULL) { return -1; } char Buf[MAX_BUF_LEN] = "\0"; int BufSize = MAX_BUF_LEN; int ret = FormatData1(Buf,&BufSize,DELETE_CMD,(char*)&key,sizeof(tKey)); if(ret == -1) { return -1; } SendData(*(tServiceHandler*)db,Buf,BufSize); BufSize = MAX_BUF_LEN; if(RecvData(*(tServiceHandler*)db,Buf,&BufSize) == 0) { fprintf(stderr,"Connection Error,%s:%d\n",__FILE__,__LINE__); return -1; } int cmd = -1; int DataNum = -1; char Data1[MAX_BUF_LEN] = "\0"; int Data1Size = MAX_BUF_LEN; char Data2[MAX_BUF_LEN] = "\0"; int Data2Size = MAX_BUF_LEN; ParseData(Buf,BufSize,&cmd,&DataNum,Data1,&Data1Size,Data2,&Data2Size); if(cmd == ERROR_RSP && DataNum == 1) { fprintf(stderr,"%s,%s:%d\n",Data1, __FILE__,__LINE__); return -1; } return 0; }
/* * get key/value */ int GetRecordRemote(int db, tKey key, tValue *pvalue) { if(db == -1 || pvalue == NULL) { return -1; } char Buf[MAX_BUF_LEN] = "\0"; int BufSize = MAX_BUF_LEN; int ret = FormatData1(Buf, &BufSize, GET_CMD, (char*)&key, sizeof(tKey)); if(ret == -1) { return -1; } SendData(db, Buf, BufSize); BufSize = MAX_BUF_LEN; if(RecvData(db, Buf, &BufSize) == 0) { fprintf(stderr, "Connection Error,%s:%d\n", __FILE__, __LINE__); return -1; } int cmd = -1; int DataNum = -1; char Data1[MAX_BUF_LEN] = "\0"; int Data1Size = MAX_BUF_LEN; char Data2[MAX_BUF_LEN] = "\0"; int Data2Size = MAX_BUF_LEN; ParseData(Buf, BufSize, &cmd, &DataNum, Data1, &Data1Size, Data2, &Data2Size); if(cmd == ERROR_RSP && DataNum == 1) { return -1; } if(cmd != GET_RSP || DataNum != 2 || key != *(tKey*)Data1 || Data1Size != sizeof(tKey)) { fprintf(stderr, "GetRecordRemote Error,%s:%d\n", __FILE__, __LINE__); return -1; } pvalue->len = Data2Size; pvalue->str = Data2; return 0; }
/* * get key/value */ int RemoteDBGetKeyValue(int db,tKey key,tValue *pvalue) { if(db == -1 || pvalue == NULL) { return -1; } char Buf[MAX_BUF_LEN] = "\0"; int BufSize = MAX_BUF_LEN; int ret = FormatData1(Buf,&BufSize,GET_CMD,(char*)key.str,key.len); if(ret == -1) { return -1; } SendData(db,Buf,BufSize); BufSize = MAX_BUF_LEN; if(RecvData(db,Buf,&BufSize) == 0) { fprintf(stderr,"Connection Error,%s:%d\n",__FILE__,__LINE__); return -1; } int cmd = -1; int DataNum = -1; char Data1[MAX_BUF_LEN] = "\0"; int Data1Size = MAX_BUF_LEN; char Data2[MAX_BUF_LEN] = "\0"; int Data2Size = MAX_BUF_LEN; ParseData(Buf,BufSize,&cmd,&DataNum,Data1,&Data1Size,Data2,&Data2Size); if(cmd == ERROR_RSP && DataNum == 1) { return -1; } if(cmd != GET_RSP || DataNum != 2 ) { fprintf(stderr,"Remote DBGetKeyValue Error,%s:%d\n", __FILE__,__LINE__); return -1; } pvalue->len = Data2Size; memcpy(pvalue->str,Data2,Data2Size); return 0; }
int main() { char Buf[MAX_BUF_LEN] = "\0"; char *str = "hello world!"; FormatData(Buf,MAX_BUF_LEN,CLOSE_CMD); int cmd = -1; int DataNum = -1; char Data1[MAX_BUF_LEN] = "\0"; int Data1Size = MAX_BUF_LEN; char Data2[MAX_BUF_LEN] = "\0"; int Data2Size = MAX_BUF_LEN; ParseData(Buf,MAX_BUF_LEN,&cmd,&DataNum,Data1,&Data1Size,Data2,&Data2Size); if(cmd == CLOSE_CMD && DataNum == 0) { printf("Test Zero Data Result: Pass\n"); } FormatData1(Buf,MAX_BUF_LEN,OPEN_CMD,str,strlen(str)); ParseData(Buf,MAX_BUF_LEN,&cmd,&DataNum,Data1,&Data1Size,Data2,&Data2Size); if(cmd == OPEN_CMD && DataNum == 1 && Data1Size == strlen(str) && strcmp(Data1,str) == 0) { printf("%s\n",Data1); printf("Test One Data Result: Pass\n"); } FormatData2(Buf,MAX_BUF_LEN,SET_CMD,str,strlen(str),str,strlen(str)); ParseData(Buf,MAX_BUF_LEN,&cmd,&DataNum,Data1,&Data1Size,Data2,&Data2Size); if(cmd == SET_CMD && DataNum == 2 && Data1Size == strlen(str) && Data2Size == strlen(str) && strcmp(Data1,str) == 0 && strcmp(Data2,str) == 0) { printf("%s\n",Data1); printf("%s\n",Data2); printf("Test Two Data Result: Pass\n"); } return 0; }