##############################################################################
# -*- coding: utf-8 -*-
# Project:     EnciendeEquipos
# Module:     NetworkUtils.py
# Purpose:     Network utilities
# Language:    Python 2.5
# Date:        17-Jan-2010.
# Ver:        17-Jan-2010.
# Author:    José L. Redrejo Rodríguez
# Copyright:   2009 - José L. Redrejo Rodríguez    <jredrejo @nospam@ debian.org>
#
# EnciendeEquipos is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# EnciendeEquipos is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
##############################################################################

import socket,struct

def startup(address):

    addr_byte = address.split(':')
    hw_addr = struct.pack('BBBBBB', int(addr_byte[0], 16),
      int(addr_byte[1], 16),
      int(addr_byte[2], 16),
      int(addr_byte[3], 16),
      int(addr_byte[4], 16),
      int(addr_byte[5], 16))

    msg = '\xff' * 6 + hw_addr * 16
    
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
    try:
        s.sendto(msg, ('<broadcast>', 9))
        s.sendto(msg, ('<broadcast>', 7))
        s.sendto(msg, ('192.168.0.255', 9))
        s.sendto(msg, ('192.168.0.255', 7))   
    except:
        s.sendto(msg, ('<broadcast>', 2000))
        s.sendto(msg, ('192.168.0.255', 2000))
    s.close()
