35 #include "goby/acomms/acomms_constants.h" 36 #include "goby/util/as.h" 37 #include "goby/util/linebasedcomms.h" 39 std::map<int, std::string> modem_id2endpoint;
41 void parse_in(
const std::string& in, std::map<std::string, std::string>* out);
43 int main(
int argc,
char* argv[])
47 std::cout <<
"usage: abc_modem_simulator [tcp listen port]" << std::endl;
56 while (server.active())
59 while (server.readline(&in))
62 boost::trim(*in.mutable_data());
64 std::cout <<
"Received: " << in.ShortDebugString() << std::endl;
66 std::map<std::string, std::string> parsed;
69 parse_in(in.data(), &parsed);
70 if (parsed[
"KEY"] ==
"CONF")
72 std::cout <<
"Got configuration: " << in.data() << std::endl;
75 if (parsed.count(
"MAC"))
77 int mac = goby::util::as<int>(parsed[
"MAC"]);
78 std::cout <<
"Set MAC address " << mac <<
" for endpoint " << in.src()
80 modem_id2endpoint[mac] = in.src();
83 else if (parsed[
"KEY"] ==
"SEND")
85 std::cout <<
"Got send: " << in.data() << std::endl;
88 out.set_src(server.local_endpoint());
90 if (!parsed.count(
"HEX"))
91 throw(std::runtime_error(
"No DATA in SEND message"));
93 if (!parsed.count(
"FROM"))
94 throw(std::runtime_error(
"No FROM in SEND message"));
96 if (!parsed.count(
"BITRATE"))
97 throw(std::runtime_error(
"No BITRATE in SEND message"));
99 int src = goby::util::as<int>(parsed[
"FROM"]);
101 if (parsed.count(
"TO"))
103 int dest = goby::util::as<int>(parsed[
"TO"]);
105 std::stringstream out_ss;
106 out_ss <<
"RECV,FROM:" << src <<
",TO:" << dest <<
",HEX:" << parsed[
"HEX"]
107 <<
",BITRATE:" << parsed[
"BITRATE"] <<
"\r\n";
108 out.set_data(out_ss.str());
112 typedef std::map<int, std::string>::const_iterator const_iterator;
113 for (const_iterator it = modem_id2endpoint.begin(),
114 n = modem_id2endpoint.end();
118 if (it->first != src)
120 out.set_dest(it->second);
121 std::cout <<
"Sending: " << out.ShortDebugString() << std::endl;
128 if (!modem_id2endpoint.count(dest))
129 throw(std::runtime_error(
"Unknown destination ID " +
130 goby::util::as<std::string>(dest)));
132 out.set_dest(modem_id2endpoint[dest]);
133 std::cout <<
"Sending: " << out.ShortDebugString() << std::endl;
136 if (parsed.count(
"ACK") && goby::util::as<bool>(parsed[
"ACK"]))
138 out.set_dest(in.src());
140 std::stringstream out_ss;
141 out_ss <<
"ACKN,FROM:" << dest <<
",TO:" << src <<
"\r\n";
142 out.set_data(out_ss.str());
143 std::cout <<
"Sending: " << out.ShortDebugString() << std::endl;
150 throw(std::runtime_error(
"No TO in SEND message"));
153 catch (std::exception& e)
155 std::cout <<
"Invalid line from modem: " << in.data() << std::endl;
156 std::cout <<
"Why: " << e.what() << std::endl;
163 std::cout <<
"server failed..." << std::endl;
167 void parse_in(
const std::string& in, std::map<std::string, std::string>* out)
169 std::vector<std::string> comma_split;
170 boost::split(comma_split, in, boost::is_any_of(
","));
171 out->insert(std::make_pair(
"KEY", comma_split.at(0)));
172 for (
int i = 1, n = comma_split.size(); i < n; ++i)
174 std::vector<std::string> colon_split;
175 boost::split(colon_split, comma_split[i], boost::is_any_of(
":"));
176 out->insert(std::make_pair(colon_split.at(0), colon_split.at(1)));
provides a basic TCP server for line by line text based communications to a one or more remote TCP cl...
const int BROADCAST_ID
special modem id for the broadcast destination - no one is assigned this address. Analogous to 192...