1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
|
/*
Copyright (c) 2008 Robin Vobruba <hoijui.quaero@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
This little tool is useful for testing the OSC Stats sender
of the Spring RTS game engine. See the README file for more info.
*/
#include "rts/lib/oscpack/OscOutboundPacketStream.h"
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
#ifdef WIN32
#include <windows.h> // for Sleep()
#else
#include <unistd.h> // for net stuff and usleep()
#endif
#include <stdlib.h> // for rand()
#include <time.h> // for time(), for initializing rand
#include <stdio.h> // for printf()
#include <string.h> // for strcmp()
static const unsigned int DEFAULT_DESTINATION_PORT = 6447;
static const char* const DEFAULT_DESTINATION_ADDRESS = "127.0.0.1";
static const char* const DEFAULT_TOPIC = "rd";
static const unsigned int OUTPUT_BUFFER_SIZE = 10240;
static inline float randFloat(float min = 0.0, float max = 10000.0)
{
float rf = rand();
rf = (float) (min + ((double)rf / RAND_MAX * (max-min)));
return rf;
}
static inline int randInt(int min = 1, int max = 100)
{
int ri = rand();
ri = (int) (min + ((double)ri / RAND_MAX * (max-min)));
return ri;
}
static void packMidiCSound(osc::OutboundPacketStream* oscPacker)
{
//printf("packing: 4i midi evt\n");
(*oscPacker)
<< osc::BeginBundleImmediate
<< osc::BeginMessage( "/midi" )
<< (int)144
<< (int)1
<< (int)75
<< (int)50
<< osc::EndMessage
<< osc::EndBundle;
return;
}
static void packRangeAndDuration(osc::OutboundPacketStream* oscPacker)
{
//printf("packing: range and duration\n");
(*oscPacker)
<< osc::BeginBundleImmediate
<< osc::BeginMessage( "/range" )
<< (float)40.0
<< (float)500.1415
<< osc::EndMessage
<< osc::BeginMessage( "/duration" )
<< 200
<< osc::EndMessage
<< osc::EndBundle;
return;
}
static void packInitDataTitles(osc::OutboundPacketStream* oscPacker)
{
//printf("packing: initial data [titles]\n");
(*oscPacker)
<< osc::BeginBundleImmediate
<< osc::BeginMessage( "/spring/info/initial/titles" )
<< "Engine name"
<< "Engine version"
<< "Number of players"
<< "Some float value"
<< osc::EndMessage
<< osc::EndBundle;
return;
}
static void packInitDataValues(osc::OutboundPacketStream* oscPacker)
{
//printf("packing: initial data [values]\n");
(*oscPacker)
<< osc::BeginBundleImmediate
<< osc::BeginMessage( "/spring/info/initial/values" )
<< "spring"
<< "0.1.abc.xzy"
<< (int) 8
<< (float) 0.123456
<< osc::EndMessage
<< osc::EndBundle;
return;
}
static void packTeamStatsTitles(osc::OutboundPacketStream* oscPacker)
{
//printf("packing: team stats [titles]\n");
(*oscPacker)
<< osc::BeginBundleImmediate
<< osc::BeginMessage( "/spring/stats/team/titles" )
<< "Team number"
<< "Metal used"
<< "Energy used"
<< "Metal produced"
<< "Energy produced"
<< "Metal excess"
<< "Energy excess"
<< "Metal received"
<< "Energy received"
<< "Metal sent"
<< "Energy sent"
<< "Metal stored"
<< "Energy stored"
<< "Active Units"
<< "Units killed"
<< "Units produced"
<< "Units died"
<< "Units received"
<< "Units sent"
<< "Units captured"
<< "Units stolen"
<< "Damage Dealt"
<< "Damage Received"
<< osc::EndMessage
<< osc::EndBundle;
return;
}
static void packTeamStatsValues(osc::OutboundPacketStream* oscPacker)
{
//printf("packing: team stats [values]\n");
(*oscPacker)
<< osc::BeginBundleImmediate
<< osc::BeginMessage( "/spring/stats/team/values" )
<< (int) randInt(0, 2)
<< (float) randFloat()
<< (float) randFloat()
<< (float) randFloat()
<< (float) randFloat()
<< (float) randFloat()
<< (float) randFloat()
<< (float) randFloat()
<< (float) randFloat()
<< (float) randFloat()
<< (float) randFloat()
<< (float) randFloat()
<< (float) randFloat()
<< (int) randInt()
<< (int) randInt()
<< (int) randInt()
<< (int) randInt()
<< (int) randInt()
<< (int) randInt()
<< (int) randInt()
<< (int) randInt()
<< (float) randFloat()
<< (float) randFloat()
<< osc::EndMessage
<< osc::EndBundle;
return;
}
static void packPlayerStatsTitles(osc::OutboundPacketStream* oscPacker)
{
//printf("packing: player stats [titles]\n");
(*oscPacker)
<< osc::BeginBundleImmediate
<< osc::BeginMessage( "/spring/stats/player/titles" )
<< "Player Name"
<< "Mouse clicks per minute"
<< "Mouse movement in pixels per minute"
<< "Keyboard presses per minute"
<< "Unit commands per minute"
<< "Average command size (units affected per command)"
<< osc::EndMessage
<< osc::EndBundle;
return;
}
static void packPlayerStatsValues(osc::OutboundPacketStream* oscPacker)
{
//printf("packing: player stats [values]\n");
(*oscPacker)
<< osc::BeginBundleImmediate
<< osc::BeginMessage( "/spring/stats/player/values" )
<< (const char*) "hoijui"
<< (float) randFloat()
<< (float) randFloat()
<< (float) randFloat()
<< (float) randFloat()
<< (float) randFloat()
<< osc::EndMessage
<< osc::EndBundle;
return;
}
static void printUsage(int argc, const char* const* argv)
{
printf("usage:\n");
printf("\t%s [destination-ip-address] [destination-port] [loop-intervall] [topic]\n", argv[0]);
printf("\n");
printf("\tdestination-ip-address default: %s\n", DEFAULT_DESTINATION_ADDRESS);
printf("\tdestination-port default: %u\n", DEFAULT_DESTINATION_PORT);
printf("\tloop-intervall time in ms between subsequent sends. default: %i, -1 means: do not loop\n", -1);
printf("\ttopic default: %s, possible values: rd, it, iv, tt, tv, pt, pv, mi\n", DEFAULT_TOPIC);
}
int main(int argc, const char* const* argv)
{
srand((unsigned int) time(NULL));
char buffer[OUTPUT_BUFFER_SIZE];
osc::OutboundPacketStream p(buffer, OUTPUT_BUFFER_SIZE);
const char* dstAddress = DEFAULT_DESTINATION_ADDRESS;
if (argc > 1) {
dstAddress = argv[1];
}
if (strcmp(dstAddress, "--help") == 0) {
printUsage(argc, argv);
return 0;
}
unsigned int dstPort = DEFAULT_DESTINATION_PORT;
if (argc > 2) {
dstPort = atoi(argv[2]);
}
int loopMilliSeconds = -1;
if (argc > 3) {
loopMilliSeconds = atoi(argv[3]);
}
static const unsigned int topicParamIndex = 4;
const char* topic = DEFAULT_TOPIC;
if (argc > 4) {
topic = argv[4];
}
printf("sending to %s:%u, sending every %ims, topic: %s ...\n",
dstAddress, dstPort, loopMilliSeconds, topic);
struct sockaddr_in destination;
destination.sin_family = AF_INET;
destination.sin_addr.s_addr = inet_addr(dstAddress);
destination.sin_port = htons(dstPort);
int sock_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
// allow broadcasting
int broadCastPerm = 1;
setsockopt(sock_fd, SOL_SOCKET, SO_BROADCAST, (void*) &broadCastPerm, sizeof(broadCastPerm));
int flags = 0;
while (true) {
if (strcmp(topic, "rd") == 0) {
packRangeAndDuration(&p);
} else if (strcmp(topic, "it") == 0) {
packInitDataTitles(&p);
} else if (strcmp(topic, "iv") == 0) {
packInitDataValues(&p);
} else if (strcmp(topic, "tt") == 0) {
packTeamStatsTitles(&p);
} else if (strcmp(topic, "tv") == 0) {
packTeamStatsValues(&p);
} else if (strcmp(topic, "pt") == 0) {
packPlayerStatsTitles(&p);
} else if (strcmp(topic, "pv") == 0) {
packPlayerStatsValues(&p);
} else if (strcmp(topic, "mi") == 0) {
packMidiCSound(&p);
} else {
printf("unknown topic: %s\n", topic);
}
if (p.IsReady()) {
sendto(sock_fd, p.Data(), p.Size(), flags,
(struct sockaddr*) &destination, sizeof(destination));
p.Clear();
}
if (loopMilliSeconds < 0) {
break;
}
#ifdef WIN32
Sleep(loopMilliSeconds);
#else
usleep(loopMilliSeconds * 1000);
#endif
}
close(sock_fd);
return 0;
}
|