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

Christian Heimke authored 13 years ago

1) /* BlinkenLib
Christian Heimke BlinkenLib v.0.5.3 (2007-12...

Christian Heimke authored 13 years ago

2)  * version 0.5.3 date 2007-12-28
3)  * Copyright 2004-2007 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.4.1 (2005-12...

Christian Heimke authored 13 years ago

30)   unsigned int 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.3 (2007-12...

Christian Heimke authored 13 years ago

38)           "version 0.5.3 date 2007-12-28\n"
39)           "Copyright 2004-2007 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.4.1 (2005-12...

Christian Heimke authored 13 years ago

55)             "  -n <number>\n"
56)             "     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

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

Christian Heimke authored 13 years ago

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

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

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

Christian Heimke authored 13 years ago

88)     //print loop message
89)     if( loop_cnt > 1 )
90)       printf( "--- loop %u/%u ---\n", loop + 1, loop_cnt );
91)     if( loop_cnt == 0 )
92)      printf( "--- loop %u ---\n", loop + 1 );
93) 
94)     //process parameters
95)     connected = 0;
96)     proto = BlinkenProtoMcuf;
97)     send_cnt = 1;
98)     for( i = 1; i < argCnt; i++ )
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

125)         }
126)         else
Christian Heimke BlinkenLib v.0.4.1 (2005-12...

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

159)         else
160)           printf( "missing destination address for \"-d\"\n" );
161)       }
162) 
163)       //protocol to use
164)       else if( strcmp( args[i], "-p" ) == 0 )
165)       {
166)         if( i + 1 < argCnt )
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)           i++;
169)           if( strcasecmp( args[i], "BLP" ) == 0 )
170)             proto = BlinkenProtoBlp;
171)           else if( strcasecmp( args[i], "EBLP" ) == 0 )
172)             proto = BlinkenProtoEblp;
173)           else if( strcasecmp( args[i], "MCUF" ) == 0 )
174)             proto = BlinkenProtoMcuf;
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( "unknown protocol \"%s\"\n", args[i] );
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

182)       //number of times to send movies
183)       else if( strcmp( args[i], "-n" ) == 0 )
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)         if( i + 1 < argCnt )
186)         {
187)           i++;
188)           if( sscanf( args[i], "%u", &ui ) == 1 && ui > 0 )
189)             send_cnt = ui;
190)           else
191)             printf( "invalid number \"%s\"\n", args[i] );
192)         }
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

202)           i++;
203)           if( ! connected ) //try to connect if not yet connected
204)           {
205)             printf( "no destination address to sent movie \"%s\" to,\n"
206)                     "  using default destination address \"127.0.0.1:2323\"\n", args[i] );
207)             addr.sin_family = AF_INET;
208)             addr.sin_port = htons( 2323 );
209)             addr.sin_addr.s_addr = htonl( INADDR_LOOPBACK );
210)             if( connect( udpSocket, (struct sockaddr *)&addr, sizeof( addr ) ) != 0 )
211)               printf( "could not set destination address to \"127.0.0.1:2323\"\n" );
212)             else
213)               connected = 1;
214)           }
215)           if( connected )
216)           {
217)             pMovie = BlinkenMovieLoad( args[i] );
218)             if( pMovie == NULL )
219)               printf( "could not read movie \"%s\"\n", args[i] );
220)             else
221)             {
222)               printf( "movie \"%s\" read\n", args[i] );
223)               for( ui = 0; ui < send_cnt; ui++ )
224)               {
225)                 printf( "sending movie \"%s\" (%u/%u)...\n", args[i], ui + 1, send_cnt );
226)                 BlinkenMovieSend( pMovie, udpSocket, proto );
227)                 printf( "movie \"%s\" sent\n", args[i] );
228)               }
229)               BlinkenMovieFree( pMovie );
230)             }
231)           }
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

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

Christian Heimke authored 13 years ago

235)         else
236)           printf( "missing input filename for \"-i\"\n" );
237)       }
238) 
239)       //number of times to loop
240)       else if( strcmp( args[i], "-l" ) == 0 )
241)       {
242)         if( i + 1 < argCnt )
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)           i++;
245)           if( sscanf( args[i], "%u", &ui ) == 1 )
246)             loop_cnt = ui;
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

251)           printf( "missing number for \"-l\"\n" );
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) 
254)       //unknown parameter
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

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

Christian Heimke authored 13 years ago

261) 
262)   //close socket
263)   close( udpSocket );
264) 
Christian Heimke BlinkenLib v.0.5.2 (2006-05...

Christian Heimke authored 13 years ago

265) #ifdef WIN32
266)   WSACleanup( );
267) #endif