-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmlSender.c
141 lines (118 loc) · 3.9 KB
/
xmlSender.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* This is the main program for clients that send records in XML format.
* They read from files on disk that contain sleeptimes and records in XML format.
*
* *** DO NOT CHANGE THIS FILE ***
*/
#include "xmlfile.h"
#include "connection.h"
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static void usage( char* cmd )
{
fprintf( stderr, "Usage: %s <id> <proxyport>\n"
" <id> - a capital ASCII letter (A, B, C, ...) giving this node a unique identity\n"
" <proxyip> - the proxy's IP address in dotted decimal format (e.g. 127.0.0.1)\n"
" <proxyport> - a 16-bit integer in host byte order identifying the proxy's port\n"
"\n",
cmd );
exit( -1 );
}
static char getId( char* arg );
static char* makeFilename( char myid );
static int getPort( char* arg );
int main( int argc, char* argv[] )
{
char* proxyip;
int proxyport;
int sock;
int ct;
int err;
char* filename;
FILE* file;
char line[10000];
char myid;
unsigned int sleeptime;
if( argc != 4 ) usage( argv[0] );
myid = getId( argv[1] );
filename = makeFilename( myid );
proxyip = argv[2];
proxyport = getPort( argv[3] );
sock = tcp_connect( proxyip, proxyport );
if( sock < 0 ) exit( -1 );
/* Send one byte to the proxy to identify this sender as an XML sender. */
err = tcp_write( sock, "X", 1 );
if( err < 0 ) exit( -1 );
fprintf( stderr, "%s:%d %s %c has sent %d bytes to proxy successfully\n", __FILE__, __LINE__, argv[0], myid, err );
/* Send one byte containing the sender's ID to the proxy. */
err = tcp_write( sock, &myid, 1 );
if( err < 0 ) exit( -1 );
fprintf( stderr, "%s:%d %s %c has sent %d bytes to proxy successfully\n", __FILE__, __LINE__, argv[0], myid, err );
file = xml_read_open( filename );
if( file == NULL ) exit( -1 );
free( filename ); /* we don't need the name any more */
/* Loop and read sleeptimes and records from the text file until the end of file. */
do
{
ct = xml_read( file, line, 10000 );
if( ct > 0 )
{
fprintf( stderr, "%s:%d %s %c has read %d bytes from file\n", __FILE__, __LINE__, argv[0], myid, ct );
err = sscanf( line, "<sleep=\"%u\" />", &sleeptime );
if( err == 1 )
{
fprintf( stderr, "%s:%d read sleep command, sleeping for %d seconds\n", __FILE__, __LINE__, sleeptime );
sleep( sleeptime );
}
else
{
err = tcp_write_loop( sock, line, ct );
if( err < 0 ) exit( -1 );
fprintf( stderr, "%s:%d %s %c has sent %d bytes to proxy successfully\n", __FILE__, __LINE__, argv[0], myid, err );
}
}
}
while( ct > 0 );
xml_close( file );
tcp_close( sock );
return 0;
}
static char getId( char* arg )
{
char id;
if( strlen( arg ) < 1 )
{
fprintf( stderr, "%s:%d The id cannot be shorter than one byte.\n", __FILE__, __LINE__ );
exit( -1 );
}
id = arg[0];
if( id < 'A' || id > 'Z' )
{
fprintf( stderr, "%s:%d The id must be a capital ASCII letter, but it is %c\n", __FILE__, __LINE__, id );
exit( -1 );
}
return id;
}
static char* makeFilename( char myid )
{
char* filename;
filename = malloc( 6 );
if( filename == NULL )
{
fprintf( stderr, "%s:%d Could not allocate 6 bytes for the filename\n", __FILE__, __LINE__ );
exit( -1 );
}
sprintf( filename, "%c.xml", myid );
return filename;
}
static int getPort( char* arg )
{
int port = atoi( arg );
if( port < 1024 || port > 65535 )
{
fprintf( stderr, "%s:%d The proxy port %d is not in the valid range.\n", __FILE__, __LINE__, port );
exit( -1 );
}
return port;
}