1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
package Client;
use strict;
use warnings;
use QtCore4;
use QtGui4;
use Server;
use PeerManager;
use Connection;
use QtCore4::isa qw( Qt::Object );
use QtCore4::signals
newMessage => ['const QString &', 'const QString &'],
newParticipant => ['const QString &'],
participantLeft => ['const QString &'];
use QtCore4::slots
newConnection => ['QTcpSocket*'],
connectionError => ['QAbstractSocket::SocketError'],
disconnected => [],
readyForUse => [];
sub peerManager() {
return this->{peerManager};
}
sub server() {
return this->{server};
}
sub peers() {
return this->{peers};
}
sub NEW
{
my ($class) = @_;
$class->SUPER::NEW();
this->{peerManager} = PeerManager(this);
this->{server} = Server();
this->{peers} = {};
peerManager->setServerPort(server->serverPort());
peerManager->startBroadcasting();
Qt::Object::connect(peerManager, SIGNAL 'newConnection(QTcpSocket*)',
this, SLOT 'newConnection(QTcpSocket*)');
Qt::Object::connect(server, SIGNAL 'newConnection(QTcpSocket*)',
this, SLOT 'newConnection(QTcpSocket*)');
}
sub sendMessage
{
my ($message) = @_;
if (!defined $message) {
return;
}
my @connections = values %{peers()};
foreach my $connection ( @connections ) {
$connection->sendMessage($message);
}
}
sub nickName
{
return peerManager->userName()->constData() . '@' . Qt::HostInfo::localHostName()
. ':' . server->serverPort();
}
sub hasConnection
{
my ($senderIp, $senderPort) = @_;
if (!defined $senderPort) {
return exists peers()->{$senderIp->toString()};
}
if (!exists peers()->{$senderIp->toString()}) {
return 0;
}
my @connections = values %{peers()};
foreach my $connection ( @connections ) {
if ($connection->peerPort() == $senderPort) {
return 1;
}
}
return 0;
}
sub newConnection
{
my ($connection) = @_;
$connection->setGreetingMessage(peerManager->userName());
this->connect($connection, SIGNAL 'error(QAbstractSocket::SocketError)',
this, SLOT 'connectionError(QAbstractSocket::SocketError)');
this->connect($connection, SIGNAL 'disconnected()', this, SLOT 'disconnected()');
this->connect($connection, SIGNAL 'readyForUse()', this, SLOT 'readyForUse()');
}
sub readyForUse
{
my $connection = sender();
if (!defined $connection || hasConnection($connection->peerAddress(),
$connection->peerPort())) {
return;
}
this->connect($connection, SIGNAL 'newMessage(QString,QString)',
this, SIGNAL 'newMessage(QString,QString)');
peers()->{$connection->peerAddress()->toString()} = $connection;
my $nick = $connection->name();
if (defined $nick) {
emit newParticipant($nick);
}
}
sub disconnected
{
my $connection = sender();
if ($connection->isa('Connection')) {
removeConnection($connection);
}
}
sub connectionError
{
my $connection = sender();
if ($connection->isa('Connection')) {
removeConnection($connection);
}
}
sub removeConnection
{
my ($connection) = @_;
if (exists peers()->{$connection->peerAddress()->toString()}) {
delete peers()->{$connection->peerAddress()->toString()};
my $nick = $connection->name();
if (defined $nick) {
emit participantLeft($nick);
}
}
#$connection->deleteLater();
}
1;
|