This section consists of pseudo codes for the modules in the project.
a. Client Design Pseudocode
/* create socket */
ret = socket(AF_INET, SOCK_STREAM, 0);
sock_server.sin_family = AF_INET;
sock_server.sin_addr.s_addr = inet_addr(server_ip);
sock_server.sin_port = htons(atoi(server_port));
/* Upload List*/
while(temp) {
send_upload_file(server_fd, dir_path, temp->fname);
temp = temp->next;
}
fp = fopen(fname, "r");
/* calculate file size */
fseek(fp, 0, SEEK_END);
fsize = ftell(fp);
fseek(fp, 0, SEEK_SET);
req.type = REQ_UPLOADFDATA;
req.len = fsize;
ret = write(server_fd, &req, sizeof(req_t));
/* send file data */
sprintf(fname, "%s/.projback/%s", dir_path, file_name);
fpw = fopen(fname, "w");
len = 0;
do {
ret = fread(buf, sizeof(char), sizeof(buf), fp);
len += ret;
if(ret > 0) {
ret = write(server_fd, buf, ret);
ret = fwrite(buf, sizeof(char), ret, fpw);
}
} while(len < fsize);
b. Server Design Pseudocode
while(1) {
/* read client req */
if(req.type == REQ_RECOVERDIR) {
dirp = opendir(dir_path);
do {
ret = readdir_r(dirp, &dent, &next_dent);
if(next_dent == NULL) {
break;
}
/* write rsp header to send file name and mtime */
len = strlen(dent.d_name);
rsp.type = RSP_FILENAME;
rsp.len = len;
ret = write(client_fd, &rsp, sizeof(rsp_t));
/* send file name */
ret = write(client_fd, dent.d_name, len);
fp = fopen(fname, "r");
/* calculate file size */
fseek(fp, 0, SEEK_END);
fsize = ftell(fp);
fseek(fp, 0, SEEK_SET);
/* send file data */
rsp.type = RSP_FILEDATA;
rsp.len = fsize;
ret = write(client_fd, &rsp, sizeof(rsp_t));
do {
ret = fread(buf, sizeof(char), sizeof(buf), fp);
len += ret;
if(ret > 0) {
ret = write(client_fd, buf, ret);
}
} while(len < fsize);
fclose(fp);
}
} while(next_dent);
/* Remove File Request*/
if(req.type == REQ_REMOVEFILE) {
memset(buf, 0, BUF_LEN);
ret = read(client_fd, buf, req.len);
sprintf(fname, "%s/%s", dir_path, buf);
remove(fname);
printf("req received remove file\n");
printf("removing file %s\n", buf);
}
/*Upload File Data*/
if(req.type == REQ_UPLOADFDATA) {
printf("req received file data\n");
fp = fopen(fname, "w");
len = 0;
do {
ret = read(client_fd, buf, ((req.len - len < BUF_LEN)? (req.len - len): BUF_LEN));
fwrite(buf, sizeof(char), ret, fp);
len += ret;
} while(len < req.len);
fclose(fp);
}
c. Server Synchronization
extern NAP_VOID hex_dump(NAP_UCHAR *buff, NAP_UINT32 size);
/*
* Sync thread function
*/
NAP_BOOL sync_send_reply(NAP_UINT32 uiSockDesc,
SYNC_MESSAGE **psRetResp,
NAP_UINT32 replyType,
NAP_UINT32 replySubtype,
NAP_UINT32 appData,
NAP_UCHAR *DirName,
NAP_UCHAR *FileName,
time_t mtime)
{
SYNC_MESSAGE *psResp = NAP_NULL;
NAP_UINT32 iResult = 0;
psResp = (SYNC_MESSAGE*) malloc(sizeof(SYNC_MESSAGE));
if (psResp == NAP_NULL
{
printf("\n%s: Malloc failed at line : %d",__FUNCTION__,__LINE__);
}
memset(psResp, 0, sizeof(SYNC_MESSAGE));
psResp->eMessageType = replyType;
psResp->AppData = appData;//guiActiveClient;
psResp->eReplyType = replySubtype;
if(DirName != NULL)
strcpy(psResp->DirName, DirName);
if(FileName != NULL)
strcpy(psResp->FileName, FileName);
psResp->mtime = mtime;
printf("\n%s: \npsResp->eMessageType = %d, psResp->AppData = %d \npsResp->eReplyType = %d \npsResp->DirName = %s \npsResp->FileName = %s",__FUNCTION__, psResp->eMessageType, psResp->AppData, psResp->eReplyType, psResp->DirName, psResp->FileName);
iResult = write(uiSockDesc, (NAP_VOID*)psResp, sizeof(SYNC_MESSAGE));
if(iResult <= 0)
{
printf("\n%s Unable to send SYNC_CLIENT_LIST_REPLY", __FUNCTION__);
return NAP_FAILURE;
}
else
{
printf("\n:%s: Sent Success", __FUNCTION__);
*psRetResp = psResp;
return NAP_SUCCESS;
}
return NAP_SUCCESS;
}