Very rude
Out of interest, this was his code
paperserver.c
Code:
#include "paperproc.h"
#define ERR_PAPER_NOT_FOUND 2
#define PAPER_FOUND_OK 4
struct paper_store {
paper_id id;
paper_author author;
paper_title title;
paper_data paper;
};
typedef struct paper_store paper_store;
struct paper_store_list {
struct paper_store *paper;
struct paper_store_list *next;
};
typedef struct paper_store_list paper_store_list;
paper_store_list* paperStore;
paper_id *add_1_svc(paperadd_param *param, struct svc_req *sr) {
printf("Recv new paper\n");
paper_store_list* newPaperStore = malloc(sizeof (paper_store_list));
paper_store* newPaper = malloc(sizeof (paper_store));
newPaperStore->paper = newPaper;
int newID;
if (paperStore == NULL) {
newID = 1;
} else {
newID = paperStore->paper->id + 1;
}
newPaperStore->paper->id = newID;
newPaperStore->paper->author = param->author;
newPaperStore->paper->title = param->title;
newPaperStore->paper->paper = param->paper;
newPaperStore->next = paperStore;
paperStore = newPaperStore;
printf("Done! Saved `%s` with ID `%d`\n", paperStore->paper->title, paperStore->paper->id);
printf("Memloc title: %x\n", *paperStore->paper->title); // different
printf("Memloc paper: %x\n", *paperStore->paper); // the same
return &paperStore->paper->id;
}
struct paper_list *list_1_svc(void *vd, struct svc_req *sr) {
printf("Serving List\n");
paper_list* returnList = malloc(sizeof (paper_list));
paper_store_list* current;
for (current = paperStore; current != NULL; current = current->next) {
paper_detail* tmp_detail = malloc(sizeof (paper_detail));
paper_list* tmp_list_node = malloc(sizeof (paper_list()));
tmp_detail->id = current->paper->id;
tmp_detail->title = current->paper->title;
tmp_detail->author = current->paper->author;
tmp_list_node->paperDetail = tmp_detail;
printf("%d, %s\n", tmp_detail->id, tmp_detail->title);
tmp_list_node->next = returnList;
returnList = tmp_list_node;
}
return returnList;
}
struct paper_detail_return *detail_1_svc(paper_id *id, struct svc_req *sr) {
printf("Memloc title: %x\n", *paperStore->paper->title); // different
printf("Memloc paper: %x\n", *paperStore->paper); // the same
paper_detail_return* out = malloc(sizeof (paper_detail_return));
printf("Recv request for details about paper `%d`\n", *id);
paper_store_list* current;
for (current = paperStore; current != NULL; current = current->next) {
if (current->paper->id == *id) {
out->statuscode = PAPER_FOUND_OK;
out->paper->title = current->paper->title;
out->paper->author = current->paper->author;
free(current);
return (out);
}
}
printf("Paper not found `%d`\n", *id);
out->statuscode = ERR_PAPER_NOT_FOUND;
return (out);
}
paper_return *fetch_1_svc(paper_id *id, struct svc_req *sr) {
paper_return* out = malloc(sizeof (paper_data));
printf("Recv request for paper `%d`\n", *id);
paper_store_list* current;
printf(">%s<\n", paperStore->paper->paper);
for (current = paperStore; current != NULL; current = current->next) {
printf(">%s<\n", current->paper->title);
if (current->paper->id == *id) {
out->statuscode = PAPER_FOUND_OK;
out->paper = current->paper->paper;
return (out);
}
}
printf("Paper not found `%d`\n", *id);
out->statuscode = ERR_PAPER_NOT_FOUND;
return (out);
}
paperclient.c
Code:
#include "paperproc.h"
#define ERR_PAPER_NOT_FOUND 2
#define PAPER_FOUND_OK 4
CLIENT *cl;
void printUsage(char **argv) {
printf("Usage:\n"
""
""
"%s add <machine> <author> <title> <data>\n"
"%s list <machine>\n"
"%s detail <machine> <paper_id>\n"
"%s fetch <machine> <paper_id>\n"
, argv[0], argv[0], argv[0], argv[0]);
}
void checkArguments(int argc, char **argv, int n) {
if (argc < n) {
printf("Error: Missing argument(s)\n\n");
printUsage(argv);
exit(EXIT_FAILURE);
}
}
paper_data readFile(char *filename) {
FILE *ifp;
char *mode = "r";
char *buffer;
unsigned long fileSize;
ifp = fopen(filename, mode);
if (ifp == NULL) {
fprintf(stderr, "Can't open input file %s!\n", filename);
exit(EXIT_FAILURE);
}
//Get file length
fseek(ifp, 0, SEEK_END);
fileSize = ftell(ifp);
fseek(ifp, 0, SEEK_SET);
//Allocate memory
buffer = (char *) malloc(fileSize + 1);
if (!buffer) {
fprintf(stderr, "Memory error!");
fclose(ifp);
exit(EXIT_FAILURE);
}
//Read file contents into buffer
fread(buffer, fileSize, 1, ifp);
fclose(ifp);
return buffer;
}
void add(int argc, char **argv) {
checkArguments(argc, argv, 6);
paperadd_param *param = malloc(sizeof (paperadd_param));
param->author = argv[3];
param->title = argv[4];
param->paper = readFile(argv[5]);
paper_id* id = add_1(param, cl);
printf("Paper added with id `%d`\n", *id);
}
void detail(int argc, char **argv) {
checkArguments(argc, argv, 4);
paper_id id = atoi(argv[3]);
paper_detail_return* awnser = detail_1(&id, cl);
if (awnser == NULL) {
printf("Error: %s\n", clnt_sperror(cl, argv[1]));
exit(EXIT_FAILURE);
}
switch (awnser->statuscode) {
case ERR_PAPER_NOT_FOUND:
printf("Error: The paper with id `%d` was not found.\n", id);
exit(EXIT_FAILURE);
break;
case PAPER_FOUND_OK:
printf("Details about paper `%d`:\n"
"Title: %s\n"
"Author: %s\n", id, awnser->paper->author, awnser->paper->title);
break;
}
}
void list() {
paper_list* awnser = list_1(NULL, cl);
while(awnser != NULL){
printf("%d: %s, %s\n", awnser->paperDetail->id, awnser->paperDetail->author, awnser->paperDetail->title);
awnser = awnser->next;
}
}
void fetch(int argc, char **argv) {
checkArguments(argc, argv, 4);
printf("Fetching document...\n");
paper_id id = atoi(argv[3]);
paper_return* awnser = fetch_1(&id, cl);
if (awnser == NULL) {
printf("Error: %s\n", clnt_sperror(cl, argv[1]));
exit(EXIT_FAILURE);
}
switch (awnser->statuscode) {
case ERR_PAPER_NOT_FOUND:
printf("Error: The paper with id `%d` was not found.\n", id);
exit(EXIT_FAILURE);
break;
case PAPER_FOUND_OK:
printf(">>>%s<<<\n", awnser->paper);
break;
}
}
int main(int argc, char **argv) {
if (argc <= 2) {
printUsage(argv);
return 1;
}
cl = clnt_create(argv[2], PAPERPROC_PROG, PAPERPROC_VERS, "tcp");
if (cl == NULL) {
printf("Error: Unable to create RPC client connection to `%s`\n", argv[2]);
exit(EXIT_FAILURE);
}
if (!strcmp(argv[1], "add")) {
add(argc, argv);
} else if (!strcmp(argv[1], "list")) {
list();
} else if (!strcmp(argv[1], "detail")) {
detail(argc, argv);
} else if (!strcmp(argv[1], "fetch")) {
fetch(argc, argv);
} else {
printf("Error: Command `%s` is invalid", argv[1]);
exit(EXIT_FAILURE);
}
clnt_destroy(cl);
return 0;
}
paperproc.x
Code:
typedef string paper_data < 10240 >;typedef string paper_title < 256 >;
typedef string paper_author < 256 >;
typedef int paper_id;
/*typedef opaque paper_data[1024];*/
struct paper_detail {
paper_id id;
paper_title title;
paper_author author;
};
struct paper_list {
paper_detail *paperDetail;
struct paper_list *next;
};
struct paperadd_param {
paper_author author;
paper_title title;
paper_data paper;
};
struct paper_return {
int statuscode;
paper_data paper;
};
-
struct paper_detail_return {
int statuscode;
paper_detail *paper;
};
Code:
program PAPERPROC_PROG
{
version PAPERPROC_VERS
{
paper_id ADD(paperadd_param) = 1;
paper_list LIST() = 2;
paper_detail_return DETAIL(paper_id) = 3;
paper_return FETCH(paper_id) = 4;
}
= 1;
}
= 0x31423456;
Wouldn't want the world missing out on it!