-
Notifications
You must be signed in to change notification settings - Fork 2
/
udpin.cc
127 lines (102 loc) · 3.66 KB
/
udpin.cc
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
/*
* udpin.cc
*
* Copyright (C) 2016 Mikael Djurfeldt <[email protected]>
*
* music-osc 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 3 of the License, or
* (at your option) any later version.
*
* libneurosim 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/>.
*
*/
#include <mpi.h>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
#include <stdexcept>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <music.hh>
#include "OurUDPProtocol.hh"
// This is the modeled time delay. We maintain
// t == UDP timestamp + DELAY.
#define DELAY OurUDPProtocol::TIMESTEP
struct OurUDPProtocol::toMusicPackage buffer;
struct OurUDPProtocol::startPackage startBuffer;
int
main (int argc, char* argv[])
{
MUSIC::Setup* setup = new MUSIC::Setup (argc, argv);
MPI::Intracomm comm = setup->communicator ();
if (comm.Get_size () > 1) {
std::cerr << "udpin: needs to run in a single MPI process\n";
exit (1);
}
MUSIC::ContOutputPort* keyPort =
setup->publishContOutput ("out");
// Declare what data we have to export
MUSIC::ArrayData dataMap (&buffer.keysPressed,
MPI_DOUBLE,
0,
OurUDPProtocol::KEYBOARDSIZE);
keyPort->map (&dataMap, 1); // buffer only one tick
MUSIC::ContOutputPort* commandPort =
setup->publishContOutput ("commands");
// Declare data to export
MUSIC::ArrayData commandMap (&buffer.commandKeys,
MPI_DOUBLE,
0,
OurUDPProtocol::COMMANDKEYS);
commandPort->map (&commandMap, 1); // buffer only one tick
double stoptime;
if (!setup->config ("stoptime", &stoptime)) {
std::cerr << "stoptime must be set in the MUSIC config file" << std::endl;
exit(1);
}
// Setup socket
int udpSocket;
struct sockaddr_in udpAddress;
unsigned int udpAddressSize=sizeof(udpAddress);
if ((udpSocket = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
throw std::runtime_error ("udpin: couldn't create socket");
memset((char *) &udpAddress, 0, sizeof(udpAddress));
udpAddress.sin_family = AF_INET;
udpAddress.sin_port = htons (OurUDPProtocol::TOMUSICPORT);
if (inet_aton (OurUDPProtocol::MIDISERVER_IP, &udpAddress.sin_addr) == 0)
throw std::runtime_error ("udpout: inet_aton() failed");
startBuffer.magicNumber = OurUDPProtocol::MAGICTOMUSIC;
startBuffer.stopTime = stoptime;
if (sendto (udpSocket, &startBuffer, sizeof(startBuffer), 0,
(struct sockaddr *) &udpAddress, udpAddressSize)
== -1)
throw std::runtime_error ("udpin: failed to send start message");
// Not really necessary since static memory is zero from start
std::fill(buffer.keysPressed.begin(), buffer.keysPressed.end(), 0.0);
MUSIC::Runtime* runtime = new MUSIC::Runtime (setup, OurUDPProtocol::TIMESTEP);
// Simulation loop
for (; runtime->time () < stoptime; runtime->tick ()) {
double timeStamp;
do {
if (recvfrom (udpSocket, &buffer, sizeof(buffer), 0,
(struct sockaddr *) &udpAddress, &udpAddressSize)
== -1)
std::runtime_error ("udpin: recvfrom()");
timeStamp = buffer.timestamp;
} while (timeStamp + DELAY < runtime->time ());
}
close (udpSocket);
runtime->finalize ();
delete runtime;
return 0;
}