#include "messages.h"
+/** This macro marks strings as translateable for Qt's linguist tools */
#define _(str) QT_TR_NOOP(str)
+
/** Defines the message codes according to RFCs 1495/281x.
* Named commands have a negative enum value.
*/
+/** \NOTE: Function handlers _must_ be global functions or static methods! */
+
+/** Set handler addresses to 0 to use the default (server) handler. */
+
BuiltinCmd builtins[] = {
{ _("admin"), _("Get information about the administrator of a server."),
_("[server]"), _("server: Server"),
- &Message::test1 },
+ 0, 0 },
- { 0, 0, 0, 0, 0 }
+ { 0, 0, 0, 0, 0, 0 }
};
extern BuiltinCmd builtins[];
+recvHandlerType Message::defaultRecvHandler;
+sendHandlerType Message::defaultSendHandler;
QHash<QString, CmdType> Message::cmdTypes;
Message::Message(QString _cmd, QStringList args) {
params = args;
}
-void Message::init() {
+void Message::init(recvHandlerType _r, sendHandlerType _s) {
+ defaultRecvHandler = _r;
+ defaultSendHandler = _s;
+
// Register builtin commands
for(int i = 0; ; i++) {
- if(!builtins[i].handler) break;
+ if(builtins[i].cmd.isEmpty()) break;
CmdType c;
c.cmd = builtins[i].cmd.toLower();
c.cmdDescr = builtins[i].cmdDescr;
c.args = builtins[i].args;
c.argsDescr = builtins[i].argsDescr;
- c.handler = builtins[i].handler;
+ c.recvHandler = ( builtins[i].recvHandler ? builtins[i].recvHandler : defaultRecvHandler);
+ c.sendHandler = ( builtins[i].sendHandler ? builtins[i].sendHandler : defaultSendHandler);
cmdTypes.insert(c.cmd, c);
}
}
-cmdhandler Message::getCmdHandler() {
+recvHandlerType Message::getRecvHandler() {
+ CmdType c = cmdTypes[cmd];
+ if(c.recvHandler) return c.recvHandler;
+ qDebug() << "No recvHandler defined for " << cmd << "!";
+ return 0;
+}
+
+sendHandlerType Message::getSendHandler() {
CmdType c = cmdTypes[cmd];
- if(c.handler) return c.handler;
- qDebug() << "No handler defined for " << cmd << "!";
+ if(c.sendHandler) return c.sendHandler;
+ qDebug() << "No sendHandler defined for " << cmd << "!";
return 0;
}
}
*/
-void Message::test1(QStringList foo) { qDebug() << "Test 1! " << cmd; };
-
-void Message::test2(QStringList bar) { qDebug() << "Test 2!"; };
#include <QString>
#include <QStringList>
+class Server;
+class Buffer;
class Message;
-typedef void (Message::* cmdhandler)(QStringList);
+typedef void (*sendHandlerType)(Message *); // handler for commands sent by the user
+typedef void (*recvHandlerType)(Message *); // handler for incoming messages
/**
* This contains information that depends (solely) on the message type, such as the handler function and help texts.
QString cmdDescr;
QString args;
QString argsDescr;
- cmdhandler handler;
+ recvHandlerType recvHandler;
+ sendHandlerType sendHandler;
};
virtual ~Message() {};
- static void init();
+ static void init(recvHandlerType defaultRevcHandler, sendHandlerType defaultSendHandler);
//static registerCmd();
//static unregisterCmd();
- cmdhandler getCmdHandler();
-
- void test1(QStringList);
- void test2(QStringList);
+ recvHandlerType getRecvHandler();
+ sendHandlerType getSendHandler();
protected:
+ static recvHandlerType defaultRecvHandler;
+ static sendHandlerType defaultSendHandler;
+
static QHash<QString, CmdType> cmdTypes;
};
QString cmdDescr;
QString args;
QString argsDescr;
- cmdhandler handler;
+ recvHandlerType recvHandler;
+ sendHandlerType sendHandler;
};
#endif
delete socket;
}
+void Server::init() {
+ Message::init(&handleServerMsg, &handleUserMsg);
+}
+
void Server::run() {
connect(socket, SIGNAL(connected()), this, SLOT(socketConnected()));
connect(socket, SIGNAL(disconnected()), this, SLOT(socketDisconnected()));
qDebug() << "Socket state changed: " << state;
}
+void Server::handleServerMsg(Message *msg) {
+
+
+}
+
+void Server::handleUserMsg(Message *msg) {
+
+
+}
#include <QtCore>
#include <QtNetwork>
+#include "messages.h"
+
#define DEFAULT_PORT 6667
/**
public:
Server();
~Server();
+ static void init();
void run();
QTcpSocket *socket;
QTextStream stream;
+ static void handleServerMsg(Message *);
+ static void handleUserMsg(Message *);
+
};
/*