~juanjonrg/p2psp/p2psp

« back to all changes in this revision

Viewing changes to src/other-scripts/splitter_v1.py

  • Committer: vicente.gonzalez.ruiz at gmail
  • Date: 2013-12-05 14:10:26 UTC
  • Revision ID: vicente.gonzalez.ruiz@gmail.com-20131205141026-kzvimjn4nqm8knem

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
listening_port = 9999
15
15
buffer_size = 256
16
16
 
17
 
source = (source_hostname, source_port)
18
 
source_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
19
 
print source_sock.getsockname(), 'Connecting to the source ', source, '...',
20
 
 
21
 
source_sock.connect(source)
22
 
 
23
 
print source_sock.getsockname(), 'Connected to ', source, '!'
24
 
 
25
 
channel='480.ogg'
26
 
#channel='134.ogg'
27
 
GET_message = 'GET /' + channel + ' HTTP/1.1\r\n'
28
 
GET_message += '\r\n'
29
 
source_sock.sendall(GET_message)
30
 
 
31
 
block_size = 1024
32
 
 
33
17
def get_peer_connection_socket():
34
18
    # {{{
35
19
 
36
 
    #sock = blocking_TCP_socket(socket.AF_INET, socket.SOCK_STREAM)
37
20
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
38
21
 
39
22
    try:
49
32
    return sock
50
33
 
51
34
    # }}}
 
35
# Socket to manage the cluster (churn).
52
36
peer_connection_sock = get_peer_connection_socket()
53
37
 
54
38
def create_cluster_sock(listening_port):
64
48
 
65
49
    return sock
66
50
    # }}}
 
51
# Socket to send the media to the cluster.
67
52
cluster_sock = create_cluster_sock(listening_port)
68
53
 
69
54
# The list of peers in the cluster. There will be always a peer in the
77
62
# Unreliability rate of a peer.
78
63
unreliability = {}
79
64
 
80
 
# Complaining rate of a peer.
81
 
complains = {}
82
 
 
83
65
# Useful definitions.
84
66
IP_ADDR = 0
85
67
PORT = 1
87
69
# The child threads will be alive only while the main thread is alive.
88
70
main_alive = True
89
71
 
90
 
# When a peer want to join a cluster, first must establish a TCP
 
72
# When a peer want to join a cluster, first it must establish a TCP
91
73
# connection with the splitter. In that connection, the splitter sends
92
74
# to the incomming peer the list of peers. Notice that the
93
75
# transmission of the list of peers (something that could need some
109
91
    def run(self):
110
92
        global peer_list
111
93
        global unreliability
112
 
        global complains
113
94
 
114
95
        print self.peer_serve_socket.getsockname(), \
115
96
            'Accepted connection from peer', \
133
114
        self.peer_serve_socket.close()
134
115
        peer_list.append(self.peer)
135
116
        unreliability[self.peer] = 0
136
 
        complains[self.peer] = 0
137
117
 
138
118
    # }}}
139
119
 
189
169
                            destination
190
170
                        peer_list.remove(destination)
191
171
                        del unreliability[destination]
192
 
                        del complains[destination]
193
172
                except:
194
173
                    # The unsupportive peer does not exit.
195
174
                    pass
198
177
    # }}}
199
178
listen_to_the_cluster().start()
200
179
 
 
180
source = (source_hostname, source_port)
 
181
source_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 
182
print source_sock.getsockname(), 'Connecting to the source ', source, '...',
 
183
 
 
184
source_sock.connect(source)
 
185
 
 
186
print source_sock.getsockname(), 'Connected to ', source, '!'
 
187
 
 
188
channel='480.ogg'
 
189
#channel='134.ogg'
 
190
GET_message = 'GET /' + channel + ' HTTP/1.1\r\n'
 
191
GET_message += '\r\n'
 
192
source_sock.sendall(GET_message)
 
193
 
 
194
block_size = 1024
 
195
 
201
196
block_number = 0
202
197
kbps = 0
203
198
class compute_kbps(Thread):
220
215
peer_index = 0
221
216
block_format_string = "H"+str(block_size)+"s" # "H1024s
222
217
 
 
218
# This is the main loop of the splitter
223
219
while True:
224
220
    try:
225
221
        # Receive data from the source
251
247
        cluster_sock.sendto(message, peer)
252
248
        peer_index = (peer_index + 1) % len(peer_list)
253
249
 
254
 
        # Decrement unreliability and complaints after every 256 packets
 
250
        # Decrement unreliability after every 256 packets
255
251
        if (block_number % 256) == 0:
256
252
            for i in unreliability:
257
253
                unreliability[i] /= 2
258
 
            for i in complains:
259
 
                complains[i] /= 2
260
254
 
261
255
        print '\r', block_number, '->', peer, '('+str(kbps)+' kbps)',
262
256
        sys.stdout.flush()