a96b9b35b3e7a8070dc24cff811c3f5ee9c1cf45
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

1) /* BlinkenLib
Christian Heimke BlinkenLib v.0.5.1 (2005-12...

Christian Heimke authored 13 years ago

2)  * version 0.5.1 date 2005-12-14
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

3)  * Copyright 2004-2005 Stefan Schuermans <1stein@schuermans.info>
4)  * Copyleft: GNU public license - http://www.gnu.org/copyleft/gpl.html
5)  * a blinkenarea.org project
6)  */
7) 
8) #include <stdio.h>
9) #include <stdlib.h>
10) #include <string.h>
Christian Heimke BlinkenLib v.0.5.1 (2005-12...

Christian Heimke authored 13 years ago

11) #include <unistd.h>
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

12) #include <sys/types.h>
13) #include <sys/socket.h>
14) #include <netinet/in.h>
15) #include <arpa/inet.h>
16) 
17) #include "BlinkenLib.h"
18) 
19) int main( int argCnt, char * * args )
20) {
21)   int i, udpSocket, connected;
22)   etBlinkenProto proto;
Christian Heimke BlinkenLib v.0.4.1 (2005-12...

Christian Heimke authored 13 years ago

23)   unsigned int send_cnt, loop_cnt, loop, ui;
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

24)   char txt[64];
25)   unsigned short port;
26)   struct sockaddr_in addr;
27)   stBlinkenMovie * pMovie;
28) 
29)   //print info
30)   printf( "BlinkenLib - BlinkenSend\n"
Christian Heimke BlinkenLib v.0.5.1 (2005-12...

Christian Heimke authored 13 years ago

31)           "version 0.5.1 date 2005-12-14\n"
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

32)           "Copyright 2004-2005 Stefan Schuermans <1stein@schuermans.info>\n"
33)           "Copyleft: GNU public license - http://www.gnu.org/copyleft/gpl.html\n"
Christian Heimke BlinkenLib v.0.5 (2005-12-06)

Christian Heimke authored 13 years ago

34)           "a blinkenarea.org project\n\n" );
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

35) 
36)   //print syntax
37)   if( argCnt <= 1 )
38)   {
39)     printf( "syntax: %s <parameter> [...]\n\n"
40)             "parameters:\n"
41)             "  -s [<ip>:]<port>\n"
42)             "     source address (defaults to 0.0.0.0:0)\n"
43)             "     must occur before -d and -i, may only occur once\n"
44)             "  -d <ip>[:<port>]\n"
45)             "     destination addess (defaults to 127.0.0.1:2323)\n"
46)             "  -p [BLP|EBLP|MCUF]\n"
47)             "     protocol to use (defaults to MCUF)\n"
Christian Heimke BlinkenLib v.0.4.1 (2005-12...

Christian Heimke authored 13 years ago

48)             "  -n <number>\n"
49)             "     set number of times to send movies (defaults to 1)\n"
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

50)             "  -i <file>\n"
Christian Heimke BlinkenLib v.0.4.1 (2005-12...

Christian Heimke authored 13 years ago

51)             "     read movie from file (*.blm, *.bmm, *.bml, *.bbm) and send it\n"
52)             "  -l <number>\n"
53)             "     loop number of times (defaults to 1, use 0 for forever)\n\n",
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

54)             args[0] );
55)     return 0;
56)   }
57) 
58)   //create udp socket
59)   udpSocket = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
60)   if( udpSocket == -1 )
61)   {
62)     printf( "cannot create UDP socket\n" );
63)     return -1;
64)   }
65) 
Christian Heimke BlinkenLib v.0.4.1 (2005-12...

Christian Heimke authored 13 years ago

66)   //loop
67)   loop_cnt = 1;
68)   for( loop = 0; loop < loop_cnt || loop_cnt == 0; loop++ )
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

69)   {
70) 
Christian Heimke BlinkenLib v.0.4.1 (2005-12...

Christian Heimke authored 13 years ago

71)     //print loop message
72)     if( loop_cnt > 1 )
73)       printf( "--- loop %u/%u ---\n", loop + 1, loop_cnt );
74)     if( loop_cnt == 0 )
75)      printf( "--- loop %u ---\n", loop + 1 );
76) 
77)     //process parameters
78)     connected = 0;
79)     proto = BlinkenProtoMcuf;
80)     send_cnt = 1;
81)     for( i = 1; i < argCnt; i++ )
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

82)     {
Christian Heimke BlinkenLib v.0.4.1 (2005-12...

Christian Heimke authored 13 years ago

83) 
84)       //source address
85)       if( strcmp( args[i], "-s" ) == 0 )
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

86)       {
Christian Heimke BlinkenLib v.0.4.1 (2005-12...

Christian Heimke authored 13 years ago

87)         if( i + 1 < argCnt )
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

88)         {
Christian Heimke BlinkenLib v.0.4.1 (2005-12...

Christian Heimke authored 13 years ago

89)           i++;
90)           if( sscanf( args[i], "%32[0-9.]:%hu", txt, &port ) == 2 )
91)           {
92)             addr.sin_family = AF_INET;
93)             addr.sin_port = htons( port );
94)             addr.sin_addr.s_addr = inet_addr( txt );
95)             if( bind( udpSocket, (struct sockaddr *)&addr, sizeof( addr ) ) != 0 )
96)               printf( "could not set source address to \"%s\"\n", args[i] );
97)           }
98)           else if( sscanf( args[i], "%hu", &port ) == 1 )
99)           {
100)             addr.sin_family = AF_INET;
101)             addr.sin_port = htons( port );
102)             addr.sin_addr.s_addr = htonl( INADDR_ANY );
103)             if( bind( udpSocket, (struct sockaddr *)&addr, sizeof( addr ) ) != 0 )
104)               printf( "could not set source address to \"%s\"\n", args[i] );
105)           }
106)           else
107)             printf( "invalid source address \"%s\"\n", args[i] );
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

108)         }
109)         else
Christian Heimke BlinkenLib v.0.4.1 (2005-12...

Christian Heimke authored 13 years ago

110)           printf( "missing source address for \"-s\"\n" );
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

111)       }
112) 
Christian Heimke BlinkenLib v.0.4.1 (2005-12...

Christian Heimke authored 13 years ago

113)       //destination address
114)       else if( strcmp( args[i], "-d" ) == 0 )
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

115)       {
Christian Heimke BlinkenLib v.0.4.1 (2005-12...

Christian Heimke authored 13 years ago

116)         if( i + 1 < argCnt )
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

117)         {
Christian Heimke BlinkenLib v.0.4.1 (2005-12...

Christian Heimke authored 13 years ago

118)           i++;
119)           if( sscanf( args[i], "%32[0-9.]:%hu", txt, &port ) == 2 )
120)           {
121)             addr.sin_family = AF_INET;
122)             addr.sin_port = htons( port );
123)             addr.sin_addr.s_addr = inet_addr( txt );
124)             if( connect( udpSocket, (struct sockaddr *)&addr, sizeof( addr ) ) != 0 )
125)               printf( "could not set destination address to \"%s\"\n", args[i] );
126)             else
127)               connected = 1;
128)           }
129)           else if( sscanf( args[i], "%32[0-9.]", txt ) == 1 )
130)           {
131)             addr.sin_family = AF_INET;
132)             addr.sin_port = htons( 2323 );
133)             addr.sin_addr.s_addr = inet_addr( txt );
134)             if( connect( udpSocket, (struct sockaddr *)&addr, sizeof( addr ) ) != 0 )
135)               printf( "could not set destination address to \"%s\"\n", args[i] );
136)             else
137)               connected = 1;
138)           }
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

139)           else
Christian Heimke BlinkenLib v.0.4.1 (2005-12...

Christian Heimke authored 13 years ago

140)             printf( "invalid destination address \"%s\"\n", args[i] );
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

141)         }
Christian Heimke BlinkenLib v.0.4.1 (2005-12...

Christian Heimke authored 13 years ago

142)         else
143)           printf( "missing destination address for \"-d\"\n" );
144)       }
145) 
146)       //protocol to use
147)       else if( strcmp( args[i], "-p" ) == 0 )
148)       {
149)         if( i + 1 < argCnt )
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

150)         {
Christian Heimke BlinkenLib v.0.4.1 (2005-12...

Christian Heimke authored 13 years ago

151)           i++;
152)           if( strcasecmp( args[i], "BLP" ) == 0 )
153)             proto = BlinkenProtoBlp;
154)           else if( strcasecmp( args[i], "EBLP" ) == 0 )
155)             proto = BlinkenProtoEblp;
156)           else if( strcasecmp( args[i], "MCUF" ) == 0 )
157)             proto = BlinkenProtoMcuf;
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

158)           else
Christian Heimke BlinkenLib v.0.4.1 (2005-12...

Christian Heimke authored 13 years ago

159)             printf( "unknown protocol \"%s\"\n", args[i] );
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

160)         }
161)         else
Christian Heimke BlinkenLib v.0.4.1 (2005-12...

Christian Heimke authored 13 years ago

162)           printf( "missing protocol for \"-p\"\n" );
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

163)       }
164) 
Christian Heimke BlinkenLib v.0.4.1 (2005-12...

Christian Heimke authored 13 years ago

165)       //number of times to send movies
166)       else if( strcmp( args[i], "-n" ) == 0 )
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

167)       {
Christian Heimke BlinkenLib v.0.4.1 (2005-12...

Christian Heimke authored 13 years ago

168)         if( i + 1 < argCnt )
169)         {
170)           i++;
171)           if( sscanf( args[i], "%u", &ui ) == 1 && ui > 0 )
172)             send_cnt = ui;
173)           else
174)             printf( "invalid number \"%s\"\n", args[i] );
175)         }
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

176)         else
Christian Heimke BlinkenLib v.0.4.1 (2005-12...

Christian Heimke authored 13 years ago

177)           printf( "missing number for \"-n\"\n" );
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

178)       }
179) 
Christian Heimke BlinkenLib v.0.4.1 (2005-12...

Christian Heimke authored 13 years ago

180)       //read movie and sent it
181)       else if( strcmp( args[i], "-i" ) == 0 )
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

182)       {
Christian Heimke BlinkenLib v.0.4.1 (2005-12...

Christian Heimke authored 13 years ago

183)         if( i + 1 < argCnt )
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

184)         {
Christian Heimke BlinkenLib v.0.4.1 (2005-12...

Christian Heimke authored 13 years ago

185)           i++;
186)           if( ! connected ) //try to connect if not yet connected
187)           {
188)             printf( "no destination address to sent movie \"%s\" to,\n"
189)                     "  using default destination address \"127.0.0.1:2323\"\n", args[i] );
190)             addr.sin_family = AF_INET;
191)             addr.sin_port = htons( 2323 );
192)             addr.sin_addr.s_addr = htonl( INADDR_LOOPBACK );
193)             if( connect( udpSocket, (struct sockaddr *)&addr, sizeof( addr ) ) != 0 )
194)               printf( "could not set destination address to \"127.0.0.1:2323\"\n" );
195)             else
196)               connected = 1;
197)           }
198)           if( connected )
199)           {
200)             pMovie = BlinkenMovieLoad( args[i] );
201)             if( pMovie == NULL )
202)               printf( "could not read movie \"%s\"\n", args[i] );
203)             else
204)             {
205)               printf( "movie \"%s\" read\n", args[i] );
206)               for( ui = 0; ui < send_cnt; ui++ )
207)               {
208)                 printf( "sending movie \"%s\" (%u/%u)...\n", args[i], ui + 1, send_cnt );
209)                 BlinkenMovieSend( pMovie, udpSocket, proto );
210)                 printf( "movie \"%s\" sent\n", args[i] );
211)               }
212)               BlinkenMovieFree( pMovie );
213)             }
214)           }
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

215)           else
Christian Heimke BlinkenLib v.0.4.1 (2005-12...

Christian Heimke authored 13 years ago

216)             printf( "no destination address to sent movie \"%s\" to\n", args[i] );
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

217)         }
Christian Heimke BlinkenLib v.0.4.1 (2005-12...

Christian Heimke authored 13 years ago

218)         else
219)           printf( "missing input filename for \"-i\"\n" );
220)       }
221) 
222)       //number of times to loop
223)       else if( strcmp( args[i], "-l" ) == 0 )
224)       {
225)         if( i + 1 < argCnt )
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

226)         {
Christian Heimke BlinkenLib v.0.4.1 (2005-12...

Christian Heimke authored 13 years ago

227)           i++;
228)           if( sscanf( args[i], "%u", &ui ) == 1 )
229)             loop_cnt = ui;
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

230)           else
Christian Heimke BlinkenLib v.0.4.1 (2005-12...

Christian Heimke authored 13 years ago

231)             printf( "invalid number \"%s\"\n", args[i] );
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

232)         }
233)         else
Christian Heimke BlinkenLib v.0.4.1 (2005-12...

Christian Heimke authored 13 years ago

234)           printf( "missing number for \"-l\"\n" );
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

235)       }
Christian Heimke BlinkenLib v.0.4.1 (2005-12...

Christian Heimke authored 13 years ago

236) 
237)       //unknown parameter
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

238)       else
Christian Heimke BlinkenLib v.0.4.1 (2005-12...

Christian Heimke authored 13 years ago

239)         printf( "unknown parameter \"%s\", call without parameters to get help\n", args[i] );
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

240) 
Christian Heimke BlinkenLib v.0.4.1 (2005-12...

Christian Heimke authored 13 years ago

241)     } //for( i ...
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

242) 
Christian Heimke BlinkenLib v.0.4.1 (2005-12...

Christian Heimke authored 13 years ago

243)   } //for( loop ...