github.com/Cloud-Foundations/Dominator@v0.3.4/hypervisor/dhcpd/packetWatchers.go (about)

     1  package dhcpd
     2  
     3  import (
     4  	proto "github.com/Cloud-Foundations/Dominator/proto/hypervisor"
     5  )
     6  
     7  // This will grab and release the lock.
     8  func (s *DhcpServer) closePacketWatchChannel(
     9  	channel <-chan proto.WatchDhcpResponse) {
    10  	s.mutex.Lock()
    11  	defer s.mutex.Unlock()
    12  	delete(s.packetWatchers, channel)
    13  }
    14  
    15  // This will grab and release the lock.
    16  func (s *DhcpServer) makePacketWatchChannel() <-chan proto.WatchDhcpResponse {
    17  	channel := make(chan proto.WatchDhcpResponse, 16)
    18  	s.mutex.Lock()
    19  	defer s.mutex.Unlock()
    20  	s.packetWatchers[channel] = channel
    21  	return channel
    22  }
    23  
    24  // This will grab and release the lock.
    25  func (s *DhcpServer) sendPacket(interfaceName string, inputPacket []byte) {
    26  	packet := make([]byte, len(inputPacket))
    27  	copy(packet, inputPacket)
    28  	msg := proto.WatchDhcpResponse{
    29  		Interface: interfaceName,
    30  		Packet:    packet,
    31  	}
    32  	s.mutex.Lock()
    33  	defer s.mutex.Unlock()
    34  	for rChannel, sChannel := range s.packetWatchers {
    35  		select {
    36  		case sChannel <- msg:
    37  		default:
    38  			delete(s.packetWatchers, rChannel)
    39  			close(sChannel)
    40  		}
    41  	}
    42  }