Browse Source

SshClient class now handles BC orders. Client class can request
broadcast.

Mihawk 12 years ago
parent
commit
865e6161b8
5 changed files with 84 additions and 32 deletions
  1. 41 3
      App.cpp
  2. 6 6
      App.h
  3. 15 7
      Client.cpp
  4. 16 14
      SshClient.cpp
  5. 6 2
      SshClient.h

+ 41 - 3
App.cpp

@@ -30,6 +30,8 @@ App::App(int &argc, char **argv) :
 	connect(myServer, SIGNAL(newConnection()), SLOT(onNewConnection()));
 
 	myClientsFrameTimerID = startTimer(0);
+
+  myQWNETSshClient->connectToHost("stomp", "b4r.org");
 }
 
 void App::onNewConnection()
@@ -352,16 +354,39 @@ void App::removeClient(const QString &host, quint16 port)
 	print("Client not found on the list.\n");
 }
 
-void App::say(const QString &msg)
+void App::say(const QString &msg, int *serverCount, int *userCount)
 {
   ActiveClient* ac;
+
+  *serverCount = 0;
+  *userCount = 0;
+
   foreach(ac, myClients)
 	{
-    ac->client()->say(msg);
+    if(ac->client()->state() == Client::ConnectedState)
+    {
+      ac->client()->say(msg);
+      *userCount += ac->queryThread()->playerCount();
+      *serverCount++;
+    }
 	}
 	print("Say command sent to all clients\n");
 }
 
+void App::say(const QString &msg)
+{
+  ActiveClient* ac;
+
+  foreach(ac, myClients)
+  {
+    if(ac->client()->state() == Client::ConnectedState)
+    {
+      ac->client()->say(msg);
+    }
+  }
+  print("Say command sent to all clients\n");
+}
+
 void App::sayTeam(const QString &msg)
 {
   ActiveClient* ac;
@@ -396,9 +421,22 @@ void App::timerEvent(QTimerEvent *e)
 	}
 }
 
+void App::requestBroadcast(const QString &type, const QString &user, const QString &server, const QString &message)
+{
+  //REQ_BC QDEV,-dev-,qw://123.123,'testuser','test, with '',``twists''$ hehe'
+  qDebug() << "REQ_BC QDEV,-" + type + "-,qw://" + server + ",'" + user + "','" + message + "'";
+  myQWNETSshClient->write("REQ_BC QDEV,-" + type + "-,qw://" + server + ",'" + user + "','" + message + "'\n");
+}
+
 void App::broadcast(const QString &msg)
 {
-	say(msg);
+  say(msg);
+}
+
+void App::broadcast(const QString &msg, int *serverCount, int *userCount)
+{
+  qDebug() << "Broadcasting" << msg;
+  say(msg, serverCount, userCount);
 }
 
 void App::cleanup()

+ 6 - 6
App.h

@@ -15,15 +15,16 @@ class ServerQueryThread;
 
 class App : public QCoreApplication
 {
-  //TODO:
-	friend class Client;
-
 	Q_OBJECT
 public:
 	explicit							App(int &argc, char **argv);
 												~App();
 
   void									print(const QString& msg);
+  void									broadcast(const QString& msg, int *serverCount, int *userCount);
+  void                  broadcast(const QString& msg);
+  void                  requestBroadcast(const QString& type, const QString& user, const QString& server, const QString& message);
+
 protected:
 	void									timerEvent(QTimerEvent *e);
 
@@ -57,8 +58,6 @@ private:
 	QSettings							mySettings;
 	int										myClientsFrameTimerID; //timer for mainloop
 
-	void									broadcast(const QString& msg);
-
 	void									loadClients();
 	void									cleanup();
 
@@ -68,7 +67,8 @@ private:
 	void									removeClient(const QString& host, quint16 port);
 	void									connectToServer(const QString& args);
 	void									disconnectFromServer(const QString& args);
-	void									say(const QString& msg);
+  void                  say(const QString& msg, int *serverCount, int *userCount);
+  void                  say(const QString& msg);
 	void									sayTeam(const QString& msg);
 	void									setTeam(const QString& team);
 	void									listClients();

+ 15 - 7
Client.cpp

@@ -102,7 +102,7 @@ void Client::retryConnection()
 
 void Client::parsePrintedLine()
 {
-	QRegExp regex("^(.+):\\s+!([A-Za-z]+)\\s*(.*)$");
+  QRegExp regex("^(.+):\\s+\\.([A-Za-z]+)\\s*(.*)$");
 
 	if(regex.indexIn(myPrintLine) == -1)
 		return;
@@ -128,17 +128,25 @@ void Client::parsePrintedLine()
 
 	if(command == "help")
 	{
-		say("type:");
-    say("> !msg <message> to broadcast a message.");
-    say("> !help to show this help message.");
+    say("> commands:");
+    say("> .qw <message>");
+    say("> .spam <message>");
+    say("> .help to show this help message.");
 		return;
 	}
 
-	if(command == "msg")
+  if(command == "qw" || command == "spam")
 	{
     say("Broadcasting...");
-		QString hostPort("[" + nick + " - " + QString(host()) + ":" + QString::number(port()) + "]");
-		myApp->broadcast(hostPort + args);
+
+    QString server(QString(host()) + ":" + QString::number(port()));
+    QString message("-" + command + "- " + nick + " - " + server + " : " + args.trimmed());
+
+    //-qw- Skillah - #crazy88 : 4on4 MIX qw.foppa.dk:27503 7/8
+    myApp->broadcast(message);
+    //myApp->requestBroadcast(command, nick, server, args.trimmed());
+    myApp->requestBroadcast("dev", nick, server, args.trimmed());
+
 		return;
 	}
 }

+ 16 - 14
SshClient.cpp

@@ -18,6 +18,7 @@ along with this program.  If not, see < http://www.gnu.org/licenses/ >.
 */
 
 #include "SshClient.h"
+#include "App.h"
 #include <QProcess>
 #include <QRegExp>
 #include <QDateTime>
@@ -25,10 +26,11 @@ along with this program.  If not, see < http://www.gnu.org/licenses/ >.
 #include <QStringList>
 #include <QDebug>
 
-SshClient::SshClient(QObject *parent) :
+SshClient::SshClient(App* app, QObject *parent) :
   QObject(parent),
+  myApp(app),
   myProcess(new QProcess(this)),
-  myCommandRegex(new QRegExp("^([0-9]{4}-[0-9]{2}-[0-9]{2}\\s[0-9]{2}:[0-9]{2}:[0-9]{2}\\s\\+[0-9]{4}):\\s([A-Za-z]+):?\\s?(.*)$")),
+  myCommandRegex(new QRegExp("^([0-9]{4}-[0-9]{2}-[0-9]{2}\\s[0-9]{2}:[0-9]{2}:[0-9]{2})\\s(\\+[0-9]{4}):\\s(.+)$")),
   myConnectedFlag(false),
   myConnectionTimerID(0),
   myPingTimerID(0),
@@ -63,10 +65,12 @@ void SshClient::read()
     line = line.trimmed();
     if(myCommandRegex->indexIn(line) != -1)
     {
-      QDateTime time = QDateTime::fromString(myCommandRegex->capturedTexts().at(1).section(' ', 0, 1), "yyyy-MM-dd HH:mm:ss");
+      QDateTime time = QDateTime::fromString(myCommandRegex->cap(1), "yyyy-MM-dd HH:mm:ss");
+      time = time.addSecs(-myCommandRegex->cap(2).toInt()/100*60*60);
+
       parse(time,
-            myCommandRegex->capturedTexts().at(2),
-            myCommandRegex->capturedTexts().at(3)
+            myCommandRegex->cap(3).section(' ', 0, 0),
+            myCommandRegex->cap(3).section(' ', 1)
             );
     }
   }
@@ -97,15 +101,13 @@ void SshClient::write(const QString &data)
 
 void SshClient::parse(const QDateTime &time, const QString &command, const QString &commandData)
 {
-  QString cmdData = commandData.trimmed();
-  if(cmdData.startsWith(": "))
-    cmdData.remove(0, 2);
+  qDebug() << time << command << commandData;
 
   /* JOINED - Another user has just joined central */
   if(command == "JOINED")
   {
     QRegExp a("^User '([a-zA-Z]+)'.+$");
-    if(a.indexIn(commandData) == -1)
+    if(a.indexIn(commandData) != -1)
     {
       if(!myClients->contains(a.capturedTexts().at(1)))
         myClients->push_back(a.capturedTexts().at(1));
@@ -117,7 +119,7 @@ void SshClient::parse(const QDateTime &time, const QString &command, const QStri
   if(command == "PARTED")
   {
     QRegExp a("^User '([a-zA-Z]+)'.+$");
-    if(a.indexIn(commandData) == -1)
+    if(a.indexIn(commandData) != -1)
       myClients->removeAll(a.capturedTexts().at(1));
     return;
   }
@@ -153,13 +155,13 @@ void SshClient::parse(const QDateTime &time, const QString &command, const QStri
     if(a.indexIn(commandData) == -1)
       return;
 
+    int serverCount, userCount;
+
+    myApp->broadcast(a.cap(3) + " " + a.cap(5) + " - " + a.cap(4) + " : " + a.cap(6), &serverCount, &userCount);
+    write("BC_RE " + a.cap(1) + " " + QString::number(userCount) + "," + QString::number(serverCount) + "\n");
 
-    qDebug() << "Broadcast Request" << commandData;
-//    BC 0x0hash hash QDEV,-dev-,qw://123.123,'testuser','test, with '',``twists''$ hehe'
     return;
   }
-
-  qDebug() << time << command << cmdData;
 }
 
 bool SshClient::connectToHost(const QString &user, const QString &host)

+ 6 - 2
SshClient.h

@@ -26,18 +26,21 @@ along with this program.  If not, see < http://www.gnu.org/licenses/ >.
 class QProcess;
 class QDateTime;
 class QStringList;
+class App;
 
 class SshClient : public QObject
 {
   Q_OBJECT
+
 public:
   enum Error { NoError, ConnectionTimedOutError };
 
-  explicit SshClient(QObject *parent = 0);
+  explicit SshClient(App* app, QObject *parent = 0);
   ~SshClient();
   bool connectToHost(const QString& user, const QString &host);
   bool isConnected() const;
   void disconnectFromHost();
+  void write(const QString& data);
 
 signals:
   void error(Error errorCode);
@@ -51,6 +54,7 @@ private slots:
   void exited(int exitCode);
 
 private:
+  App*      myApp;
   QProcess* myProcess;
   QRegExp*  myCommandRegex;
   bool      myConnectedFlag;
@@ -60,7 +64,7 @@ private:
   QStringList* myClients;
 
   void parse(const QDateTime& time, const QString& command, const QString& commandData);
-  void write(const QString& data);
+
   void ping();
   void pong();
 };