Skip to content

Commit e2e11d8

Browse files
committed
test_server: add options for verbose output
* -v produce verbose output * -T print the list of top players
1 parent 2561c87 commit e2e11d8

File tree

1 file changed

+51
-5
lines changed

1 file changed

+51
-5
lines changed

test_server.c

+51-5
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,49 @@ static int port=DEFAULT_PORT;
2929
static struct sockaddr_in server;
3030
static char *name=NULL;
3131
static int ttime=1;
32+
static int verbose=0; /* whether to print a verbose message */
33+
static int toplist=0; /* whether to print the list of top players */
3234
int fd;
3335

3436

37+
/* print info based on the recieved packet */
38+
static void print_info (char * packet)
39+
{
40+
int n_pl; /* number of players */
41+
42+
n_pl=get_int(packet+1);
43+
if (verbose) {
44+
if (n_pl)
45+
printf ((n_pl>1)? "There are %d players on the server.\n"
46+
: "There is %d player on the server.\n", n_pl);
47+
else
48+
printf ("This server is not being used.\n");
49+
} else
50+
printf("%d\n",n_pl);
51+
if (toplist && n_pl) {
52+
unsigned int n_tp=*(packet+5); /* number of players on top list */
53+
54+
if (!n_tp)
55+
printf("The top list is empty.\n");
56+
else {
57+
unsigned int i; /* iterator */
58+
/* pointer to the next set of player-specific info */
59+
char * p = packet+6;
60+
61+
packet[256-1]='\0';
62+
printf ("Players on top list (%d):\n", n_tp);
63+
printf ("rank\tfrags\tdeaths\tcolor\tname\n");
64+
for (i=0; i<n_tp; i++) {
65+
printf ("#%d\t%d\t%d\t%d\t%s\n",
66+
i+1, get_int(p), get_int(p+4),
67+
*(p+8), p+9);
68+
p+=10+strlen(p+9);
69+
}
70+
}
71+
}
72+
}
73+
74+
3575
/* find server address from name and put it into server structure */
3676
static void find_server(void)
3777
{
@@ -58,7 +98,6 @@ static void init_socket(void)
5898
static int contact_server(void)
5999
{
60100
static char packet[256];
61-
int a;
62101

63102
fd_set fds;
64103
struct timeval tv;
@@ -80,8 +119,7 @@ static int contact_server(void)
80119
}
81120

82121
if ((*packet)!=P_INFO){fprintf(stderr,"Server error.\n");return 1;}
83-
a=get_int(packet+1);
84-
printf("%d\n",a);
122+
print_info(packet);
85123
return 0;
86124
}
87125

@@ -92,7 +130,9 @@ static void print_help(void)
92130
{
93131
printf( "0verkill server testing program.\n"
94132
"(c)2000 Brainsoft\n"
95-
"Usage: test_server [-h] [-p <port number>] [-t <timeout>] -a <address>\n");
133+
"Usage: test_server [-hvT] [-t <timeout>] [-p <port number>] -a <address>\n"
134+
" -v produce verbose output\n"
135+
" -T print the list of top players (implies -v)\n");
96136
}
97137

98138

@@ -117,10 +157,16 @@ static void parse_command_line(int argc,char **argv)
117157
{
118158
int a;
119159

120-
while((a=getopt(argc,argv,"hp:a:t:")) != -1)
160+
while((a=getopt(argc,argv,"hvTp:a:t:")) != -1)
121161
{
122162
switch(a)
123163
{
164+
case 'T':
165+
toplist = 1;
166+
case 'v':
167+
verbose = 1;
168+
break;
169+
124170
case 'a':
125171
name=optarg;
126172
break;

0 commit comments

Comments
 (0)