24 #include <fvutils/net/fuse_client.h> 26 #include <fvutils/net/fuse_transceiver.h> 27 #include <fvutils/net/fuse_message_queue.h> 28 #include <fvutils/net/fuse_message.h> 29 #include <fvutils/net/fuse_client_handler.h> 31 #include <core/threading/mutex.h> 32 #include <core/threading/wait_condition.h> 33 #include <core/exceptions/software.h> 34 #include <netcomm/socket/stream.h> 35 #include <netcomm/utils/exceptions.h> 38 #include <netinet/in.h> 64 FuseClient::FuseClient(
const char *hostname,
unsigned short int port,
68 __hostname = strdup(hostname);
77 __mutex =
new Mutex();
78 __recv_mutex =
new Mutex();
81 __greeting_mutex =
new Mutex();
85 __greeting_received =
false;
94 while ( ! __inbound_msgq->empty() ) {
97 __inbound_msgq->pop();
99 delete __inbound_msgq;
101 while ( ! __outbound_msgq->empty() ) {
104 __outbound_msgq->pop();
106 delete __outbound_msgq;
110 delete __recv_waitcond;
112 delete __greeting_mutex;
113 delete __greeting_waitcond;
121 __socket->
connect(__hostname, __port);
124 greetmsg->
version = htonl(FUSE_CURRENT_VERSION);
162 __recv_mutex->
lock();
223 __recv_mutex->
lock();
225 __recv_waitcond->
wait();
240 __recv_mutex->
lock();
242 __recv_waitcond->
wait();
255 __recv_mutex->
lock();
257 __recv_waitcond->
wait();
272 __socket->
poll(__wait_timeout , Socket::POLL_IN);
300 __inbound_msgq->
lock();
301 while ( ! __inbound_msgq->empty() ) {
304 if ( m->
type() == FUSE_MT_GREETING ) {
306 if ( ntohl(gm->
version) != FUSE_CURRENT_VERSION ) {
310 __greeting_mutex->
lock();
311 __greeting_received =
true;
313 __greeting_mutex->
unlock();
322 __inbound_msgq->pop();
340 __recv_mutex->
lock();
341 __recv_waitcond->
wait();
356 __greeting_mutex->
lock();
357 while (! __greeting_received) {
358 __greeting_waitcond->
wait();
360 __greeting_mutex->
unlock();
virtual void connect(const char *hostname, const unsigned short int port)
Connect socket.
void unlock() const
Unlock list.
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.
Fawkes library namespace.
void unlock()
Unlock the mutex.
void wake_all()
Wake up all waiting threads.
uint32_t type() const
Get message type.
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.
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.
void lock() const
Lock queue.
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()
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.
virtual void fuse_inbound_received(FuseNetworkMessage *m)=0
Message received.
MT * msg() const
Get correctly casted payload.