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

Christian Heimke authored 13 years ago

1) /* BlinkenLib
Christian Heimke BlinkenLib v.0.5.4 (2008-01...

Christian Heimke authored 13 years ago

2)  * version 0.5.4 date 2008-01-10
3)  * Copyright 2004-2008 Stefan Schuermans <stefan@schuermans.info>
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

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.2 (2006-05...

Christian Heimke authored 13 years ago

11) #ifdef WIN32
12) #include <winsock2.h>
13) #define close closesocket
14) #define strcasecmp stricmp
15) #else
Christian Heimke BlinkenLib v.0.5.1 (2005-12...

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

17) #include <sys/types.h>
18) #include <sys/socket.h>
19) #include <netinet/in.h>
20) #include <arpa/inet.h>
Christian Heimke BlinkenLib v.0.5.2 (2006-05...

Christian Heimke authored 13 years ago

21) #endif
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

22) 
23) #include "BlinkenLib.h"
24) 
25) int main( int argCnt, char * * args )
26) {
Christian Heimke BlinkenLib v.0.5.2 (2006-05...

Christian Heimke authored 13 years ago

27)   int i, connected;
28)   SOCKET udpSocket;
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

29)   etBlinkenProto proto;
Christian Heimke BlinkenLib v.0.5.4 (2008-01...

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

31)   char txt[64];
32)   unsigned short port;
33)   struct sockaddr_in addr;
34)   stBlinkenMovie * pMovie;
35) 
36)   //print info
37)   printf( "BlinkenLib - BlinkenSend\n"
Christian Heimke BlinkenLib v.0.5.4 (2008-01...

Christian Heimke authored 13 years ago

38)           "version 0.5.4 date 2008-01-10\n"
39)           "Copyright 2004-2008 Stefan Schuermans <stefan@schuermans.info>\n"
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

40)           "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

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

Christian Heimke authored 13 years ago

42) 
43)   //print syntax
44)   if( argCnt <= 1 )
45)   {
46)     printf( "syntax: %s <parameter> [...]\n\n"
47)             "parameters:\n"
48)             "  -s [<ip>:]<port>\n"
49)             "     source address (defaults to 0.0.0.0:0)\n"
50)             "     must occur before -d and -i, may only occur once\n"
51)             "  -d <ip>[:<port>]\n"
52)             "     destination addess (defaults to 127.0.0.1:2323)\n"
53)             "  -p [BLP|EBLP|MCUF]\n"
54)             "     protocol to use (defaults to MCUF)\n"
Christian Heimke BlinkenLib v.0.5.4 (2008-01...

Christian Heimke authored 13 years ago

55)             "  -t <milliseconds>\n"
56)             "     set maximum idle time between frames (defaults to 3000, use 0 for infinite)\n"
Christian Heimke BlinkenLib v.0.4.1 (2005-12...

Christian Heimke authored 13 years ago

57)             "  -n <number>\n"
58)             "     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

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

Christian Heimke authored 13 years ago

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

63)             args[0] );
64)     return 0;
65)   }
66) 
Christian Heimke BlinkenLib v.0.5.2 (2006-05...

Christian Heimke authored 13 years ago

67) #ifdef WIN32
68)   {
69)     WSADATA WsaData;
70)     WSAStartup( 0x0101, &WsaData );
71)   }
72) #endif
73) 
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

74)   //create udp socket
75)   udpSocket = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
Christian Heimke BlinkenLib v.0.5.2 (2006-05...

Christian Heimke authored 13 years ago

76)   if( udpSocket == INVALID_SOCKET )
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

77)   {
78)     printf( "cannot create UDP socket\n" );
Christian Heimke BlinkenLib v.0.5.2 (2006-05...

Christian Heimke authored 13 years ago

79) #ifdef WIN32
80)     WSACleanup( );
81) #endif
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

82)     return -1;
83)   }
84) 
Christian Heimke BlinkenLib v.0.4.1 (2005-12...

Christian Heimke authored 13 years ago

85)   //loop
86)   loop_cnt = 1;
87)   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

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

Christian Heimke authored 13 years ago

90)     //print loop message
91)     if( loop_cnt > 1 )
92)       printf( "--- loop %u/%u ---\n", loop + 1, loop_cnt );
93)     if( loop_cnt == 0 )
94)      printf( "--- loop %u ---\n", loop + 1 );
95) 
96)     //process parameters
97)     connected = 0;
98)     proto = BlinkenProtoMcuf;
Christian Heimke BlinkenLib v.0.5.4 (2008-01...

Christian Heimke authored 13 years ago

99)     maxidle = 3000;
Christian Heimke BlinkenLib v.0.4.1 (2005-12...

Christian Heimke authored 13 years ago

100)     send_cnt = 1;
101)     for( i = 1; i < argCnt; i++ )
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

109)           i++;
110)           if( sscanf( args[i], "%32[0-9.]:%hu", txt, &port ) == 2 )
111)           {
112)             addr.sin_family = AF_INET;
113)             addr.sin_port = htons( port );
114)             addr.sin_addr.s_addr = inet_addr( txt );
115)             if( bind( udpSocket, (struct sockaddr *)&addr, sizeof( addr ) ) != 0 )
116)               printf( "could not set source address to \"%s\"\n", args[i] );
117)           }
118)           else if( sscanf( args[i], "%hu", &port ) == 1 )
119)           {
120)             addr.sin_family = AF_INET;
121)             addr.sin_port = htons( port );
122)             addr.sin_addr.s_addr = htonl( INADDR_ANY );
123)             if( bind( udpSocket, (struct sockaddr *)&addr, sizeof( addr ) ) != 0 )
124)               printf( "could not set source address to \"%s\"\n", args[i] );
125)           }
126)           else
127)             printf( "invalid source address \"%s\"\n", args[i] );
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

128)         }
129)         else
Christian Heimke BlinkenLib v.0.4.1 (2005-12...

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

138)           i++;
139)           if( sscanf( args[i], "%32[0-9.]:%hu", txt, &port ) == 2 )
140)           {
141)             addr.sin_family = AF_INET;
142)             addr.sin_port = htons( port );
143)             addr.sin_addr.s_addr = inet_addr( txt );
144)             if( connect( udpSocket, (struct sockaddr *)&addr, sizeof( addr ) ) != 0 )
145)               printf( "could not set destination address to \"%s\"\n", args[i] );
146)             else
147)               connected = 1;
148)           }
149)           else if( sscanf( args[i], "%32[0-9.]", txt ) == 1 )
150)           {
151)             addr.sin_family = AF_INET;
152)             addr.sin_port = htons( 2323 );
153)             addr.sin_addr.s_addr = inet_addr( txt );
154)             if( connect( udpSocket, (struct sockaddr *)&addr, sizeof( addr ) ) != 0 )
155)               printf( "could not set destination address to \"%s\"\n", args[i] );
156)             else
157)               connected = 1;
158)           }
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( "invalid destination address \"%s\"\n", args[i] );
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

162)         else
163)           printf( "missing destination address for \"-d\"\n" );
164)       }
165) 
166)       //protocol to use
167)       else if( strcmp( args[i], "-p" ) == 0 )
168)       {
169)         if( i + 1 < argCnt )
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

171)           i++;
172)           if( strcasecmp( args[i], "BLP" ) == 0 )
173)             proto = BlinkenProtoBlp;
174)           else if( strcasecmp( args[i], "EBLP" ) == 0 )
175)             proto = BlinkenProtoEblp;
176)           else if( strcasecmp( args[i], "MCUF" ) == 0 )
177)             proto = BlinkenProtoMcuf;
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

183)       }
184) 
Christian Heimke BlinkenLib v.0.5.4 (2008-01...

Christian Heimke authored 13 years ago

185)       //maximum idle time
186)       else if( strcmp( args[i], "-t" ) == 0 )
187)       {
188)         if( i + 1 < argCnt )
189)         {
190)           i++;
191)           if( sscanf( args[i], "%u", &ui ) == 1 )
192)             maxidle = ui;
193)           else
194)             printf( "invalid maximum idle time value \"%s\"\n", args[i] );
195)         }
196)         else
197)           printf( "missing maximum idle time value for \"-t\"\n" );
198)       }
199) 
Christian Heimke BlinkenLib v.0.4.1 (2005-12...

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

203)         if( i + 1 < argCnt )
204)         {
205)           i++;
206)           if( sscanf( args[i], "%u", &ui ) == 1 && ui > 0 )
207)             send_cnt = ui;
208)           else
209)             printf( "invalid number \"%s\"\n", args[i] );
210)         }
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

213)       }
214) 
Christian Heimke BlinkenLib v.0.5.4 (2008-01...

Christian Heimke authored 13 years ago

215)       //read movie and send it
Christian Heimke BlinkenLib v.0.4.1 (2005-12...

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

220)           i++;
221)           if( ! connected ) //try to connect if not yet connected
222)           {
223)             printf( "no destination address to sent movie \"%s\" to,\n"
224)                     "  using default destination address \"127.0.0.1:2323\"\n", args[i] );
225)             addr.sin_family = AF_INET;
226)             addr.sin_port = htons( 2323 );
227)             addr.sin_addr.s_addr = htonl( INADDR_LOOPBACK );
228)             if( connect( udpSocket, (struct sockaddr *)&addr, sizeof( addr ) ) != 0 )
229)               printf( "could not set destination address to \"127.0.0.1:2323\"\n" );
230)             else
231)               connected = 1;
232)           }
233)           if( connected )
234)           {
235)             pMovie = BlinkenMovieLoad( args[i] );
236)             if( pMovie == NULL )
237)               printf( "could not read movie \"%s\"\n", args[i] );
238)             else
239)             {
240)               printf( "movie \"%s\" read\n", args[i] );
241)               for( ui = 0; ui < send_cnt; ui++ )
242)               {
243)                 printf( "sending movie \"%s\" (%u/%u)...\n", args[i], ui + 1, send_cnt );
Christian Heimke BlinkenLib v.0.5.4 (2008-01...

Christian Heimke authored 13 years ago

244)                 BlinkenMovieSend( pMovie, udpSocket, proto, maxidle );
Christian Heimke BlinkenLib v.0.4.1 (2005-12...

Christian Heimke authored 13 years ago

245)                 printf( "movie \"%s\" sent\n", args[i] );
246)               }
247)               BlinkenMovieFree( pMovie );
248)             }
249)           }
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

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

Christian Heimke authored 13 years ago

253)         else
254)           printf( "missing input filename for \"-i\"\n" );
255)       }
256) 
257)       //number of times to loop
258)       else if( strcmp( args[i], "-l" ) == 0 )
259)       {
260)         if( i + 1 < argCnt )
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

267)         }
268)         else
Christian Heimke BlinkenLib v.0.4.1 (2005-12...

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

271) 
272)       //unknown parameter
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

278)   } //for( loop ...
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

279) 
280)   //close socket
281)   close( udpSocket );
282) 
Christian Heimke BlinkenLib v.0.5.2 (2006-05...

Christian Heimke authored 13 years ago

283) #ifdef WIN32
284)   WSACleanup( );
285) #endif