miercuri, 24 iulie 2013

#5 - C implementation of command server API

It was a long week with lots of commits and changes on my project. I
almost finished the level 0 implementation. Right now with those basic
prototypes a client can handle and implement almost all the mercurial
commands.  Now he can easily establish the connection with mercurial
command server and then send and receive data through this connection.

On our weekly meeting, my mentor, Giovanni taught me a lesson about
the ‘log’ issue, how I must handle the huge mass of data, and how to
deliver this data through the pipe, directly to the user. We agreed
that we must use a ‘trunk’ to carry the data. This ‘trunk’ will have a
fixed capacity and will not carry the entire data in one delivery.

After this short lesson, Giovanni asked me to implement some 
mercurial commands with my level 0 API, to show its purpose and 
mechanism.

Here are some examples for those commands:

- init command:

hg_handle *hg_init_by_hand()
{
pid_t cpid;
int status;
hg_handle *handle;
char command[50];
sprintf(command, "hg init %s", INIT_REPO);
if((cpid = fork()) < 0) {
printf("Fork failed\n");
return NULL;
} else if(cpid == 0) {
execl("/bin/sh", "sh", "-c", command, NULL);
printf("dadads\n\n");
} else {
waitpid( cpid, &status, 0);
}
handle = hg_open(INIT_REPO, "");
return handle;
}

- log command:

int hg_log_by_hand(hg_handle *handle)
{
char buff[4096];
char *comm[] = {"log", "-v"};
int exitcode;
hg_rawcommand(handle, comm, 2);
while(hg_rawread(handle, buff, 4096)){
printf("%s", buff);
}
if(hg_channel(handle) == 'r'){
exitcode = hg_exitcode(handle);
printf("exitcode = %d\n", exitcode);
}
return exitcode;

}

- verify command:

int hg_verify_by_hand(hg_handle *handle)
{
int exitcode = 0;
char *comm[] = {"verify"};
char buff[4096];
hg_rawcommand(handle, comm, 1);
while(hg_channel(handle) != 'r'){
if(hg_channel(handle) == 'o'){
hg_rawread(handle, buff, 4096);
printf("out = %s", buff);
}
else if(hg_channel(handle) == 'e'){
hg_rawread(handle, buff, 4096);
printf("err = %s", buff);
}
}
if(hg_channel(handle) == 'r'){
exitcode = hg_exitcode(handle);
printf("exitcode = %d\n", exitcode);
}
return exitcode;
}

You can find more of those commands on my bitbucket repository:

Niciun comentariu:

Trimiteți un comentariu