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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

2)  * version 0.4.1 date 2005-12-05
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)  * powered by eventphone.de
7)  */
8) 
9) #include <stdio.h>
10) #include <stdlib.h>
11) #include <string.h>
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.4.1 (2005-12...

Christian Heimke authored 13 years ago

31)           "version 0.4.1 date 2005-12-05\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"
34)           "a blinkenarea.org project\n"
35)           "powered by eventphone.de\n\n" );
36) 
37)   //print syntax
38)   if( argCnt <= 1 )
39)   {
40)     printf( "syntax: %s <parameter> [...]\n\n"
41)             "parameters:\n"
42)             "  -s [<ip>:]<port>\n"
43)             "     source address (defaults to 0.0.0.0:0)\n"
44)             "     must occur before -d and -i, may only occur once\n"
45)             "  -d <ip>[:<port>]\n"
46)             "     destination addess (defaults to 127.0.0.1:2323)\n"
47)             "  -p [BLP|EBLP|MCUF]\n"
48)             "     protocol to use (defaults to MCUF)\n"
Christian Heimke BlinkenLib v.0.4.1 (2005-12...

Christian Heimke authored 13 years ago

49)             "  -n <number>\n"
50)             "     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

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

Christian Heimke authored 13 years ago

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

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

Christian Heimke authored 13 years ago

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

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

84) 
85)       //source address
86)       if( strcmp( args[i], "-s" ) == 0 )
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)         if( i + 1 < argCnt )
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

114)       //destination address
115)       else if( strcmp( args[i], "-d" ) == 0 )
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)         if( i + 1 < argCnt )
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

181)       //read movie and sent it
182)       else if( strcmp( args[i], "-i" ) == 0 )
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)         if( i + 1 < argCnt )
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

244)   } //for( loop ...