24 #include <core/exceptions/software.h> 25 #include <core/threading/mutex.h> 26 #include <core/threading/wait_condition.h> 27 #include <fvutils/net/fuse_client.h> 28 #include <fvutils/net/fuse_client_handler.h> 29 #include <fvutils/net/fuse_message.h> 30 #include <fvutils/net/fuse_message_queue.h> 31 #include <fvutils/net/fuse_transceiver.h> 32 #include <netcomm/socket/stream.h> 33 #include <netcomm/utils/exceptions.h> 34 #include <netinet/in.h> 42 namespace firevision {
59 FuseClient::FuseClient(
const char *hostname,
unsigned short int port,
FuseClientHandler *handler)
62 hostname_ = strdup(hostname);
72 recv_mutex_ =
new Mutex();
75 greeting_mutex_ =
new Mutex();
79 greeting_received_ =
false;
87 while (!inbound_msgq_->empty()) {
94 while (!outbound_msgq_->empty()) {
97 outbound_msgq_->pop();
99 delete outbound_msgq_;
103 delete recv_waitcond_;
105 delete greeting_mutex_;
106 delete greeting_waitcond_;
113 socket_->
connect(hostname_, port_);
117 greetmsg->
version = htonl(FUSE_CURRENT_VERSION);
118 outbound_msgq_->push(
211 recv_waitcond_->
wait();
227 recv_waitcond_->
wait();
241 recv_waitcond_->
wait();
254 socket_->
poll(wait_timeout_ , Socket::POLL_IN);
281 inbound_msgq_->
lock();
282 while (!inbound_msgq_->empty()) {
285 if (m->
type() == FUSE_MT_GREETING) {
287 if (ntohl(gm->
version) != FUSE_CURRENT_VERSION) {
291 greeting_mutex_->
lock();
292 greeting_received_ =
true;
294 greeting_mutex_->
unlock();
303 inbound_msgq_->pop();
321 recv_waitcond_->
wait();
335 greeting_mutex_->
lock();
336 while (!greeting_received_) {
337 greeting_waitcond_->
wait();
339 greeting_mutex_->
unlock();
virtual void connect(const char *hostname, const unsigned short int port)
Connect socket.
static void recv(fawkes::StreamSocket *s, FuseNetworkMessageQueue *msgq, unsigned int max_num_msgs=8)
Receive data.
virtual void close()
Close socket.
Wait until a given condition holds.
void enqueue_and_wait(FuseNetworkMessage *message)
Enqueue message and wait for reply.
void disconnect()
Disconnect.
void unref()
Decrement reference count and conditionally delete this instance.
void unlock() const
Unlock list.
Fawkes library namespace.
void unlock()
Unlock the mutex.
void wake_all()
Wake up all waiting threads.
void enqueue(FuseNetworkMessage *m)
Enqueue message.
Thread class encapsulation of pthreads.
static void send(fawkes::StreamSocket *s, FuseNetworkMessageQueue *msgq)
Send messages.
TCP stream socket over IP.
virtual bool available()
Check if data is available.
uint32_t version
version from FUSE_version_t
void wait()
Wait for messages.
uint32_t type() const
Get message type.
Base class for exceptions in Fawkes.
version packet, bi-directional
A LockQueue of FuseNetworkMessage to hold messages in inbound and outbound queues.
virtual void fuse_invalid_server_version(uint32_t local_version, uint32_t remote_version)=0
Invalid version string received.
void wait()
Wait for the condition forever.
virtual ~FuseClient()
Destructor.
Thrown if the connection died during an operation.
void print_trace()
Prints trace to stderr.
virtual void loop()
Thread loop.
virtual short poll(int timeout=-1, short what=POLL_IN|POLL_HUP|POLL_PRI|POLL_RDHUP)
Wait for some event on socket.
void wait_greeting()
Wait for greeting message.
void push_locked(const Type &x)
Push element to queue with lock protection.
void lock() const
Lock queue.
void lock()
Lock this mutex.
virtual void fuse_connection_died()=0
Connection died.
Mutex mutual exclusion lock.
virtual void fuse_connection_established()=0
Connection has been established.
MT * msg() const
Get correctly casted payload.
virtual void fuse_inbound_received(FuseNetworkMessage *m)=0
Message received.