[884ea85] | 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 | #/** |
---|
[761960be] | 14 | # ogMcastSyntax |
---|
[bd390f1] | 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. |
---|
[884ea85] | 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. |
---|
[bd390f1] | 23 | #@note Requisitos: upd-cast 2009 |
---|
[0fbc05e] | 24 | #@todo localvar check versionudp |
---|
[884ea85] | 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 | |
---|
[761960be] | 33 | function ogMcastSyntax () |
---|
[884ea85] | 34 | { |
---|
[bd390f1] | 35 | |
---|
[59e3998] | 36 | local ISUDPCAST PARM SESSION SESSIONPARM MODE PORTBASE PERROR |
---|
[bd390f1] | 37 | local METHOD ADDRESS BITRATE NCLIENTS MAXTIME CERROR |
---|
[761960be] | 38 | local TOOL LEVEL DEVICE MBUFFER SYNTAXSERVER SYNTAXCLIENT |
---|
[bd390f1] | 39 | |
---|
[884ea85] | 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 "\ |
---|
[e8df652] | 45 | "$FUNCNAME RECEIVERFILE str_sessionCLIENT str_file " \ |
---|
[fa43c91] | 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 " \ |
---|
[0fbc05e] | 48 | "sessionClient syntax: portbase " \ |
---|
| 49 | "sessionClient example: 9000 " |
---|
[884ea85] | 50 | return |
---|
| 51 | fi |
---|
[59e3998] | 52 | PERROR=0 |
---|
[884ea85] | 53 | |
---|
[bd390f1] | 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 |
---|
[59e3998] | 65 | ogCheckStringInGroup "$1" "SENDPARTITION sendpartition RECEIVERPARTITION receiverpartition SENDFILE sendfile RECEIVERFILE receiverfile" || ogRaiseError $OG_ERR_FORMAT "1st param: $1" || PERROR=1 #return $? |
---|
[bd390f1] | 66 | |
---|
| 67 | # 2º param check |
---|
| 68 | echo "$1" | grep "SEND" > /dev/null && MODE=server || MODE=client |
---|
| 69 | |
---|
[59e3998] | 70 | #TODO: diferenciamos los paramatros especificos de la sessión multicast |
---|
| 71 | #SI: controlamos todos los parametros de la sessión multicast. |
---|
[56d2d6f] | 72 | [ $MODE == "client" ] && SESSIONPARM=1 || SESSIONPARM=6 |
---|
[bd390f1] | 73 | OIFS=$IFS; IFS=':' ; SESSION=($2); IFS=$OIFS |
---|
[59e3998] | 74 | |
---|
| 75 | |
---|
| 76 | [[ ${#SESSION[*]} == $SESSIONPARM ]] || ogRaiseError $OG_ERR_FORMAT "parametros session multicast no completa" || PERROR=2# return $? |
---|
| 77 | |
---|
| 78 | |
---|
[bd390f1] | 79 | #controlamos el PORTBASE de la sesion. Comun.- |
---|
| 80 | PORTBASE=${SESSION[0]} |
---|
[59e3998] | 81 | ogCheckStringInGroup ${SESSION[0]} "9000 9002 9004 9006 9008 9010" || ogRaiseError $OG_ERR_FORMAT "McastSession portbase ${SESSION[0]}" || PERROR=3 #return $? |
---|
[bd390f1] | 82 | if [ $MODE == "server" ] |
---|
| 83 | then |
---|
[59e3998] | 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 $? |
---|
[bd390f1] | 85 | METHOD=${SESSION[1]} |
---|
[59e3998] | 86 | ogCheckIpAddress ${SESSION[2]} || ogRaiseError $OG_ERR_FORMAT "McastSession address ${SESSION[2]}" || PERROR=5 #return $? |
---|
[bd390f1] | 87 | ADDRESS=${SESSION[2]} |
---|
[59e3998] | 88 | ogCheckStringInReg ${SESSION[3]} "^[0-9]{1,2}\M$" || ogRaiseError $OG_ERR_FORMAT "McastSession bitrate ${SESSION[3]}" || PERROR=6 # return $? |
---|
[bd390f1] | 89 | BITRATE=${SESSION[3]} |
---|
[59e3998] | 90 | ogCheckStringInReg ${SESSION[4]} "^[0-9]{1,10}$" || ogRaiseError $OG_ERR_FORMAT "McastSession nclients ${SESSION[4]}" || PERROR=7 # return $? |
---|
[bd390f1] | 91 | NCLIENTS=${SESSION[4]} |
---|
[59e3998] | 92 | ogCheckStringInReg ${SESSION[5]} "^[0-9]{1,10}$" || ogRaiseError $OG_ERR_FORMAT "McastSession maxtime ${SESSION[5]}" || PERROR=8 # return $? |
---|
[bd390f1] | 93 | MAXTIME=${SESSION[5]} |
---|
| 94 | fi |
---|
[884ea85] | 95 | |
---|
[bd390f1] | 96 | #3er param check - que puede ser un dispositvo o un fichero. |
---|
[59e3998] | 97 | #ogGetPath "$3" > /dev/null || ogRaiseError $OG_ERR_NOTFOUND " device or file $3" || PERROR=9 #return $? |
---|
[bd390f1] | 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 |
---|
[59e3998] | 104 | ogCheckStringInGroup "$4" "partclone partimage ntfsclone" || ogRaiseError $OG_ERR_NOTFOUND " herramienta $4 no soportada" || PERROR=10 #return $? |
---|
[bd390f1] | 105 | TOOL=$4 |
---|
[59e3998] | 106 | ogCheckStringInGroup "$5" "lzop gzip 0 1" || ogRaiseError $OG_ERR_NOTFOUND " compresor $5 no valido" || PERROR=11 #return $? |
---|
[bd390f1] | 107 | LEVEL=$5 |
---|
| 108 | fi |
---|
| 109 | |
---|
[59e3998] | 110 | |
---|
| 111 | [ "$PERROR" -gt "0" ] && return 1 |
---|
[bd390f1] | 112 | |
---|
| 113 | # Valores estandar no configurables. |
---|
| 114 | CERROR="8x8/128" |
---|
[884ea85] | 115 | |
---|
| 116 | # opción del usuo de tuberia intermedia en memoria mbuffer. |
---|
[fa43c91] | 117 | which mbuffer > /dev/null && MBUFFER=" --pipe 'mbuffer -q -m 20M' " |
---|
[884ea85] | 118 | |
---|
| 119 | # Generamos la instrucción base de multicast -Envio,Recepcion- |
---|
[fa43c91] | 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 " |
---|
[761960be] | 121 | SYNTAXCLIENT="udp-receiver $MBUFFER --portbase $PORTBASE " |
---|
[884ea85] | 122 | |
---|
| 123 | |
---|
| 124 | case "$1" in |
---|
[59e3998] | 125 | SENDPARTITION) |
---|
[761960be] | 126 | PROG1=`ogCreateImageSyntax $DEVICE " " $TOOL $LEVEL | awk -F"|" '{print $1 "|" $3}' | tr -d ">"` |
---|
| 127 | echo "$PROG1 | $SYNTAXSERVER" |
---|
[884ea85] | 128 | ;; |
---|
| 129 | RECEIVERPARTITION) |
---|
[761960be] | 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 " |
---|
[884ea85] | 133 | ;; |
---|
| 134 | SENDFILE) |
---|
[761960be] | 135 | echo "$SYNTAXSERVER --file $3" |
---|
[884ea85] | 136 | ;; |
---|
| 137 | RECEIVERFILE) |
---|
[761960be] | 138 | echo "$SYNTAXCLIENT --file $3" |
---|
[884ea85] | 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: |
---|
[bd390f1] | 156 | #@version 0.91 - Definición de Protocol.lib |
---|
[884ea85] | 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. |
---|
[bd390f1] | 166 | local ARGS SOURCE TARGET COMMAND DEVICE |
---|
[884ea85] | 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 |
---|
[bd390f1] | 181 | /*) # Camino completo. */ (Comentrio Doxygen) |
---|
[884ea85] | 182 | SOURCE=$(ogGetPath "$1") |
---|
[bd390f1] | 183 | ARG=2 |
---|
| 184 | DEVICE="$1" |
---|
[884ea85] | 185 | ;; |
---|
| 186 | [1-9]*) # ndisco npartición. |
---|
| 187 | SOURCE=$(ogGetPath "$1" "$2" "$3") |
---|
| 188 | ARG=4 |
---|
[bd390f1] | 189 | DEVICE="$1 $2 $3" |
---|
[884ea85] | 190 | ;; |
---|
| 191 | *) # Otros: repo, cache, cdrom (no se permiten caminos relativos). |
---|
| 192 | SOURCE=$(ogGetPath "$1" "$2") |
---|
| 193 | ARG=3 |
---|
[bd390f1] | 194 | DEVICE="$1 $2 " |
---|
[884ea85] | 195 | ;; |
---|
| 196 | esac |
---|
| 197 | |
---|
| 198 | |
---|
| 199 | # Error si no se reciben los argumentos ARG necesarios según la opcion. |
---|
[bd390f1] | 200 | [ $# == "$ARG" ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
[884ea85] | 201 | |
---|
| 202 | # Comprobar fichero origen |
---|
[bd390f1] | 203 | ogGetPath $SOURCE &> /dev/null || ogRaiseError $OG_ERR_NOTFOUND " device or file $DEVICE not found" || return $? |
---|
[884ea85] | 204 | |
---|
| 205 | SESSION=${!ARG} |
---|
| 206 | # llamando a la funcion con param1 session $SESSION y param2 $SOURCE |
---|
[761960be] | 207 | COMMAND=`ogMcastSyntax "SENDFILE" "$SESSION" "$SOURCE"` |
---|
[884ea85] | 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: |
---|
[bd390f1] | 223 | #@version 0.91 - Definición de Protocol.lib |
---|
[884ea85] | 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 ] ]" \ |
---|
[bd390f1] | 240 | "$FUNCNAME 9000 /PS1_PH1.img" \ |
---|
| 241 | "$FUNCNAME 9000 CACHE /aula1/PS2_PH4.img" \ |
---|
| 242 | "$FUNCNAME 9000 1 1 /isos/linux.iso" |
---|
[884ea85] | 243 | return |
---|
| 244 | fi |
---|
| 245 | |
---|
| 246 | ARGS="$@" |
---|
| 247 | case "$2" in |
---|
| 248 | /*) # Camino completo. */ (Comentrio Doxygen) |
---|
| 249 | TARGETDIR=$(ogGetParentPath "$2") |
---|
[bd390f1] | 250 | ARG=2 |
---|
[884ea85] | 251 | ;; |
---|
| 252 | [1-9]*) # ndisco npartición. |
---|
| 253 | TARGETDIR=$(ogGetParentPath "$2" "$3" "$4") |
---|
[bd390f1] | 254 | ARG=4 |
---|
[884ea85] | 255 | ;; |
---|
| 256 | *) # Otros: repo, cache, cdrom (no se permiten caminos relativos). |
---|
| 257 | TARGETDIR=$(ogGetParentPath "$2" "$3") |
---|
[bd390f1] | 258 | ARG=3 |
---|
[884ea85] | 259 | ;; |
---|
| 260 | esac |
---|
| 261 | |
---|
| 262 | # Error si no se reciben los argumentos ARG necesarios según la opcion. |
---|
[bd390f1] | 263 | [ $# == "$ARG" ] || ogRaiseError $OG_ERR_FORMAT "Parametros no admitidos"|| return $? |
---|
[884ea85] | 264 | |
---|
| 265 | #obtenemos el nombre del fichero a descargar. |
---|
| 266 | TARGETFILE=`basename ${!ARG}` |
---|
| 267 | |
---|
| 268 | #generamos la instrucción a ejecutar. |
---|
[761960be] | 269 | COMMAND=`ogMcastSyntax RECEIVERFILE "$1" $TARGETDIR/$TARGETFILE ` |
---|
[e8df652] | 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" \ |
---|
[fa43c91] | 298 | "$FUNCNAME 1 1 9000:full-duplex:239.194.37.31:50M:20:2 partclone lzop" |
---|
[e8df652] | 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. |
---|
[761960be] | 311 | COMMAND=`ogMcastSyntax SENDPARTITION "$3" $PART $4 $5` |
---|
[e8df652] | 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. |
---|
[761960be] | 350 | COMMAND=`ogMcastSyntax RECEIVERPARTITION "$3" $PART $4 $5` |
---|
[884ea85] | 351 | echo $COMMAND |
---|
| 352 | eval $COMMAND |
---|
| 353 | } |
---|
| 354 | |
---|
[bd390f1] | 355 | |
---|
| 356 | ########################################## |
---|
[bba08bf] | 357 | ############## funciones torrent |
---|
[884ea85] | 358 | #/** |
---|
[2ab031e5] | 359 | # ogTorrentStart [ str_repo | int_ndisk int_npart ] Relative_path_file.torrent | SessionProtocol |
---|
[1678fa1] | 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. |
---|
[bba08bf] | 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 |
---|
[2ab031e5] | 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 | |
---|
[bba08bf] | 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 |
---|
[1678fa1] | 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 |
---|
[2ab031e5] | 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 |
---|
[bba08bf] | 383 | #*/ ## |
---|
[2ab031e5] | 384 | |
---|
[1678fa1] | 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. |
---|
[bba08bf] | 390 | |
---|
| 391 | ogTorrentStart () |
---|
| 392 | { |
---|
[2ab031e5] | 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 |
---|
[bba08bf] | 406 | |
---|
| 407 | case "$1" in |
---|
| 408 | /*) # Camino completo. */ (Comentrio Doxygen) |
---|
| 409 | SOURCE=$(ogGetPath "$1") |
---|
[2ab031e5] | 410 | ARG=2 |
---|
[bba08bf] | 411 | ;; |
---|
| 412 | [1-9]*) # ndisco npartición. |
---|
| 413 | SOURCE=$(ogGetPath "$1" "$2" "$3") |
---|
[2ab031e5] | 414 | ARG=4 |
---|
[bba08bf] | 415 | ;; |
---|
[2ab031e5] | 416 | *) # Otros: Solo cache (no se permiten caminos relativos). |
---|
[bba08bf] | 417 | SOURCE=$(ogGetPath "$1" "$2" 2>/dev/null) |
---|
[2ab031e5] | 418 | ARG=3 |
---|
| 419 | ;; |
---|
[bba08bf] | 420 | esac |
---|
| 421 | |
---|
[2ab031e5] | 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 $? |
---|
[bba08bf] | 441 | |
---|
| 442 | TARGET=`echo $SOURCE | awk -F.torrent '{print $1}'` |
---|
| 443 | DIRSOURCE=`ogGetParentPath $SOURCE` |
---|
| 444 | cd $DIRSOURCE |
---|
| 445 | |
---|
[1678fa1] | 446 | |
---|
[bba08bf] | 447 | |
---|
[2ab031e5] | 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 | |
---|
[761960be] | 461 | #SYNTAXSEEDER="echo MODE seeder ctorrent ; (sleep \$TIME && kill -9 \`pidof ctorrent\`) & ; ctorrent \${SOURCE}" |
---|
[1678fa1] | 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} ] |
---|
[bba08bf] | 465 | then |
---|
[1678fa1] | 466 | echo "imagen ya descargada" |
---|
| 467 | case "$MODE" in |
---|
| 468 | seeder|SEEDER) |
---|
[2ab031e5] | 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} |
---|
[1678fa1] | 472 | esac |
---|
| 473 | return 0 |
---|
[bba08bf] | 474 | fi |
---|
| 475 | |
---|
[1678fa1] | 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 |
---|
[bba08bf] | 488 | fi |
---|
| 489 | |
---|
[1678fa1] | 490 | if [ "$OPTION" == "DOWNLOAD" ] |
---|
[bba08bf] | 491 | then |
---|
[2ab031e5] | 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" |
---|
[914d834] | 495 | ctorrent -f -X "sleep 15; kill -2 \$(pidof ctorrent)" -C 100 ${SOURCE} -s ${TARGET} -b ${SOURCE}.bf >> $OGLOGFILE |
---|
[2ab031e5] | 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 |
---|
[bba08bf] | 507 | cd /tmp |
---|
| 508 | } |
---|
| 509 | |
---|
[2ab031e5] | 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. |
---|
[0fbc05e] | 533 | local ARGS ARG SOURCE EXT IPTORRENT |
---|
[2ab031e5] | 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). |
---|
[0fbc05e] | 557 | EXT=$(ogGetImageType "$1" "$2") |
---|
| 558 | SOURCE=$(ogGetPath "$1" "$2.$EXT") |
---|
[2ab031e5] | 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 | |
---|
[bba08bf] | 581 | #/** |
---|
[914d834] | 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 |
---|
[884ea85] | 588 | #@note |
---|
[914d834] | 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 |
---|
[884ea85] | 593 | #@date |
---|
| 594 | #*/ ## |
---|
[914d834] | 595 | function ogUpdateCacheIsNecesary () |
---|
[884ea85] | 596 | { |
---|
| 597 | |
---|
| 598 | # Variables locales. |
---|
[914d834] | 599 | local ERROR SOURCE CACHE FILESOURCE MD5SOURCE FILETARGET MD5TARGET |
---|
| 600 | ERROR=0 |
---|
[884ea85] | 601 | |
---|
| 602 | # Si se solicita, mostrar ayuda. |
---|
| 603 | if [ "$*" == "help" ]; then |
---|
[914d834] | 604 | ogHelp "$FUNCNAME $FUNCNAME [ str_repo] [ [Relative_path_image] " \ |
---|
| 605 | "$FUNCNAME REPO /PS1_PH1.img" \ |
---|
| 606 | "$FUNCNAME REPO /ogclient.sqfs" |
---|
| 607 | |
---|
[884ea85] | 608 | return |
---|
| 609 | fi |
---|
| 610 | |
---|
[914d834] | 611 | # Error si no se reciben los argumentos ARG necesarios según la opcion. |
---|
| 612 | [ $# == "2" ] || ogRaiseError $OG_ERR_FORMAT "Parametros no admitidos"|| return $? |
---|
[884ea85] | 613 | |
---|
| 614 | |
---|
[914d834] | 615 | ogCheckStringInGroup "$1" "REPO repo" || ogRaiseError $OG_ERR_FORMAT "El contendor $1 no es valido, solo se admite REPO" || return $? |
---|
[884ea85] | 616 | |
---|
[914d834] | 617 | FILESOURCE=`ogGetPath $1 $2` |
---|
| 618 | FILETARGET=`ogGetPath CACHE $2` |
---|
[884ea85] | 619 | |
---|
[914d834] | 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 |
---|
[884ea85] | 628 | fi |
---|
[914d834] | 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 |
---|
[884ea85] | 635 | fi |
---|
| 636 | |
---|
[914d834] | 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) |
---|
[bd390f1] | 659 | fi |
---|
| 660 | |
---|
[914d834] | 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 |
---|
[bd390f1] | 672 | |
---|
[914d834] | 673 | } |
---|
[bd390f1] | 674 | |
---|
| 675 | |
---|
| 676 | |
---|