Fawkes API
Fawkes Development Version
|
24 #include <arpa/inet.h>
25 #include <blackboard/internal/instance_factory.h>
26 #include <blackboard/internal/interface_mem_header.h>
27 #include <blackboard/internal/notifier.h>
28 #include <blackboard/net/interface_proxy.h>
29 #include <blackboard/net/messages.h>
30 #include <core/threading/refc_rwlock.h>
31 #include <logging/liblogger.h>
32 #include <netcomm/fawkes/client.h>
33 #include <netcomm/fawkes/message.h>
56 FawkesNetworkMessage *msg,
57 BlackBoardNotifier * notifier,
58 Interface * interface,
62 if (msg->msgid() != MSG_BB_OPEN_SUCCESS) {
63 throw Exception(
"Expected open success message");
66 void * payload = msg->payload();
71 instance_serial_ = ntohl(osm->
serial);
80 throw Exception(
"Network message does not carry chunk of expected size");
83 rwlock_ =
new RefCountRWLock();
84 mem_chunk_ = malloc(
sizeof(interface_header_t) + data_size_);
85 data_chunk_ = (
char *)mem_chunk_ +
sizeof(interface_header_t);
86 memset(mem_chunk_, 0,
sizeof(interface_header_t) + data_size_);
87 memcpy(data_chunk_, (
char *)payload +
sizeof(bb_iopensucc_msg_t), data_size_);
89 interface_header_t *ih = (interface_header_t *)mem_chunk_;
91 strncpy(ih->type,
interface->
type(), INTERFACE_TYPE_SIZE_ - 1);
92 strncpy(ih->id,
interface->
id(), INTERFACE_ID_SIZE_ - 1);
94 ih->flag_writer_active = (has_writer_ ? 1 : 0);
95 ih->num_readers = num_readers_;
98 interface->set_instance_serial(instance_serial_);
99 interface->set_memory(0, mem_chunk_, data_chunk_);
116 if (msg->
msgid() != MSG_BB_DATA_CHANGED) {
118 "Expected data changed BB message, but "
119 "received message of type %u, ignoring.",
124 void * payload = msg->payload();
126 if (ntohl(dm->
serial) != instance_serial_) {
128 "Serial mismatch, expected %u, "
129 "but got %u, ignoring.",
135 if (ntohl(dm->
data_size) != data_size_) {
137 "Data size mismatch, expected %zu, "
138 "but got %zu, ignoring.",
144 memcpy(data_chunk_, (
char *)payload +
sizeof(bb_idata_msg_t), data_size_);
155 if (msg->msgid() != MSG_BB_INTERFACE_MESSAGE) {
157 "Expected interface BB message, but "
158 "received message of type %u, ignoring.",
163 void * payload = msg->payload();
165 if (ntohl(mm->
serial) != instance_serial_) {
167 "Serial mismatch (msg), expected %u, "
168 "but got %u, ignoring.",
176 "Received interface message, but this"
177 "is a reading instance (%s), ignoring.",
185 im->set_hops(ntohl(mm->
hops) + 1);
187 if (im->hops() > 1) {
189 "Message IDs are not stable across more than one hop, "
190 "message of type %s for interface %s has %u hops",
196 if (ntohl(mm->
data_size) != im->datasize()) {
198 "Message data size mismatch, expected "
199 "%zu, but got %zu, ignoring.",
206 im->set_from_chunk((
char *)payload +
sizeof(bb_imessage_msg_t));
212 }
catch (Exception &e) {
213 e.append(
"Failed to enqueue interface message for %s, ignoring", interface_->
uid());
234 if (num_readers_ > 0) {
266 return instance_serial_;
275 return instance_serial_;
300 std::list<std::string>
317 void * payload = malloc(payload_size);
324 clid_, FAWKES_CID_BLACKBOARD, MSG_BB_DATA_CHANGED, payload, payload_size);
334 void * payload = calloc(1, payload_size);
337 unsigned int msgid = next_msg_id();
338 dm->
msgid = htonl(msgid);
341 strncpy(dm->
msg_type, message->
type(), INTERFACE_MESSAGE_TYPE_SIZE_ - 1);
346 clid_, FAWKES_CID_BLACKBOARD, MSG_BB_INTERFACE_MESSAGE, payload, payload_size);
virtual unsigned int num_readers(const Interface *interface) const
Get number of readers.
const void * datachunk() const
Get data chunk.
virtual void notify_of_data_change(const Interface *interface)
Notify of data change.
BlackBoardInterfaceProxy(FawkesNetworkClient *client, FawkesNetworkMessage *msg, BlackBoardNotifier *notifier, Interface *interface, bool readwrite)
Constructor.
void reader_added(unsigned int event_serial)
Reader has been added.
uint32_t writer_readers
combined writer reader information.
virtual bool exists_writer(const Interface *interface) const
Check if a writer exists for the given interface.
void enqueue(FawkesNetworkMessage *message)
Enqueue message to send.
virtual std::string writer(const Interface *interface) const
Get writer of interface.
uint32_t data_size
size in bytes of the following data.
const void * datachunk() const
Get pointer to data.
virtual Message * create_message(const char *type) const =0
virtual void transmit(Message *message)
Transmit message.
bool is_writer() const
Check if this is a writing instance.
uint32_t data_size
size in bytes of the following data.
unsigned int serial() const
Get instance serial of interface.
static void log_error(const char *component, const char *format,...)
Log error message.
void set_id(unsigned int message_id)
Set message ID.
uint32_t serial
instance serial to unique identify this instance
const unsigned char * hash() const
Get interface hash.
uint32_t hops
number of hops this message already passed
void reader_removed(unsigned int event_serial)
Reader has been removed.
char msg_type[INTERFACE_MESSAGE_TYPE_SIZE_]
message type
const char * type() const
Get type of interface.
const char * id() const
Get identifier of interface.
bool notify_of_message_received(const Interface *interface, Message *message)
Notify of message received Notify all subscribers of the given interface of an incoming message This ...
void notify_of_reader_removed(const Interface *interface, unsigned int event_instance_serial)
Notify that reader has been removed.
uint32_t data_size
data for message
static void log_warn(const char *component, const char *format,...)
Log warning message.
void process_interface_message(FawkesNetworkMessage *msg)
Process MSG_BB_INTERFACE message.
void process_data_changed(FawkesNetworkMessage *msg)
Process MSG_BB_DATA_CHANGED message.
void msgq_append(Message *message)
Enqueue message.
void notify_of_reader_added(const Interface *interface, unsigned int event_instance_serial)
Notify that reader has been added.
unsigned int datasize() const
Get size of data.
~BlackBoardInterfaceProxy()
Destructor.
const char * type() const
Get message type.
uint32_t serial
instance serial to unique identify this instance
const char * uid() const
Get unique identifier of interface.
unsigned int clid() const
Get client ID of assigned client.
Interface open success The serial denotes a unique instance of an interface within the (remote) Black...
void notify_of_writer_added(const Interface *interface, unsigned int event_instance_serial)
Notify that writer has been added.
void writer_added(unsigned int event_serial)
Writer has been added.
void notify_of_data_change(const Interface *interface)
Notify of data change.
unsigned short serial() const
Get instance serial of interface.
unsigned int hops() const
Get number of hops.
void writer_removed(unsigned int event_serial)
Writer has been removed.
unsigned int datasize() const
Get data size.
uint32_t serial
interface instance serial
unsigned short int msgid() const
Get message type ID.
void notify_of_writer_removed(const Interface *interface, unsigned int event_instance_serial)
Notify that writer has been removed.
virtual std::list< std::string > readers(const Interface *interface) const
Get owners of interfaces who opened for reading.
Interface * interface() const
Get instance serial of interface.