~p2psp/p2psp/jpl850

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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/python 

import socket 
import struct
import time
import argparse






class Gatherer():
    
    BUFFER_SIZE = 512
    BLOCK_SIZE = 1024
    HEADER_SIZE = 10 * BLOCK_SIZE
    PUERTO_SOURCE = 8000
    PUERTO_SPLITTER = 8001
    PUERTO_PLAYER = 8002
    SOURCE_HOST = '127.0.0.1'
    SPLITTER_HOST = '127.0.0.1'    
    CHANNEL = 'ejemplo.ogg'
    GET_MESSAGE = "GET /" + CHANNEL + " HTTP/1.1\r\n\r\n"
    SOURCE = (SOURCE_HOST, PUERTO_SOURCE)
    SPLITTER = (SPLITTER_HOST, PUERTO_SPLITTER)
    BLOCK_FORMAT = "H" + str(BLOCK_SIZE) + "s"
    PEER_FORMAT = "4sH"
    

    def __init__(self, source_hostname = SOURCE_HOST, source_port = PUERTO_SOURCE, splitter_host = SPLITTER_HOST, splitter_port = PUERTO_SPLITTER, block_size = BLOCK_SIZE , player_port = PUERTO_PLAYER, channel = CHANNEL, buffer_size = BUFFER_SIZE):
        
        if source_hostname:
            Gatherer.SOURCE_HOST = source_hostname
        if source_port:
            Gatherer.PUERTO_SOURCE = int(source_port)
        if splitter_host:
            Gatherer.SPLITTER_HOST = splitter_host
        if splitter_port:
            Gatherer.PUERTO_SPLITTER = int(splitter_port)
        if block_size:
            Gatherer.BLOCK_SIZE = int(block_size)
        if player_port:
            Gatherer.PUERTO_PLAYER = int(player_port)
        if channel:
            Gatherer.CHANNEL = channel
        if buffer_size:
            Gatherer.BUFFER_SIZE = int(buffer_size)
        
        
        
        
        
        
        
        
        
        self.numero_bloques = 0
        self.punto_reproduccion = None
        self.buffer = [None] * self.BUFFER_SIZE
        self.sock_splitter = self.conectar_socket_splitter()
        self.sock_player = self.esperar_socket_player()
        
        header = self.obtener_header()
        self.enviar_header_player(header)
        
        
        
        self.sock_cluster = self.crear_socket_cluster()
        
        print "Cerrando conexion TCP con el splitter..."
        self.sock_splitter.close()
        
        

    @staticmethod
    def evitar_bloqueo_socket(sock):
        try:
            sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        except:
            pass
    
    
    
    
    
    
    def conectar_socket_splitter(self):
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        Gatherer.evitar_bloqueo_socket(sock)
        sock.connect(('localhost', self.PUERTO_SPLITTER))
        print "Conectado al splitter correctamente"
        return sock
        
    
    
    def esperar_socket_player(self):
        # Esta funcion inicializa el socket que enviara al reproductor el stream de video,
        # se queda a la espera de la conexion del reproductor y devuelve el socket una vez conectado
        
        
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        Gatherer.evitar_bloqueo_socket(sock)
        sock.bind((self.SOURCE_HOST, self.PUERTO_PLAYER))  # escuchamos al reproductor en el puerto 8002
        sock.listen(0)
        print "Esperando conexion del reproductor en el puerto 8002..."
        sock, player = sock.accept()
        print "Reproductor conectado al gatherer"
        sock.setblocking(0)
        return sock
    
    
    
    # En este punto ya tenemos enlazado al reproductor con nuestro programa
    
    def obtener_header(self):
        # en esta funcion nos conectamos al source y obtener los 10 primeros bloques de video
        
        print "Reclamando header al source..."
        intentos = 0
        sock_source = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        Gatherer.evitar_bloqueo_socket(sock_source)
        sock_source.connect(self.SOURCE)
        sock_source.sendall(self.GET_MESSAGE)
        header = sock_source.recv(self.HEADER_SIZE)
        while len(header) < self.HEADER_SIZE:
            intentos += 1
            if intentos > 3:
                time.sleep(1)
                sock_source.close()
                sock_source = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                sock_source.connect(self.SOURCE)
            
            header += sock_source.recv((self.HEADER_SIZE) - len(header))
        
        sock_source.close()
        print "Header descargado"
        return header
    
    
    
    
    
    def enviar_header_player(self, header):
        print "Enviando header al reproductor..."
        self.sock_player.sendall(header)
    
   
    def crear_socket_cluster(self):
        print "Abriendo conexion UDP ... "
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        Gatherer.evitar_bloqueo_socket(sock)
        sock.bind(('', self.sock_splitter.getsockname()[1]))
        sock.settimeout(1)
        return sock
    
    def recibir_bloque_cluster(self):
        message, sender = self.sock_cluster.recvfrom(struct.calcsize(self.BLOCK_FORMAT))
            
        if sender == (self.SOURCE_HOST, self.PUERTO_SPLITTER):
            print "Bloque recibido del splitter --> ", sender
        else:
            print "Bloque recibido del peer --> ", sender
        
        
        numero_bloque , bloque = struct.unpack(self.BLOCK_FORMAT, message)
        
        numero_bloque = socket.ntohs(numero_bloque)
        
        
        return numero_bloque, bloque
    
    
    def guardar_bloque_buffer(self, numero_bloque, bloque):
        print "Guardando bloque en el buffer en la posicion ", numero_bloque % self.BUFFER_SIZE
        self.buffer[numero_bloque % self.BUFFER_SIZE] = bloque
        self.numero_bloques += 1
    
    def enviar_bloque_player(self):
        print "Enviando al reproductor el bloque", self.punto_reproduccion
        
        if(self.numero_bloques > (self.BUFFER_SIZE / 2)):
            self.sock_player.sendall(self.buffer[self.punto_reproduccion % self.BUFFER_SIZE])
            self.punto_reproduccion = (self.punto_reproduccion + 1) % self.BUFFER_SIZE
    
    
    def go(self):
        
        print "Transmitiendo..."
        self.punto_reproduccion, bloque = self.recibir_bloque_cluster()
        #hacemos la primera recepcion del bloque con el fin de obtener el punto de reproduccion
        self.guardar_bloque_buffer(self.punto_reproduccion, bloque)
        self.enviar_bloque_player()
    
        
        while True:
            self.numero_bloques, bloque = self.recibir_bloque_cluster()
            self.guardar_bloque_buffer(self.numero_bloques, bloque)
            self.enviar_bloque_player()
            
            
            
            


parser = argparse.ArgumentParser(description='Gatherer --> Proyecto p2psp')


parser.add_argument('--player_port')

parser.add_argument('--splitter_host')

parser.add_argument('--splitter_port')

parser.add_argument('--source_hostname')

parser.add_argument('--channel')

parser.add_argument('--source_port')

parser.add_argument('--block_size')

parser.add_argument('--buffer_size')
            



args = parser.parse_known_args()[0]




gatherer = Gatherer(args.source_hostname, args.source_port, args.splitter_host, args.splitter_port, args.block_size, args.player_port, args.channel, args.buffer_size)
gatherer.go()