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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

30)           "version 0.5 date 2005-12-06\n"
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

31)           "Copyright 2004-2005 Stefan Schuermans <1stein@schuermans.info>\n"
32)           "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

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

47)             "  -n <number>\n"
48)             "     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

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

Christian Heimke authored 13 years ago

50)             "     read movie from file (*.blm, *.bmm, *.bml, *.bbm) and send it\n"
51)             "  -l <number>\n"
52)             "     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

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

Christian Heimke authored 13 years ago

65)   //loop
66)   loop_cnt = 1;
67)   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

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

215)             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

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

238)         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

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

242)   } //for( loop ...