Serve listens for machines attempting to boot, and uses Booter to help them.
()
| 200 | // Serve listens for machines attempting to boot, and uses Booter to |
| 201 | // help them. |
| 202 | func (s *Server) Serve() error { |
| 203 | if s.DHCPPort == 0 { |
| 204 | s.DHCPPort = portDHCP |
| 205 | } |
| 206 | if s.TFTPPort == 0 { |
| 207 | s.TFTPPort = portTFTP |
| 208 | } |
| 209 | if s.PXEPort == 0 { |
| 210 | s.PXEPort = portPXE |
| 211 | } |
| 212 | if s.HTTPPort == 0 { |
| 213 | s.HTTPPort = portHTTP |
| 214 | } |
| 215 | |
| 216 | newDHCP := dhcp4.NewConn |
| 217 | if s.DHCPNoBind { |
| 218 | newDHCP = dhcp4.NewSnooperConn |
| 219 | } |
| 220 | |
| 221 | dhcp, err := newDHCP(fmt.Sprintf("%s:%d", s.Address, s.DHCPPort)) |
| 222 | if err != nil { |
| 223 | return err |
| 224 | } |
| 225 | tftp, err := net.ListenPacket("udp", fmt.Sprintf("%s:%d", s.Address, s.TFTPPort)) |
| 226 | if err != nil { |
| 227 | dhcp.Close() |
| 228 | return err |
| 229 | } |
| 230 | pxe, err := net.ListenPacket("udp4", fmt.Sprintf("%s:%d", s.Address, s.PXEPort)) |
| 231 | if err != nil { |
| 232 | dhcp.Close() |
| 233 | tftp.Close() |
| 234 | return err |
| 235 | } |
| 236 | http, err := net.Listen("tcp", fmt.Sprintf("%s:%d", s.Address, s.HTTPPort)) |
| 237 | if err != nil { |
| 238 | dhcp.Close() |
| 239 | tftp.Close() |
| 240 | pxe.Close() |
| 241 | return err |
| 242 | } |
| 243 | |
| 244 | s.events = make(map[string][]machineEvent) |
| 245 | // 5 buffer slots, one for each goroutine, plus one for |
| 246 | // Shutdown(). We only ever pull the first error out, but shutdown |
| 247 | // will likely generate some spurious errors from the other |
| 248 | // goroutines, and we want them to be able to dump them without |
| 249 | // blocking. |
| 250 | s.errs = make(chan error, 6) |
| 251 | |
| 252 | s.debug("Init", "Starting Pixiecore goroutines") |
| 253 | |
| 254 | go func() { s.errs <- s.serveDHCP(dhcp) }() |
| 255 | go func() { s.errs <- s.servePXE(pxe) }() |
| 256 | go func() { s.errs <- s.serveTFTP(tftp) }() |
| 257 | go func() { s.errs <- serveHTTP(http, s.serveHTTP) }() |
| 258 | |
| 259 | // Wait for either a fatal error, or Shutdown(). |