#include <iostream>
#include "chat.pb.h"
#include "goby/acomms/amac.h"
#include "goby/acomms/bind.h"
#include "goby/acomms/dccl.h"
#include "goby/acomms/modem_driver.h"
#include "goby/acomms/queue.h"
#include "goby/common/time.h"
#include "goby/util/as.h"
#include <boost/lexical_cast.hpp>
#include "chat_curses.h"
using goby::util::as;
int startup_failure();
std::ofstream fout_;
int my_id_;
int buddy_id_;
int main(int argc, char* argv[])
{
if (argc != 5)
return startup_failure();
std::string serial_port = argv[1];
try
{
my_id_ = boost::lexical_cast<int>(argv[2]);
buddy_id_ = boost::lexical_cast<int>(argv[3]);
}
catch (boost::bad_lexical_cast&)
{
std::cerr << "bad value for my_id: " << argv[2] << " or buddy_id: " << argv[3]
<< ". these must be unsigned integers." << std::endl;
return startup_failure();
}
std::string log_file = argv[4];
fout_.open(log_file.c_str());
if (!fout_.is_open())
{
std::cerr << "bad value for log_file: " << log_file << std::endl;
return startup_failure();
}
bind(mm_driver_, q_manager_, mac_);
dccl_->
validate<ChatMessage>();
q_manager_cfg.
set_modem_id(my_id_);
q_entry->
set_protobuf_name(
"ChatMessage");
#ifdef USE_FLEXIBLE_DATA_PACKET
#endif
src_role->
set_type(goby::acomms::protobuf::QueuedMessageEntry::SOURCE_ID);
src_role->
set_field(
"source");
dest_role->set_type(goby::acomms::protobuf::QueuedMessageEntry::DESTINATION_ID);
dest_role->set_field("destination");
driver_cfg.
set_modem_id(my_id_);
driver_cfg.
set_serial_port(serial_port);
#ifdef USE_FLEXIBLE_DATA_PACKET
driver_cfg.AddExtension(micromodem::protobuf::Config::nvram_cfg,
"psk.packet.mod_hdr_version,1");
#endif
#ifdef USE_TWO_WAY_PING
#endif
mac_cfg.
set_type(goby::acomms::protobuf::MAC_FIXED_DECENTRALIZED);
mac_cfg.
set_modem_id(my_id_);
my_slot.
set_dest(buddy_id_);
#ifdef USE_FLEXIBLE_DATA_PACKET
my_slot.
set_type(goby::acomms::protobuf::ModemTransmission::DRIVER_SPECIFIC);
my_slot.SetExtension(micromodem::protobuf::type,
micromodem::protobuf::MICROMODEM_FLEXIBLE_DATA);
my_slot.
set_max_frame_bytes(32);
#elif defined(USE_TWO_WAY_PING)
my_slot.set_type(goby::acomms::protobuf::ModemTransmission::DRIVER_SPECIFIC);
my_slot.SetExtension(micromodem::protobuf::type, micromodem::protobuf::MICROMODEM_TWO_WAY_PING);
#else
my_slot.set_type(goby::acomms::protobuf::ModemTransmission::DATA);
my_slot.set_rate(0);
#endif
my_slot.
set_slot_seconds(12);
buddy_slot.set_src(buddy_id_);
buddy_slot.set_dest(my_id_);
buddy_slot.set_slot_seconds(12);
#ifdef USE_FLEXIBLE_DATA_PACKET
buddy_slot.set_type(goby::acomms::protobuf::ModemTransmission::DRIVER_SPECIFIC);
buddy_slot.SetExtension(micromodem::protobuf::type,
micromodem::protobuf::MICROMODEM_FLEXIBLE_DATA);
buddy_slot.set_max_frame_bytes(32);
buddy_slot.set_rate(1);
#elif defined(USE_TWO_WAY_PING)
buddy_slot.set_type(goby::acomms::protobuf::ModemTransmission::DRIVER_SPECIFIC);
buddy_slot.SetExtension(micromodem::protobuf::type,
micromodem::protobuf::MICROMODEM_TWO_WAY_PING);
#else
buddy_slot.set_type(goby::acomms::protobuf::ModemTransmission::DATA);
buddy_slot.set_rate(0);
#endif
if (my_id_ < buddy_id_)
{
mac_cfg.
add_slot()->CopyFrom(my_slot);
mac_cfg.add_slot()->CopyFrom(buddy_slot);
}
else
{
mac_cfg.add_slot()->CopyFrom(buddy_slot);
mac_cfg.add_slot()->CopyFrom(my_slot);
}
try
{
dccl_->
set_cfg(dccl_cfg);
}
catch (std::runtime_error& e)
{
std::cerr << "exception at startup: " << e.what() << std::endl;
return startup_failure();
}
for (;;)
{
std::string line;
if (!line.empty())
{
ChatMessage message_out;
message_out.set_telegram(line);
message_out.set_destination(buddy_id_);
message_out.set_source(my_id_);
}
try
{
}
catch (std::runtime_error& e)
{
std::cerr << "exception while running: " << e.what() << std::endl;
return 1;
}
}
return 0;
}
int startup_failure()
{
std::cerr << "usage: chat /dev/tty_modem my_id buddy_id log_file" << std::endl;
return 1;
}
{
if (mac_msg.
src() == my_id_)
curses_.
post_message(
"{control} starting send to my buddy");
else if (mac_msg.src() == buddy_id_)
curses_.
post_message(
"{control} my buddy might be sending to me now");
}
{
if (rx_msg.GetExtension(micromodem::protobuf::type) ==
micromodem::protobuf::MICROMODEM_TWO_WAY_PING &&
rx_msg.HasExtension(micromodem::protobuf::ranging_reply))
{
rx_msg.GetExtension(micromodem::protobuf::ranging_reply);
if (range_reply.
one_way_travel_time_size() > 0)
{
double owtt = range_reply.
one_way_travel_time(0);
}
}
}
{
ChatMessage typed_message_in;
typed_message_in.CopyFrom(message_in);
curses_.
post_message(typed_message_in.source(), typed_message_in.telegram());
}
{
ChatMessage typed_original_message;
typed_original_message.CopyFrom(original_message);
std::string("{ acknowledged receiving message starting with: " +
typed_original_message.telegram().substr(0, 5) + " }"));
}