Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Server: Added ShellUser and ShellUsers
ShellUser and the ShellUsers collection manage the state of all the shell connections open to a server.
- Loading branch information
Showing
5 changed files
with
291 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** @file shelluser.h Remote user of a shell connection. | ||
* @ingroup server | ||
* | ||
* @authors Copyright © 2013 Jaakko Keränen <jaakko.keranen@iki.fi> | ||
* | ||
* @par License | ||
* GPL: http://www.gnu.org/licenses/gpl.html | ||
* | ||
* <small>This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by the | ||
* Free Software Foundation; either version 2 of the License, or (at your | ||
* option) any later version. This program is distributed in the hope that it | ||
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty | ||
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General | ||
* Public License for more details. You should have received a copy of the GNU | ||
* General Public License along with this program; if not, see: | ||
* http://www.gnu.org/licenses</small> | ||
*/ | ||
|
||
#ifndef SERVER_SHELLUSER_H | ||
#define SERVER_SHELLUSER_H | ||
|
||
#include <de/Socket> | ||
#include <de/shell/Link> | ||
|
||
/** | ||
* Remote user of a shell connection. | ||
* | ||
* Contains all the user-specific data pertinent to a remote connected shell | ||
* user. | ||
* | ||
* This is a server-side class representing a remote user connected to the | ||
* server through a shell link. | ||
* | ||
* @ingroup server | ||
*/ | ||
class ShellUser : public de::shell::Link | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
/** | ||
* Constructs a new shell user from a previously opened socket. | ||
* | ||
* @param socket Open socket. User takes ownership. | ||
*/ | ||
ShellUser(de::Socket *socket); | ||
|
||
virtual ~ShellUser(); | ||
|
||
protected slots: | ||
void handleIncomingPackets(); | ||
|
||
private: | ||
struct Instance; | ||
Instance *d; | ||
}; | ||
|
||
#endif // SERVER_SHELLUSER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** @file shellusers.h All remote shell users. | ||
* @ingroup server | ||
* | ||
* @authors Copyright © 2013 Jaakko Keränen <jaakko.keranen@iki.fi> | ||
* | ||
* @par License | ||
* GPL: http://www.gnu.org/licenses/gpl.html | ||
* | ||
* <small>This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by the | ||
* Free Software Foundation; either version 2 of the License, or (at your | ||
* option) any later version. This program is distributed in the hope that it | ||
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty | ||
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General | ||
* Public License for more details. You should have received a copy of the GNU | ||
* General Public License along with this program; if not, see: | ||
* http://www.gnu.org/licenses</small> | ||
*/ | ||
|
||
#ifndef SERVER_SHELLUSERS_H | ||
#define SERVER_SHELLUSERS_H | ||
|
||
#include <QObject> | ||
#include <QSet> | ||
#include "shelluser.h" | ||
|
||
/** | ||
* All remote shell users. | ||
*/ | ||
class ShellUsers : public QObject | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
ShellUsers(); | ||
|
||
~ShellUsers(); | ||
|
||
/** | ||
* Adds a new remote shell user to the set of connected users. Users are | ||
* automatically removed from this collection and deleted when they are | ||
* disconnected. | ||
* | ||
* @param user User. Ownership transferred. | ||
*/ | ||
void add(ShellUser *user); | ||
|
||
int count() const; | ||
|
||
protected slots: | ||
void userDisconnected(); | ||
|
||
private: | ||
QSet<ShellUser *> _users; | ||
}; | ||
|
||
#endif // SERVER_SHELLUSERS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/** @file shelluser.cpp Remote user of a shell connection. | ||
* @ingroup server | ||
* | ||
* @authors Copyright © 2013 Jaakko Keränen <jaakko.keranen@iki.fi> | ||
* | ||
* @par License | ||
* GPL: http://www.gnu.org/licenses/gpl.html | ||
* | ||
* <small>This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by the | ||
* Free Software Foundation; either version 2 of the License, or (at your | ||
* option) any later version. This program is distributed in the hope that it | ||
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty | ||
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General | ||
* Public License for more details. You should have received a copy of the GNU | ||
* General Public License along with this program; if not, see: | ||
* http://www.gnu.org/licenses</small> | ||
*/ | ||
|
||
#include "shelluser.h" | ||
#include <de/shell/Protocol> | ||
#include <de/LogSink> | ||
#include <de/Log> | ||
#include <de/LogBuffer> | ||
|
||
#include "con_main.h" | ||
|
||
using namespace de; | ||
|
||
struct ShellUser::Instance : public LogSink | ||
{ | ||
ShellUser &self; | ||
|
||
/// Log entries to be sent are collected here. | ||
shell::LogEntryPacket logEntryPacket; | ||
|
||
Instance(ShellUser &inst) : self(inst) | ||
{ | ||
// We will send all log entries to a shell user. | ||
LogBuffer::appBuffer().addSink(*this); | ||
} | ||
|
||
~Instance() | ||
{ | ||
LogBuffer::appBuffer().removeSink(*this); | ||
} | ||
|
||
LogSink &operator << (LogEntry const &entry) | ||
{ | ||
logEntryPacket.add(entry); | ||
return *this; | ||
} | ||
|
||
LogSink &operator << (String const &) | ||
{ | ||
return *this; | ||
} | ||
|
||
/** | ||
* Sends the accumulated log entries over the link. | ||
*/ | ||
void flush() | ||
{ | ||
if(!logEntryPacket.isEmpty() && self.status() == shell::Link::Connected) | ||
{ | ||
self << logEntryPacket; | ||
logEntryPacket.clear(); | ||
} | ||
} | ||
}; | ||
|
||
ShellUser::ShellUser(Socket *socket) : shell::Link(socket), d(new Instance(*this)) | ||
{ | ||
connect(this, SIGNAL(packetsReady()), this, SLOT(handleIncomingPackets())); | ||
} | ||
|
||
ShellUser::~ShellUser() | ||
{ | ||
delete d; | ||
} | ||
|
||
void ShellUser::handleIncomingPackets() | ||
{ | ||
forever | ||
{ | ||
QScopedPointer<Packet> packet(nextPacket()); | ||
if(packet.isNull()) break; | ||
|
||
switch(protocol().recognize(packet.data())) | ||
{ | ||
case shell::Protocol::Command: | ||
Con_Execute(CMDS_CONSOLE, protocol().command(*packet).toUtf8().constData(), false, true); | ||
break; | ||
|
||
default: | ||
break; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** @file shellusers.cpp All remote shell users. | ||
* @ingroup server | ||
* | ||
* @authors Copyright © 2013 Jaakko Keränen <jaakko.keranen@iki.fi> | ||
* | ||
* @par License | ||
* GPL: http://www.gnu.org/licenses/gpl.html | ||
* | ||
* <small>This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by the | ||
* Free Software Foundation; either version 2 of the License, or (at your | ||
* option) any later version. This program is distributed in the hope that it | ||
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty | ||
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General | ||
* Public License for more details. You should have received a copy of the GNU | ||
* General Public License along with this program; if not, see: | ||
* http://www.gnu.org/licenses</small> | ||
*/ | ||
|
||
#include "shellusers.h" | ||
|
||
ShellUsers::ShellUsers() | ||
{ | ||
} | ||
|
||
ShellUsers::~ShellUsers() | ||
{ | ||
foreach(ShellUser *user, _users) | ||
{ | ||
delete user; | ||
} | ||
} | ||
|
||
void ShellUsers::add(ShellUser *user) | ||
{ | ||
LOG_INFO("New shell user from %s") << user->address(); | ||
|
||
_users.insert(user); | ||
connect(user, SIGNAL(disconnected()), this, SLOT(userDisconnected())); | ||
} | ||
|
||
int ShellUsers::count() const | ||
{ | ||
return _users.size(); | ||
} | ||
|
||
void ShellUsers::userDisconnected() | ||
{ | ||
DENG2_ASSERT(dynamic_cast<ShellUser *>(sender()) != 0); | ||
|
||
ShellUser *user = static_cast<ShellUser *>(sender()); | ||
_users.remove(user); | ||
|
||
LOG_INFO("Shell user from %s has disconnected") << user->address(); | ||
|
||
user->deleteLater(); | ||
} |