Convert wahjamsrv SessionArchive timer to QTimer

The wahjamsrv SessionArchive feature stores audio in session archive
directories so it can be mixed after the jam has finished.  Each session
archive directory contains the audio for a preconfigured number of
minutes.

This patch converts the session archive timer to QTimer.  Perhaps the
idle state can be eliminated in the future so the timer is only active
when authenticated users are logged in.  This could be done by turning
User_Group into a QObject and emitting signals for authentication
success and and log out.  The current implementation only triggers every
30 seconds though so there will not be much difference.

Signed-off-by: Stefan Hajnoczi <stefanha@gmail.com>
Last packet debugging

This should not be merged into wahjam.git/master

Signed-off-by: Stefan Hajnoczi <stefanha@gmail.com>
Remove interval count from User_Group

User_Group watches when the interval ends and prints a log message with
the bpi/bpm.  Remove this for now since it needs to be converted to a
QTimer but doesn't appear like useful functionality.

Signed-off-by: Stefan Hajnoczi <stefanha@gmail.com>
Use signals/slots for QTcpSocket in Net_Connection

This patch makes Net_Connection use QTcpSocket signals.  It leaves the
->Run() interface which the client and server still call.  The next step
is to add signals to the Net_Connection interface so that the client and
server can also become event-driven.

Signed-off-by: Stefan Hajnoczi <stefanha@gmail.com>
Convert Net_Connection keepalive timers to QTimer

Signed-off-by: Stefan Hajnoczi <stefanha@gmail.com>
Add signals to Net_Connection interface

Make it possible for Net_Connection to be used in an event-driven
fashion with the messagesReady() signal, which is emitted each time one
or more new messages arrive.

The disconnected() signal is emitted when the connection enters an error
state or has been closed.

For now the ->Run() method is still available so that the client and
server can be converted to an event-driven architecture independently.

Signed-off-by: Stefan Hajnoczi <stefanha@gmail.com>
Keep track of User_Group in User_Connection

Keep the User_Group in a User_Connection field so we don't have to pass
it around all the time.  This will be critical once we hook up the
Net_Connection signals/slots and we cannot pass the group directly to
the slot.

Signed-off-by: Stefan Hajnoczi <stefanha@gmail.com>
Hook up User_Connection to Net_Connection::messagesReady()

Process messages in a slot instead of polling in ->Run().

Note that processMessage() has a lot of reindentation changes but the
code is almost unchanged except that m_netcon.Run() has been removed
where unnecessary.

Signed-off-by: Stefan Hajnoczi <stefanha@gmail.com>
Cache remote address in Net_Connection

The QTcpSocket::peerAddress() is only available when the socket is in
the connected state.  Since we want to report the remote address on
disconnect we need to keep it cached.

Signed-off-by: Stefan Hajnoczi <stefanha@gmail.com>
Use signals/slots for User_Connection and User_Group

This patch removes ->Run() for User_Connection, User_Group, and Server.
These functions can be converted to event-driven code by processing
incoming network messages, responding to timeouts, and removing users on
disconnect using signals/slots.

The user lookup and authentication process also uses ->Run() but can
easily be converted to use signals/slots.

The main loop is still spins because we check whether SIGHUP or SIGINT
want to reload the configuration file or shutdown the server.  A future
patch will address this so wahjamsrv can finally use app.exec() and be
fully event-driven.

Signed-off-by: Stefan Hajnoczi <stefanha@gmail.com>
Fix User_Group and Server ownership

Take advantage of the QObject parent relationship so that User_Group is
a child of Server.  This ensures that User_Group is automatically
deleted when Server is destroyed without ad-hoc User_Group instance
ownership rules.

Reported-by: Ikkei Shimomura (tea) <Ikkei.Shimomura@gmail.com>
Signed-off-by: Stefan Hajnoczi <stefanha@gmail.com>
Adapt signal handlers to Qt event loop

This commit finally converts the main loop to app.exec().  The last
missing piece was the POSIX signal handlers that need to be adapted to
the Qt event loop.

Qt integration for POSIX signals is poor so we use ugly adapter code
that converts signals into writes on a socketpair(2) file descriptor.

The good news is that the server is now event-driven and only consumes
CPU when there is event processing work to be done!

Signed-off-by: Stefan Hajnoczi <stefanha@gmail.com>
Remove Net_Connection::Send() "queue full" comment

Qt's QTcpConnection now handles the send queue for us and
Net_Message::Send() can no longer fail in this way.  Remove the comment.

Reported-by: Ikkei Shimomura (tea) <Ikkei.Shimomura@gmail.com>
Signed-off-by: Stefan Hajnoczi <stefanha@gmail.com>
Make NJClient inherit from QObject

In order to take advantage of signals/slots NJClient needs to become a
QObject.  This will allow us to use Net_Connection signals and replace
->Run() with a QTimer in future patches.

Signed-off-by: Stefan Hajnoczi <stefanha@gmail.com>
Use QTimer instead of ClientRunThread to call NJClient::Run()

ClientRunThread invokes NJClient::Run() every 20 milliseconds and
notifies MainWindow when state changes.  It turns out we can use QTimer
for the timer interval and get rid of the thread!  This makes life
easier because we only access NJClient from the Qt event loop and no
longer need a mutex.

The next step is to push the timer and state change signals down into
NJClient itself so MainWindow does not have to do so much babysitting.

Signed-off-by: Stefan Hajnoczi <stefanha@gmail.com>
Move client signals and interval timer to NJClient

There is no need for MainWindow to drive NJClient::Run() or to check for
state changes.  NJClient should do these things internally.  Move the
->Run() timer to NJClient and also emit the state change signals
directly from NJClient.

Note that state change signals should really be emitted where the state
change happens instead of by monitoring variables for changes in
NJClient::tick().  This will be fixed later.

Signed-off-by: Stefan Hajnoczi <stefanha@gmail.com>
Remove MainWindow::RunTick

Signed-off-by: Stefan Hajnoczi <stefanha@gmail.com>
Emit NJClient::userInfoChanged() directly

There is no need for a member variable, we can signal userInfoChange()
immediately.

Signed-off-by: Stefan Hajnoczi <stefanha@gmail.com>
Hook NJClient up to Net_Connection signals

Respond to incoming network messages in an event-driven fashion instead
of relying on ->Run().  By hooking up Net_Connection signals we get
notified immediately when new messages have arrived.

Signed-off-by: Stefan Hajnoczi <stefanha@gmail.com>
Make wahjamsrv a console application under Windows

The server has no GUI and therefore should be built as a console
application under Windows.  This links in the appropriate libraries and
makes console API functions available, which may be useful in the
future.

Reported-by: Ikkei Shimomura (tea) <Ikkei.Shimomura@gmail.com>
Signed-off-by: Stefan Hajnoczi <stefanha@gmail.com>