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 | #/** |
---|
14 | # ogMcastSyntax |
---|
15 | #@brief Función para generar la instrucción de ejucción la transferencia de datos multicast |
---|
16 | #@param 1 Tipo de operación [ SENDPARTITION RECEIVERPARTITION SENDFILE RECEIVERFILE ] |
---|
17 | #@param 2 Sesión Mulicast |
---|
18 | #@param 3 Dispositivo (opción PARTITION) o fichero(opción FILE) que será enviado. |
---|
19 | #@param 4 Tools de clonación (opcion PARTITION) |
---|
20 | #@param 5 Tools de compresion (opcion PARTITION) |
---|
21 | #@return instrucción para ser ejecutada. |
---|
22 | #@exception OG_ERR_FORMAT formato incorrecto. |
---|
23 | #@note Requisitos: upd-cast 2009 |
---|
24 | #@todo localvar check versionudp |
---|
25 | #@version 0.91 - Definición de FileTransfer |
---|
26 | #@author Antonio Doblas Viso, Universidad de Málaga |
---|
27 | #@date 2010/05/09 |
---|
28 | #*/ ## |
---|
29 | #/** |
---|
30 | # |
---|
31 | |
---|
32 | |
---|
33 | function ogMcastSyntax () |
---|
34 | { |
---|
35 | |
---|
36 | local ISUDPCAST PARM SESSION SESSIONPARM MODE PORTBASE PERROR |
---|
37 | local METHOD ADDRESS BITRATE NCLIENTS MAXTIME CERROR |
---|
38 | local TOOL LEVEL DEVICE MBUFFER SYNTAXSERVER SYNTAXCLIENT |
---|
39 | |
---|
40 | # Si se solicita, mostrar ayuda. |
---|
41 | if [ "$*" == "help" -o "$2" == "help" ]; then |
---|
42 | ogHelp "$FUNCNAME SENDPARTITION str_sessionSERVER str_device str_tools str_level" \ |
---|
43 | "$FUNCNAME RECEIVERPARTITION str_sessionCLIENT str_device str_tools str_level "\ |
---|
44 | "$FUNCNAME SENDFILE str_sessionSERVER str_file "\ |
---|
45 | "$FUNCNAME RECEIVERFILE str_sessionCLIENT str_file " \ |
---|
46 | "sessionServer syntax: portbase:method:mcastaddress:speed:nclients:ntimeWaitingUntilNclients " \ |
---|
47 | "sessionServer example: 9000:full-duplex|half-duplex|broadcast:239.194.17.36:80M:50:60 " \ |
---|
48 | "sessionClient syntax: portbase " \ |
---|
49 | "sessionClient example: 9000 " |
---|
50 | return |
---|
51 | fi |
---|
52 | PERROR=0 |
---|
53 | |
---|
54 | #si no tenemos updcast o su version superior 2009 udpcast error. |
---|
55 | ISUDPCAST=$(udp-receiver --help 2>&1) |
---|
56 | echo $ISUDPCAST | grep start-timeout > /dev/null || ogRaiseError $OG_ERR_FORMAT "upd-cast no existe o version antigua -requerida 2009-"|| return $? |
---|
57 | |
---|
58 | |
---|
59 | # Error si no se reciben $PARM parámetros. |
---|
60 | echo "$1" | grep "PARTITION" > /dev/null && PARM=5 || PARM=3 |
---|
61 | [ "$#" -eq "$PARM" ] || ogRaiseError $OG_ERR_FORMAT "sin parametros"|| return $? |
---|
62 | |
---|
63 | |
---|
64 | # 1er param check |
---|
65 | ogCheckStringInGroup "$1" "SENDPARTITION sendpartition RECEIVERPARTITION receiverpartition SENDFILE sendfile RECEIVERFILE receiverfile" || ogRaiseError $OG_ERR_FORMAT "1st param: $1" || PERROR=1 #return $? |
---|
66 | |
---|
67 | # 2º param check |
---|
68 | echo "$1" | grep "SEND" > /dev/null && MODE=server || MODE=client |
---|
69 | |
---|
70 | #TODO: diferenciamos los paramatros especificos de la sessión multicast |
---|
71 | #SI: controlamos todos los parametros de la sessión multicast. |
---|
72 | [ $MODE == "client" ] && SESSIONPARM=1 || SESSIONPARM=6 |
---|
73 | OIFS=$IFS; IFS=':' ; SESSION=($2); IFS=$OIFS |
---|
74 | |
---|
75 | |
---|
76 | [[ ${#SESSION[*]} == $SESSIONPARM ]] || ogRaiseError $OG_ERR_FORMAT "parametros session multicast no completa" || PERROR=2# return $? |
---|
77 | |
---|
78 | |
---|
79 | #controlamos el PORTBASE de la sesion. Comun.- |
---|
80 | PORTBASE=${SESSION[0]} |
---|
81 | ogCheckStringInGroup ${SESSION[0]} "9000 9002 9004 9006 9008 9010" || ogRaiseError $OG_ERR_FORMAT "McastSession portbase ${SESSION[0]}" || PERROR=3 #return $? |
---|
82 | if [ $MODE == "server" ] |
---|
83 | then |
---|
84 | ogCheckStringInGroup ${SESSION[1]} "full-duplex FULL-DUPLEX half-duplex HALF-DUPLEX broadcast BROADCAST" || ogRaiseError $OG_ERR_FORMAT "McastSession method ${SESSION[1]}" || PERROR=4 #return $? |
---|
85 | METHOD=${SESSION[1]} |
---|
86 | ogCheckIpAddress ${SESSION[2]} || ogRaiseError $OG_ERR_FORMAT "McastSession address ${SESSION[2]}" || PERROR=5 #return $? |
---|
87 | ADDRESS=${SESSION[2]} |
---|
88 | ogCheckStringInReg ${SESSION[3]} "^[0-9]{1,2}\M$" || ogRaiseError $OG_ERR_FORMAT "McastSession bitrate ${SESSION[3]}" || PERROR=6 # return $? |
---|
89 | BITRATE=${SESSION[3]} |
---|
90 | ogCheckStringInReg ${SESSION[4]} "^[0-9]{1,10}$" || ogRaiseError $OG_ERR_FORMAT "McastSession nclients ${SESSION[4]}" || PERROR=7 # return $? |
---|
91 | NCLIENTS=${SESSION[4]} |
---|
92 | ogCheckStringInReg ${SESSION[5]} "^[0-9]{1,10}$" || ogRaiseError $OG_ERR_FORMAT "McastSession maxtime ${SESSION[5]}" || PERROR=8 # return $? |
---|
93 | MAXTIME=${SESSION[5]} |
---|
94 | fi |
---|
95 | |
---|
96 | #3er param check - que puede ser un dispositvo o un fichero. |
---|
97 | #ogGetPath "$3" > /dev/null || ogRaiseError $OG_ERR_NOTFOUND " device or file $3" || PERROR=9 #return $? |
---|
98 | DEVICE=$3 |
---|
99 | |
---|
100 | #4 y 5 param check . solo si es sobre particiones. |
---|
101 | if [ "$PARM" == "5" ] |
---|
102 | then |
---|
103 | # 4 param check |
---|
104 | ogCheckStringInGroup "$4" "partclone partimage ntfsclone" || ogRaiseError $OG_ERR_NOTFOUND " herramienta $4 no soportada" || PERROR=10 #return $? |
---|
105 | TOOL=$4 |
---|
106 | ogCheckStringInGroup "$5" "lzop gzip 0 1" || ogRaiseError $OG_ERR_NOTFOUND " compresor $5 no valido" || PERROR=11 #return $? |
---|
107 | LEVEL=$5 |
---|
108 | fi |
---|
109 | |
---|
110 | |
---|
111 | [ "$PERROR" -gt "0" ] && return 1 |
---|
112 | |
---|
113 | # Valores estandar no configurables. |
---|
114 | CERROR="8x8/128" |
---|
115 | |
---|
116 | # opción del usuo de tuberia intermedia en memoria mbuffer. |
---|
117 | which mbuffer > /dev/null && MBUFFER=" --pipe 'mbuffer -q -m 20M' " |
---|
118 | |
---|
119 | # Generamos la instrucción base de multicast -Envio,Recepcion- |
---|
120 | SYNTAXSERVER="udp-sender $MBUFFER --portbase $PORTBASE --$METHOD --mcast-data-address $ADDRESS --fec $CERROR --max-bitrate $BITRATE --ttl 16 --min-clients $NCLIENTS --max-wait $MAXTIME " |
---|
121 | SYNTAXCLIENT="udp-receiver $MBUFFER --portbase $PORTBASE " |
---|
122 | |
---|
123 | |
---|
124 | case "$1" in |
---|
125 | SENDPARTITION) |
---|
126 | PROG1=`ogCreateImageSyntax $DEVICE " " $TOOL $LEVEL | awk -F"|" '{print $1 "|" $3}' | tr -d ">"` |
---|
127 | echo "$PROG1 | $SYNTAXSERVER" |
---|
128 | ;; |
---|
129 | RECEIVERPARTITION) |
---|
130 | COMPRESSOR=`ogRestoreImageSyntax " " $DEVICE $TOOL $LEVEL | awk -F\| '{print $1}'` |
---|
131 | TOOLS=`ogRestoreImageSyntax " " $DEVICE $TOOL $LEVEL | awk -F\| '{print $NF}'` |
---|
132 | echo "$SYNTAXCLIENT | $COMPRESSOR | $TOOLS " |
---|
133 | ;; |
---|
134 | SENDFILE) |
---|
135 | echo "$SYNTAXSERVER --file $3" |
---|
136 | ;; |
---|
137 | RECEIVERFILE) |
---|
138 | echo "$SYNTAXCLIENT --file $3" |
---|
139 | ;; |
---|
140 | *) |
---|
141 | ;; |
---|
142 | esac |
---|
143 | } |
---|
144 | |
---|
145 | |
---|
146 | |
---|
147 | #/** |
---|
148 | # ogMcastSendFile [ str_repo | int_ndisk int_npart ] /Relative_path_file sessionMulticast |
---|
149 | #@brief Envía un fichero por multicast ORIGEN(fichero) DESTINO(sessionmulticast) |
---|
150 | #@param (2 parámetros) $1 path_aboluto_fichero $2 sesionMcast |
---|
151 | #@param (3 parámetros) $1 Contenedor REPO|CACHE $2 path_absoluto_fichero $3 sesionMulticast |
---|
152 | #@param (4 parámetros) $1 disk $2 particion $3 path_absoluto_fichero $4 sesionMulticast |
---|
153 | #@return |
---|
154 | #@exception OG_ERR_FORMAT formato incorrecto. |
---|
155 | #@note Requisitos: |
---|
156 | #@version 0.91 - Definición de Protocol.lib |
---|
157 | #@author Antonio Doblas Viso, Universidad de Málaga |
---|
158 | #@date 2010/05/09 |
---|
159 | #*/ ## |
---|
160 | #/** |
---|
161 | # |
---|
162 | |
---|
163 | function ogMcastSendFile () |
---|
164 | { |
---|
165 | # Variables locales. |
---|
166 | local ARGS SOURCE TARGET COMMAND DEVICE |
---|
167 | #ARGS usado para controlar ubicación de la sesion multicast |
---|
168 | |
---|
169 | # Si se solicita, mostrar ayuda. |
---|
170 | if [ "$*" == "help" ]; then |
---|
171 | ogHelp "$FUNCNAME [str_REPOSITORY] [int_ndisk int_npart] /Relative_path_file sesionMcast" \ |
---|
172 | "$FUNCNAME 1 1 /aula1/winxp.img sesionMcast" \ |
---|
173 | "$FUNCNAME REPO /aula1/ubuntu.iso sesionMcast" \ |
---|
174 | "$FUNCNAME CACHE /aula1/winxp.img sesionMcast" \ |
---|
175 | "$FUNCNAME /opt/opengnsys/images/aula1/hd500.vmx sesionMcast" |
---|
176 | return |
---|
177 | fi |
---|
178 | |
---|
179 | ARGS="$@" |
---|
180 | case "$1" in |
---|
181 | /*) # Camino completo. */ (Comentrio Doxygen) |
---|
182 | SOURCE=$(ogGetPath "$1") |
---|
183 | ARG=2 |
---|
184 | DEVICE="$1" |
---|
185 | ;; |
---|
186 | [1-9]*) # ndisco npartición. |
---|
187 | SOURCE=$(ogGetPath "$1" "$2" "$3") |
---|
188 | ARG=4 |
---|
189 | DEVICE="$1 $2 $3" |
---|
190 | ;; |
---|
191 | *) # Otros: repo, cache, cdrom (no se permiten caminos relativos). |
---|
192 | SOURCE=$(ogGetPath "$1" "$2") |
---|
193 | ARG=3 |
---|
194 | DEVICE="$1 $2 " |
---|
195 | ;; |
---|
196 | esac |
---|
197 | |
---|
198 | |
---|
199 | # Error si no se reciben los argumentos ARG necesarios según la opcion. |
---|
200 | [ $# == "$ARG" ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
201 | |
---|
202 | # Comprobar fichero origen |
---|
203 | ogGetPath $SOURCE &> /dev/null || ogRaiseError $OG_ERR_NOTFOUND " device or file $DEVICE not found" || return $? |
---|
204 | |
---|
205 | SESSION=${!ARG} |
---|
206 | # llamando a la funcion con param1 session $SESSION y param2 $SOURCE |
---|
207 | COMMAND=`ogMcastSyntax "SENDFILE" "$SESSION" "$SOURCE"` |
---|
208 | echo $COMMAND |
---|
209 | eval $COMMAND |
---|
210 | } |
---|
211 | |
---|
212 | |
---|
213 | |
---|
214 | #/** |
---|
215 | # ogMcastReceiverFile sesion Multicast [ str_repo | int_ndisk int_npart ] /Relative_path_file |
---|
216 | #@brief Recibe un fichero multicast ORIGEN(sesionmulticast) DESTINO(fichero) |
---|
217 | #@param (2 parámetros) $1 sesionMcastCLIENT $2 path_aboluto_fichero_destino |
---|
218 | #@param (3 parámetros) $1 sesionMcastCLIENT $2 Contenedor REPO|CACHE $3 path_absoluto_fichero_destino |
---|
219 | #@param (4 parámetros) $1 sesionMcastCLIENT $2 disk $3 particion $4 path_absoluto_fichero_destino |
---|
220 | #@return |
---|
221 | #@exception OG_ERR_FORMAT formato incorrecto. |
---|
222 | #@note Requisitos: |
---|
223 | #@version 0.91 - Definición de Protocol.lib |
---|
224 | #@author Antonio Doblas Viso, Universidad de Málaga |
---|
225 | #@date 2010/05/09 |
---|
226 | #*/ ## |
---|
227 | #/** |
---|
228 | # |
---|
229 | |
---|
230 | ogMcastReceiverFile () |
---|
231 | { |
---|
232 | |
---|
233 | # Variables locales. |
---|
234 | local ARGS ARG TARGETDIR TARGETFILE |
---|
235 | |
---|
236 | |
---|
237 | # Si se solicita, mostrar ayuda. |
---|
238 | if [ "$*" == "help" ]; then |
---|
239 | ogHelp "$FUNCNAME" "$FUNCNAME [ str_portMcast] [ [Relative_path_file] | [str_REPOSITORY path_file] | [int_ndisk int_npart path_file ] ]" \ |
---|
240 | "$FUNCNAME 9000 /PS1_PH1.img" \ |
---|
241 | "$FUNCNAME 9000 CACHE /aula1/PS2_PH4.img" \ |
---|
242 | "$FUNCNAME 9000 1 1 /isos/linux.iso" |
---|
243 | return |
---|
244 | fi |
---|
245 | |
---|
246 | ARGS="$@" |
---|
247 | case "$2" in |
---|
248 | /*) # Camino completo. */ (Comentrio Doxygen) |
---|
249 | TARGETDIR=$(ogGetParentPath "$2") |
---|
250 | ARG=2 |
---|
251 | ;; |
---|
252 | [1-9]*) # ndisco npartición. |
---|
253 | TARGETDIR=$(ogGetParentPath "$2" "$3" "$4") |
---|
254 | ARG=4 |
---|
255 | ;; |
---|
256 | *) # Otros: repo, cache, cdrom (no se permiten caminos relativos). |
---|
257 | TARGETDIR=$(ogGetParentPath "$2" "$3") |
---|
258 | ARG=3 |
---|
259 | ;; |
---|
260 | esac |
---|
261 | |
---|
262 | # Error si no se reciben los argumentos ARG necesarios según la opcion. |
---|
263 | [ $# == "$ARG" ] || ogRaiseError $OG_ERR_FORMAT "Parametros no admitidos"|| return $? |
---|
264 | |
---|
265 | #obtenemos el nombre del fichero a descargar. |
---|
266 | TARGETFILE=`basename ${!ARG}` |
---|
267 | |
---|
268 | #generamos la instrucción a ejecutar. |
---|
269 | COMMAND=`ogMcastSyntax RECEIVERFILE "$1" $TARGETDIR/$TARGETFILE ` |
---|
270 | echo $COMMAND |
---|
271 | eval $COMMAND |
---|
272 | } |
---|
273 | |
---|
274 | #/** |
---|
275 | # ogMcastSendPartition |
---|
276 | #@brief Función para enviar el contenido de una partición a multiples particiones remotas. |
---|
277 | #@param |
---|
278 | #@param |
---|
279 | #@param |
---|
280 | #@return |
---|
281 | #@exception |
---|
282 | #@note |
---|
283 | #@todo: ogIsLocked siempre devuelve 1. crear ticket |
---|
284 | #@version 0.1 - Integración para OpenGNSys. |
---|
285 | #@author |
---|
286 | #@date |
---|
287 | #*/ ## |
---|
288 | |
---|
289 | ogMcastSendPartition () |
---|
290 | { |
---|
291 | |
---|
292 | # Variables locales |
---|
293 | local PART COMMAND |
---|
294 | |
---|
295 | # Si se solicita, mostrar ayuda. |
---|
296 | if [ "$*" == "help" ]; then |
---|
297 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npart SessionMulticastSERVER tools compresor" \ |
---|
298 | "$FUNCNAME 1 1 9000:full-duplex:239.194.37.31:50M:20:2 partclone lzop" |
---|
299 | return |
---|
300 | fi |
---|
301 | # Error si no se reciben 5 parámetros. |
---|
302 | [ "$#" == 5 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
303 | #chequeamos la particion. |
---|
304 | PART=$(ogDiskToDev "$1" "$2") || return $? |
---|
305 | |
---|
306 | #ogIsLocked $1 $2 || ogRaiseError $OG_ERR_LOCKED "$1,$2" || return $? |
---|
307 | ogUnmount $1 $2 |
---|
308 | |
---|
309 | |
---|
310 | #generamos la instrucción a ejecutar. |
---|
311 | COMMAND=`ogMcastSyntax SENDPARTITION "$3" $PART $4 $5` |
---|
312 | echo $COMMAND |
---|
313 | eval $COMMAND |
---|
314 | } |
---|
315 | |
---|
316 | #/** |
---|
317 | # ogMcastReceiverPartition |
---|
318 | #@brief Función para recibir directamente en la partición el contenido de un fichero imagen remoto enviado por multicast. |
---|
319 | #@param |
---|
320 | #@param |
---|
321 | #@param |
---|
322 | #@return |
---|
323 | #@exception |
---|
324 | #@note |
---|
325 | #@todo: |
---|
326 | #@version 0.1 - Integración para OpenGNSys. |
---|
327 | #@author |
---|
328 | #@date |
---|
329 | #*/ ## |
---|
330 | ogMcastReceiverPartition () |
---|
331 | { |
---|
332 | # Variables locales |
---|
333 | local PART COMMAND |
---|
334 | |
---|
335 | # Si se solicita, mostrar ayuda. |
---|
336 | if [ "$*" == "help" ]; then |
---|
337 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npart SessionMulticastCLIENT tools compresor" \ |
---|
338 | "$FUNCNAME 1 1 9000 partclone lzop" |
---|
339 | return |
---|
340 | fi |
---|
341 | # Error si no se reciben 5 parámetros. |
---|
342 | [ "$#" == 5 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
343 | #chequeamos la particion. |
---|
344 | PART=$(ogDiskToDev "$1" "$2") || return $? |
---|
345 | |
---|
346 | #ogIsLocked $1 $2 || ogRaiseError $OG_ERR_LOCKED "$1,$2" || return $? |
---|
347 | ogUnmount $1 $2 |
---|
348 | |
---|
349 | #generamos la instrucción a ejecutar. |
---|
350 | COMMAND=`ogMcastSyntax RECEIVERPARTITION "$3" $PART $4 $5` |
---|
351 | echo $COMMAND |
---|
352 | eval $COMMAND |
---|
353 | } |
---|
354 | |
---|
355 | |
---|
356 | ########################################## |
---|
357 | ############## funciones torrent |
---|
358 | #/** |
---|
359 | # ogTorrentStart [ str_repo | int_ndisk int_npart ] Relative_path_file.torrent | SessionProtocol |
---|
360 | #@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. |
---|
361 | #@param str_pathDirectory str_Relative_path_file |
---|
362 | #@param int_disk int_partition str_Relative_path_file |
---|
363 | #@param str_REPOSITORY(CACHE - LOCAL) str_Relative_path_file |
---|
364 | #@param (2 parámetros) $1 path_aboluto_fichero_torrent $2 Parametros_Session_Torrent |
---|
365 | #@param (3 parámetros) $1 Contenedor CACHE $2 path_absoluto_fichero_Torrent $3 Parametros_Session_Torrent |
---|
366 | #@param (4 parámetros) $1 disk $2 particion $3 path_absoluto_fichero_Torrent 4$ Parametros_Session_Torrent |
---|
367 | |
---|
368 | #@return |
---|
369 | #@note |
---|
370 | #@todo: |
---|
371 | #@version 0.1 - Integración para OpenGNSys. |
---|
372 | #@author Antonio J. Doblas Viso. Universidad de Málaga |
---|
373 | #@date |
---|
374 | #@version 0.2 - Chequeo del tamaño de imagen descargado. |
---|
375 | #@author Irina . Univesidad de Sevilla. |
---|
376 | #@date |
---|
377 | #@version 0.3 - Control de los modos de operación, y estado de descarga. |
---|
378 | #@author Antonio J. Doblas Viso. Univesidad de Málaga. |
---|
379 | #@date |
---|
380 | #@version 0.4 - Enviadando señal (2) a ctorrent permiendo la comunicación final con tracker |
---|
381 | #@author Antonio J. Doblas Viso. Univesidad de Málaga. |
---|
382 | #@date |
---|
383 | #*/ ## |
---|
384 | |
---|
385 | #protocoloTORRENT mode:time |
---|
386 | #mode=seeder -> Dejar el equipo seedeando hasta que transcurra el tiempo indicado o un kill desde consola, |
---|
387 | #mode=peer -> seedear mientras descarga |
---|
388 | #mode=leecher -> NO seedear mientras descarga |
---|
389 | #time tiempo que una vez descargada la imagen queremos dejar al cliente como seeder. |
---|
390 | |
---|
391 | ogTorrentStart () |
---|
392 | { |
---|
393 | |
---|
394 | # Variables locales. |
---|
395 | local ARGS ARG TARGETDIR TARGETFILE SESSION ERROR |
---|
396 | ERROR=0 |
---|
397 | |
---|
398 | # Si se solicita, mostrar ayuda. |
---|
399 | if [ "$*" == "help" ]; then |
---|
400 | ogHelp "$FUNCNAME $FUNCNAME [ str_repo] [ [Relative_path_fileTORRENT] | [str_REPOSITORY path_fileTORRENT] | [int_ndisk int_npart path_fileTORRENT ] ] SessionTorrent" \ |
---|
401 | "$FUNCNAME CACHE /PS1_PH1.img.torrent seeder:10000" \ |
---|
402 | "$FUNCNAME /opt/opengnsys/cache/linux.iso peer:60" \ |
---|
403 | "$FUNCNAME 1 1 /linux.iso.torrent leecher:60" |
---|
404 | return |
---|
405 | fi |
---|
406 | |
---|
407 | case "$1" in |
---|
408 | /*) # Camino completo. */ (Comentrio Doxygen) |
---|
409 | SOURCE=$(ogGetPath "$1") |
---|
410 | ARG=2 |
---|
411 | ;; |
---|
412 | [1-9]*) # ndisco npartición. |
---|
413 | SOURCE=$(ogGetPath "$1" "$2" "$3") |
---|
414 | ARG=4 |
---|
415 | ;; |
---|
416 | *) # Otros: Solo cache (no se permiten caminos relativos). |
---|
417 | SOURCE=$(ogGetPath "$1" "$2" 2>/dev/null) |
---|
418 | ARG=3 |
---|
419 | ;; |
---|
420 | esac |
---|
421 | |
---|
422 | # Error si no se reciben los argumentos ARG necesarios según la opcion. |
---|
423 | [ $# == "$ARG" ] || ogRaiseError $OG_ERR_FORMAT "Parametros no admitidos"|| return $? |
---|
424 | |
---|
425 | #controlar source, que no se haga al repo. |
---|
426 | if [ $ARG == "3" ] |
---|
427 | then |
---|
428 | 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 $? |
---|
429 | fi |
---|
430 | if [ $ARG == "2" ] |
---|
431 | then |
---|
432 | if `ogCheckStringInReg "$1" "^/opt/opengnsys/images"` |
---|
433 | then |
---|
434 | 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" |
---|
435 | return $? |
---|
436 | fi |
---|
437 | fi |
---|
438 | |
---|
439 | #controlar el source, para que sea un torrent. |
---|
440 | ctorrent -x ${SOURCE} &> /dev/null; [ $? -eq 0 ] || ogRaiseError $OG_ERR_NOTFOUND "${ARGS% $*}" || return $? |
---|
441 | |
---|
442 | TARGET=`echo $SOURCE | awk -F.torrent '{print $1}'` |
---|
443 | DIRSOURCE=`ogGetParentPath $SOURCE` |
---|
444 | cd $DIRSOURCE |
---|
445 | |
---|
446 | |
---|
447 | |
---|
448 | SESSION=${!ARG} |
---|
449 | OIFS=$IFS; IFS=':' ; SESSION=($SESSION); IFS=$OIFS |
---|
450 | [[ ${#SESSION[*]} == 2 ]] || ogRaiseError $OG_ERR_FORMAT "parametros session Torrent no completa: modo:tiempo" || ERROR=1# return $? |
---|
451 | #controlamos el modo de operación del cliente- |
---|
452 | ogCheckStringInGroup ${SESSION[0]} "seeder SEEDER peer PEER leecher LEECHER" || ogRaiseError $OG_ERR_FORMAT "valor modo Torrent no valido ${SESSION[0]}" || ERROR=1 #return $? |
---|
453 | MODE=${SESSION[0]} |
---|
454 | #contolamos el tiempo para el seeder o una vez descargada la imagen como peer o leecher. |
---|
455 | ogCheckStringInReg ${SESSION[1]} "^[0-9]{1,10}$" || ogRaiseError $OG_ERR_FORMAT "valor tiempo no valido ${SESSION[1]}" || ERROR=1 # return $? |
---|
456 | TIME=${SESSION[1]} |
---|
457 | # si ha habido error en el control de parametros error. |
---|
458 | [ "$ERROR" == "1" ] && return 1 |
---|
459 | |
---|
460 | |
---|
461 | #SYNTAXSEEDER="echo MODE seeder ctorrent ; (sleep \$TIME && kill -9 \`pidof ctorrent\`) & ; ctorrent \${SOURCE}" |
---|
462 | |
---|
463 | # si No fichero .bf, y Si fichero destino imagen ya descargada y su chequeo fue comprobado en su descarga inicial. |
---|
464 | if [ ! -f ${SOURCE}.bf -a -f ${TARGET} ] |
---|
465 | then |
---|
466 | echo "imagen ya descargada" |
---|
467 | case "$MODE" in |
---|
468 | seeder|SEEDER) |
---|
469 | echo "MODE seeder ctorrent" #### ${SOURCE} -X 'sleep $TIME; kill -9 \$(pidof ctorrent)' -C 100" |
---|
470 | (sleep $TIME && kill -2 `pidof ctorrent`) & |
---|
471 | ctorrent -f ${SOURCE} |
---|
472 | esac |
---|
473 | return 0 |
---|
474 | fi |
---|
475 | |
---|
476 | #Si no existe bf ni fichero destino descarga inicial. |
---|
477 | if [ ! -f ${SOURCE}.bf -a ! -f ${TARGET} ] |
---|
478 | then |
---|
479 | OPTION=DOWNLOAD |
---|
480 | echo "descarga inicial" |
---|
481 | fi |
---|
482 | |
---|
483 | # Si fichero bf descarga anterior no completada -. |
---|
484 | if [ -f ${SOURCE}.bf -a -f ${TARGET} ] |
---|
485 | then |
---|
486 | echo Continuar con Descargar inicial no terminada. |
---|
487 | OPTION=DOWNLOAD |
---|
488 | fi |
---|
489 | |
---|
490 | if [ "$OPTION" == "DOWNLOAD" ] |
---|
491 | then |
---|
492 | case "$MODE" in |
---|
493 | peer|PEER) |
---|
494 | echo "Donwloading Torrent as peer" ### echo "ctorrent -X 'sleep $TIME; kill -9 \$(pidof ctorrent)' -C 100 $SOURCE -s $TARGET -b ${SOURCE}.bf" |
---|
495 | ctorrent -f -X "sleep 15; kill -2 \$(pidof ctorrent)" -C 100 ${SOURCE} -s ${TARGET} -b ${SOURCE}.bf >> $OGLOGFILE |
---|
496 | ;; |
---|
497 | leecher|LEECHER) |
---|
498 | echo "Donwloading Torrent as leecher" # echo "ctorrent ${SOURCE} -X 'sleep $TIME; kill -9 \$(pidof ctorrent)' -C 100 -U 0" |
---|
499 | ctorrent ${SOURCE} -X "sleep $TIME; kill -2 \$(pidof ctorrent)" -C 100 -U 0 |
---|
500 | ;; |
---|
501 | seeder|SEEDER) |
---|
502 | echo "MODE seeder ctorrent" #### ${SOURCE} -X 'sleep $TIME; kill -9 \$(pidof ctorrent)' -C 100" |
---|
503 | ctorrent -f -X "sleep $TIME; kill -2 \$(pidof ctorrent)" -C 100 ${SOURCE} -s ${TARGET} -b ${SOURCE}.bf |
---|
504 | ;; |
---|
505 | esac |
---|
506 | fi |
---|
507 | cd /tmp |
---|
508 | } |
---|
509 | |
---|
510 | #/** |
---|
511 | # ogCreateTorrent [ str_repo | int_ndisk int_npart ] Relative_path_file |
---|
512 | #@brief Función para crear el fichero torrent. |
---|
513 | #@param str_pathDirectory str_Relative_path_file |
---|
514 | #@param int_disk int_partition str_Relative_path_file |
---|
515 | #@param str_REPOSITORY(CACHE - LOCAL) str_Relative_path_file |
---|
516 | #@return |
---|
517 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
518 | #@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo. |
---|
519 | #@exception OG_ERR_PARTITION Tipo de partición desconocido o no se puede montar. |
---|
520 | #@exception OG_ERR_NOTOS La partición no tiene instalado un sistema operativo. |
---|
521 | #@note |
---|
522 | #@version 0.1 - Integración para OpenGNSys. |
---|
523 | #@author Antonio J. Doblas Viso. Universidad de Málaga |
---|
524 | #@date |
---|
525 | #@version 0.2 - Integración para btlaunch. |
---|
526 | #@author Irina . Univesidad de Sevilla. |
---|
527 | #@date |
---|
528 | #*/ ## |
---|
529 | |
---|
530 | ogCreateTorrent () |
---|
531 | { |
---|
532 | # Variables locales. |
---|
533 | local ARGS ARG SOURCE EXT IPTORRENT |
---|
534 | |
---|
535 | |
---|
536 | # Si se solicita, mostrar ayuda. |
---|
537 | if [ "$*" == "help" ]; then |
---|
538 | ogHelp "$FUNCNAME" "$FUNCNAME [str_REPOSITORY] [int_ndisk int_npart] Relative_path_file IpBttrack" \ "$FUNCNAME 1 1 /aula1/winxp 10.1.15.23" \ |
---|
539 | "$FUNCNAME REPO /aula1/winxp 10.1.15.45" |
---|
540 | |
---|
541 | return |
---|
542 | fi |
---|
543 | |
---|
544 | # Error si se quiere crear el fichero en cache y no existe |
---|
545 | [ "$1" != "CACHE" ] || `ogFindCache >/dev/null` || ogRaiseError $OG_ERR_NOTFOUND "CACHE"|| return $? |
---|
546 | |
---|
547 | case "$1" in |
---|
548 | /*) # Camino completo. */ (Comentrio Doxygen) |
---|
549 | SOURCE=$(ogGetPath "$1.img") |
---|
550 | ARG=2 |
---|
551 | ;; |
---|
552 | [1-9]*) # ndisco npartición. |
---|
553 | SOURCE=$(ogGetPath "$1" "$2" "$3.img") |
---|
554 | ARG=4 |
---|
555 | ;; |
---|
556 | *) # Otros: repo, cache, cdrom (no se permiten caminos relativos). |
---|
557 | EXT=$(ogGetImageType "$1" "$2") |
---|
558 | SOURCE=$(ogGetPath "$1" "$2.$EXT") |
---|
559 | ARG=3 |
---|
560 | ;; |
---|
561 | esac |
---|
562 | |
---|
563 | # Error si no se reciben los argumentos ARG necesarios según la opcion. |
---|
564 | [ $# -eq "$ARG" ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
565 | |
---|
566 | |
---|
567 | # Error si no existe la imagen |
---|
568 | [ $SOURCE ] || ogRaiseError $OG_ERR_NOTFOUND || return $? |
---|
569 | |
---|
570 | [ -r $SOURCE.torrent ] && mv "$SOURCE.torrent" "$SOURCE.torrent.ant" && echo "Esperamos que se refresque el servidor" && sleep 20 |
---|
571 | |
---|
572 | IPTORRENT="${!#}" |
---|
573 | # Si ponemos el path completo cuando creamos el fichero torrent da error |
---|
574 | cd `dirname $SOURCE` |
---|
575 | echo ctorrent -t `basename $SOURCE` -u http://$IPTORRENT:6969/announce -s $SOURCE.torrent |
---|
576 | ctorrent -t `basename $SOURCE` -u http://$IPTORRENT:6969/announce -s $SOURCE.torrent |
---|
577 | |
---|
578 | } |
---|
579 | |
---|
580 | |
---|
581 | #/** |
---|
582 | # ogUpdateCacheIsNecesary [ str_repo ] Relative_path_file_OGIMG_with_/ |
---|
583 | #@brief Comprueba que el fichero que se desea almacenar en la cache del cliente, no esta. |
---|
584 | #@param 1 str_REPO |
---|
585 | #@param 2 str_Relative_path_file_OGIMG_with_/ |
---|
586 | #@return 0 si es necesario actualizar el fichero. |
---|
587 | #@return 1 si la imagen ya esta en la cache, por lo tanto no es necesario actualizar el fichero |
---|
588 | #@note |
---|
589 | #@todo: Proceso en el caso de que el fichero tenga el mismo nombre, pero su contenido sea distinto. |
---|
590 | #@todo: Se dejan mensajes mientras se confirma su funcionamiento. |
---|
591 | #@version 0.1 - Integracion para OpenGNSys. |
---|
592 | #@author Antonio J. Doblas Viso. Universidad de Malaga |
---|
593 | #@date |
---|
594 | #*/ ## |
---|
595 | function ogUpdateCacheIsNecesary () |
---|
596 | { |
---|
597 | |
---|
598 | # Variables locales. |
---|
599 | local ERROR SOURCE CACHE FILESOURCE MD5SOURCE FILETARGET MD5TARGET |
---|
600 | ERROR=0 |
---|
601 | |
---|
602 | # Si se solicita, mostrar ayuda. |
---|
603 | if [ "$*" == "help" ]; then |
---|
604 | ogHelp "$FUNCNAME $FUNCNAME [ str_repo] [ [Relative_path_image] " \ |
---|
605 | "$FUNCNAME REPO /PS1_PH1.img" \ |
---|
606 | "$FUNCNAME REPO /ogclient.sqfs" |
---|
607 | |
---|
608 | return |
---|
609 | fi |
---|
610 | |
---|
611 | # Error si no se reciben los argumentos ARG necesarios según la opcion. |
---|
612 | [ $# == "2" ] || ogRaiseError $OG_ERR_FORMAT "Parametros no admitidos"|| return $? |
---|
613 | |
---|
614 | |
---|
615 | ogCheckStringInGroup "$1" "REPO repo" || ogRaiseError $OG_ERR_FORMAT "El contendor $1 no es valido, solo se admite REPO" || return $? |
---|
616 | |
---|
617 | FILESOURCE=`ogGetPath $1 $2` |
---|
618 | FILETARGET=`ogGetPath CACHE $2` |
---|
619 | |
---|
620 | echo "paso 1. si no existe la imagen, confirmamos que es necesaria la actualizacion de la cache." |
---|
621 | if [ -z $FILETARGET ] |
---|
622 | then |
---|
623 | # borramos el fichero bf del torrent, en el caso de que se hubiese quedado de algun proceso fallido |
---|
624 | ogDeleteFile CACHE /$2.torrent.bf &> /dev/null |
---|
625 | ogDeleteFile CACHE /$2.sum &> /dev/null |
---|
626 | echo "Salida con valor 0, paso 1, la cache no contiene esa imagen " |
---|
627 | return 0 |
---|
628 | fi |
---|
629 | echo "Paso 2. Comprobamos que la imagen no estuviese en un proceso previo torrent" |
---|
630 | if ogGetPath $FILETARGET.torrent.bf > /dev/null |
---|
631 | then |
---|
632 | echo "Salida con valor 0, paso 2 la imagen esta en un estado de descarga torrent interrumpido." |
---|
633 | #TODO: comprobar los md5 para asegurarnos que la imagen es la misma. |
---|
634 | return 0 |
---|
635 | fi |
---|
636 | |
---|
637 | ## En este punto la imagen en el repo y en la cache se llaman igual, |
---|
638 | echo "paso 4. recuperamos o calculamos los md5 de los ficheros" |
---|
639 | if [ -f $FILESOURCE.sum ] |
---|
640 | then |
---|
641 | echo "leyendo el sum del fichero sum del repo" |
---|
642 | MD5SOURCE=$(cat $FILESOURCE.sum) |
---|
643 | #elif [ -f $FILETARGET.torrent ] |
---|
644 | #then |
---|
645 | # echo "leyendo el sum del fichero torrent de la cache" |
---|
646 | # MD5SOURCE=$(ctorrent -x $FILETARGET.torrent | grep Comment | awk -F": " '{print $2}') |
---|
647 | else |
---|
648 | echo "calculando el sun del repo" |
---|
649 | MD5SOURCE=$(md5sum $FILESOURCE | cut -f1 -d" ") |
---|
650 | fi |
---|
651 | if [ -f $FILETARGET.sum ] |
---|
652 | then |
---|
653 | echo "leyendo el sum de la cache" |
---|
654 | MD5TARGET=$(cat $FILETARGET.sum) |
---|
655 | else |
---|
656 | echo "calculando el sum de la cache" |
---|
657 | md5sum $FILETARGET | cut -f1 -d" " > $FILETARGET.sum |
---|
658 | MD5TARGET=$(cat $FILETARGET.sum) |
---|
659 | fi |
---|
660 | |
---|
661 | echo "Paso 5. comparamos los md5" |
---|
662 | #TODO: que hacer cuando los md5 son distintos. Por defecto borrar. |
---|
663 | if [ "$MD5SOURCE" == "$MD5TARGET" ] |
---|
664 | then |
---|
665 | echo "paso5.A la imagen esta en cache" |
---|
666 | return 1 |
---|
667 | else |
---|
668 | echo "paso 5.b la imagen en cache es distinta, borramos la imagen anterior y devolvemos 0 para confirmar la actualizacion" |
---|
669 | rm $FILETARGET |
---|
670 | return 0 |
---|
671 | fi |
---|
672 | |
---|
673 | } |
---|
674 | |
---|
675 | |
---|
676 | |
---|