Changed tabs to 4 spaces. Rule.
--- a/client.c Wed Mar 21 20:59:03 2007 +0100
+++ b/client.c Wed Mar 21 21:01:36 2007 +0100
@@ -3,10 +3,10 @@
int shutdown_server()
{
- struct msg m;
- int res;
+ struct msg m;
+ int res;
- m.type = KILL;
- res = write(server_socket, &m, sizeof(m));
- assert(res != -1);
+ m.type = KILL;
+ res = write(server_socket, &m, sizeof(m));
+ assert(res != -1);
}
--- a/main.c Wed Mar 21 20:59:03 2007 +0100
+++ b/main.c Wed Mar 21 21:01:36 2007 +0100
@@ -12,44 +12,44 @@
void parse_opts(int argc, char **argv)
{
- int c;
+ int c;
- /* Parse options */
- while(1) {
- c = getopt(argc, argv, "k");
+ /* Parse options */
+ while(1) {
+ c = getopt(argc, argv, "k");
- if (c == -1)
- break;
+ if (c == -1)
+ break;
- switch(c)
- {
- case 'k':
- kill_server = 1;
- break;
- }
- }
+ switch(c)
+ {
+ case 'k':
+ kill_server = 1;
+ break;
+ }
+ }
- if (kill_server == 0)
- need_server = 1;
+ if (kill_server == 0)
+ need_server = 1;
}
int main(int argc, char **argv)
{
- parse_opts(argc, argv);
+ parse_opts(argc, argv);
- need_server = 1;
+ need_server = 1;
- if (need_server)
- {
- fprintf(stderr, "Ensure server up.\n");
- ensure_server_up();
- }
-
- if (kill_server)
- {
- printf("Trying to kill server.\n");
- shutdown_server();
- }
+ if (need_server)
+ {
+ fprintf(stderr, "Ensure server up.\n");
+ ensure_server_up();
+ }
+
+ if (kill_server)
+ {
+ printf("Trying to kill server.\n");
+ shutdown_server();
+ }
- return 0;
+ return 0;
}
--- a/msg.h Wed Mar 21 20:59:03 2007 +0100
+++ b/msg.h Wed Mar 21 21:01:36 2007 +0100
@@ -2,17 +2,17 @@
enum msg_types
{
- KILL
+ KILL
};
struct msg
{
- enum msg_types type;
+ enum msg_types type;
- union
- {
- int data1;
- int data2;
- } u;
+ union
+ {
+ int data1;
+ int data2;
+ } u;
};
--- a/server.c Wed Mar 21 20:59:03 2007 +0100
+++ b/server.c Wed Mar 21 21:01:36 2007 +0100
@@ -12,13 +12,13 @@
enum
{
- MAXCONN=100
+ MAXCONN=100
};
enum Break
{
- BREAK,
- NOBREAK
+ BREAK,
+ NOBREAK
};
void server_loop(int ls);
@@ -28,119 +28,119 @@
void server_main()
{
- int ls,cs;
- struct sockaddr_un addr;
- int res;
+ int ls,cs;
+ struct sockaddr_un addr;
+ int res;
- ls = socket(PF_UNIX, SOCK_STREAM, 0);
- assert(ls != -1);
+ ls = socket(PF_UNIX, SOCK_STREAM, 0);
+ assert(ls != -1);
- addr.sun_family = AF_UNIX;
- strcpy(addr.sun_path, path);
+ addr.sun_family = AF_UNIX;
+ strcpy(addr.sun_path, path);
- res = bind(ls, (struct sockaddr *) &addr, sizeof(addr));
- if (res == -1)
- {
- perror("Error binding.");
- return;
- }
+ res = bind(ls, (struct sockaddr *) &addr, sizeof(addr));
+ if (res == -1)
+ {
+ perror("Error binding.");
+ return;
+ }
- res = listen(ls, 0);
- if (res == -1)
- {
- perror("Error listening.");
- return;
- }
+ res = listen(ls, 0);
+ if (res == -1)
+ {
+ perror("Error listening.");
+ return;
+ }
- printf("server loop\n");
- server_loop(ls);
+ printf("server loop\n");
+ server_loop(ls);
}
void server_loop(int ls)
{
- fd_set readset;
- int connections[MAXCONN];
- int nconnections;
- int i;
- int maxfd;
- int keep_loop = 1;
+ fd_set readset;
+ int connections[MAXCONN];
+ int nconnections;
+ int i;
+ int maxfd;
+ int keep_loop = 1;
- while (keep_loop)
- {
- FD_ZERO(&readset);
- FD_SET(ls,&readset);
- maxfd = ls;
- for(i=0; i< nconnections; ++i)
- {
- FD_SET(connections[i], &readset);
- if (connections[i] > maxfd)
- maxfd = connections[i];
- }
- printf("select: nc = %i\n", nconnections);
- select(maxfd + 1, &readset, NULL, NULL, NULL);
- printf("Select unblocks\n");
- if (FD_ISSET(ls,&readset))
- {
- int cs;
- cs = accept(ls, NULL, NULL);
- assert(cs != -1);
- connections[nconnections++] = cs;
- }
- for(i=0; i< nconnections; ++i)
- if (FD_ISSET(connections[i], &readset))
- {
- enum Break b;
- b = client_read(i, connections,
- &nconnections);
- /* Check if we should break */
- if (b == BREAK)
- keep_loop = 0;
- }
- }
+ while (keep_loop)
+ {
+ FD_ZERO(&readset);
+ FD_SET(ls,&readset);
+ maxfd = ls;
+ for(i=0; i< nconnections; ++i)
+ {
+ FD_SET(connections[i], &readset);
+ if (connections[i] > maxfd)
+ maxfd = connections[i];
+ }
+ printf("select: nc = %i\n", nconnections);
+ select(maxfd + 1, &readset, NULL, NULL, NULL);
+ printf("Select unblocks\n");
+ if (FD_ISSET(ls,&readset))
+ {
+ int cs;
+ cs = accept(ls, NULL, NULL);
+ assert(cs != -1);
+ connections[nconnections++] = cs;
+ }
+ for(i=0; i< nconnections; ++i)
+ if (FD_ISSET(connections[i], &readset))
+ {
+ enum Break b;
+ b = client_read(i, connections,
+ &nconnections);
+ /* Check if we should break */
+ if (b == BREAK)
+ keep_loop = 0;
+ }
+ }
- end_server(ls);
+ end_server(ls);
}
void end_server(int ls)
{
- close(ls);
- unlink(path);
+ close(ls);
+ unlink(path);
}
void remove_connection(int index, int *connections, int *nconnections)
{
- int i;
- for(i=index; i<(*nconnections-1); ++i)
- {
- connections[i] = connections[i+1];
- }
- (*nconnections)--;
+ int i;
+ for(i=index; i<(*nconnections-1); ++i)
+ {
+ connections[i] = connections[i+1];
+ }
+ (*nconnections)--;
}
enum Break
client_read(int index, int *connections, int *nconnections)
{
- struct msg data;
- int s;
- int res;
+ struct msg data;
+ int s;
+ int res;
- s = connections[index];
+ s = connections[index];
- /* Read the message */
- res = read(s, &data, sizeof(data));
- assert(res != -1);
- if (res == 0)
- {
- close(s);
- remove_connection(index, connections, nconnections);
- return NOBREAK;
- }
+ /* Read the message */
+ res = read(s, &data, sizeof(data));
+ assert(res != -1);
+ if (res == 0)
+ {
+ close(s);
+ remove_connection(index, connections, nconnections);
+ return NOBREAK;
+ }
- printf("recv. data.type = %i\n", data.type);
+ printf("recv. data.type = %i\n", data.type);
- /* Process message */
- if (data.type == KILL)
- return BREAK; /* break in the parent*/
+ /* Process message */
+ if (data.type == KILL)
+ return BREAK; /* break in the parent*/
- return NOBREAK; /* normal */
+ return NOBREAK; /* normal */
}
--- a/server_start.c Wed Mar 21 20:59:03 2007 +0100
+++ b/server_start.c Wed Mar 21 21:01:36 2007 +0100
@@ -8,68 +8,68 @@
int try_connect(int s)
{
- struct sockaddr_un addr;
- int res;
+ struct sockaddr_un addr;
+ int res;
- addr.sun_family = AF_UNIX;
- strcpy(addr.sun_path, "/tmp/prova.socket");
+ addr.sun_family = AF_UNIX;
+ strcpy(addr.sun_path, "/tmp/prova.socket");
- res = connect(s, (struct sockaddr *) &addr, sizeof(addr));
- return res;
+ res = connect(s, (struct sockaddr *) &addr, sizeof(addr));
+ return res;
}
void wait_server_up()
{
- printf("Wait server up\n");
- sleep(1);
+ printf("Wait server up\n");
+ sleep(1);
}
void fork_server()
{
- int pid;
+ int pid;
- /* !!! stdin/stdout */
+ /* !!! stdin/stdout */
- pid = fork();
- switch (pid)
- {
- case 0: /* Child */
- server_main();
- break;
- case -1: /* Error */
- return;
- default: /* Parent */
- ;
- }
+ pid = fork();
+ switch (pid)
+ {
+ case 0: /* Child */
+ server_main();
+ break;
+ case -1: /* Error */
+ return;
+ default: /* Parent */
+ ;
+ }
}
int ensure_server_up()
{
- int res;
+ int res;
- server_socket = socket(PF_UNIX, SOCK_STREAM, 0);
- assert(server_socket != -1);
+ server_socket = socket(PF_UNIX, SOCK_STREAM, 0);
+ assert(server_socket != -1);
- res = try_connect(server_socket);
+ res = try_connect(server_socket);
- /* Good connection */
- if (res == 0)
- return 1;
+ /* Good connection */
+ if (res == 0)
+ return 1;
- /* If error other than "No one listens on the other end"... */
- if (errno != ENOENT)
- return 0;
+ /* If error other than "No one listens on the other end"... */
+ if (errno != ENOENT)
+ return 0;
- /* Try starting the server */
- fork_server();
- wait_server_up();
- res = try_connect(server_socket);
+ /* Try starting the server */
+ fork_server();
+ wait_server_up();
+ res = try_connect(server_socket);
- /* The second time didn't work. Abort. */
- if (res == -1)
- return 0;
+ /* The second time didn't work. Abort. */
+ if (res == -1)
+ return 0;
- printf("Good connection 2\n");
- /* Good connection on the second time */
- return 1;
+ printf("Good connection 2\n");
+ /* Good connection on the second time */
+ return 1;
}