1 | #!/bin/bash |
---|
2 | #/** |
---|
3 | #@file Protocol.lib |
---|
4 | #@brief Librería o clase Protocol |
---|
5 | #@class FileTransfer |
---|
6 | #@brief Funciones para transmisión de datos |
---|
7 | #@version 0.91 |
---|
8 | #@warning License: GNU GPLv3+ |
---|
9 | #*/ |
---|
10 | |
---|
11 | |
---|
12 | #/** |
---|
13 | # ogCheckStringInGroup |
---|
14 | #@brief Función para determinar si el elemento pertenece a un conjunto |
---|
15 | #@param 1 elemento a comprobar |
---|
16 | #@param 2 grupo de elementos para comprobar tipo "valor1 valor2 valor3" |
---|
17 | #@return 0 si pertenece al grupo |
---|
18 | #@return 1 si NO pertenece al grupo |
---|
19 | #@exception OG_ERR_FORMAT formato incorrecto. |
---|
20 | #@note |
---|
21 | #@TODO: |
---|
22 | #@version 0.91 - Definición de |
---|
23 | #@author Antonio Doblas Viso, Universidad de Málaga |
---|
24 | #@date 2010/05/09 |
---|
25 | #*/ ## |
---|
26 | #/** |
---|
27 | ogCheckStringInGroup () |
---|
28 | { |
---|
29 | # Si se solicita, mostrar ayuda. |
---|
30 | if [ "$*" == "help" ]; then |
---|
31 | ogHelp "$FUNCNAME str_elemento str_grupo" \ |
---|
32 | "$FUNCNAME full-duplex \"full-duplex half-duplex broadcast\" " |
---|
33 | return |
---|
34 | fi |
---|
35 | |
---|
36 | # Error si no se recibe 2 parámetro. |
---|
37 | [ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
38 | |
---|
39 | |
---|
40 | for i in `echo $2` |
---|
41 | do |
---|
42 | if [ "$1" == "$i" ] |
---|
43 | then |
---|
44 | return 0 |
---|
45 | fi |
---|
46 | done |
---|
47 | |
---|
48 | return 1 |
---|
49 | } |
---|
50 | |
---|
51 | #/** |
---|
52 | # ogCheckStringInReg |
---|
53 | #@brief Función para determinar si el elemento contiene una "expresión regular" |
---|
54 | #@param 1 elemento a comprobar |
---|
55 | #@param 2 expresión regular" |
---|
56 | #@return 0 si coincide con la expresión |
---|
57 | #@return 1 si NO coincide con la expresión |
---|
58 | #@exception OG_ERR_FORMAT formato incorrecto. |
---|
59 | #@note |
---|
60 | #@TODO: |
---|
61 | #@version 0.91 - Definición de |
---|
62 | #@author Antonio Doblas Viso, Universidad de Málaga |
---|
63 | #@date 2010/05/09 |
---|
64 | #*/ ## |
---|
65 | #/** |
---|
66 | |
---|
67 | |
---|
68 | ogCheckStringInReg() |
---|
69 | { |
---|
70 | |
---|
71 | local REG |
---|
72 | |
---|
73 | # Si se solicita, mostrar ayuda. |
---|
74 | if [ "$*" == "help" ]; then |
---|
75 | ogHelp "$FUNCNAME str_elemento str_expresión_regular" \ |
---|
76 | "$FUNCNAME 50M \"^[0-9]{1,2}\M$\" " |
---|
77 | return |
---|
78 | fi |
---|
79 | |
---|
80 | # Error si no se recibe 2 parámetro. |
---|
81 | [ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
82 | |
---|
83 | REG=$2 |
---|
84 | [[ $1 =~ $REG ]] && return 0 || return 1 |
---|
85 | } |
---|
86 | |
---|
87 | |
---|
88 | |
---|
89 | #/** |
---|
90 | # ogCheckIpAddress |
---|
91 | #@brief Función para determinar si una cadena es una dirección ipv4 válida |
---|
92 | #@param 1 string de la ip a comprobar |
---|
93 | #@return 0 si es una dirección válida |
---|
94 | #@return 1 si NO es una dirección válida |
---|
95 | #@exception OG_ERR_FORMAT formato incorrecto. |
---|
96 | #@note |
---|
97 | #@TODO: |
---|
98 | #@version 0.91 - Definición de |
---|
99 | #@author Antonio Doblas Viso, Universidad de Málaga |
---|
100 | #@date 2010/05/09 |
---|
101 | #*/ ## |
---|
102 | #/** |
---|
103 | ogCheckIpAddress() |
---|
104 | { |
---|
105 | local REG IP arrIP |
---|
106 | |
---|
107 | # Si se solicita, mostrar ayuda. |
---|
108 | if [ "$*" == "help" ]; then |
---|
109 | ogHelp "$FUNCNAME str_IpAddressToCheck" \ |
---|
110 | "$FUNCNAME 192.18.35.3" |
---|
111 | return |
---|
112 | fi |
---|
113 | |
---|
114 | # Error si no se recibe 1 parámetro. |
---|
115 | [ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
116 | |
---|
117 | |
---|
118 | IP=$1 |
---|
119 | REG="^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$" |
---|
120 | if [[ "$IP" =~ $REG ]] |
---|
121 | then |
---|
122 | OIFS=$IFS; |
---|
123 | IFS='.' ; |
---|
124 | arrIP=($IP) |
---|
125 | IFS=$OIFS |
---|
126 | if [[ ${arrIP[0]} -le 255 && ${arrIP[1]} -le 255 && ${arrIP[2]} -le 255 && ${arrIP[3]} -le 255 ]] |
---|
127 | then |
---|
128 | return 0 |
---|
129 | fi |
---|
130 | fi |
---|
131 | return 1 |
---|
132 | } |
---|
133 | |
---|
134 | |
---|
135 | |
---|
136 | #/** |
---|
137 | # ogGenerateSintaxMcast |
---|
138 | #@brief Función para generar la instrucción de ejucción la transferencia de datos multicast |
---|
139 | #@param 1 Tipo de operación [ SENDPARTITION RECEIVERPARTITION SENDFILE RECEIVERFILE ] |
---|
140 | #@param 2 Sesión Mulicast |
---|
141 | #@param 3 Dispositivo (opción PARTITION) o fichero(opción FILE) que será enviado. |
---|
142 | #@param 4 Tools de clonación (opcion PARTITION) |
---|
143 | #@param 5 Tools de compresion (opcion PARTITION) |
---|
144 | #@return instrucción para ser ejecutada. |
---|
145 | #@exception OG_ERR_FORMAT formato incorrecto. |
---|
146 | #@note Requisitos: upd-cast 2009 |
---|
147 | #@TODO: localvar check versionudp |
---|
148 | #@version 0.91 - Definición de FileTransfer |
---|
149 | #@author Antonio Doblas Viso, Universidad de Málaga |
---|
150 | #@date 2010/05/09 |
---|
151 | #*/ ## |
---|
152 | #/** |
---|
153 | # |
---|
154 | |
---|
155 | |
---|
156 | function ogGenerateSintaxMcast () |
---|
157 | { |
---|
158 | |
---|
159 | local ISUDPCAST PARM SESSION SESSIONPARM MODE PORTBASE |
---|
160 | local METHOD ADDRESS BITRATE NCLIENTS MAXTIME CERROR |
---|
161 | local TOOL LEVEL DEVICE MBUFFER SINTAXSERVER SINTAXCLIENT |
---|
162 | |
---|
163 | # Si se solicita, mostrar ayuda. |
---|
164 | if [ "$*" == "help" -o "$2" == "help" ]; then |
---|
165 | ogHelp "$FUNCNAME SENDPARTITION str_sessionSERVER str_device str_tools str_level" \ |
---|
166 | "$FUNCNAME RECEIVERPARTITION str_sessionCLIENT str_device str_tools str_level "\ |
---|
167 | "$FUNCNAME SENDFILE str_sessionSERVER str_file "\ |
---|
168 | "$FUNCNAME RECEIVERFILE str_sessionCLIENT str_file " |
---|
169 | return |
---|
170 | fi |
---|
171 | |
---|
172 | #si no tenemos updcast o su version superior 2009 udpcast error. |
---|
173 | ISUDPCAST=$(udp-receiver --help 2>&1) |
---|
174 | echo $ISUDPCAST | grep start-timeout > /dev/null || ogRaiseError $OG_ERR_FORMAT "upd-cast no existe o version antigua -requerida 2009-"|| return $? |
---|
175 | |
---|
176 | |
---|
177 | # Error si no se reciben $PARM parámetros. |
---|
178 | echo "$1" | grep "PARTITION" > /dev/null && PARM=5 || PARM=3 |
---|
179 | [ "$#" -eq "$PARM" ] || ogRaiseError $OG_ERR_FORMAT "sin parametros"|| return $? |
---|
180 | |
---|
181 | |
---|
182 | # 1er param check |
---|
183 | ogCheckStringInGroup "$1" "SENDPARTITION RECEIVERPARTITION SENDFILE RECEIVERFILE" || ogRaiseError $OG_ERR_FORMAT "1st param: $1" || ERROR=1 #return $? |
---|
184 | |
---|
185 | # 2º param check |
---|
186 | echo "$1" | grep "SEND" > /dev/null && MODE=server || MODE=client |
---|
187 | |
---|
188 | #TODO: diferenciamos o no???? |
---|
189 | #controlamos todos los parametros de la sessión multicast. |
---|
190 | #Dejo para el cliente como 6, pero solo necesita el primero. |
---|
191 | [ $MODE == "client" ] && SESSIONPARM=6 || SESSIONPARM=6 |
---|
192 | OIFS=$IFS; IFS=':' ; SESSION=($2); IFS=$OIFS |
---|
193 | [[ ${#SESSION[*]} == $SESSIONPARM ]] || ogRaiseError $OG_ERR_FORMAT "parametros session multicast no completa" || ERROR=1# return $? |
---|
194 | #controlamos el PORTBASE de la sesion. Comun.- |
---|
195 | PORTBASE=${SESSION[0]} |
---|
196 | ogCheckStringInGroup ${SESSION[0]} "9000 9002 9004 9006 9008 9010" || ogRaiseError $OG_ERR_FORMAT "McastSession portbase ${SESSION[0]}" || ERROR=1 #return $? |
---|
197 | if [ $MODE == "server" ] |
---|
198 | then |
---|
199 | ogCheckStringInGroup ${SESSION[1]} "full-duplex FULL-DUPLEX half-duplex HALF-DUPLEX broadcast BROADCAST" || ogRaiseError $OG_ERR_FORMAT "McastSession method ${SESSION[1]}" || ERROR=1 #return $? |
---|
200 | METHOD=${SESSION[1]} |
---|
201 | ogCheckIpAddress ${SESSION[2]} || ogRaiseError $OG_ERR_FORMAT "McastSession address ${SESSION[2]}" || ERROR=1 #return $? |
---|
202 | ADDRESS=${SESSION[2]} |
---|
203 | ogCheckStringInReg ${SESSION[3]} "^[0-9]{1,2}\M$" || ogRaiseError $OG_ERR_FORMAT "McastSession bitrate ${SESSION[3]}" || ERROR=1 # return $? |
---|
204 | BITRATE=${SESSION[3]} |
---|
205 | ogCheckStringInReg ${SESSION[4]} "^[0-9]{1,10}$" || ogRaiseError $OG_ERR_FORMAT "McastSession nclients ${SESSION[4]}" || ERROR=1 # return $? |
---|
206 | NCLIENTS=${SESSION[4]} |
---|
207 | ogCheckStringInReg ${SESSION[5]} "^[0-9]{1,10}$" || ogRaiseError $OG_ERR_FORMAT "McastSession maxtime ${SESSION[5]}" || ERROR=1 # return $? |
---|
208 | MAXTIME=${SESSION[5]} |
---|
209 | fi |
---|
210 | |
---|
211 | |
---|
212 | |
---|
213 | #3er param check - que puede ser un dispositvo o un fichero. |
---|
214 | #ogGetPath "$3" > /dev/null || ogRaiseError $OG_ERR_NOTFOUND " device or file $3" || ERROR=1 #return $? |
---|
215 | DEVICE=$3 |
---|
216 | |
---|
217 | #4 y 5 param check . solo si es sobre particiones. |
---|
218 | if [ "$PARM" == "5" ] |
---|
219 | then |
---|
220 | # 4 param check |
---|
221 | ogCheckStringInGroup "$4" "partclone partimage ntfsclone" || ogRaiseError $OG_ERR_NOTFOUND " herramienta $4 no soportada" || ERROR=1 #return $? |
---|
222 | TOOL=$4 |
---|
223 | ogCheckStringInGroup "$5" "lzop gzip 0 1" || ogRaiseError $OG_ERR_NOTFOUND " compresor $5 no valido" || ERROR=1 #return $? |
---|
224 | LEVEL=$5 |
---|
225 | fi |
---|
226 | |
---|
227 | [ "$ERROR" == "1" ] && return 1 |
---|
228 | |
---|
229 | # Valores estandar no configurables. |
---|
230 | CERROR="8x8/128" |
---|
231 | |
---|
232 | # opción del usuo de tuberia intermedia en memoria mbuffer. |
---|
233 | which mbuffer > /dev/null && MBUFFER=" --pipe 'mbuffer -m 20M' " |
---|
234 | |
---|
235 | # Generamos la instrucción base de multicast -Envio,Recepcion- |
---|
236 | SINTAXSERVER="udp-sender $MBUFFER --portbase $PORTBASE --$METHOD --mcast-data-address $ADDRESS --fec $CERROR --max-bitrate $BITRATE --ttl 1 --min-clients $NCLIENTS --max-wait $MAXTIME " |
---|
237 | SINTAXCLIENT="udp-receiver $MBUFFER --portbase $PORTBASE " |
---|
238 | |
---|
239 | |
---|
240 | case "$1" in |
---|
241 | SENDPARTITION) |
---|
242 | PROG1=`ogGenerateSintaxForCreateImage $DEVICE " " $TOOL $LEVEL | awk -F"|" '{print $1 "|" $3}' | tr -d ">"` |
---|
243 | echo "$PROG1 | $SINTAXSERVER" |
---|
244 | ;; |
---|
245 | RECEIVERPARTITION) |
---|
246 | COMPRESSOR=`ogGenerateSintaxForRestoreImage " " $DEVICE $TOOL $LEVEL | awk -F\| '{print $1}'` |
---|
247 | TOOLS=`ogGenerateSintaxForRestoreImage " " $DEVICE $TOOL $LEVEL | awk -F\| '{print $3}'` |
---|
248 | echo "$SINTAXCLIENT | $COMPRESSOR | $TOOLS " |
---|
249 | ;; |
---|
250 | SENDFILE) |
---|
251 | echo "$SINTAXSERVER --file $3" |
---|
252 | ;; |
---|
253 | RECEIVERFILE) |
---|
254 | echo "$SINTAXCLIENT --file $3" |
---|
255 | ;; |
---|
256 | *) |
---|
257 | ;; |
---|
258 | esac |
---|
259 | } |
---|
260 | |
---|
261 | |
---|
262 | |
---|
263 | #/** |
---|
264 | # ogMcastSendFile [ str_repo | int_ndisk int_npart ] /Relative_path_file sessionMulticast |
---|
265 | #@brief Envía un fichero por multicast ORIGEN(fichero) DESTINO(sessionmulticast) |
---|
266 | #@param (2 parámetros) $1 path_aboluto_fichero $2 sesionMcast |
---|
267 | #@param (3 parámetros) $1 Contenedor REPO|CACHE $2 path_absoluto_fichero $3 sesionMulticast |
---|
268 | #@param (4 parámetros) $1 disk $2 particion $3 path_absoluto_fichero $4 sesionMulticast |
---|
269 | #@return |
---|
270 | #@exception OG_ERR_FORMAT formato incorrecto. |
---|
271 | #@note Requisitos: |
---|
272 | #@version 0.91 - Definición de Protocol.lib |
---|
273 | #@author Antonio Doblas Viso, Universidad de Málaga |
---|
274 | #@date 2010/05/09 |
---|
275 | #*/ ## |
---|
276 | #/** |
---|
277 | # |
---|
278 | |
---|
279 | function ogMcastSendFile () |
---|
280 | { |
---|
281 | # Variables locales. |
---|
282 | local ARGS SOURCE TARGET COMMAND DEVICE |
---|
283 | #ARGS usado para controlar ubicación de la sesion multicast |
---|
284 | |
---|
285 | # Si se solicita, mostrar ayuda. |
---|
286 | if [ "$*" == "help" ]; then |
---|
287 | ogHelp "$FUNCNAME [str_REPOSITORY] [int_ndisk int_npart] /Relative_path_file sesionMcast" \ |
---|
288 | "$FUNCNAME 1 1 /aula1/winxp.img sesionMcast" \ |
---|
289 | "$FUNCNAME REPO /aula1/ubuntu.iso sesionMcast" \ |
---|
290 | "$FUNCNAME CACHE /aula1/winxp.img sesionMcast" \ |
---|
291 | "$FUNCNAME /opt/opengnsys/images/aula1/hd500.vmx sesionMcast" |
---|
292 | return |
---|
293 | fi |
---|
294 | |
---|
295 | ARGS="$@" |
---|
296 | case "$1" in |
---|
297 | /*) # Camino completo. */ (Comentrio Doxygen) |
---|
298 | SOURCE=$(ogGetPath "$1") |
---|
299 | ARG=2 |
---|
300 | DEVICE="$1" |
---|
301 | ;; |
---|
302 | [1-9]*) # ndisco npartición. |
---|
303 | SOURCE=$(ogGetPath "$1" "$2" "$3") |
---|
304 | ARG=4 |
---|
305 | DEVICE="$1 $2 $3" |
---|
306 | ;; |
---|
307 | *) # Otros: repo, cache, cdrom (no se permiten caminos relativos). |
---|
308 | SOURCE=$(ogGetPath "$1" "$2") |
---|
309 | ARG=3 |
---|
310 | DEVICE="$1 $2 " |
---|
311 | ;; |
---|
312 | esac |
---|
313 | |
---|
314 | |
---|
315 | # Error si no se reciben los argumentos ARG necesarios según la opcion. |
---|
316 | [ $# == "$ARG" ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
317 | |
---|
318 | # Comprobar fichero origen |
---|
319 | ogGetPath $SOURCE &> /dev/null || ogRaiseError $OG_ERR_NOTFOUND " device or file $DEVICE not found" || return $? |
---|
320 | |
---|
321 | SESSION=${!ARG} |
---|
322 | # llamando a la funcion con param1 session $SESSION y param2 $SOURCE |
---|
323 | COMMAND=`ogGenerateSintaxMcast "SENDFILE" "$SESSION" "$SOURCE"` |
---|
324 | echo $COMMAND |
---|
325 | eval $COMMAND |
---|
326 | } |
---|
327 | |
---|
328 | |
---|
329 | |
---|
330 | #/** |
---|
331 | # ogMcastReceiverFile sesion Multicast [ str_repo | int_ndisk int_npart ] /Relative_path_file |
---|
332 | #@brief Recibe un fichero multicast ORIGEN(sesionmulticast) DESTINO(fichero) |
---|
333 | #@param (2 parámetros) $1 sesionMcastCLIENT $2 path_aboluto_fichero_destino |
---|
334 | #@param (3 parámetros) $1 sesionMcastCLIENT $2 Contenedor REPO|CACHE $3 path_absoluto_fichero_destino |
---|
335 | #@param (4 parámetros) $1 sesionMcastCLIENT $2 disk $3 particion $4 path_absoluto_fichero_destino |
---|
336 | #@return |
---|
337 | #@exception OG_ERR_FORMAT formato incorrecto. |
---|
338 | #@note Requisitos: |
---|
339 | #@version 0.91 - Definición de Protocol.lib |
---|
340 | #@author Antonio Doblas Viso, Universidad de Málaga |
---|
341 | #@date 2010/05/09 |
---|
342 | #*/ ## |
---|
343 | #/** |
---|
344 | # |
---|
345 | |
---|
346 | ogMcastReceiverFile () |
---|
347 | { |
---|
348 | |
---|
349 | # Variables locales. |
---|
350 | local ARGS ARG TARGETDIR TARGETFILE |
---|
351 | |
---|
352 | |
---|
353 | # Si se solicita, mostrar ayuda. |
---|
354 | if [ "$*" == "help" ]; then |
---|
355 | ogHelp "$FUNCNAME" "$FUNCNAME [ str_portMcast] [ [Relative_path_file] | [str_REPOSITORY path_file] | [int_ndisk int_npart path_file ] ]" \ |
---|
356 | "$FUNCNAME 9000 /PS1_PH1.img" \ |
---|
357 | "$FUNCNAME 9000 CACHE /aula1/PS2_PH4.img" \ |
---|
358 | "$FUNCNAME 9000 1 1 /isos/linux.iso" |
---|
359 | return |
---|
360 | fi |
---|
361 | |
---|
362 | ARGS="$@" |
---|
363 | case "$2" in |
---|
364 | /*) # Camino completo. */ (Comentrio Doxygen) |
---|
365 | TARGETDIR=$(ogGetParentPath "$2") |
---|
366 | ARG=2 |
---|
367 | ;; |
---|
368 | [1-9]*) # ndisco npartición. |
---|
369 | TARGETDIR=$(ogGetParentPath "$2" "$3" "$4") |
---|
370 | ARG=4 |
---|
371 | ;; |
---|
372 | *) # Otros: repo, cache, cdrom (no se permiten caminos relativos). |
---|
373 | TARGETDIR=$(ogGetParentPath "$2" "$3") |
---|
374 | ARG=3 |
---|
375 | ;; |
---|
376 | esac |
---|
377 | |
---|
378 | # Error si no se reciben los argumentos ARG necesarios según la opcion. |
---|
379 | [ $# == "$ARG" ] || ogRaiseError $OG_ERR_FORMAT "Parametros no admitidos"|| return $? |
---|
380 | |
---|
381 | #obtenemos el nombre del fichero a descargar. |
---|
382 | TARGETFILE=`basename ${!ARG}` |
---|
383 | |
---|
384 | #generamos la instrucción a ejecutar. |
---|
385 | COMMAND=`ogGenerateSintaxMcast RECEIVERFILE "$1" $TARGETDIR/$TARGETFILE ` |
---|
386 | echo $COMMAND |
---|
387 | eval $COMMAND |
---|
388 | } |
---|
389 | |
---|
390 | |
---|
391 | ########################################## |
---|
392 | ############## funciones torrent |
---|
393 | #/** |
---|
394 | # ogTorrentStart [ str_repo | int_ndisk int_npart ] Relative_path_file.torrent | SessionProtocol |
---|
395 | #@brief Función iniciar P2P - requiere un tracker para todos los modos, y un seeder para los modos peer y leecher y los ficheros .torrent. |
---|
396 | #@param str_pathDirectory str_Relative_path_file |
---|
397 | #@param int_disk int_partition str_Relative_path_file |
---|
398 | #@param str_REPOSITORY(CACHE - LOCAL) str_Relative_path_file |
---|
399 | #@param (2 parámetros) $1 path_aboluto_fichero_torrent $2 Parametros_Session_Torrent |
---|
400 | #@param (3 parámetros) $1 Contenedor CACHE $2 path_absoluto_fichero_Torrent $3 Parametros_Session_Torrent |
---|
401 | #@param (4 parámetros) $1 disk $2 particion $3 path_absoluto_fichero_Torrent 4$ Parametros_Session_Torrent |
---|
402 | |
---|
403 | #@return |
---|
404 | #@note |
---|
405 | #@todo: |
---|
406 | #@version 0.1 - Integración para OpenGNSys. |
---|
407 | #@author Antonio J. Doblas Viso. Universidad de Málaga |
---|
408 | #@date |
---|
409 | #@version 0.2 - Chequeo del tamaño de imagen descargado. |
---|
410 | #@author Irina . Univesidad de Sevilla. |
---|
411 | #@date |
---|
412 | #@version 0.3 - Control de los modos de operación, y estado de descarga. |
---|
413 | #@author Antonio J. Doblas Viso. Univesidad de Málaga. |
---|
414 | #@date |
---|
415 | #@version 0.4 - Enviadando señal (2) a ctorrent permiendo la comunicación final con tracker |
---|
416 | #@author Antonio J. Doblas Viso. Univesidad de Málaga. |
---|
417 | #@date |
---|
418 | #*/ ## |
---|
419 | |
---|
420 | #protocoloTORRENT mode:time |
---|
421 | #mode=seeder -> Dejar el equipo seedeando hasta que transcurra el tiempo indicado o un kill desde consola, |
---|
422 | #mode=peer -> seedear mientras descarga |
---|
423 | #mode=leecher -> NO seedear mientras descarga |
---|
424 | #time tiempo que una vez descargada la imagen queremos dejar al cliente como seeder. |
---|
425 | |
---|
426 | ogTorrentStart () |
---|
427 | { |
---|
428 | |
---|
429 | # Variables locales. |
---|
430 | local ARGS ARG TARGETDIR TARGETFILE SESSION ERROR |
---|
431 | ERROR=0 |
---|
432 | |
---|
433 | # Si se solicita, mostrar ayuda. |
---|
434 | if [ "$*" == "help" ]; then |
---|
435 | ogHelp "$FUNCNAME $FUNCNAME [ str_repo] [ [Relative_path_fileTORRENT] | [str_REPOSITORY path_fileTORRENT] | [int_ndisk int_npart path_fileTORRENT ] ] SessionTorrent" \ |
---|
436 | "$FUNCNAME CACHE /PS1_PH1.img.torrent seeder:10000" \ |
---|
437 | "$FUNCNAME /opt/opengnsys/cache/linux.iso peer:60" \ |
---|
438 | "$FUNCNAME 1 1 /linux.iso.torrent leecher:60" |
---|
439 | return |
---|
440 | fi |
---|
441 | |
---|
442 | case "$1" in |
---|
443 | /*) # Camino completo. */ (Comentrio Doxygen) |
---|
444 | SOURCE=$(ogGetPath "$1") |
---|
445 | ARG=2 |
---|
446 | ;; |
---|
447 | [1-9]*) # ndisco npartición. |
---|
448 | SOURCE=$(ogGetPath "$1" "$2" "$3") |
---|
449 | ARG=4 |
---|
450 | ;; |
---|
451 | *) # Otros: Solo cache (no se permiten caminos relativos). |
---|
452 | SOURCE=$(ogGetPath "$1" "$2" 2>/dev/null) |
---|
453 | ARG=3 |
---|
454 | ;; |
---|
455 | esac |
---|
456 | |
---|
457 | # Error si no se reciben los argumentos ARG necesarios según la opcion. |
---|
458 | [ $# == "$ARG" ] || ogRaiseError $OG_ERR_FORMAT "Parametros no admitidos"|| return $? |
---|
459 | |
---|
460 | #controlar source, que no se haga al repo. |
---|
461 | if [ $ARG == "3" ] |
---|
462 | then |
---|
463 | ogCheckStringInGroup "$1" "CACHE cache" || ogRaiseError $OG_ERR_FORMAT "La descarga torrent solo se hace desde local, copia el torrent a la cache y realiza la operación desde esa ubicación" || return $? |
---|
464 | fi |
---|
465 | if [ $ARG == "2" ] |
---|
466 | then |
---|
467 | if `ogCheckStringInReg "$1" "^/opt/opengnsys/images"` |
---|
468 | then |
---|
469 | ogRaiseError $OG_ERR_FORMAT "La descarga torrent solo se hace desde local, copia el torrent a la cache y realiza la operación desde esa ubicación" |
---|
470 | return $? |
---|
471 | fi |
---|
472 | fi |
---|
473 | |
---|
474 | #controlar el source, para que sea un torrent. |
---|
475 | ctorrent -x ${SOURCE} &> /dev/null; [ $? -eq 0 ] || ogRaiseError $OG_ERR_NOTFOUND "${ARGS% $*}" || return $? |
---|
476 | |
---|
477 | TARGET=`echo $SOURCE | awk -F.torrent '{print $1}'` |
---|
478 | DIRSOURCE=`ogGetParentPath $SOURCE` |
---|
479 | cd $DIRSOURCE |
---|
480 | |
---|
481 | |
---|
482 | |
---|
483 | SESSION=${!ARG} |
---|
484 | OIFS=$IFS; IFS=':' ; SESSION=($SESSION); IFS=$OIFS |
---|
485 | [[ ${#SESSION[*]} == 2 ]] || ogRaiseError $OG_ERR_FORMAT "parametros session Torrent no completa: modo:tiempo" || ERROR=1# return $? |
---|
486 | #controlamos el modo de operación del cliente- |
---|
487 | ogCheckStringInGroup ${SESSION[0]} "seeder SEEDER peer PEER leecher LEECHER" || ogRaiseError $OG_ERR_FORMAT "valor modo Torrent no valido ${SESSION[0]}" || ERROR=1 #return $? |
---|
488 | MODE=${SESSION[0]} |
---|
489 | #contolamos el tiempo para el seeder o una vez descargada la imagen como peer o leecher. |
---|
490 | ogCheckStringInReg ${SESSION[1]} "^[0-9]{1,10}$" || ogRaiseError $OG_ERR_FORMAT "valor tiempo no valido ${SESSION[1]}" || ERROR=1 # return $? |
---|
491 | TIME=${SESSION[1]} |
---|
492 | # si ha habido error en el control de parametros error. |
---|
493 | [ "$ERROR" == "1" ] && return 1 |
---|
494 | |
---|
495 | |
---|
496 | #SINTAXSEEDER="echo MODE seeder ctorrent ; (sleep \$TIME && kill -9 \`pidof ctorrent\`) & ; ctorrent \${SOURCE}" |
---|
497 | |
---|
498 | # si No fichero .bf, y Si fichero destino imagen ya descargada y su chequeo fue comprobado en su descarga inicial. |
---|
499 | if [ ! -f ${SOURCE}.bf -a -f ${TARGET} ] |
---|
500 | then |
---|
501 | echo "imagen ya descargada" |
---|
502 | case "$MODE" in |
---|
503 | seeder|SEEDER) |
---|
504 | echo "MODE seeder ctorrent" #### ${SOURCE} -X 'sleep $TIME; kill -9 \$(pidof ctorrent)' -C 100" |
---|
505 | (sleep $TIME && kill -2 `pidof ctorrent`) & |
---|
506 | ctorrent -f ${SOURCE} |
---|
507 | esac |
---|
508 | return 0 |
---|
509 | fi |
---|
510 | |
---|
511 | #Si no existe bf ni fichero destino descarga inicial. |
---|
512 | if [ ! -f ${SOURCE}.bf -a ! -f ${TARGET} ] |
---|
513 | then |
---|
514 | OPTION=DOWNLOAD |
---|
515 | echo "descarga inicial" |
---|
516 | fi |
---|
517 | |
---|
518 | # Si fichero bf descarga anterior no completada -. |
---|
519 | if [ -f ${SOURCE}.bf -a -f ${TARGET} ] |
---|
520 | then |
---|
521 | echo Continuar con Descargar inicial no terminada. |
---|
522 | OPTION=DOWNLOAD |
---|
523 | fi |
---|
524 | |
---|
525 | if [ "$OPTION" == "DOWNLOAD" ] |
---|
526 | then |
---|
527 | case "$MODE" in |
---|
528 | peer|PEER) |
---|
529 | echo "Donwloading Torrent as peer" ### echo "ctorrent -X 'sleep $TIME; kill -9 \$(pidof ctorrent)' -C 100 $SOURCE -s $TARGET -b ${SOURCE}.bf" |
---|
530 | ctorrent -f -X "sleep $TIME; kill -2 \$(pidof ctorrent)" -C 100 ${SOURCE} -s ${TARGET} -b ${SOURCE}.bf |
---|
531 | ;; |
---|
532 | leecher|LEECHER) |
---|
533 | echo "Donwloading Torrent as leecher" # echo "ctorrent ${SOURCE} -X 'sleep $TIME; kill -9 \$(pidof ctorrent)' -C 100 -U 0" |
---|
534 | ctorrent ${SOURCE} -X "sleep $TIME; kill -2 \$(pidof ctorrent)" -C 100 -U 0 |
---|
535 | ;; |
---|
536 | seeder|SEEDER) |
---|
537 | echo "MODE seeder ctorrent" #### ${SOURCE} -X 'sleep $TIME; kill -9 \$(pidof ctorrent)' -C 100" |
---|
538 | ctorrent -f -X "sleep $TIME; kill -2 \$(pidof ctorrent)" -C 100 ${SOURCE} -s ${TARGET} -b ${SOURCE}.bf |
---|
539 | ;; |
---|
540 | esac |
---|
541 | fi |
---|
542 | if [ "$OPTION" == "DOWNLOAD" ] |
---|
543 | then |
---|
544 | echo "comprobando el fichero descargado. Espere: " |
---|
545 | #echo " ctorrent -s $TARGET -c $SOURCE | tail -n1 | cut -f2 -d\( " |
---|
546 | TOTAL=`ctorrent -s $TARGET -c $SOURCE | tail -n1 | cut -f2 -d\(` |
---|
547 | if [ "$TOTAL" == "100%)" ] |
---|
548 | then |
---|
549 | return 0 |
---|
550 | else |
---|
551 | return 1 |
---|
552 | fi |
---|
553 | |
---|
554 | fi |
---|
555 | |
---|
556 | cd /tmp |
---|
557 | } |
---|
558 | |
---|
559 | #/** |
---|
560 | # ogCreateTorrent [ str_repo | int_ndisk int_npart ] Relative_path_file |
---|
561 | #@brief Función para crear el fichero torrent. |
---|
562 | #@param str_pathDirectory str_Relative_path_file |
---|
563 | #@param int_disk int_partition str_Relative_path_file |
---|
564 | #@param str_REPOSITORY(CACHE - LOCAL) str_Relative_path_file |
---|
565 | #@return |
---|
566 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
567 | #@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo. |
---|
568 | #@exception OG_ERR_PARTITION Tipo de partición desconocido o no se puede montar. |
---|
569 | #@exception OG_ERR_NOTOS La partición no tiene instalado un sistema operativo. |
---|
570 | #@note |
---|
571 | #@version 0.1 - Integración para OpenGNSys. |
---|
572 | #@author Antonio J. Doblas Viso. Universidad de Málaga |
---|
573 | #@date |
---|
574 | #@version 0.2 - Integración para btlaunch. |
---|
575 | #@author Irina . Univesidad de Sevilla. |
---|
576 | #@date |
---|
577 | #*/ ## |
---|
578 | |
---|
579 | ogCreateTorrent () |
---|
580 | { |
---|
581 | # Variables locales. |
---|
582 | local ARGS ARG SOURCE IPTORRENT |
---|
583 | |
---|
584 | |
---|
585 | # Si se solicita, mostrar ayuda. |
---|
586 | if [ "$*" == "help" ]; then |
---|
587 | ogHelp "$FUNCNAME" "$FUNCNAME [str_REPOSITORY] [int_ndisk int_npart] Relative_path_file IpBttrack" \ "$FUNCNAME 1 1 /aula1/winxp 10.1.15.23" \ |
---|
588 | "$FUNCNAME REPO /aula1/winxp 10.1.15.45" |
---|
589 | |
---|
590 | return |
---|
591 | fi |
---|
592 | |
---|
593 | # Error si se quiere crear el fichero en cache y no existe |
---|
594 | [ "$1" != "CACHE" ] || `ogFindCache >/dev/null` || ogRaiseError $OG_ERR_NOTFOUND "CACHE"|| return $? |
---|
595 | |
---|
596 | case "$1" in |
---|
597 | /*) # Camino completo. */ (Comentrio Doxygen) |
---|
598 | SOURCE=$(ogGetPath "$1.img") |
---|
599 | ARG=2 |
---|
600 | ;; |
---|
601 | [1-9]*) # ndisco npartición. |
---|
602 | SOURCE=$(ogGetPath "$1" "$2" "$3.img") |
---|
603 | ARG=4 |
---|
604 | ;; |
---|
605 | *) # Otros: repo, cache, cdrom (no se permiten caminos relativos). |
---|
606 | SOURCE=$(ogGetPath "$1" "$2.img") |
---|
607 | ARG=3 |
---|
608 | ;; |
---|
609 | esac |
---|
610 | |
---|
611 | # Error si no se reciben los argumentos ARG necesarios según la opcion. |
---|
612 | [ $# -eq "$ARG" ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
613 | |
---|
614 | |
---|
615 | # Error si no existe la imagen |
---|
616 | [ $SOURCE ] || ogRaiseError $OG_ERR_NOTFOUND || return $? |
---|
617 | |
---|
618 | [ -r $SOURCE.torrent ] && mv "$SOURCE.torrent" "$SOURCE.torrent.ant" && echo "Esperamos que se refresque el servidor" && sleep 20 |
---|
619 | |
---|
620 | IPTORRENT="${!#}" |
---|
621 | # Si ponemos el path completo cuando creamos el fichero torrent da error |
---|
622 | cd `dirname $SOURCE` |
---|
623 | echo ctorrent -t `basename $SOURCE` -u http://$IPTORRENT:6969/announce -s $SOURCE.torrent |
---|
624 | ctorrent -t `basename $SOURCE` -u http://$IPTORRENT:6969/announce -s $SOURCE.torrent |
---|
625 | |
---|
626 | } |
---|
627 | |
---|
628 | |
---|
629 | |
---|
630 | |
---|
631 | |
---|
632 | |
---|
633 | ############################################################ |
---|
634 | ### FUNCIONES MULTICAST EN DESARROLLO ########## |
---|
635 | |
---|
636 | |
---|
637 | #/** |
---|
638 | # ogMcastSessionCheck [session] |
---|
639 | #@brief Controla los parámetros de una sessión multicast |
---|
640 | #@param str_session |
---|
641 | #@return (nada, por determinar) |
---|
642 | #@exception OG_ERR_FORMAT formato incorrecto. |
---|
643 | #@note Requisitos: |
---|
644 | #@version 0.91 - Definición de FileTransfer |
---|
645 | #@author Antonio Doblas Viso, Universidad de Málaga |
---|
646 | #@date 2010/05/09 |
---|
647 | #*/ ## |
---|
648 | ogMcastSessionCheck () |
---|
649 | { |
---|
650 | echo "en pruebas" |
---|
651 | #sessionSERVER PORTBASE:METHOD:ADDRESS:BITRATE:NCLINTS:MAXTIME:TOOLCLONE:COMPRESSOR |
---|
652 | #sessionCLIENT PORTBASE:TOOLCLONE:COMPRESSOR |
---|
653 | #PORTBASE: 9000 (incluye el 9001), 9002 (incluye el 9003), 9004 |
---|
654 | #METHOD: full-duplex half-duplex brodcast |
---|
655 | #ADDRESS: Dirección habilitada para la transferencia. 339.194.ip2.ip3 |
---|
656 | #BITRATE: 90M 80M 70M |
---|
657 | #NCLIENTS: |
---|
658 | #MAXTIME: |
---|
659 | #TOOLCLONE: |
---|
660 | #COMPRESSOR: |
---|
661 | } |
---|
662 | |
---|
663 | |
---|
664 | #/** |
---|
665 | # ogMcastSessionList |
---|
666 | #@brief Lista las sessiones abiertas multicast. |
---|
667 | #@param (en pruebas) |
---|
668 | #@return (nada, por determinar) |
---|
669 | #@exception OG_ERR_FORMAT formato incorrecto. |
---|
670 | #@note Requisitos: |
---|
671 | #@version 0.91 - Definición de FileTransfer |
---|
672 | #@author Antonio Doblas Viso, Universidad de Málaga |
---|
673 | #@date 2010/05/09 |
---|
674 | #*/ ## |
---|
675 | ogMcastSessionList() |
---|
676 | { |
---|
677 | echo "en preubas" |
---|
678 | #puertos a utilizar 9000 - 9008 => se definen 10 sesiones |
---|
679 | #lsof -i -nP | grep 900 | awk '{print $9}' | awk -F: '{print $2}' | uniq |
---|
680 | } |
---|
681 | |
---|
682 | #/** |
---|
683 | # ogMcastSessionDelete |
---|
684 | #@brief Libera una session Mulitcast. |
---|
685 | #@param (en pruebas) |
---|
686 | #@return (nada, por determinar) |
---|
687 | #@exception OG_ERR_FORMAT formato incorrecto. |
---|
688 | #@note Requisitos: |
---|
689 | #@version 0.91 - Definición de FileTransfer |
---|
690 | #@author Antonio Doblas Viso, Universidad de Málaga |
---|
691 | #@date 2010/05/09 |
---|
692 | #*/ ## |
---|
693 | ogMcastSessionDelete() |
---|
694 | { |
---|
695 | echo "en pruebas" |
---|
696 | } |
---|
697 | |
---|
698 | #/** |
---|
699 | # ogMcastSendPartition |
---|
700 | #@brief Función para enviar el contenido de una partición a multiples particiones remotas. |
---|
701 | #@param |
---|
702 | #@param |
---|
703 | #@param |
---|
704 | #@return |
---|
705 | #@exception |
---|
706 | #@note |
---|
707 | #@todo: |
---|
708 | #@version 0.1 - Integración para OpenGNSys. |
---|
709 | #@author |
---|
710 | #@date |
---|
711 | #*/ ## |
---|
712 | |
---|
713 | ogMcastSendPartition () |
---|
714 | { |
---|
715 | |
---|
716 | # Variables locales |
---|
717 | local PART ERRCODE |
---|
718 | |
---|
719 | # Si se solicita, mostrar ayuda. |
---|
720 | if [ "$*" == "help" ]; then |
---|
721 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npart " \ |
---|
722 | "$FUNCNAME 1 1 " |
---|
723 | return |
---|
724 | fi |
---|
725 | # Error si no se reciben 2 parámetros. |
---|
726 | [ $# -lt 2 ] && ogRaiseError $OG_ERR_FORMAT && return $? |
---|
727 | |
---|
728 | PART=$(ogDiskToDev "$1" "$2") || return $? |
---|
729 | if ogIsLocked $1 $2; then |
---|
730 | ogRaiseError $OG_ERR_LOCKED "$1,$2" |
---|
731 | return $? |
---|
732 | fi |
---|
733 | |
---|
734 | |
---|
735 | echo "en pruebas" |
---|
736 | |
---|
737 | } |
---|
738 | |
---|
739 | |
---|
740 | #/** |
---|
741 | # ogMcastRestorePartition |
---|
742 | #@brief Función para recibir directamente en la partición el contenido de un fichero imagen remoto enviado por multicast. |
---|
743 | #@param |
---|
744 | #@param |
---|
745 | #@param |
---|
746 | #@return |
---|
747 | #@exception |
---|
748 | #@note |
---|
749 | #@todo: |
---|
750 | #@version 0.1 - Integración para OpenGNSys. |
---|
751 | #@author |
---|
752 | #@date |
---|
753 | #*/ ## |
---|
754 | ogMcastRestorePartition () |
---|
755 | { |
---|
756 | echo "en pruebas" |
---|
757 | } |
---|
758 | |
---|
759 | ########################## FUNCIONES TORRENT EN DESARROLLO |
---|
760 | ################################################## |
---|
761 | #/** |
---|
762 | # ogStartTracker |
---|
763 | #@brief Inicia el tracker |
---|
764 | #@param str_pathDirectory str_Relative_path_file |
---|
765 | #@param int_disk int_partition str_Relative_path_file |
---|
766 | #@param str_REPOSITORY(CACHE - LOCAL) str_Relative_path_file |
---|
767 | #@return |
---|
768 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
769 | #@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo. |
---|
770 | #@exception OG_ERR_PARTITION Tipo de partición desconocido o no se puede montar. |
---|
771 | #@exception OG_ERR_NOTOS La partición no tiene instalado un sistema operativo. |
---|
772 | #@note |
---|
773 | #@version 0.1 - Integración para OpenGNSys. |
---|
774 | #@author |
---|
775 | #@date |
---|
776 | #*/ ## |
---|
777 | ogStartTracker () |
---|
778 | { |
---|
779 | echo " bug2: usar el tracker del bittornado. " |
---|
780 | echo " solucion bug2: apt-get autoremove bittorrent python-bittorrent " |
---|
781 | echo "soulución bug2: apt-get install bittornado. " |
---|
782 | echo "#Paso 2: iniciar track " |
---|
783 | local TESTBTT |
---|
784 | TESTBTT=`ps aux | grep bttrack | egrep -v grep | wc -l` |
---|
785 | if [ "$TESTBTT" == "0" ]; then |
---|
786 | bttrack --reannounce_interval 10 --port 6969 --dfile /root/dstate --logfile /root/bttracker.log --save_dfile_interval 10 --timeout_downloaders_interval 10 2>/dev/null & |
---|
787 | else |
---|
788 | echo "bttrack iniciado" |
---|
789 | fi |
---|
790 | |
---|
791 | } |
---|
792 | |
---|
793 | ogShareTorrent () |
---|
794 | { |
---|
795 | # Variables locales. |
---|
796 | local ARGS SOURCE TARGET |
---|
797 | |
---|
798 | |
---|
799 | # Si se solicita, mostrar ayuda. |
---|
800 | if [ "$*" == "help" ]; then |
---|
801 | ogHelp "$FUNCNAME" "$FUNCNAME [str_REPOSITORY] [int_ndisk int_npart] Relative_path_file" \ |
---|
802 | "$FUNCNAME 1 1 /aula1/winxp" \ |
---|
803 | "$FUNCNAME REPO /aula1/winxp" |
---|
804 | |
---|
805 | return |
---|
806 | fi |
---|
807 | |
---|
808 | # Error si se quiere compartir un fichero en cache y no existe la cache |
---|
809 | [ "$1" != "CACHE" ] || `ogFindCache >/dev/null` || ogRaiseError $OG_ERR_NOTFOUND "CACHE"|| return $? |
---|
810 | |
---|
811 | case "$1" in |
---|
812 | /*) # Camino completo. */ (Comentrio Doxygen) |
---|
813 | SOURCE=$(ogGetPath "$1.img") |
---|
814 | ARG=1 |
---|
815 | ;; |
---|
816 | [1-9]*) # ndisco npartición. |
---|
817 | SOURCE=$(ogGetPath "$1" "$2" "$3.img") |
---|
818 | ARG=3 |
---|
819 | ;; |
---|
820 | *) # Otros: repo, cache, cdrom (no se permiten caminos relativos). |
---|
821 | SOURCE=$(ogGetPath "$1" "$2.img" 2>/dev/null) |
---|
822 | ARG=2 |
---|
823 | ;; |
---|
824 | esac |
---|
825 | |
---|
826 | |
---|
827 | # Error si no se reciben los argumentos ARG necesarios según la opcion. |
---|
828 | [ $# -ne "$ARG" ] && ogRaiseError $OG_ERR_FORMAT && return $? |
---|
829 | |
---|
830 | # Comprobar fichero origen |
---|
831 | [ -n "$SOURCE.torrent" ] || ogRaiseError $OG_ERR_NOTFOUND "${ARGS% $*}" || return $? |
---|
832 | |
---|
833 | echo ctorrent $SOURCE.torrent -d |
---|
834 | ctorrent $SOURCE.torrent -d |
---|
835 | |
---|
836 | } |
---|
837 | |
---|
838 | |
---|
839 | ogTorrentReceiverFile () |
---|
840 | { |
---|
841 | # obligatorio disponer de cache |
---|
842 | # origen destino. |
---|
843 | #origen es el .torrent |
---|
844 | # origen: REPO /file.torrent |
---|
845 | # Si REPO, (nfs, http) copiamos a CACHE. |
---|
846 | |
---|
847 | # destino es el fichero final. |
---|
848 | # destino: CACHE /file |
---|
849 | # destino: disk part /file |
---|
850 | |
---|
851 | # Variables locales. |
---|
852 | local ARGS ARG SOURCEDIR SOURCEFILE TARGETDIR TARGETFILE |
---|
853 | |
---|
854 | # Si se solicita, mostrar ayuda. |
---|
855 | if [ "$*" == "help" ]; then |
---|
856 | ogHelp "$FUNCNAME" "$FUNCNAME [str_REPOSITORY] [int_ndisk int_npart] Relative_path_file" \ |
---|
857 | "$FUNCNAME REPO /aula1/winxp.torrent CACHE /aula1/winxp" \ |
---|
858 | "$FUNCNAME REPO /aula1/winxp.torrent 1 1 /iso" |
---|
859 | fi |
---|
860 | # Comprobamos que existe Cache |
---|
861 | `ogFindCache > /dev/null` || ogRaiseError $OG_ERR_NOTFOUND "CACHE" || return $? |
---|
862 | |
---|
863 | # guardamos torrent. controlamos tamaño del fichero y comprobamos con cache. |
---|
864 | ogCopyFile $1 $2 $3 $4 |
---|
865 | |
---|
866 | |
---|
867 | } |
---|
868 | |
---|
869 | ogReceiveTorrent () |
---|
870 | { |
---|
871 | # Variables locales. |
---|
872 | local ARGS ARG TARGETDIR TARGETFILE |
---|
873 | |
---|
874 | |
---|
875 | |
---|
876 | # Si se solicita, mostrar ayuda. |
---|
877 | if [ "$*" == "help" ]; then |
---|
878 | ogHelp "$FUNCNAME" "$FUNCNAME [str_REPOSITORY] [int_ndisk int_npart] Relative_path_file" \ |
---|
879 | "$FUNCNAME 1 1 /aula1/winxp" \ |
---|
880 | "$FUNCNAME REPO /aula1/winxp" \ |
---|
881 | "$FUNCNAME /mnt/sda2/winxp" |
---|
882 | |
---|
883 | |
---|
884 | return |
---|
885 | fi |
---|
886 | |
---|
887 | # Comprobamos que existe Cache |
---|
888 | `ogFindCache > /dev/null` || ogRaiseError $OG_ERR_NOTFOUND "CACHE" || return $? |
---|
889 | |
---|
890 | ARGS="$*" |
---|
891 | case "$1" in |
---|
892 | /*) # Camino completo. */ (Comentrio Doxygen) |
---|
893 | TARGETDIR=$(ogGetParentPath "$1") |
---|
894 | ARG=1 |
---|
895 | ;; |
---|
896 | [1-9]*) # ndisco npartición. |
---|
897 | TARGETDIR=$(ogGetParentPath "$1" "$2" "$3") |
---|
898 | ARG=3 |
---|
899 | ;; |
---|
900 | *) # Otros: repo, cache, cdrom (no se permiten caminos relativos). |
---|
901 | TARGETDIR=$(ogGetParentPath "$1" "$2") |
---|
902 | ARG=2 |
---|
903 | ;; |
---|
904 | esac |
---|
905 | #echo $TARGETDIR |
---|
906 | |
---|
907 | # Error si no se reciben los argumentos ARG necesarios según la opcion. |
---|
908 | [ "$#" -ne "$ARG" ] && ogRaiseError $OG_ERR_FORMAT && return $? |
---|
909 | |
---|
910 | #obtenemos el nombre del fichero a descargar. |
---|
911 | TARGETFILE=`basename ${!ARG}` |
---|
912 | CACHE=`ogMountCache` |
---|
913 | |
---|
914 | # Error si no existe el fichero torrent |
---|
915 | [ -r $TARGETDIR/$TARGETFILE.img.torrent ] || ogRaiseError $OG_ERR_NOTFOUND || return $? |
---|
916 | [ -d ${CACHE}/$OGIMG ] || ogRaiseError $OG_ERR_NOTFOUND || return $? |
---|
917 | |
---|
918 | TimeToWaitForCloseAfterDownload=20 # tiempo que deseamos que el cliente este como semilla, despues de la descarga. |
---|
919 | |
---|
920 | # Si el fichero ya se ha bajado antes de iniciar el ctorrent no se ejecuta el comando kill, siguen encendido siempre |
---|
921 | # Comprobamos si se ha descargado antes, si es asi nos salimos |
---|
922 | Total=`ctorrent -s ${CACHE}/$OGIMG/${TARGETFILE}.img -c $TARGETDIR/$TARGETFILE.img.torrent|tail -n1|cut -f2 -d\(` |
---|
923 | [ $Total == "100%)" ] && return 0 |
---|
924 | |
---|
925 | echo "ctorrent -X 'sleep $TimeToWaitForCloseAfterDownload; kill -9 \$(pidof ctorrent)' $TARGETDIR/$TARGETFILE.img.torrent -s ${CACHE}/$OGIMG/${TARGETFILE}.img -b ${CACHE}/$OGIMG/$TARGETFILE.bf -C 100" |
---|
926 | ctorrent -X "sleep $TimeToWaitForCloseAfterDownload; kill -9 \$(pidof ctorrent)" $TARGETDIR/$TARGETFILE.img.torrent -s ${CACHE}/$OGIMG/${TARGETFILE}.img -b ${CACHE}/$OGIMG/$TARGETFILE.bf -C 100 |
---|
927 | |
---|
928 | [ -r ${CACHE}/$OGIMG/$TARGETFILE.bf ] && rm ${CACHE}/$OGIMG/$TARGETFILE.bf |
---|
929 | } |
---|
930 | |
---|
931 | ogCheckSessionMulticast () |
---|
932 | { |
---|
933 | # $1 tipo de sesion server client |
---|
934 | # 2 la sesisón. |
---|
935 | #variables locales |
---|
936 | local NPARM MODE SESSION |
---|
937 | |
---|
938 | # Si se solicita, mostrar ayuda. |
---|
939 | if [ "$*" == "help" ]; then |
---|
940 | ogHelp "$FUNCNAME str_mode array_session" \ |
---|
941 | "$FUNCNAME client PORTBASE"\ |
---|
942 | "$FUNCNAME server PORTBASE:METHOD:ADDRESS:BITRATE:NCLIENTS:MAXTIME" |
---|
943 | return |
---|
944 | fi |
---|
945 | |
---|
946 | |
---|
947 | # Error si no se recibe 2 parámetro. |
---|
948 | [ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
949 | |
---|
950 | # controlamos el primer parametro. |
---|
951 | ogCheckStringInGroup "$1" "client server" || ogRaiseError $OG_ERR_OUTOFLIMIT || return $? |
---|
952 | |
---|
953 | #controlamos el segundo parametro. La sessión multicast. |
---|
954 | [ $1 == "client" ] && NPARM=1 || NPARM=6 |
---|
955 | OIFS=$IFS; IFS=':' ; SESSION=($2); IFS=$OIFS |
---|
956 | [[ ${#SESSION[*]} == $NPARM ]] || ogRaiseError $OG_ERR_OUTOFLIMIT || return $? |
---|
957 | |
---|
958 | #controlamos el PORTBASE de la sesion. Comun.- |
---|
959 | ogCheckStringInGroup ${SESSION[0]} "9000 9002 9004 9006 9008 9010" || ogRaiseError $OG_ERR_OUTOFLIMIT || return $? |
---|
960 | |
---|
961 | if [ "$1" == "server" ] |
---|
962 | then |
---|
963 | # $METHOD |
---|
964 | ogCheckStringInGroup ${SESSION[1]} "full-duplex half-duplex broadcast" || ogRaiseError $OG_ERR_OUTOFLIMIT || return $? |
---|
965 | # $ADDRESS |
---|
966 | ogCheckIpAddress ${SESSION[2]} || ogRaiseError $OG_ERR_OUTOFLIMIT || return $? |
---|
967 | #BITRATE |
---|
968 | ogCheckStringInReg ${SESSION[3]} "^[1-9]{1,2}M$" || ogRaiseError $OG_ERR_OUTOFLIMIT || return $? |
---|
969 | #NCLIENTS |
---|
970 | ogCheckStringInReg ${SESSION[4]} "^[0-9]{1,10}$" || ogRaiseError $OG_ERR_OUTOFLIMIT || return $? |
---|
971 | #MAXTIME |
---|
972 | ogCheckStringInReg ${SESSION[5]} "^[0-9]{1,10}$" || ogRaiseError $OG_ERR_OUTOFLIMIT " " || return $? |
---|
973 | fi |
---|
974 | return 0 |
---|
975 | } |
---|