<commit_before>// Copyright 2015 Google Inc. All Rights Reserved.
// Author: jacobsa@google.com (Aaron Jacobs)

package syncutil

import (
	"flag"
	"sync"
)

var fCheckInvariants = flag.Bool("syncutil.check_invariants", false, "Crash when registered invariants are violated.")

// A reader/writer mutex like sync.RWMutex that additionally runs a check for
// registered invariants at times when invariants should hold, when enabled.
//
// Must be created with NewInvariantMutex. See that function for more details.
//
// A typical use looks like this:
//
//     type myStruct struct {
//       mu InvariantMutex
//
//       // INVARIANT: nextGeneration == currentGeneration + 1
//       currentGeneration int  // GUARDED_BY(mu)
//       nextGeneration    int  // GUARDED_BY(mu)
//     }
//
//     // The constructor function for myStruct sets up the mutex to
//     // call the checkInvariants method.
//     func newMyStruct() *myStruct {
//       s := &myStruct{
//         currentGeneration: 1,
//         nextGeneration:    2,
//       }
//
//       s.mu = NewInvariantMutex(func() { s.checkInvariants() })
//       return s
//     }
//
//     type (s *myStruct) checkInvariants() {
//       if s.nextGeneration != s.currentGeneration + 1 {
//         panic(
//           fmt.Sprintf("%v != %v + 1", s.nextGeneration, s.currentGeneration))
//       }
//     }
//
//     // When the flag is set, invariants will be checked at entry to and exit
//     // from this function.
//     func (s *myStruct) setGeneration(n int) {
//       s.mu.Lock()
//       defer s.mu.Unlock()
//
//       currentGeneration = n
//       nextGeneration = n + 1
//     }
//
type InvariantMutex struct {
	mu    sync.RWMutex
	check func()
}

func (i *InvariantMutex) Lock() {
	i.mu.Lock()
	i.checkIfEnabled()
}

func (i *InvariantMutex) Unlock() {
	i.checkIfEnabled()
	i.mu.Unlock()
}

func (i *InvariantMutex) RLock() {
	i.mu.RLock()
	i.checkIfEnabled()
}

func (i *InvariantMutex) RUnlock() {
	i.checkIfEnabled()
	i.mu.RUnlock()
}

func (i *InvariantMutex) checkIfEnabled() {
	if *fCheckInvariants {
		i.check()
	}
}

// Create a reader/writer mutex which, when the flag -syncutil.check_invariants
// is set, will call the supplied function at moments when invariants protected
// by the mutex should hold (e.g. just after acquiring the lock). The function
// should crash if an invariant is violated. It should not have side effects,
// as there are no guarantees that it will run.
func NewInvariantMutex(check func()) InvariantMutex {
	if check == nil {
		panic("check must be non-nil.")
	}

	return InvariantMutex{
		check: check,
	}
}
<commit_msg>Added motivating text for InvariantMutex.<commit_after>// Copyright 2015 Google Inc. All Rights Reserved.
// Author: jacobsa@google.com (Aaron Jacobs)

package syncutil

import (
	"flag"
	"sync"
)

var fCheckInvariants = flag.Bool("syncutil.check_invariants", false, "Crash when registered invariants are violated.")

// A reader/writer mutex like sync.RWMutex that additionally runs a check for
// registered invariants at times when invariants should hold, when enabled.
// This can aid debugging subtle code by crashing early as soon as something
// unexpected happens.
//
// Must be created with NewInvariantMutex. See that function for more details.
//
// A typical use looks like this:
//
//     type myStruct struct {
//       mu InvariantMutex
//
//       // INVARIANT: nextGeneration == currentGeneration + 1
//       currentGeneration int  // GUARDED_BY(mu)
//       nextGeneration    int  // GUARDED_BY(mu)
//     }
//
//     // The constructor function for myStruct sets up the mutex to
//     // call the checkInvariants method.
//     func newMyStruct() *myStruct {
//       s := &myStruct{
//         currentGeneration: 1,
//         nextGeneration:    2,
//       }
//
//       s.mu = NewInvariantMutex(func() { s.checkInvariants() })
//       return s
//     }
//
//     type (s *myStruct) checkInvariants() {
//       if s.nextGeneration != s.currentGeneration + 1 {
//         panic(
//           fmt.Sprintf("%v != %v + 1", s.nextGeneration, s.currentGeneration))
//       }
//     }
//
//     // When the flag is set, invariants will be checked at entry to and exit
//     // from this function.
//     func (s *myStruct) setGeneration(n int) {
//       s.mu.Lock()
//       defer s.mu.Unlock()
//
//       currentGeneration = n
//       nextGeneration = n + 1
//     }
//
type InvariantMutex struct {
	mu    sync.RWMutex
	check func()
}

func (i *InvariantMutex) Lock() {
	i.mu.Lock()
	i.checkIfEnabled()
}

func (i *InvariantMutex) Unlock() {
	i.checkIfEnabled()
	i.mu.Unlock()
}

func (i *InvariantMutex) RLock() {
	i.mu.RLock()
	i.checkIfEnabled()
}

func (i *InvariantMutex) RUnlock() {
	i.checkIfEnabled()
	i.mu.RUnlock()
}

func (i *InvariantMutex) checkIfEnabled() {
	if *fCheckInvariants {
		i.check()
	}
}

// Create a reader/writer mutex which, when the flag -syncutil.check_invariants
// is set, will call the supplied function at moments when invariants protected
// by the mutex should hold (e.g. just after acquiring the lock). The function
// should crash if an invariant is violated. It should not have side effects,
// as there are no guarantees that it will run.
func NewInvariantMutex(check func()) InvariantMutex {
	if check == nil {
		panic("check must be non-nil.")
	}

	return InvariantMutex{
		check: check,
	}
}
<|endoftext|>


<commit_before>package termite

import (
	"log"
	"net"
	"os"
	"rpc"
	"sync"
	"time"
)

type mirrorConnection struct {
	workerAddr string // key in map.
	rpcClient  *rpc.Client
	connection net.Conn

	// Protected by mirrorConnections.Mutex.
	maxJobs       int
	availableJobs int
}

func (me *mirrorConnection) sendFiles(infos []AttrResponse) {
	req := UpdateRequest{
		Files: infos,
	}
	rep := UpdateResponse{}
	err := me.rpcClient.Call("Mirror.Update", &req, &rep)
	if err != nil {
		log.Println("Mirror.Update failure", err)
	}
}

type mirrorConnections struct {
	master      *Master
	coordinator string

	// Condition for mutex below.
	sync.Cond

	// Protects all of the below.
	sync.Mutex
	workers       []string
	mirrors       map[string]*mirrorConnection
	wantedMaxJobs int
}

func (me *mirrorConnections) refreshWorkers() {
	client, err := rpc.DialHTTP("tcp", me.coordinator)
	if err != nil {
		log.Println("dialing coordinator:", err)
		return
	}
	req := 0
	rep := Registered{}
	err = client.Call("Coordinator.List", &req, &rep)
	if err != nil {
		log.Println("coordinator rpc error:", err)
		return
	}

	newWorkers := []string{}
	for _, v := range rep.Registrations {
		newWorkers = append(newWorkers, v.Address)
	}
	if len(newWorkers) == 0 {
		log.Println("coordinator has no workers for us.")
		return
	}

	me.Mutex.Lock()
	defer me.Mutex.Unlock()
	me.workers = newWorkers
}

func newMirrorConnections(m *Master, workers []string, coordinator string, maxJobs int) *mirrorConnections {
	mc := &mirrorConnections{
		master:        m,
		wantedMaxJobs: maxJobs,
		workers:       workers,
		mirrors:       make(map[string]*mirrorConnection),
		coordinator:   coordinator,
	}
	if coordinator != "" {
		if workers != nil {
			log.Println("coordinator will overwrite workers.")
		}

		go mc.periodWorkersRefresh()
	}
	mc.Cond.L = &mc.Mutex
	return mc
}

const _WORKER_REFRESH_DELAY = 60

func (me *mirrorConnections) periodWorkersRefresh() {
	me.refreshWorkers()
	for {
		c := time.After(_WORKER_REFRESH_DELAY * 1e9)
		<-c
		me.refreshWorkers()
	}
}

func (me *mirrorConnections) availableJobs() int {
	a := 0
	for _, mc := range me.mirrors {
		a += mc.availableJobs
	}
	return a
}

func (me *mirrorConnections) maxJobs() int {
	a := 0
	for _, mc := range me.mirrors {
		a += mc.maxJobs
	}
	return a
}

func (me *mirrorConnections) broadcastFiles(origin *mirrorConnection, infos []AttrResponse) {
	for _, w := range me.mirrors {
		if origin != w {
			go w.sendFiles(infos)
		}
	}
}

// Gets a mirrorConnection to run on.  Will block if none available
func (me *mirrorConnections) pick() (*mirrorConnection, os.Error) {
	me.Mutex.Lock()
	defer me.Mutex.Unlock()

	if me.availableJobs() <= 0 {
		me.tryConnect()
	}

	if me.maxJobs() == 0 {
		// Didn't connect to anything.  Should
		// probably direct the wrapper to compile
		// locally.
		return nil, os.NewError("No workers found at all.")
	}

	for me.availableJobs() == 0 {
		me.Cond.Wait()
	}

	var found *mirrorConnection
	for _, v := range me.mirrors {
		if v.availableJobs > 0 {
			found = v
		}
	}
	found.availableJobs--
	return found, nil
}

func (me *mirrorConnections) drop(mc *mirrorConnection, err os.Error) {
	me.Mutex.Lock()
	defer me.Mutex.Unlock()

	// TODO - should blacklist the address.
	log.Printf("Dropping mirror %s. Reason: %s", mc.workerAddr, err)
	mc.connection.Close()
	me.mirrors[mc.workerAddr] = nil, false
}

func (me *mirrorConnections) jobDone(mc *mirrorConnection) {
	me.Mutex.Lock()
	defer me.Mutex.Unlock()

	mc.availableJobs++
	me.Cond.Signal()
}

// Tries to connect to one extra worker.  Must already hold mutex.
func (me *mirrorConnections) tryConnect() {
	// We want to max out capacity of each worker, as that helps
	// with cache hit rates on the worker.
	wanted := me.wantedMaxJobs - me.maxJobs()
	if wanted <= 0 {
		return
	}

	for _, addr := range me.workers {
		_, ok := me.mirrors[addr]
		if ok {
			continue
		}
		log.Println("Creating mirror on", addr)
		mc, err := me.master.createMirror(addr, wanted)
		if err != nil {
			log.Println("nonfatal error creating mirror:", err)
			continue
		}
		mc.workerAddr = addr
		me.mirrors[addr] = mc
	}
}
<commit_msg>Fix leak of HTTP client connections.<commit_after>package termite

import (
	"log"
	"net"
	"os"
	"rpc"
	"sync"
	"time"
)

type mirrorConnection struct {
	workerAddr string // key in map.
	rpcClient  *rpc.Client
	connection net.Conn

	// Protected by mirrorConnections.Mutex.
	maxJobs       int
	availableJobs int
}

func (me *mirrorConnection) sendFiles(infos []AttrResponse) {
	req := UpdateRequest{
		Files: infos,
	}
	rep := UpdateResponse{}
	err := me.rpcClient.Call("Mirror.Update", &req, &rep)
	if err != nil {
		log.Println("Mirror.Update failure", err)
	}
}

type mirrorConnections struct {
	master      *Master
	coordinator string

	// Condition for mutex below.
	sync.Cond

	// Protects all of the below.
	sync.Mutex
	workers       []string
	mirrors       map[string]*mirrorConnection
	wantedMaxJobs int
}

func (me *mirrorConnections) refreshWorkers() {
	client, err := rpc.DialHTTP("tcp", me.coordinator)
	if err != nil {
		log.Println("dialing coordinator:", err)
		return
	}
	defer client.Close()
	req := 0
	rep := Registered{}
	err = client.Call("Coordinator.List", &req, &rep)
	if err != nil {
		log.Println("coordinator rpc error:", err)
		return
	}

	newWorkers := []string{}
	for _, v := range rep.Registrations {
		newWorkers = append(newWorkers, v.Address)
	}
	if len(newWorkers) == 0 {
		log.Println("coordinator has no workers for us.")
		return
	}

	me.Mutex.Lock()
	defer me.Mutex.Unlock()
	me.workers = newWorkers
}

func newMirrorConnections(m *Master, workers []string, coordinator string, maxJobs int) *mirrorConnections {
	mc := &mirrorConnections{
		master:        m,
		wantedMaxJobs: maxJobs,
		workers:       workers,
		mirrors:       make(map[string]*mirrorConnection),
		coordinator:   coordinator,
	}
	if coordinator != "" {
		if workers != nil {
			log.Println("coordinator will overwrite workers.")
		}

		go mc.periodWorkersRefresh()
	}
	mc.Cond.L = &mc.Mutex
	return mc
}

const _WORKER_REFRESH_DELAY = 60

func (me *mirrorConnections) periodWorkersRefresh() {
	me.refreshWorkers()
	for {
		c := time.After(_WORKER_REFRESH_DELAY * 1e9)
		<-c
		me.refreshWorkers()
	}
}

func (me *mirrorConnections) availableJobs() int {
	a := 0
	for _, mc := range me.mirrors {
		a += mc.availableJobs
	}
	return a
}

func (me *mirrorConnections) maxJobs() int {
	a := 0
	for _, mc := range me.mirrors {
		a += mc.maxJobs
	}
	return a
}

func (me *mirrorConnections) broadcastFiles(origin *mirrorConnection, infos []AttrResponse) {
	for _, w := range me.mirrors {
		if origin != w {
			go w.sendFiles(infos)
		}
	}
}

// Gets a mirrorConnection to run on.  Will block if none available
func (me *mirrorConnections) pick() (*mirrorConnection, os.Error) {
	me.Mutex.Lock()
	defer me.Mutex.Unlock()

	if me.availableJobs() <= 0 {
		me.tryConnect()
	}

	if me.maxJobs() == 0 {
		// Didn't connect to anything.  Should
		// probably direct the wrapper to compile
		// locally.
		return nil, os.NewError("No workers found at all.")
	}

	for me.availableJobs() == 0 {
		me.Cond.Wait()
	}

	var found *mirrorConnection
	for _, v := range me.mirrors {
		if v.availableJobs > 0 {
			found = v
		}
	}
	found.availableJobs--
	return found, nil
}

func (me *mirrorConnections) drop(mc *mirrorConnection, err os.Error) {
	me.Mutex.Lock()
	defer me.Mutex.Unlock()

	// TODO - should blacklist the address.
	log.Printf("Dropping mirror %s. Reason: %s", mc.workerAddr, err)
	mc.connection.Close()
	me.mirrors[mc.workerAddr] = nil, false
}

func (me *mirrorConnections) jobDone(mc *mirrorConnection) {
	me.Mutex.Lock()
	defer me.Mutex.Unlock()

	mc.availableJobs++
	me.Cond.Signal()
}

// Tries to connect to one extra worker.  Must already hold mutex.
func (me *mirrorConnections) tryConnect() {
	// We want to max out capacity of each worker, as that helps
	// with cache hit rates on the worker.
	wanted := me.wantedMaxJobs - me.maxJobs()
	if wanted <= 0 {
		return
	}

	for _, addr := range me.workers {
		_, ok := me.mirrors[addr]
		if ok {
			continue
		}
		log.Println("Creating mirror on", addr)
		mc, err := me.master.createMirror(addr, wanted)
		if err != nil {
			log.Println("nonfatal error creating mirror:", err)
			continue
		}
		mc.workerAddr = addr
		me.mirrors[addr] = mc
	}
}
<|endoftext|>


<commit_before>// +build windows

package i18n_test

import (
	"os"
	"path/filepath"

	"github.com/XenoPhex/jibber_jabber"
	"github.com/cloudfoundry/cli/cf/i18n"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("i18n.Init() function", func() {
	BeforeEach(func() {
		i18n.Resources_path = filepath.Join("cf", "i18n", "test_fixtures")
		//All these tests require the system language to be English
		Ω(jibber_jabber.DetectIETF()).Should(Equal("en-US"))
	})

	Context("creates a valid T function", func() {
		BeforeEach(func() {
			os.Setenv("LC_ALL", "en_US.UTF-8")
		})

		It("returns a usable T function for simple strings", func() {
			T := i18n.Init()
			Ω(T).ShouldNot(BeNil())

			translation := T("Hello world!")
			Ω("Hello world!").Should(Equal(translation))
		})

		It("returns a usable T function for complex strings (interpolated)", func() {
			T := i18n.Init()
			Ω(T).ShouldNot(BeNil())

			translation := T("Hello {{.Name}}!", map[string]interface{}{"Name": "Anand"})
			Ω("Hello Anand!").Should(Equal(translation))
		})
	})
})
<commit_msg>Fix windows test so they use updated translations.<commit_after>// +build windows

package i18n_test

import (
	"os"
	"path/filepath"

	"github.com/XenoPhex/jibber_jabber"
	"github.com/cloudfoundry/cli/cf/i18n"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("i18n.Init() function", func() {
	BeforeEach(func() {
		i18n.Resources_path = filepath.Join("cf", "i18n", "test_fixtures")
		//All these tests require the system language to be English
		Ω(jibber_jabber.DetectIETF()).Should(Equal("en-US"))
	})

	Context("creates a valid T function", func() {
		BeforeEach(func() {
			os.Setenv("LC_ALL", "en_US.UTF-8")
		})

		It("returns a usable T function for simple strings", func() {
			T := i18n.Init()
			Ω(T).ShouldNot(BeNil())

			translation := T("Change user password")
			Ω("Change user password").Should(Equal(translation))
		})

		It("returns a usable T function for complex strings (interpolated)", func() {
			T := i18n.Init()
			Ω(T).ShouldNot(BeNil())

			translation := T("Deleting domain {{.DomainName}} as {{.Username}}...", map[string]interface{}{"DomainName": "foo", "Name": "Anand"})
			Ω("Deleting domain foo as Anand...").Should(Equal(translation))
		})
	})
})
<|endoftext|>


<commit_before>package command

import (
	"fmt"
	"io/ioutil"
	"math"
	"os"
	"path/filepath"

	"github.com/anthonynsimon/bild/imgio"
	"github.com/jung-kurt/gofpdf"
	"github.com/urfave/cli"
	"go.uber.org/zap"
)

// Create create command
func Create() cli.Command {

	logger, _ := zap.NewProduction()
	sugar := logger.Sugar()

	return cli.Command{
		Name: "create",
		Flags: []cli.Flag{
			cli.StringFlag{
				Name:  "pageSize, s",
				Value: "A4",
				Usage: "Acceptable values are A3, A4, A5, Letter, or Legal.",
			},
			cli.StringFlag{
				Name:  "imgPath, p",
				Value: "./img",
			},
			cli.Float64Flag{
				Name:  "columnNum, c",
				Value: 2,
			},
			cli.Float64Flag{
				Name:  "margin, m",
				Value: 0,
			},
			cli.Float64Flag{
				Name:  "marginTop, t",
				Value: 1.1,
			},
			cli.Float64Flag{
				Name:  "xInitPosition, x",
				Value: 5,
			},
			cli.Float64Flag{
				Name:  "yInitPosition, y",
				Value: 5,
			},
			cli.BoolFlag{
				Name:   "debug, d",
				Hidden: true,
			},
		},
		Action: func(c *cli.Context) error {

			var root, _ = os.Getwd()

			var imageMapPath = make(map[string][]string)

			files, e := ioutil.ReadDir(c.String("imgPath"))

			if e != nil {
				sugar.Info(e.Error())
			}

			for _, f := range files {

				err := filepath.Walk(filepath.Join(root, c.String("imgPath"), f.Name()), func(imgFilePath string, info os.FileInfo, err error) error {

					_, imgErr := imgio.Open(imgFilePath)
					if imgErr != nil {
						return nil
					}

					imageMapPath[f.Name()] = append(imageMapPath[f.Name()], imgFilePath)

					if c.Bool("debug") {
						sugar.Info(imgFilePath)
					}
					return nil
				})

				if c.Bool("debug") {
					sugar.Info(imageMapPath)
				}

				pdf := gofpdf.New("P", "mm", c.String("pageSize"), "")
				pdf.AddPage()

				w, h := pdf.GetPageSize()

				if c.Bool("debug") {
					sugar.Info(w)
					sugar.Info(h)
				}

				// parameter
				columnNum := c.Float64("columnNum")
				offset := c.Float64("margin")
				imageWidth := float64((w - (offset * 2) - 20) / columnNum)
				imageHeight := float64(0)
				xInitPosition := c.Float64("xInitPosition")
				yInitPosition := c.Float64("yInitPosition")
				columnCount := float64(0)

				if f.Name() == ".DS_Store" {
					continue
				}

				fmt.Printf("Create %s.pdf\n", filepath.Base(f.Name()))
				y := float64(0)

				for _, v := range imageMapPath {
					imageHeight = h / float64(len(v))
					for i, v1 := range v {

						if i > 0 && math.Mod(float64(i), columnNum) == 0 {
							columnCount++
						}

						x := xInitPosition + math.Mod(float64(i), columnNum)*(imageWidth+offset) + math.Mod(float64(i), columnNum)*5

						if i > int(columnNum-1) {
							y = yInitPosition + imageHeight*columnCount*c.Float64("marginTop")
						} else {
							y = yInitPosition + imageHeight*columnCount
						}

						pdf.Image(v1, x, y, imageWidth, imageHeight, false, "", 0, "")
						fmt.Println(v1)
						if c.Bool("debug") {
							sugar.Infof("i %s\n", i)
							sugar.Infof("x %s\n", x)
							sugar.Infof("y %s\n", y)
							sugar.Infof("width %s\n", imageWidth)
							sugar.Infof("width %s\n", imageHeight)
							sugar.Infof("mod %s\n", math.Mod(float64(i), columnNum))
							sugar.Infof("count %s\n", columnCount)
							sugar.Info(y)
						}
					}
				}

				imageMapPath = make(map[string][]string)
				local := filepath.Join(root, f.Name()+".pdf")

				fmt.Println(local)

				if c.Bool("debug") {
					sugar.Info(local)
				}

				erri := pdf.OutputFileAndClose(local)

				if erri != nil {
					panic(erri)
				}

				if err != nil {
					panic(err)
				}
			}

			return nil
		},
	}
}
<commit_msg>fixed bug and add new parameter<commit_after>package command

import (
	"fmt"
	"io/ioutil"
	"math"
	"os"
	"path/filepath"

	"github.com/anthonynsimon/bild/imgio"
	"github.com/jung-kurt/gofpdf"
	"github.com/urfave/cli"
	"go.uber.org/zap"
)

// Create create command
func Create() cli.Command {

	logger, _ := zap.NewProduction()
	sugar := logger.Sugar()

	return cli.Command{
		Name: "create",
		Flags: []cli.Flag{
			cli.StringFlag{
				Name:  "orientation, r",
				Value: "P",
				Usage: "P or Portrait. For landscape mode, specify L or Landscape.",
			},
			cli.StringFlag{
				Name:  "pageSize, s",
				Value: "A4",
				Usage: "Acceptable values are A3, A4, A5, Letter, or Legal.",
			},
			cli.StringFlag{
				Name:  "imgPath, p",
				Value: "./img",
			},
			cli.Float64Flag{
				Name:  "columnNum, c",
				Value: 2,
			},
			cli.Float64Flag{
				Name:  "margin, m",
				Value: 0,
			},
			cli.Float64Flag{
				Name:  "marginTop, t",
				Value: 1.1,
			},
			cli.Float64Flag{
				Name:  "marginLeft, l",
				Value: 5,
			},
			cli.Float64Flag{
				Name:  "xInitPosition, x",
				Value: 5,
			},
			cli.Float64Flag{
				Name:  "yInitPosition, y",
				Value: 5,
			},
			cli.BoolFlag{
				Name:   "debug, d",
				Hidden: true,
			},
		},
		Action: func(c *cli.Context) error {

			var root, _ = os.Getwd()

			var imageMapPath = make(map[string][]string)

			files, e := ioutil.ReadDir(c.String("imgPath"))

			if e != nil {
				sugar.Info(e.Error())
			}

			for _, f := range files {

				err := filepath.Walk(filepath.Join(root, c.String("imgPath"), f.Name()), func(imgFilePath string, info os.FileInfo, err error) error {

					_, imgErr := imgio.Open(imgFilePath)
					if imgErr != nil {
						return nil
					}

					imageMapPath[f.Name()] = append(imageMapPath[f.Name()], imgFilePath)

					if c.Bool("debug") {
						sugar.Info(imgFilePath)
					}
					return nil
				})

				if c.Bool("debug") {
					sugar.Info(imageMapPath)
				}

				pdf := gofpdf.New(c.String("orientation"), "mm", c.String("pageSize"), "")
				pdf.AddPage()

				w, h := pdf.GetPageSize()

				if c.Bool("debug") {
					sugar.Info(w)
					sugar.Info(h)
				}

				// parameter
				columnNum := c.Float64("columnNum")
				offset := c.Float64("margin")
				xInitPosition := c.Float64("xInitPosition")
				yInitPosition := c.Float64("yInitPosition")
				columnCount := float64(0)

				if f.Name() == ".DS_Store" {
					continue
				}

				fmt.Printf("Create %s.pdf\n", filepath.Base(f.Name()))
				y := float64(0)

				for _, v := range imageMapPath {
					imgTotal := float64(len(v))
					imageHeight := h / imgTotal

					if imgTotal < columnNum {
						columnNum = imgTotal
					}

					imageWidth := float64((w - (offset * 2) - 20) / columnNum)

					columnCount = float64(0)

					for i, v1 := range v {

						if i > 0 && math.Mod(float64(i), columnNum) == 0 {
							columnCount++
						}

						x := xInitPosition + math.Mod(float64(i), columnNum)*(imageWidth+offset) + math.Mod(float64(i), columnNum)*c.Float64("marginLeft")

						if i > int(columnNum-1) {
							y = yInitPosition + imageHeight*columnCount*c.Float64("marginTop")
						} else {
							y = yInitPosition + imageHeight*columnCount
						}

						pdf.Image(v1, x, y, imageWidth, imageHeight, false, "", 0, "")
						fmt.Println(v1)
						if c.Bool("debug") {
							sugar.Infof("i %s\n", i)
							sugar.Infof("x %s\n", x)
							sugar.Infof("y %s\n", y)
							sugar.Infof("width %s\n", imageWidth)
							sugar.Infof("width %s\n", imageHeight)
							sugar.Infof("mod %s\n", math.Mod(float64(i), columnNum))
							sugar.Infof("count %s\n", columnCount)
							sugar.Info(y)
						}
					}
				}

				imageMapPath = make(map[string][]string)
				local := filepath.Join(root, f.Name()+".pdf")

				fmt.Println(local)

				if c.Bool("debug") {
					sugar.Info(local)
				}

				erri := pdf.OutputFileAndClose(local)

				if erri != nil {
					panic(erri)
				}

				if err != nil {
					panic(err)
				}
			}

			return nil
		},
	}
}
<|endoftext|>


<commit_before>package checksmtp

import (
	"os"

	"net/smtp"

	"fmt"

	"crypto/tls"

	"net"

	"time"

	"github.com/jessevdk/go-flags"
	"github.com/mackerelio/checkers"
)

var opts struct {
	Host     string  `short:"H" long:"host" default:"localhost" description:"Hostname"`
	Port     string  `short:"p" long:"port" default:"25" description:"Port"`
	FQDN     string  `short:"F" long:"fqdn" description:"FQDN used for HELO"`
	SMTPS    bool    `short:"s" long:"smtps" description:"Use SMTP over TLS"`
	StartTLS bool    `short:"S" long:"starttls" description:"Use STARTTLS"`
	Auth     string  `short:"A" long:"authmech" description:"SMTP AUTH Authentication Mechanisms (only PLAIN supported)"`
	User     string  `short:"U" long:"authuser" description:"SMTP AUTH username"`
	Password string  `short:"P" long:"authpassword" description:"SMTP AUTH password"`
	Warning  float64 `short:"w" long:"warning" default:"3" description:"Warning threshold (sec)"`
	Critical float64 `short:"c" long:"critical" default:"5" description:"Critical threshold (sec)"`
	Timeout  int     `short:"t" long:"timeout" default:"10" description:"Timeout (sec)"`
}

// Do the plugin
func Do() {
	ckr := run(os.Args[1:])
	ckr.Name = "SMTP"
	ckr.Exit()
}

func makeConn(host, port string, timeout int, isSMTPS bool, tlsConfig *tls.Config) (net.Conn, error) {
	d := net.Dialer{Timeout: time.Duration(timeout) * time.Second}
	if isSMTPS {
		return tls.DialWithDialer(&d, "tcp", fmt.Sprintf("%s:%s", host, port), tlsConfig)
	}
	return d.Dial("tcp", fmt.Sprintf("%s:%s", opts.Host, opts.Port))
}

func run(args []string) *checkers.Checker {
	_, err := flags.ParseArgs(&opts, args)
	if err != nil {
		os.Exit(1)
	}

	if opts.Auth != "" && opts.Auth != "PLAIN" {
		return checkers.Unknown("invalid SMTP AUTH Authentication Mechanisms (only PLAIN supported)")
	}

	fqdn := opts.FQDN
	if fqdn == "" {
		localHostName, err := os.Hostname()
		if err != nil {
			return checkers.Unknown(err.Error())
		}
		fqdn = localHostName
	}

	tlsConfig := &tls.Config{
		InsecureSkipVerify: true,
		ServerName:         opts.Host,
	}

	conn, err := makeConn(opts.Host, opts.Port, opts.Timeout, opts.SMTPS, tlsConfig)
	if err != nil {
		return checkers.Critical(err.Error())
	}

	stTime := time.Now()

	c, err := smtp.NewClient(conn, opts.Host)
	if err != nil {
		return checkers.Critical(err.Error())
	}
	defer c.Quit()

	if err := c.Hello(fqdn); err != nil {
		return checkers.Critical(err.Error())
	}

	if opts.StartTLS {
		if err := c.StartTLS(tlsConfig); err != nil {
			return checkers.Critical(err.Error())
		}
	}

	if opts.Auth == "PLAIN" {
		auth := smtp.PlainAuth("", opts.User, opts.Password, opts.Host)
		if err := c.Auth(auth); err != nil {
			return checkers.Critical(err.Error())
		}
	}

	if err := c.Mail(""); err != nil {
		c.Reset()
		return checkers.Critical(err.Error())
	}
	c.Reset()

	elapsed := time.Since(stTime)

	msg := fmt.Sprintf("%.3f seconds response time", elapsed.Seconds())

	if elapsed.Seconds() > opts.Critical {
		return checkers.Critical(msg)
	} else if elapsed.Seconds() > opts.Warning {
		return checkers.Warning(msg)
	} else {
		return checkers.Ok(msg)
	}
}
<commit_msg>Remove default value of threshold options<commit_after>package checksmtp

import (
	"os"

	"net/smtp"

	"fmt"

	"crypto/tls"

	"net"

	"time"

	"github.com/jessevdk/go-flags"
	"github.com/mackerelio/checkers"
)

var opts struct {
	Host     string  `short:"H" long:"host" default:"localhost" description:"Hostname"`
	Port     string  `short:"p" long:"port" default:"25" description:"Port"`
	FQDN     string  `short:"F" long:"fqdn" description:"FQDN used for HELO"`
	SMTPS    bool    `short:"s" long:"smtps" description:"Use SMTP over TLS"`
	StartTLS bool    `short:"S" long:"starttls" description:"Use STARTTLS"`
	Auth     string  `short:"A" long:"authmech" description:"SMTP AUTH Authentication Mechanisms (only PLAIN supported)"`
	User     string  `short:"U" long:"authuser" description:"SMTP AUTH username"`
	Password string  `short:"P" long:"authpassword" description:"SMTP AUTH password"`
	Warning  float64 `short:"w" long:"warning" description:"Warning threshold (sec)"`
	Critical float64 `short:"c" long:"critical" description:"Critical threshold (sec)"`
	Timeout  int     `short:"t" long:"timeout" default:"10" description:"Timeout (sec)"`
}

// Do the plugin
func Do() {
	ckr := run(os.Args[1:])
	ckr.Name = "SMTP"
	ckr.Exit()
}

func makeConn(host, port string, timeout int, isSMTPS bool, tlsConfig *tls.Config) (net.Conn, error) {
	d := net.Dialer{Timeout: time.Duration(timeout) * time.Second}
	if isSMTPS {
		return tls.DialWithDialer(&d, "tcp", fmt.Sprintf("%s:%s", host, port), tlsConfig)
	}
	return d.Dial("tcp", fmt.Sprintf("%s:%s", opts.Host, opts.Port))
}

func run(args []string) *checkers.Checker {
	_, err := flags.ParseArgs(&opts, args)
	if err != nil {
		os.Exit(1)
	}

	if opts.Warning == 0 && opts.Critical == 0 {
		return checkers.Unknown("require threshold option (warning or critical)")
	}

	if opts.Auth != "" && opts.Auth != "PLAIN" {
		return checkers.Unknown("invalid SMTP AUTH Authentication Mechanisms (only PLAIN supported)")
	}

	fqdn := opts.FQDN
	if fqdn == "" {
		localHostName, err := os.Hostname()
		if err != nil {
			return checkers.Unknown(err.Error())
		}
		fqdn = localHostName
	}

	tlsConfig := &tls.Config{
		InsecureSkipVerify: true,
		ServerName:         opts.Host,
	}

	conn, err := makeConn(opts.Host, opts.Port, opts.Timeout, opts.SMTPS, tlsConfig)
	if err != nil {
		return checkers.Critical(err.Error())
	}

	stTime := time.Now()

	c, err := smtp.NewClient(conn, opts.Host)
	if err != nil {
		return checkers.Critical(err.Error())
	}
	defer c.Quit()

	if err := c.Hello(fqdn); err != nil {
		return checkers.Critical(err.Error())
	}

	if opts.StartTLS {
		if err := c.StartTLS(tlsConfig); err != nil {
			return checkers.Critical(err.Error())
		}
	}

	if opts.Auth == "PLAIN" {
		auth := smtp.PlainAuth("", opts.User, opts.Password, opts.Host)
		if err := c.Auth(auth); err != nil {
			return checkers.Critical(err.Error())
		}
	}

	if err := c.Mail(""); err != nil {
		c.Reset()
		return checkers.Critical(err.Error())
	}
	c.Reset()

	elapsed := time.Since(stTime)

	msg := fmt.Sprintf("%.3f seconds response time", elapsed.Seconds())

	if opts.Critical != 0 && elapsed.Seconds() > opts.Critical {
		return checkers.Critical(msg)
	} else if opts.Warning != 0 && elapsed.Seconds() > opts.Warning {
		return checkers.Warning(msg)
	} else {
		return checkers.Ok(msg)
	}
}
<|endoftext|>


<commit_before>package binding

import (
	"testing"
	"time"
)

func TestParseTimeErrorParsing(t *testing.T) {
	_, err := parseTime([]string{"this is sparta"})
	if err == nil {
		t.Fatal("expected an error, got nothing")
	}
}

func TestParseTime(t *testing.T) {
	tt, err := parseTime([]string{"2017-01-01"})
	if err != nil {
		t.Fatal(err)
	}
	expected := time.Date(2017, time.January, 1, 0, 0, 0, 0, time.UTC)
	if tt != expected {
		t.Fatal("expected %v, got %v", expected, tt)
	}
}<commit_msg>changed tests to use require<commit_after>package binding

import (
	"testing"
	"time"

	"github.com/stretchr/testify/require"
)

func TestParseTimeErrorParsing(t *testing.T) {
	r := require.New(t)
	_, err := parseTime([]string{"this is sparta"})
	r.Error(err)
}

func TestParseTime(t *testing.T) {
	r := require.New(t)
	tt, err := parseTime([]string{"2017-01-01"})
	r.NoError(err)
	expected := time.Date(2017, time.January, 1, 0, 0, 0, 0, time.UTC)
	r.Equal(expected, tt)
}
<|endoftext|>


<commit_before>/*
Copyright 2014 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package runtime includes helper functions for working with API objects
// that follow the kubernetes API object conventions, which are:
//
// 0. Your API objects have a common metadata struct member, TypeMeta.
// 1. Your code refers to an internal set of API objects.
// 2. In a separate package, you have an external set of API objects.
// 3. The external set is considered to be versioned, and no breaking
//    changes are ever made to it (fields may be added but not changed
//    or removed).
// 4. As your api evolves, you'll make an additional versioned package
//    with every major change.
// 5. Versioned packages have conversion functions which convert to
//    and from the internal version.
// 6. You'll continue to support older versions according to your
//    deprecation policy, and you can easily provide a program/library
//    to update old versions into new versions because of 5.
// 7. All of your serializations and deserializations are handled in a
//    centralized place.
//
// Package runtime provides a conversion helper to make 5 easy, and the
// Encode/Decode/DecodeInto trio to accomplish 7. You can also register
// additional "codecs" which use a version of your choice. It's
// recommended that you register your types with runtime in your
// package's init function.
//
// As a bonus, a few common types useful from all api objects and versions
// are provided in types.go.
package runtime // import "k8s.io/apimachinery/pkg/runtime"
<commit_msg>apimachinery/pkg/runtime/doc: Split list into paragraphs<commit_after>/*
Copyright 2014 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package runtime includes helper functions for working with API objects
// that follow the kubernetes API object conventions, which are:
//
// 0. Your API objects have a common metadata struct member, TypeMeta.
//
// 1. Your code refers to an internal set of API objects.
//
// 2. In a separate package, you have an external set of API objects.
//
// 3. The external set is considered to be versioned, and no breaking
// changes are ever made to it (fields may be added but not changed
// or removed).
//
// 4. As your api evolves, you'll make an additional versioned package
// with every major change.
//
// 5. Versioned packages have conversion functions which convert to
// and from the internal version.
//
// 6. You'll continue to support older versions according to your
// deprecation policy, and you can easily provide a program/library
// to update old versions into new versions because of 5.
//
// 7. All of your serializations and deserializations are handled in a
// centralized place.
//
// Package runtime provides a conversion helper to make 5 easy, and the
// Encode/Decode/DecodeInto trio to accomplish 7. You can also register
// additional "codecs" which use a version of your choice. It's
// recommended that you register your types with runtime in your
// package's init function.
//
// As a bonus, a few common types useful from all api objects and versions
// are provided in types.go.
package runtime // import "k8s.io/apimachinery/pkg/runtime"
<|endoftext|>


<commit_before>// This is Free Software covered by the terms of the MIT license.
// See LICENSE file for details.
// (c) 2016 by Sascha L. Teichmann.
// See the full list of contributors in the CONTRIBUTORS file.

package main

import (
	"bufio"
	"fmt"
	"log"
	"os"
	"strings"
	"text/template"
)

var types = []string{
	"int", "int8", "int16", "int32", "int64",
	"uint", "uint8", "uint16", "uint32", "uint64",
}

var headerTmplTxt = `// THIS IS A MACHINE GENERATED FILE!
// BE CAREFUL WITH EDITING BY HAND.
//
// This is Free Software covered by the terms of the MIT license.
// See LICENSE file for details.
// (c) 2016 by Sascha L. Teichmann.
// See the full list of contributors in the CONTRIBUTORS file.

package genmap
`

var mapTmplTxt = `
{{ $F := .From | title -}}
{{- $T := .To | title -}}
{{- $TYPE :=  printf "Map%sTo%s" $F $T -}}
{{- $ENTRY :=  printf "entry%sTo%s" $F $T -}}

type {{ $ENTRY }} struct {
	k    {{.From }}
	v    {{ .To }}
	next *{{ $ENTRY }}
}

// {{ $TYPE }} implements a hash map from {{ .From }} to {{ .To }}.
type {{ $TYPE }} struct {
	mask     int
	slots    []*{{ $ENTRY }}
	used     int
	size     int
	max      int
	freelist *{{ $ENTRY }}
}

// New{{ $TYPE }} creates a new {{ $TYPE }} with at least a size of size.
func New{{ $TYPE }}(size int) *{{ $TYPE }} {
	shift := nextShiftPowerOfTwo(size)
	return &{{ $TYPE }}{
		mask:  ^(^0 << shift),
		max:   maxFill(1 << shift),
		slots: make([]*{{ $ENTRY }}, 1<<shift),
	}
}

// Size returns the current size of the map.
func (h *{{ $TYPE }}) Size() int {
	return h.size
}

// Get looks up a key k and returns its value. 0 if not found.
func (h *{{ $TYPE }}) Get(k {{ .From }}) {{ .To }} {
	for e := h.slots[int(k)&h.mask]; e != nil; e = e.next {
		if e.k == k {
			return e.v
		}
	}
	return 0
}

// Contains looks up a key k and returns true if it is found else false.
func (h *{{ $TYPE }}) Contains(k {{ .From }}) bool {
	for e := h.slots[int(k)&h.mask]; e != nil; e = e.next {
		if e.k == k {
			return true
		}
	}
	return false
}

// Modify looks up a key k and calls function fn with a pointer to its value.
func (h *{{ $TYPE }}) Modify(k {{ .From }}, fn func(v *{{ .To }})) {
	for e := h.slots[int(k)&h.mask]; e != nil; e = e.next {
		if e.k == k {
			fn(&e.v)
			return
		}
	}
}

// Find looks up a key k and returns its value and true.
// 0 and false if not found.
func (h *{{ $TYPE }}) Find(k {{ .From }}) ({{ .To }}, bool) {
	for e := h.slots[int(k)&h.mask]; e != nil; e = e.next {
		if e.k == k {
			return e.v, true
		}
	}
	return 0, false
}

// Visit calls a given function fn for every key/value pair in the map.
func (h *{{ $TYPE }}) Visit(fn func({{ .From }}, {{ .To }})) {
	for _, e := range h.slots {
		for ; e != nil; e = e.next {
			fn(e.k, e.v)
		}
	}
}

// Add increments the value associated with key k by the value of v.
// It creates a new key/value pair k/v in the map if the key does not exist.
func (h *{{ $TYPE }}) Add(k {{ .From }}, v {{ .To }}) {
	p := &h.slots[int(k)&h.mask]
	for e := *p; e != nil; e = e.next {
		if e.k == k {
			e.v += v
			return
		}
	}
	n := h.alloc(k, v)
	if *p != nil {
		n.next = *p
		*p = n
	} else {
		*p = n
		h.used++
		if h.used > h.max {
			h.rehash()
		}
	}
}

// Put sets the value associated with key k to the value of v.
// It creates a new key/value pair k/v in the map if the key does not exist.
func (h *{{ $TYPE }}) Put(k {{ .From }}, v {{ .To }}) {
	p := &h.slots[int(k)&h.mask]
	for e := *p; e != nil; e = e.next {
		if e.k == k {
			e.v = v
			return
		}
	}
	n := h.alloc(k, v)
	if *p != nil {
		n.next = *p
		*p = n
	} else {
		*p = n
		h.used++
		if h.used > h.max {
			h.rehash()
		}
	}
}

// Remove removes the key/value pair associated with key k from this map..
func (h *{{ $TYPE }}) Remove(k {{ .From }}) {
	p := &h.slots[int(k)&h.mask]
	var parent *{{ $ENTRY }}
	for e := *p; e != nil; e = e.next {
		if e.k == k {
			if parent == nil { // head
				if e.next == nil { // last in chain
					h.used--
				}
				*p = e.next
			} else {
				parent.next = e.next
			}
			h.free(e)
			return
		}
		parent = e
	}
}

// Inc increments a value associated with key k by one.
// A new entry is created with value 1 if the key
// does not exist.
func (h *{{ $TYPE }}) Inc(k {{ .From }}) {
	p := &h.slots[int(k)&h.mask]
	for e := *p; e != nil; e = e.next {
		if e.k == k {
			e.v++
			return
		}
	}
	n := h.alloc(k, 1)
	if *p != nil {
		n.next = *p
		*p = n
	} else {
		*p = n
		h.used++
		if h.used > h.max {
			h.rehash()
		}
	}
}

func (h *{{ $TYPE }}) rehash() {
	ns := len(h.slots) << 1
	nslots := make([]*{{ $ENTRY }}, ns)
	nmask := (h.mask << 1) | 1
	h.mask = nmask
	nu := 0
	for i, e := range h.slots {
		if e == nil {
			continue
		}
		for e != nil {
			n := e.next
			p := &nslots[int(e.k)&nmask]
			if *p == nil {
				nu++
			}
			e.next = *p
			*p = e
			e = n
		}
		h.slots[i] = nil
	}
	h.used = nu
	h.slots = nslots
	h.max = maxFill(ns)
}

func (h *{{ $TYPE }}) free(entry *{{ $ENTRY }}) {
	entry.next = h.freelist
	h.freelist = entry
	h.size--
}

func (h *{{ $TYPE }}) alloc(k {{ .From }}, v {{ .To }}) *{{ $ENTRY }} {
	if h.freelist == nil {
		entries := make([]{{ $ENTRY }}, 256)
		for i := 0; i < 256-1; i++ {
			entries[i].next = &entries[i+1]
		}
		h.freelist = &entries[0]
	}
	h.size++
	x := h.freelist
	h.freelist = x.next
	x.next = nil
	x.k = k
	x.v = v
	return x
}
`

var funcMap = template.FuncMap{
	"title": strings.Title,
}

var (
	headerTmpl = template.Must(template.New("header").Funcs(funcMap).Parse(headerTmplTxt))
	mapTmpl    = template.Must(template.New("maps").Funcs(funcMap).Parse(mapTmplTxt))
)

func main() {
	for _, from := range types {
		filename := fmt.Sprintf("map_%s.go", from)
		log.Printf("Generating %s...\n", filename)

		f, err := os.Create(filename)
		if err != nil {
			log.Printf("creating %s failed: %v\n", filename, err)
			continue
		}
		out := bufio.NewWriter(f)

		if err = headerTmpl.Execute(out, nil); err != nil {
			log.Printf("templating header %s failed: %v\n", filename, err)
			f.Close()
			continue
		}

		var parameters = struct {
			From string
			To   string
		}{
			From: from,
		}

		for _, parameters.To = range types {
			if err = mapTmpl.Execute(out, &parameters); err != nil {
				log.Printf("templating %s failed: %v\n", filename, err)
			}
		}

		if err = out.Flush(); err != nil {
			log.Printf("flushing %s failed: %v\n", filename, err)
		}
		if err = f.Close(); err != nil {
			log.Printf("closing %s failed: %v\n", filename, err)
		}
	}
}
<commit_msg>Break generating maps for a file if templating fails.<commit_after>// This is Free Software covered by the terms of the MIT license.
// See LICENSE file for details.
// (c) 2016 by Sascha L. Teichmann.
// See the full list of contributors in the CONTRIBUTORS file.

package main

import (
	"bufio"
	"fmt"
	"log"
	"os"
	"strings"
	"text/template"
)

var types = []string{
	"int", "int8", "int16", "int32", "int64",
	"uint", "uint8", "uint16", "uint32", "uint64",
}

var headerTmplTxt = `// THIS IS A MACHINE GENERATED FILE!
// BE CAREFUL WITH EDITING BY HAND.
//
// This is Free Software covered by the terms of the MIT license.
// See LICENSE file for details.
// (c) 2016 by Sascha L. Teichmann.
// See the full list of contributors in the CONTRIBUTORS file.

package genmap
`

var mapTmplTxt = `
{{ $F := .From | title -}}
{{- $T := .To | title -}}
{{- $TYPE :=  printf "Map%sTo%s" $F $T -}}
{{- $ENTRY :=  printf "entry%sTo%s" $F $T -}}

type {{ $ENTRY }} struct {
	k    {{.From }}
	v    {{ .To }}
	next *{{ $ENTRY }}
}

// {{ $TYPE }} implements a hash map from {{ .From }} to {{ .To }}.
type {{ $TYPE }} struct {
	mask     int
	slots    []*{{ $ENTRY }}
	used     int
	size     int
	max      int
	freelist *{{ $ENTRY }}
}

// New{{ $TYPE }} creates a new {{ $TYPE }} with at least a size of size.
func New{{ $TYPE }}(size int) *{{ $TYPE }} {
	shift := nextShiftPowerOfTwo(size)
	return &{{ $TYPE }}{
		mask:  ^(^0 << shift),
		max:   maxFill(1 << shift),
		slots: make([]*{{ $ENTRY }}, 1<<shift),
	}
}

// Size returns the current size of the map.
func (h *{{ $TYPE }}) Size() int {
	return h.size
}

// Get looks up a key k and returns its value. 0 if not found.
func (h *{{ $TYPE }}) Get(k {{ .From }}) {{ .To }} {
	for e := h.slots[int(k)&h.mask]; e != nil; e = e.next {
		if e.k == k {
			return e.v
		}
	}
	return 0
}

// Contains looks up a key k and returns true if it is found else false.
func (h *{{ $TYPE }}) Contains(k {{ .From }}) bool {
	for e := h.slots[int(k)&h.mask]; e != nil; e = e.next {
		if e.k == k {
			return true
		}
	}
	return false
}

// Modify looks up a key k and calls function fn with a pointer to its value.
func (h *{{ $TYPE }}) Modify(k {{ .From }}, fn func(v *{{ .To }})) {
	for e := h.slots[int(k)&h.mask]; e != nil; e = e.next {
		if e.k == k {
			fn(&e.v)
			return
		}
	}
}

// Find looks up a key k and returns its value and true.
// 0 and false if not found.
func (h *{{ $TYPE }}) Find(k {{ .From }}) ({{ .To }}, bool) {
	for e := h.slots[int(k)&h.mask]; e != nil; e = e.next {
		if e.k == k {
			return e.v, true
		}
	}
	return 0, false
}

// Visit calls a given function fn for every key/value pair in the map.
func (h *{{ $TYPE }}) Visit(fn func({{ .From }}, {{ .To }})) {
	for _, e := range h.slots {
		for ; e != nil; e = e.next {
			fn(e.k, e.v)
		}
	}
}

// Add increments the value associated with key k by the value of v.
// It creates a new key/value pair k/v in the map if the key does not exist.
func (h *{{ $TYPE }}) Add(k {{ .From }}, v {{ .To }}) {
	p := &h.slots[int(k)&h.mask]
	for e := *p; e != nil; e = e.next {
		if e.k == k {
			e.v += v
			return
		}
	}
	n := h.alloc(k, v)
	if *p != nil {
		n.next = *p
		*p = n
	} else {
		*p = n
		h.used++
		if h.used > h.max {
			h.rehash()
		}
	}
}

// Put sets the value associated with key k to the value of v.
// It creates a new key/value pair k/v in the map if the key does not exist.
func (h *{{ $TYPE }}) Put(k {{ .From }}, v {{ .To }}) {
	p := &h.slots[int(k)&h.mask]
	for e := *p; e != nil; e = e.next {
		if e.k == k {
			e.v = v
			return
		}
	}
	n := h.alloc(k, v)
	if *p != nil {
		n.next = *p
		*p = n
	} else {
		*p = n
		h.used++
		if h.used > h.max {
			h.rehash()
		}
	}
}

// Remove removes the key/value pair associated with key k from this map..
func (h *{{ $TYPE }}) Remove(k {{ .From }}) {
	p := &h.slots[int(k)&h.mask]
	var parent *{{ $ENTRY }}
	for e := *p; e != nil; e = e.next {
		if e.k == k {
			if parent == nil { // head
				if e.next == nil { // last in chain
					h.used--
				}
				*p = e.next
			} else {
				parent.next = e.next
			}
			h.free(e)
			return
		}
		parent = e
	}
}

// Inc increments a value associated with key k by one.
// A new entry is created with value 1 if the key
// does not exist.
func (h *{{ $TYPE }}) Inc(k {{ .From }}) {
	p := &h.slots[int(k)&h.mask]
	for e := *p; e != nil; e = e.next {
		if e.k == k {
			e.v++
			return
		}
	}
	n := h.alloc(k, 1)
	if *p != nil {
		n.next = *p
		*p = n
	} else {
		*p = n
		h.used++
		if h.used > h.max {
			h.rehash()
		}
	}
}

func (h *{{ $TYPE }}) rehash() {
	ns := len(h.slots) << 1
	nslots := make([]*{{ $ENTRY }}, ns)
	nmask := (h.mask << 1) | 1
	h.mask = nmask
	nu := 0
	for i, e := range h.slots {
		if e == nil {
			continue
		}
		for e != nil {
			n := e.next
			p := &nslots[int(e.k)&nmask]
			if *p == nil {
				nu++
			}
			e.next = *p
			*p = e
			e = n
		}
		h.slots[i] = nil
	}
	h.used = nu
	h.slots = nslots
	h.max = maxFill(ns)
}

func (h *{{ $TYPE }}) free(entry *{{ $ENTRY }}) {
	entry.next = h.freelist
	h.freelist = entry
	h.size--
}

func (h *{{ $TYPE }}) alloc(k {{ .From }}, v {{ .To }}) *{{ $ENTRY }} {
	if h.freelist == nil {
		entries := make([]{{ $ENTRY }}, 256)
		for i := 0; i < 256-1; i++ {
			entries[i].next = &entries[i+1]
		}
		h.freelist = &entries[0]
	}
	h.size++
	x := h.freelist
	h.freelist = x.next
	x.next = nil
	x.k = k
	x.v = v
	return x
}
`

var funcMap = template.FuncMap{
	"title": strings.Title,
}

var (
	headerTmpl = template.Must(template.New("header").Funcs(funcMap).Parse(headerTmplTxt))
	mapTmpl    = template.Must(template.New("maps").Funcs(funcMap).Parse(mapTmplTxt))
)

func main() {
	for _, from := range types {
		filename := fmt.Sprintf("map_%s.go", from)
		log.Printf("Generating %s...\n", filename)

		f, err := os.Create(filename)
		if err != nil {
			log.Printf("creating %s failed: %v\n", filename, err)
			continue
		}
		out := bufio.NewWriter(f)

		if err = headerTmpl.Execute(out, nil); err != nil {
			log.Printf("templating header %s failed: %v\n", filename, err)
			f.Close()
			continue
		}

		var parameters = struct {
			From string
			To   string
		}{
			From: from,
		}

		for _, parameters.To = range types {
			if err = mapTmpl.Execute(out, &parameters); err != nil {
				log.Printf("templating %s failed: %v\n", filename, err)
				break
			}
		}

		if err = out.Flush(); err != nil {
			log.Printf("flushing %s failed: %v\n", filename, err)
		}
		if err = f.Close(); err != nil {
			log.Printf("closing %s failed: %v\n", filename, err)
		}
	}
}
<|endoftext|>


<commit_before>package main

import (
	"github.com/AutoRoute/node"
	"github.com/AutoRoute/tuntap"

	"flag"
	"fmt"
	"log"
	"net"
	"runtime"
	"strconv"
	"strings"
)

var listen = flag.String("listen", "[::1]:34321",
	"The address to listen to incoming connections on")
var nolisten = flag.Bool("nolisten", false, "Disables listening")
var connect = flag.String("connect", "",
	"Comma separated list of addresses to connect to")
var autodiscover = flag.Bool("auto", false,
	"Whether we should try and find neighboring routers")
var dev_names = flag.String("devs", "",
	"Comma separated list of interfaces to discover on")
var tcptun = flag.String("tcptun", "",
	"Address to try and tcp tunnel to")
var keyfile = flag.String("keyfile", "",
	"The keyfile we should check for a key and write our current key to")
var btc_host = flag.String("btc_host", "localhost:8333",
	"The bitcoin daemon we should connect to")
var btc_user = flag.String("btc_user", "user",
	"The bitcoin daemon username")
var btc_pass = flag.String("btc_pass", "password",
	"The bitcoin daemon password")
var fake_money = flag.Bool("fake_money", false, "Enables a money system which is purely fake")

func Probe(key node.PrivateKey, n *node.Server, dev net.Interface, port uint16) {
	if dev.Name == "lo" {
		return
	}

	log.Printf("Probing %q", dev.Name)

	ll_addr, err := node.GetLinkLocalAddr(dev)
	if err != nil {
		log.Printf("Error probing %q: %v", dev.Name, err)
		return
	}

	neighbors := node.FindNeighbors(dev, ll_addr, key.PublicKey(), port)
	for neighbor := range neighbors {
		log.Printf("Neighbour Found %x", neighbor.NodeAddr)
		err := n.Connect(fmt.Sprintf("[%s%%%s]:%v", neighbor.LLAddrStr, dev.Name, neighbor.Port))
		if err != nil {
			log.Printf("Error connecting: %v", err)
			return
		}
		log.Printf("Connection established to %x", neighbor.NodeAddr)
	}
}

func main() {

	runtime.GOMAXPROCS(runtime.NumCPU())

	log.Print("Starting")

	flag.Parse()
	quit := make(chan bool)

	var key node.PrivateKey
	var err error

	if len(*keyfile) > 0 {
		key, err = node.LoadKey(*keyfile)
	} else {
		log.Print("Generating key")
		key, err = node.NewECDSAKey()
		if err != nil {
			log.Fatal(err)
		}
	}
	log.Printf("Key is %x", key.PublicKey().Hash())

	money := node.FakeMoney()
	if !*fake_money {
		log.Printf("Connecting to bitcoin daemon")
		rpc, err := node.NewRPCMoney(*btc_host, *btc_user, *btc_pass)
		if err != nil {
			log.Printf("Error connection to bitcoin daemon")
		}
		money = rpc
	}
	log.Printf("Connected")
	n := node.NewServer(key, money)

	if *autodiscover {
		devs := make([]net.Interface, 0)

		if *dev_names == "" {
			devs, err = net.Interfaces()
			if err != nil {
				log.Fatal(err)
			}
		} else {
			for _, dev_name := range strings.Split(*dev_names, ",") {
				dev, err := net.InterfaceByName(dev_name)
				if err != nil {
					log.Fatal(err)
				}
				devs = append(devs, *dev)
			}
		}

		parsed_listen_addr := strings.Split(*listen, ":")
		port64, err := strconv.ParseUint(parsed_listen_addr[len(parsed_listen_addr)-1], 10, 16)
		port := uint16(port64)
		if err != nil {
			log.Fatal(err)
		}
		port = uint16(port)
		for _, dev := range devs {
			go Probe(key, n, dev, port)
		}
	}

	if !*nolisten {
		log.Print("Starting to listen")
		err := n.Listen(*listen)
		if err != nil {
			log.Printf("Error listening: %v", err)
		}
	}

	for _, ip := range strings.Split(*connect, ",") {
		if len(ip) == 0 {
			continue
		}
		err := n.Connect(ip)
		if err != nil {
			log.Printf("Error connecting to %s: %v", ip, err)
		}
	}

	if len(*tcptun) > 0 {
		log.Printf("Establishing tcp tunnel to %v", *tcptun)
		i, err := tuntap.Open("tun%d", tuntap.DevTun)
		if err != nil {
			log.Fatal(err)
		}
		dest := ""
		_, err = fmt.Sscanf(*tcptun, "%x", &dest)
		if err != nil {
			log.Fatal(err)
		}
		t := node.NewTCPTunnel(i, n.Node(), node.NodeAddress(dest), 1)
		t = t
		<-quit
	}

	<-quit
}
<commit_msg>Don't restrict ourselves to localhost<commit_after>package main

import (
	"github.com/AutoRoute/node"
	"github.com/AutoRoute/tuntap"

	"flag"
	"fmt"
	"log"
	"net"
	"runtime"
	"strconv"
	"strings"
)

var listen = flag.String("listen", "[::]:34321",
	"The address to listen to incoming connections on")
var nolisten = flag.Bool("nolisten", false, "Disables listening")
var connect = flag.String("connect", "",
	"Comma separated list of addresses to connect to")
var autodiscover = flag.Bool("auto", false,
	"Whether we should try and find neighboring routers")
var dev_names = flag.String("devs", "",
	"Comma separated list of interfaces to discover on")
var tcptun = flag.String("tcptun", "",
	"Address to try and tcp tunnel to")
var keyfile = flag.String("keyfile", "",
	"The keyfile we should check for a key and write our current key to")
var btc_host = flag.String("btc_host", "localhost:8333",
	"The bitcoin daemon we should connect to")
var btc_user = flag.String("btc_user", "user",
	"The bitcoin daemon username")
var btc_pass = flag.String("btc_pass", "password",
	"The bitcoin daemon password")
var fake_money = flag.Bool("fake_money", false, "Enables a money system which is purely fake")

func Probe(key node.PrivateKey, n *node.Server, dev net.Interface, port uint16) {
	if dev.Name == "lo" {
		return
	}

	log.Printf("Probing %q", dev.Name)

	ll_addr, err := node.GetLinkLocalAddr(dev)
	if err != nil {
		log.Printf("Error probing %q: %v", dev.Name, err)
		return
	}

	neighbors := node.FindNeighbors(dev, ll_addr, key.PublicKey(), port)
	for neighbor := range neighbors {
		log.Printf("Neighbour Found %x", neighbor.NodeAddr)
		err := n.Connect(fmt.Sprintf("[%s%%%s]:%v", neighbor.LLAddrStr, dev.Name, neighbor.Port))
		if err != nil {
			log.Printf("Error connecting: %v", err)
			return
		}
		log.Printf("Connection established to %x", neighbor.NodeAddr)
	}
}

func main() {

	runtime.GOMAXPROCS(runtime.NumCPU())

	log.Print("Starting")

	flag.Parse()
	quit := make(chan bool)

	var key node.PrivateKey
	var err error

	if len(*keyfile) > 0 {
		key, err = node.LoadKey(*keyfile)
	} else {
		log.Print("Generating key")
		key, err = node.NewECDSAKey()
		if err != nil {
			log.Fatal(err)
		}
	}
	log.Printf("Key is %x", key.PublicKey().Hash())

	money := node.FakeMoney()
	if !*fake_money {
		log.Printf("Connecting to bitcoin daemon")
		rpc, err := node.NewRPCMoney(*btc_host, *btc_user, *btc_pass)
		if err != nil {
			log.Printf("Error connection to bitcoin daemon")
		}
		money = rpc
	}
	log.Printf("Connected")
	n := node.NewServer(key, money)

	if *autodiscover {
		devs := make([]net.Interface, 0)

		if *dev_names == "" {
			devs, err = net.Interfaces()
			if err != nil {
				log.Fatal(err)
			}
		} else {
			for _, dev_name := range strings.Split(*dev_names, ",") {
				dev, err := net.InterfaceByName(dev_name)
				if err != nil {
					log.Fatal(err)
				}
				devs = append(devs, *dev)
			}
		}

		parsed_listen_addr := strings.Split(*listen, ":")
		port64, err := strconv.ParseUint(parsed_listen_addr[len(parsed_listen_addr)-1], 10, 16)
		port := uint16(port64)
		if err != nil {
			log.Fatal(err)
		}
		port = uint16(port)
		for _, dev := range devs {
			go Probe(key, n, dev, port)
		}
	}

	if !*nolisten {
		log.Print("Starting to listen")
		err := n.Listen(*listen)
		if err != nil {
			log.Printf("Error listening: %v", err)
		}
	}

	for _, ip := range strings.Split(*connect, ",") {
		if len(ip) == 0 {
			continue
		}
		err := n.Connect(ip)
		if err != nil {
			log.Printf("Error connecting to %s: %v", ip, err)
		}
	}

	if len(*tcptun) > 0 {
		log.Printf("Establishing tcp tunnel to %v", *tcptun)
		i, err := tuntap.Open("tun%d", tuntap.DevTun)
		if err != nil {
			log.Fatal(err)
		}
		dest := ""
		_, err = fmt.Sscanf(*tcptun, "%x", &dest)
		if err != nil {
			log.Fatal(err)
		}
		t := node.NewTCPTunnel(i, n.Node(), node.NodeAddress(dest), 1)
		t = t
		<-quit
	}

	<-quit
}
<|endoftext|>


<commit_before>package raft

import (
	"encoding/binary"
	"errors"

	"github.com/boltdb/bolt"
	pb "github.com/relab/raft/raftpb"
)

var (
	stateBucket = []byte("state")
	logBucket   = []byte("log")
)

var ErrKeyNotFound = errors.New("key not found")

type FileStorage struct {
	*bolt.DB

	nextIndex uint64
}

func NewFileStorage(path string, overwrite bool) (*FileStorage, error) {
	db, err := bolt.Open(path, 0600, nil)

	if err != nil {
		return nil, err
	}

	tx, err := db.Begin(true)

	if err != nil {
		return nil, err
	}

	defer tx.Rollback()

	if overwrite {
		tx.DeleteBucket(stateBucket)
		tx.DeleteBucket(logBucket)
	}

	if _, err := tx.CreateBucketIfNotExists(stateBucket); err != nil {
		return nil, err
	}

	if _, err := tx.CreateBucketIfNotExists(logBucket); err != nil {
		return nil, err
	}

	nextIndex := get(tx.Bucket(stateBucket), KeyLogLength)

	if err := tx.Commit(); err != nil {
		return nil, err
	}

	return &FileStorage{
		DB:        db,
		nextIndex: nextIndex,
	}, nil
}

func (fs *FileStorage) Set(key uint64, value uint64) error {
	tx, err := fs.Begin(true)

	if err != nil {
		return err
	}

	defer tx.Rollback()

	if err := set(tx.Bucket(stateBucket), key, value); err != nil {
		return err
	}

	return tx.Commit()
}

func set(bucket *bolt.Bucket, key uint64, value uint64) error {
	k := make([]byte, 8)
	v := make([]byte, 8)
	binary.BigEndian.PutUint64(k, key)
	binary.BigEndian.PutUint64(v, value)

	return bucket.Put(k, v)
}

func (fs *FileStorage) Get(key uint64) (uint64, error) {
	tx, err := fs.Begin(false)

	if err != nil {
		return 0, err
	}

	defer tx.Rollback()

	return get(tx.Bucket(stateBucket), key), nil
}

func get(bucket *bolt.Bucket, key uint64) uint64 {
	k := make([]byte, 8)
	binary.BigEndian.PutUint64(k, key)

	if val := bucket.Get(k); val != nil {
		return binary.BigEndian.Uint64(val)
	}

	// Default to 0. This lets us get KeyTerm and KeyVotedFor without having
	// to set them first.
	return 0
}

func (fs *FileStorage) StoreEntries(entries []*pb.Entry) error {
	tx, err := fs.Begin(true)

	if err != nil {
		return err
	}

	defer tx.Rollback()

	k := make([]byte, 8)
	bucket := tx.Bucket(logBucket)

	for _, entry := range entries {
		binary.BigEndian.PutUint64(k, fs.nextIndex)

		val, err := entry.Marshal()

		if err != nil {
			return err
		}

		if err := bucket.Put(k, val); err != nil {
			return err
		}

		fs.nextIndex++
	}

	if err := set(tx.Bucket(stateBucket), KeyLogLength, fs.nextIndex); err != nil {
		return err
	}

	return tx.Commit()
}

func (fs *FileStorage) GetEntry(index uint64) (*pb.Entry, error) {
	tx, err := fs.Begin(false)

	if err != nil {
		return nil, err
	}

	defer tx.Rollback()

	bucket := tx.Bucket(logBucket)

	k := make([]byte, 8)
	binary.BigEndian.PutUint64(k, index)

	if val := bucket.Get(k); val != nil {
		var entry pb.Entry
		err := entry.Unmarshal(val)

		if err != nil {
			return nil, err
		}

		return &entry, nil
	}

	return nil, ErrKeyNotFound
}

func (fs *FileStorage) GetEntries(from, to uint64) ([]*pb.Entry, error) {
	tx, err := fs.Begin(false)

	if err != nil {
		return nil, err
	}

	defer tx.Rollback()

	bucket := tx.Bucket(logBucket)

	entries := make([]*pb.Entry, to-from)
	k := make([]byte, 8)

	for i := from; i < to; i++ {
		binary.BigEndian.PutUint64(k, i)

		if val := bucket.Get(k); val != nil {
			var entry pb.Entry
			err := entry.Unmarshal(val)

			if err != nil {
				return nil, err
			}

			entries[i-from] = &entry
		}
	}

	return entries, nil
}

func (fs *FileStorage) RemoveEntriesFrom(index uint64) error {
	tx, err := fs.Begin(true)

	if err != nil {
		return err
	}

	defer tx.Rollback()

	c := tx.Bucket(logBucket).Cursor()
	k := make([]byte, 8)
	binary.BigEndian.PutUint64(k, index)
	c.Seek(k)

	for i := index; i < fs.nextIndex; i++ {
		if err := c.Delete(); err != nil {
			return err
		}

		c.Next()
	}

	fs.nextIndex = index

	if err := set(tx.Bucket(stateBucket), KeyLogLength, fs.nextIndex); err != nil {
		return err
	}

	return tx.Commit()
}

func (fs *FileStorage) NumEntries() uint64 {
	return fs.nextIndex
}
<commit_msg>filestorage.go: Add documentation<commit_after>package raft

import (
	"encoding/binary"
	"errors"

	"github.com/boltdb/bolt"
	pb "github.com/relab/raft/raftpb"
)

var (
	stateBucket = []byte("state")
	logBucket   = []byte("log")
)

// ErrKeyNotFound that the given key could not be found in the storage.
var ErrKeyNotFound = errors.New("key not found")

// FileStorage is an implementation of the Storage interface for file based
// storage.
type FileStorage struct {
	*bolt.DB

	nextIndex uint64
}

// NewFileStorage returns a new FileStorage using the file given with the path
// argument. Overwrite decides whether to use the database if it already exists
// or overwrite it.
func NewFileStorage(path string, overwrite bool) (*FileStorage, error) {
	db, err := bolt.Open(path, 0600, nil)

	if err != nil {
		return nil, err
	}

	tx, err := db.Begin(true)

	if err != nil {
		return nil, err
	}

	defer tx.Rollback()

	if overwrite {
		if err := tx.DeleteBucket(stateBucket); err != nil {
			return nil, err
		}

		if err := tx.DeleteBucket(logBucket); err != nil {
			return nil, err
		}
	}

	if _, err := tx.CreateBucketIfNotExists(stateBucket); err != nil {
		return nil, err
	}

	if _, err := tx.CreateBucketIfNotExists(logBucket); err != nil {
		return nil, err
	}

	nextIndex := get(tx.Bucket(stateBucket), KeyLogLength)

	if err := tx.Commit(); err != nil {
		return nil, err
	}

	return &FileStorage{
		DB:        db,
		nextIndex: nextIndex,
	}, nil
}

// Set implements the Storage interface.
func (fs *FileStorage) Set(key uint64, value uint64) error {
	tx, err := fs.Begin(true)

	if err != nil {
		return err
	}

	defer tx.Rollback()

	if err := set(tx.Bucket(stateBucket), key, value); err != nil {
		return err
	}

	return tx.Commit()
}

func set(bucket *bolt.Bucket, key uint64, value uint64) error {
	k := make([]byte, 8)
	v := make([]byte, 8)
	binary.BigEndian.PutUint64(k, key)
	binary.BigEndian.PutUint64(v, value)

	return bucket.Put(k, v)
}

// Get implements the Storage interface.
func (fs *FileStorage) Get(key uint64) (uint64, error) {
	tx, err := fs.Begin(false)

	if err != nil {
		return 0, err
	}

	defer tx.Rollback()

	return get(tx.Bucket(stateBucket), key), nil
}

func get(bucket *bolt.Bucket, key uint64) uint64 {
	k := make([]byte, 8)
	binary.BigEndian.PutUint64(k, key)

	if val := bucket.Get(k); val != nil {
		return binary.BigEndian.Uint64(val)
	}

	// Default to 0. This lets us get KeyTerm and KeyVotedFor without having
	// to set them first.
	return 0
}

// StoreEntries implements the Storage interface.
func (fs *FileStorage) StoreEntries(entries []*pb.Entry) error {
	tx, err := fs.Begin(true)

	if err != nil {
		return err
	}

	defer tx.Rollback()

	k := make([]byte, 8)
	bucket := tx.Bucket(logBucket)

	for _, entry := range entries {
		binary.BigEndian.PutUint64(k, fs.nextIndex)

		val, err := entry.Marshal()

		if err != nil {
			return err
		}

		if err := bucket.Put(k, val); err != nil {
			return err
		}

		fs.nextIndex++
	}

	if err := set(tx.Bucket(stateBucket), KeyLogLength, fs.nextIndex); err != nil {
		return err
	}

	return tx.Commit()
}

// GetEntry implements the Storage interface.
func (fs *FileStorage) GetEntry(index uint64) (*pb.Entry, error) {
	tx, err := fs.Begin(false)

	if err != nil {
		return nil, err
	}

	defer tx.Rollback()

	bucket := tx.Bucket(logBucket)

	k := make([]byte, 8)
	binary.BigEndian.PutUint64(k, index)

	if val := bucket.Get(k); val != nil {
		var entry pb.Entry
		err := entry.Unmarshal(val)

		if err != nil {
			return nil, err
		}

		return &entry, nil
	}

	return nil, ErrKeyNotFound
}

// GetEntries implements the Storage interface.
func (fs *FileStorage) GetEntries(from, to uint64) ([]*pb.Entry, error) {
	tx, err := fs.Begin(false)

	if err != nil {
		return nil, err
	}

	defer tx.Rollback()

	bucket := tx.Bucket(logBucket)

	entries := make([]*pb.Entry, to-from)
	k := make([]byte, 8)

	for i := from; i < to; i++ {
		binary.BigEndian.PutUint64(k, i)

		if val := bucket.Get(k); val != nil {
			var entry pb.Entry
			err := entry.Unmarshal(val)

			if err != nil {
				return nil, err
			}

			entries[i-from] = &entry
		}
	}

	return entries, nil
}

// RemoveEntriesFrom implements the Storage interface.
func (fs *FileStorage) RemoveEntriesFrom(index uint64) error {
	tx, err := fs.Begin(true)

	if err != nil {
		return err
	}

	defer tx.Rollback()

	c := tx.Bucket(logBucket).Cursor()
	k := make([]byte, 8)
	binary.BigEndian.PutUint64(k, index)
	c.Seek(k)

	for i := index; i < fs.nextIndex; i++ {
		if err := c.Delete(); err != nil {
			return err
		}

		c.Next()
	}

	fs.nextIndex = index

	if err := set(tx.Bucket(stateBucket), KeyLogLength, fs.nextIndex); err != nil {
		return err
	}

	return tx.Commit()
}

// NumEntries implements the Storage interface.
func (fs *FileStorage) NumEntries() uint64 {
	return fs.nextIndex
}
<|endoftext|>


<commit_before>// Copyright 2016 Stratumn SAS. All rights reserved.
// Use of this source code is governed by an Apache License 2.0
// LICENSE file.

// Package btctimestamper implements a fake Bitcoin timestamper which can be
// used for testing.
package btctimestamper

import (
	"bytes"
	"errors"
	"io/ioutil"

	"github.com/btcsuite/btcd/btcec"
	"github.com/btcsuite/btcd/chaincfg"
	"github.com/btcsuite/btcd/chaincfg/chainhash"
	"github.com/btcsuite/btcd/txscript"
	"github.com/btcsuite/btcd/wire"
	"github.com/btcsuite/btcutil"
	"github.com/stratumn/goprivate/blockchain"
	"github.com/stratumn/goprivate/blockchain/btc"
	"github.com/stratumn/sdk/types"
)

const (
	// DefaultFee is the default transaction fee.
	DefaultFee = int64(15000)
)

// Config contains configuration options for the timestamper.
type Config struct {
	// An unspent transaction finder.
	UnspentFinder btc.UnspentFinder

	// A transaction broadcaster.
	Broadcaster btc.Broadcaster

	// A wallet import format key.
	WIF string

	// Transaction fee
	Fee int64
}

// Timestamper is the type that implements
// github.com/stratumn/goprivate/blockchain.Timestamper.
type Timestamper struct {
	config    *Config
	net       btc.Network
	netParams *chaincfg.Params
	privKey   *btcec.PrivateKey
	pubKey    *btcec.PublicKey
	address   *btcutil.AddressPubKeyHash
}

// New creates an instance of a Timestamper.
func New(config *Config) (*Timestamper, error) {
	WIF, err := btcutil.DecodeWIF(config.WIF)
	if err != nil {
		return nil, err
	}

	ts := &Timestamper{
		config:  config,
		privKey: WIF.PrivKey,
		pubKey:  WIF.PrivKey.PubKey(),
	}

	if WIF.IsForNet(&chaincfg.TestNet3Params) {
		ts.net = btc.NetworkTest3
		ts.netParams = &chaincfg.TestNet3Params
	} else if WIF.IsForNet(&chaincfg.MainNetParams) {
		ts.net = btc.NetworkMain
		ts.netParams = &chaincfg.MainNetParams
	}

	if ts.netParams == nil {
		return nil, errors.New("unsupported network")
	}

	pubKeyHash := btcutil.Hash160(ts.pubKey.SerializeUncompressed())
	ts.address, err = btcutil.NewAddressPubKeyHash(pubKeyHash, ts.netParams)
	if err != nil {
		return nil, err
	}

	return ts, nil
}

// Network implements fmt.Stringer.
func (ts *Timestamper) Network() blockchain.Network {
	return ts.net
}

// TimestampHash implements
// github.com/stratumn/goprivate/blockchain.HashTimestamper.
func (ts *Timestamper) TimestampHash(hash *types.Bytes32) (types.TransactionID, error) {
	var prevPKScripts [][]byte

	addr := (*types.ReversedBytes20)(ts.address.Hash160())
	outputs, total, err := ts.config.UnspentFinder.FindUnspent(addr, ts.config.Fee)
	if err != nil {
		return nil, err
	}

	tx := wire.NewMsgTx(wire.TxVersion)
	for _, output := range outputs {
		prevPKScripts = append(prevPKScripts, output.PKScript)
		out := wire.NewOutPoint((*chainhash.Hash)(&output.TXHash), uint32(output.Index))
		tx.AddTxIn(wire.NewTxIn(out, nil))
	}

	payToAddrOut, err := ts.createPayToAddrTxOut(total - ts.config.Fee)
	if err != nil {
		return nil, err
	}
	tx.AddTxOut(payToAddrOut)

	nullDataOut, err := ts.createNullDataTxOut(hash)
	if err != nil {
		return nil, err
	}
	tx.AddTxOut(nullDataOut)

	if err = ts.signTx(tx, prevPKScripts); err != nil {
		return nil, err
	}
	if err = ts.validateTx(tx, prevPKScripts); err != nil {
		return nil, err
	}

	buf := bytes.NewBuffer(nil)
	if err = tx.Serialize(buf); err != nil {
		return nil, err
	}
	raw, err := ioutil.ReadAll(buf)
	if err != nil {
		return nil, err
	}
	err = ts.config.Broadcaster.Broadcast(raw)
	if err != nil {
		return nil, err
	}

	// Reverse the bytes!
	var txHash32 types.Bytes32
	for i, b := range tx.TxHash() {
		txHash32[types.Bytes32Size-i-1] = b
	}

	return txHash32[:], nil
}

func (ts *Timestamper) createPayToAddrTxOut(amount int64) (*wire.TxOut, error) {
	PKScript, err := txscript.PayToAddrScript(ts.address)
	if err != nil {
		return nil, err
	}

	return wire.NewTxOut(amount, PKScript), nil
}

func (ts *Timestamper) createNullDataTxOut(hash *types.Bytes32) (*wire.TxOut, error) {
	PKScript, err := txscript.NewScriptBuilder().AddOp(txscript.OP_RETURN).AddData(hash[:]).Script()
	if err != nil {
		return nil, err
	}

	return wire.NewTxOut(0, PKScript), nil
}

func (ts *Timestamper) signTx(tx *wire.MsgTx, prevPKScripts [][]byte) error {
	for index, PKScript := range prevPKScripts {
		sig, err := txscript.SignTxOutput(ts.netParams, tx, 0, PKScript,
			txscript.SigHashAll, txscript.KeyClosure(ts.lookupKey), nil, nil)
		if err != nil {
			return err
		}

		tx.TxIn[index].SignatureScript = sig
	}

	return nil
}

const validateTxEngineFlags = txscript.ScriptBip16 | txscript.ScriptVerifyDERSignatures |
	txscript.ScriptStrictMultiSig | txscript.ScriptDiscourageUpgradableNops

func (ts *Timestamper) validateTx(tx *wire.MsgTx, prevPKScripts [][]byte) error {
	for _, PKScript := range prevPKScripts {
		vm, err := txscript.NewEngine(PKScript, tx, 0, validateTxEngineFlags, nil)
		if err != nil {
			return err
		}
		if err := vm.Execute(); err != nil {
			return err
		}
	}

	return nil
}

func (ts *Timestamper) lookupKey(btcutil.Address) (*btcec.PrivateKey, bool, error) {
	// Second value means uncompressed.
	return ts.privKey, false, nil
}
<commit_msg>blockchain: bufferedbatch support<commit_after>// Copyright 2016 Stratumn SAS. All rights reserved.
// Use of this source code is governed by an Apache License 2.0
// LICENSE file.

// Package btctimestamper implements a fake Bitcoin timestamper which can be
// used for testing.
package btctimestamper

import (
	"bytes"
	"errors"
	"io/ioutil"

	"github.com/btcsuite/btcd/btcec"
	"github.com/btcsuite/btcd/chaincfg"
	"github.com/btcsuite/btcd/chaincfg/chainhash"
	"github.com/btcsuite/btcd/txscript"
	"github.com/btcsuite/btcd/wire"
	"github.com/btcsuite/btcutil"
	"github.com/stratumn/goprivate/blockchain"
	"github.com/stratumn/goprivate/blockchain/btc"
	"github.com/stratumn/sdk/types"
)

const (
	// DefaultFee is the default transaction fee.
	DefaultFee = int64(15000)
)

// Config contains configuration options for the timestamper.
type Config struct {
	// An unspent transaction finder.
	UnspentFinder btc.UnspentFinder

	// A transaction broadcaster.
	Broadcaster btc.Broadcaster

	// A wallet import format key.
	WIF string

	// Transaction fee
	Fee int64
}

// Timestamper is the type that implements
// github.com/stratumn/goprivate/blockchain.Timestamper.
type Timestamper struct {
	config    *Config
	net       btc.Network
	netParams *chaincfg.Params
	privKey   *btcec.PrivateKey
	pubKey    *btcec.PublicKey
	address   *btcutil.AddressPubKeyHash
}

// New creates an instance of a Timestamper.
func New(config *Config) (*Timestamper, error) {
	WIF, err := btcutil.DecodeWIF(config.WIF)
	if err != nil {
		return nil, err
	}

	ts := &Timestamper{
		config:  config,
		privKey: WIF.PrivKey,
		pubKey:  WIF.PrivKey.PubKey(),
	}

	if WIF.IsForNet(&chaincfg.TestNet3Params) {
		ts.net = btc.NetworkTest3
		ts.netParams = &chaincfg.TestNet3Params
	} else if WIF.IsForNet(&chaincfg.MainNetParams) {
		ts.net = btc.NetworkMain
		ts.netParams = &chaincfg.MainNetParams
	}

	if ts.netParams == nil {
		return nil, errors.New("unsupported network")
	}

	pubKeyHash := btcutil.Hash160(ts.pubKey.SerializeUncompressed())
	ts.address, err = btcutil.NewAddressPubKeyHash(pubKeyHash, ts.netParams)
	if err != nil {
		return nil, err
	}

	return ts, nil
}

// Network implements fmt.Stringer.
func (ts *Timestamper) Network() blockchain.Network {
	return ts.net
}

// TimestampHash implements
// github.com/stratumn/goprivate/blockchain.HashTimestamper.
func (ts *Timestamper) TimestampHash(hash *types.Bytes32) (types.TransactionID, error) {
	var prevPKScripts [][]byte

	addr := (*types.ReversedBytes20)(ts.address.Hash160())
	outputs, total, err := ts.config.UnspentFinder.FindUnspent(addr, ts.config.Fee)
	if err != nil {
		return nil, err
	}

	tx := wire.NewMsgTx(wire.TxVersion)
	for _, output := range outputs {
		prevPKScripts = append(prevPKScripts, output.PKScript)
		out := wire.NewOutPoint((*chainhash.Hash)(&output.TXHash), uint32(output.Index))
		tx.AddTxIn(wire.NewTxIn(out, nil, nil))
	}

	payToAddrOut, err := ts.createPayToAddrTxOut(total - ts.config.Fee)
	if err != nil {
		return nil, err
	}
	tx.AddTxOut(payToAddrOut)

	nullDataOut, err := ts.createNullDataTxOut(hash)
	if err != nil {
		return nil, err
	}
	tx.AddTxOut(nullDataOut)

	if err = ts.signTx(tx, prevPKScripts); err != nil {
		return nil, err
	}
	if err = ts.validateTx(tx, prevPKScripts); err != nil {
		return nil, err
	}

	buf := bytes.NewBuffer(nil)
	if err = tx.Serialize(buf); err != nil {
		return nil, err
	}
	raw, err := ioutil.ReadAll(buf)
	if err != nil {
		return nil, err
	}
	err = ts.config.Broadcaster.Broadcast(raw)
	if err != nil {
		return nil, err
	}

	// Reverse the bytes!
	var txHash32 types.Bytes32
	for i, b := range tx.TxHash() {
		txHash32[types.Bytes32Size-i-1] = b
	}

	return txHash32[:], nil
}

func (ts *Timestamper) createPayToAddrTxOut(amount int64) (*wire.TxOut, error) {
	PKScript, err := txscript.PayToAddrScript(ts.address)
	if err != nil {
		return nil, err
	}

	return wire.NewTxOut(amount, PKScript), nil
}

func (ts *Timestamper) createNullDataTxOut(hash *types.Bytes32) (*wire.TxOut, error) {
	PKScript, err := txscript.NewScriptBuilder().AddOp(txscript.OP_RETURN).AddData(hash[:]).Script()
	if err != nil {
		return nil, err
	}

	return wire.NewTxOut(0, PKScript), nil
}

func (ts *Timestamper) signTx(tx *wire.MsgTx, prevPKScripts [][]byte) error {
	for index, PKScript := range prevPKScripts {
		sig, err := txscript.SignTxOutput(ts.netParams, tx, 0, PKScript,
			txscript.SigHashAll, txscript.KeyClosure(ts.lookupKey), nil, nil)
		if err != nil {
			return err
		}

		tx.TxIn[index].SignatureScript = sig
	}

	return nil
}

const validateTxEngineFlags = txscript.ScriptBip16 | txscript.ScriptVerifyDERSignatures |
	txscript.ScriptStrictMultiSig | txscript.ScriptDiscourageUpgradableNops

func (ts *Timestamper) validateTx(tx *wire.MsgTx, prevPKScripts [][]byte) error {
	for _, PKScript := range prevPKScripts {
		vm, err := txscript.NewEngine(PKScript, tx, 0, validateTxEngineFlags, nil, nil, 0)
		if err != nil {
			return err
		}
		if err := vm.Execute(); err != nil {
			return err
		}
	}

	return nil
}

func (ts *Timestamper) lookupKey(btcutil.Address) (*btcec.PrivateKey, bool, error) {
	// Second value means uncompressed.
	return ts.privKey, false, nil
}
<|endoftext|>


<commit_before>// UVa 10905 - Children's Game

package main

import (
	"fmt"
	"math/big"
	"os"
	"strings"
)

func solve(nums []string) string {
	var oldNum, newNum big.Int
here:
	for found := true; found; {
		found = false
		for i := 0; i < len(nums)-1; i++ {
			for j := 1; j < len(nums); j++ {
				oldNum.SetString(strings.Join(nums, ""), 10)
				nums[i], nums[j] = nums[j], nums[i]
				newNum.SetString(strings.Join(nums, ""), 10)
				if newNum.Cmp(&oldNum) > 0 {
					found = true
					continue here
				}
				nums[i], nums[j] = nums[j], nums[i]
			}
		}
	}
	return strings.Join(nums, "")
}

func main() {
	in, _ := os.Open("10905.in")
	defer in.Close()
	out, _ := os.Create("10905.out")
	defer out.Close()

	var n int
	for {
		if fmt.Fscanf(in, "%d", &n); n == 0 {
			break
		}
		nums := make([]string, n)
		for i := range nums {
			fmt.Fscanf(in, "%s", &nums[i])
		}
		fmt.Fprintln(out, solve(nums))
	}
}
<commit_msg>much faster solution<commit_after>// UVa 10905 - Children's Game

package main

import (
	"fmt"
	"math/big"
	"os"
	"strings"
)

func solve(nums []string) string {
	var oldNum, newNum big.Int
here:
	for found := true; found; {
		found = false
		for i := 0; i < len(nums)-1; i++ {
			oldNum.SetString(strings.Join(nums, ""), 10)
			nums[i], nums[i+1] = nums[i+1], nums[i]
			newNum.SetString(strings.Join(nums, ""), 10)
			if newNum.Cmp(&oldNum) > 0 {
				found = true
				continue here
			}
			nums[i], nums[i+1] = nums[i+1], nums[i]
		}
	}
	return strings.Join(nums, "")
}

func main() {
	in, _ := os.Open("10905.in")
	defer in.Close()
	out, _ := os.Create("10905.out")
	defer out.Close()

	var n int
	for {
		if fmt.Fscanf(in, "%d", &n); n == 0 {
			break
		}
		nums := make([]string, n)
		for i := range nums {
			fmt.Fscanf(in, "%s", &nums[i])
		}
		fmt.Fprintln(out, solve(nums))
	}
}
<|endoftext|>


<commit_before>// Concurrent computation of pi.
// See http://goo.gl/ZuTZM.
//
// This demonstrates Go's ability to handle
// large numbers of concurrent processes.
// It is an unreasonable way to calculate pi.
package main

import (
    "fmt"
    "math"
)

func main() {
    fmt.Println(pi(5000))
}

// pi launches n goroutines to compute an
// approximation of pi.
func pi(n int) float64 {
    ch := make(chan float64)
    for k := 0; k <= n; k++ {
        go term(ch, float64(k))
    }
    f := 0.0
    for k := 0; k <= n; k++ {
        f += <-ch
    }
    return f
}

func term(ch chan float64, k float64) {
    ch <- 4 * math.Pow(-1, k) / (2*k + 1)
}
<commit_msg>Update public/libs/bower_components/ace-builds/demo/kitchen-sink/docs/golang.go<commit_after><|endoftext|>