1. Dave Cheney
  2. heartbeat

Commits

Dave Cheney  committed 1ccc0d8

go version of heartbeat

  • Participants
  • Parent commits 3d83d99
  • Branches default

Comments (0)

Files changed (5)

File heartbeatd/Makefile

View file
+include $(GOROOT)/src/Make.inc
+
+TARG=heartbeatd
+GOFILES=\
+	main.go\
+
+include $(GOROOT)/src/Make.cmd

File heartbeatd/main.go

View file
+package main
+
+import (
+	"log"
+	"net"
+	"time"
+	
+	"goprotobuf.googlecode.com/hg/proto"
+	"openvz"
+)
+
+func toUint32(ip net.IP) uint32 {
+	addr := uint32(ip[0]) << 24
+	addr += uint32(ip[1]) << 16
+	addr += uint32(ip[2]) << 8
+	addr += uint32(ip[3])
+	return addr
+}
+
+func nextSequence() *uint64 {
+	return proto.Uint64(1)
+}
+
+func zone() *uint32 {
+	return proto.Uint32(1)
+}
+
+func area() *uint32 {
+	return proto.Uint32(1)
+}
+func host() *uint32 {
+	hostaddr := net.ParseIP("127.0.0.1")
+	if hostaddr == nil {
+		log.Exit("Unable to resolve host address")
+	}
+	return proto.Uint32(toUint32(hostaddr))
+}
+
+const (
+	ONE_SECOND = 1 * 1000 * 1000 * 1000
+)
+
+func main() {
+	BROADCAST, err := net.ResolveUDPAddr("255.255.255.255:7777")
+	if err != nil {
+		log.Exit("Address resolution error:", err)
+	}
+
+	socket, err := net.DialUDP("udp4", nil, BROADCAST)
+	if err != nil {
+		log.Exit("Unable to establish connection:", err)
+	}
+	
+	for true {
+		ann := &openvz.Announcement { Zone: zone(), Area: area(), Host: host(), Sequence: nextSequence() }
+
+		data, err := proto.Marshal(ann)
+		if err != nil {
+			log.Exit("marshaling error:", err)
+		}
+	
+		written, err := socket.Write(data)
+		if err != nil {
+			log.Exit("Unable to write data:", err)
+		}
+		
+		log.Stdout("written: %d ", written)
+		
+		time.Sleep(ONE_SECOND)
+		
+	}
+}

File heartbeatd/proto

View file
+../proto

File proto/Makefile

View file
+include $(GOROOT)/src/Make.inc
+
+TARG=openvz
+GOFILES=openvz.pb.go 
+
+include $(GOROOT)/src/Make.pkg
+include $(GOROOT)/src/pkg/goprotobuf.googlecode.com/hg/Make.protobuf

File proto/openvz.proto

View file
+package openvz;
+
+message Container {
+	required uint32 id = 1; // container id
+}
+
+message Announcement {
+	required uint32 zone = 1; // zone id	
+	required uint32 area = 2; // area id
+	required uint32 host = 3; // host id, should match the ospf router id	
+	required uint64 sequence = 4; // sequence, must increment for every announcement from the host
+	
+	repeated Container containers = 5; // running containers on this host
+}
+