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