File: PeerManager.pm

package info (click to toggle)
qt4-perl 4.8.4-1.2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 8,636 kB
  • ctags: 8,100
  • sloc: perl: 42,963; cpp: 28,039; makefile: 160; xml: 98; sh: 4
file content (189 lines) | stat: -rw-r--r-- 4,733 bytes parent folder | download | duplicates (3)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package PeerManager;

use strict;
use warnings;
use QtCore4;
use QtGui4;
use QtNetwork4;
use QtCore4::isa qw( Qt::Object );
use QtCore4::signals
    newConnection => ['QTcpSocket*'];
use QtCore4::slots
    sendBroadcastDatagram => [],
    readBroadcastDatagram => [];
use Client;
use Connection;
use List::MoreUtils qw( first_index );

sub client() {
    return this->{client};
}

sub broadcastAddresses() {
    return this->{broadcastAddresses};
}

sub ipAddresses() {
    return this->{ipAddresses};
}

sub broadcastSocket() {
    return this->{broadcastSocket};
}

sub broadcastTimer() {
    return this->{broadcastTimer};
}

sub username() {
    return this->{username};
}

sub serverPort() {
    return this->{serverPort};
}

    #Client *client;
    #Qt::List<Qt::HostAddress> broadcastAddresses;
    #Qt::List<Qt::HostAddress> ipAddresses;
    #Qt::UdpSocket broadcastSocket;
    #Qt::Timer broadcastTimer;
    #Qt::ByteArray username;
    #int serverPort;

my $BroadcastInterval = 2000;
my $broadcastPort = 45000;

sub NEW
{
    my ($class, $client) = @_;
    $class->SUPER::NEW($client);
    this->{client} = $client;

    my @envVariables = qw( USERNAME.* USER.* USERDOMAIN.*
                 HOSTNAME.* DOMAINNAME.* );

    my $environment = Qt::Process::systemEnvironment();
    foreach my $string ( @envVariables ) {
        my $index = first_index{ $_ =~ m/$string/ } @{$environment};
        if ($index != -1) {
            my @stringList = split m/=/, $environment->[$index];
            if (scalar @stringList == 2) {
                utf8::decode($stringList[1]);
                this->{username} = Qt::ByteArray($stringList[1]);
                last;
            }
        }
    }

    if (!defined username()) {
        this->{username} = Qt::ByteArray('unknown');
    }

    updateAddresses();
    this->{serverPort} = 0;

    this->{broadcastSocket} = Qt::UdpSocket();
    broadcastSocket()->bind(Qt::HostAddress(Qt::HostAddress::Any()), $broadcastPort, Qt::UdpSocket::ShareAddress()
                         | Qt::UdpSocket::ReuseAddressHint());
    this->connect(broadcastSocket, SIGNAL 'readyRead()',
            this, SLOT 'readBroadcastDatagram()');

    this->{broadcastTimer} = Qt::Timer(this);
    broadcastTimer()->setInterval($BroadcastInterval);
    this->connect(broadcastTimer(), SIGNAL 'timeout()',
            this, SLOT 'sendBroadcastDatagram()');
}

sub setServerPort
{
    my ($port) = @_;
    this->{serverPort} = $port;
}

sub userName
{
    return username();
}

sub startBroadcasting
{
    broadcastTimer()->start();
}

sub isLocalHostAddress
{
    my ($address) = @_;
    foreach my $localAddress ( @{ipAddresses()} ) {
        if ($address == $localAddress) {
            return 1;
        }
    }
    return 0;
}

sub sendBroadcastDatagram
{
    my $datagram = Qt::ByteArray(username);
    $datagram->append('@');
    $datagram->append(Qt::CString(serverPort()));

    my $validBroadcastAddresses = 1;
    foreach my $address ( @{broadcastAddresses()} ) {
        if (broadcastSocket()->writeDatagram($datagram, $address,
                                          $broadcastPort) == -1) {
            $validBroadcastAddresses = 0;
        }
    }

    if (!$validBroadcastAddresses) {
        updateAddresses();
    }
}

sub readBroadcastDatagram
{
    while (broadcastSocket()->hasPendingDatagrams()) {
        my $senderIp = Qt::HostAddress();
        my $senderPort;
        my $datagram = '';
        my $datagramSize = broadcastSocket()->pendingDatagramSize();
        if (broadcastSocket()->readDatagram(\$datagram, $datagramSize,
                                         $senderIp, \$senderPort) == -1) {
            next;
        }

        my @list = split m/@/, $datagram;
        if (scalar @list != 2) {
            next;
        }

        my $senderServerPort = $list[1];
        if (isLocalHostAddress($senderIp) && $senderServerPort == serverPort()) {
            next;
        }

        if (!client->hasConnection($senderIp)) {
            my $connection = Connection(this);
            emit newConnection($connection);
            $connection->connectToHost($senderIp, $senderServerPort);
        }
    }
}

sub updateAddresses
{
    this->{broadcastAddresses} = [];
    this->{ipAddresses} = [];
    foreach my $interface ( @{Qt::NetworkInterface::allInterfaces()} ) {
        foreach my $entry ( @{$interface->addressEntries()} ) {
            my $broadcastAddress = $entry->broadcast();
            if ($broadcastAddress != Qt::HostAddress::Null() && $entry->ip() != Qt::HostAddress::LocalHost()) {
                push @{this->{broadcastAddresses}}, $broadcastAddress;
                push @{this->{ipAddresses}}, $entry->ip();
            }
        }
    }
}

1;