<commit_before>// package shell implements a remote API interface for a running ipfs daemon
package shell

import (
	"bufio"
	"encoding/json"
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	gohttp "net/http"
	"os"

	files "github.com/whyrusleeping/go-multipart-files"
	tar "github.com/whyrusleeping/tar-utils"
)

type Shell struct {
	url     string
	httpcli *gohttp.Client
}

func NewShell(url string) *Shell {
	return &Shell{
		url: url,
		httpcli: &gohttp.Client{
			Transport: &gohttp.Transport{
				DisableKeepAlives: true,
			},
		},
	}
}

func (s *Shell) newRequest(command string, args ...string) *Request {
	return NewRequest(s.url, command, args...)
}

type IdOutput struct {
	ID              string
	PublicKey       string
	Addresses       []string
	AgentVersion    string
	ProtocolVersion string
}

// ID gets information about a given peer.  Arguments:
//
// peer: peer.ID of the node to look up.  If no peer is specified,
//   return information about the local peer.
func (s *Shell) ID(peer ...string) (*IdOutput, error) {
	if len(peer) > 1 {
		return nil, fmt.Errorf("Too many peer arguments")
	}

	resp, err := NewRequest(s.url, "id", peer...).Send(s.httpcli)
	if err != nil {
		return nil, err
	}

	defer resp.Close()
	if resp.Error != nil {
		return nil, resp.Error
	}

	decoder := json.NewDecoder(resp.Output)
	out := new(IdOutput)
	err = decoder.Decode(out)
	if err != nil {
		return nil, err
	}

	return out, nil
}

// Cat the content at the given path. Callers need to drain and close the returned reader after usage.
func (s *Shell) Cat(path string) (io.ReadCloser, error) {
	resp, err := NewRequest(s.url, "cat", path).Send(s.httpcli)
	if err != nil {
		return nil, err
	}
	if resp.Error != nil {
		return nil, resp.Error
	}

	return resp.Output, nil
}

type object struct {
	Hash string
}

// Add a file to ipfs from the given reader, returns the hash of the added file
func (s *Shell) Add(r io.Reader) (string, error) {
	var rc io.ReadCloser
	if rclose, ok := r.(io.ReadCloser); ok {
		rc = rclose
	} else {
		rc = ioutil.NopCloser(r)
	}

	// handler expects an array of files
	fr := files.NewReaderFile("", "", rc, nil)
	slf := files.NewSliceFile("", "", []files.File{fr})
	fileReader := files.NewMultiFileReader(slf, true)

	req := NewRequest(s.url, "add")
	req.Body = fileReader

	resp, err := req.Send(s.httpcli)
	if err != nil {
		return "", err
	}
	defer resp.Close()
	if resp.Error != nil {
		return "", resp.Error
	}

	var out object
	err = json.NewDecoder(resp.Output).Decode(&out)
	if err != nil {
		return "", err
	}

	return out.Hash, nil
}

func (s *Shell) AddLink(target string) (string, error) {
	link := files.NewLinkFile("", "", target, nil)
	slf := files.NewSliceFile("", "", []files.File{link})
	reader := files.NewMultiFileReader(slf, true)

	req := s.newRequest("add")
	req.Body = reader

	resp, err := req.Send(s.httpcli)
	if err != nil {
		return "", err
	}
	defer resp.Close()
	if resp.Error != nil {
		return "", resp.Error
	}

	var out object
	err = json.NewDecoder(resp.Output).Decode(&out)
	if err != nil {
		return "", err
	}

	return out.Hash, nil
}

// AddDir adds a directory recursively with all of the files under it
func (s *Shell) AddDir(dir string) (string, error) {
	stat, err := os.Lstat(dir)
	if err != nil {
		return "", err
	}

	sf, err := files.NewSerialFile("", dir, stat)
	if err != nil {
		return "", err
	}
	slf := files.NewSliceFile("", dir, []files.File{sf})
	reader := files.NewMultiFileReader(slf, true)

	req := NewRequest(s.url, "add")
	req.Opts["r"] = "true"
	req.Body = reader

	resp, err := req.Send(s.httpcli)
	if err != nil {
		return "", err
	}
	defer resp.Close()
	if resp.Error != nil {
		return "", resp.Error
	}

	dec := json.NewDecoder(resp.Output)
	var final string
	for {
		var out object
		err = dec.Decode(&out)
		if err != nil {
			if err == io.EOF {
				break
			}
			return "", err
		}
		final = out.Hash
	}

	if final == "" {
		return "", errors.New("no results received")
	}

	return final, nil
}

const (
	TRaw = iota
	TDirectory
	TFile
	TMetadata
	TSymlink
)

// List entries at the given path
func (s *Shell) List(path string) ([]*LsLink, error) {
	resp, err := NewRequest(s.url, "ls", path).Send(s.httpcli)
	if err != nil {
		return nil, err
	}
	defer resp.Close()

	if resp.Error != nil {
		return nil, resp.Error
	}

	var out struct{ Objects []LsObject }
	err = json.NewDecoder(resp.Output).Decode(&out)
	if err != nil {
		return nil, err
	}

	return out.Objects[0].Links, nil
}

type LsLink struct {
	Hash string
	Name string
	Size uint64
	Type int
}

type LsObject struct {
	Links []*LsLink
	LsLink
}

// Pin the given path
func (s *Shell) Pin(path string) error {
	req := NewRequest(s.url, "pin/add", path)
	req.Opts["r"] = "true"

	resp, err := req.Send(s.httpcli)
	if err != nil {
		return err
	}
	defer resp.Close()
	if resp.Error != nil {
		return resp.Error
	}

	return nil
}

// Unpin the given path
func (s *Shell) Unpin(path string) error {
	req := NewRequest(s.url, "pin/rm", path)
	req.Opts["r"] = "true"

	resp, err := req.Send(s.httpcli)
	if err != nil {
		return err
	}
	defer resp.Close()
	if resp.Error != nil {
		return resp.Error
	}

	return nil
}

type PeerInfo struct {
	Addrs []string
	ID    string
}

func (s *Shell) FindPeer(peer string) (*PeerInfo, error) {
	resp, err := s.newRequest("dht/findpeer", peer).Send(s.httpcli)
	if err != nil {
		return nil, err
	}
	defer resp.Close()

	if resp.Error != nil {
		return nil, resp.Error
	}

	str := struct{ Responses []PeerInfo }{}
	err = json.NewDecoder(resp.Output).Decode(&str)
	if err != nil {
		return nil, err
	}

	if len(str.Responses) == 0 {
		return nil, errors.New("peer not found")
	}

	return &str.Responses[0], nil
}

func (s *Shell) Refs(hash string, recursive bool) (<-chan string, error) {
	req := s.newRequest("refs", hash)
	if recursive {
		req.Opts["r"] = "true"
	}

	resp, err := req.Send(s.httpcli)
	if err != nil {
		return nil, err
	}

	if resp.Error != nil {
		return nil, resp.Error
	}

	out := make(chan string)
	go func() {
		defer resp.Close()
		scan := bufio.NewScanner(resp.Output)
		for scan.Scan() {
			if len(scan.Text()) > 0 {
				out <- scan.Text()
			}
		}
		close(out)
	}()

	return out, nil
}

func (s *Shell) Patch(root, action string, args ...string) (string, error) {
	cmdargs := append([]string{root, action}, args...)
	resp, err := s.newRequest("object/patch", cmdargs...).Send(s.httpcli)
	if err != nil {
		return "", err
	}
	defer resp.Close()

	if resp.Error != nil {
		return "", resp.Error
	}

	dec := json.NewDecoder(resp.Output)
	var out object
	err = dec.Decode(&out)
	if err != nil {
		return "", err
	}

	return out.Hash, nil
}

func (s *Shell) PatchLink(root, path, childhash string, create bool) (string, error) {
	cmdargs := []string{root, "add-link", path, childhash}

	req := s.newRequest("object/patch", cmdargs...)
	if create {
		req.Opts["create"] = "true"
	}

	resp, err := req.Send(s.httpcli)
	if err != nil {
		return "", err
	}
	defer resp.Close()

	if resp.Error != nil {
		return "", resp.Error
	}

	var out object
	err = json.NewDecoder(resp.Output).Decode(&out)
	if err != nil {
		return "", err
	}

	return out.Hash, nil
}

func (s *Shell) Get(hash, outdir string) error {
	resp, err := s.newRequest("get", hash).Send(s.httpcli)
	if err != nil {
		return err
	}
	defer resp.Close()

	if resp.Error != nil {
		return resp.Error
	}

	extractor := &tar.Extractor{Path: outdir}
	return extractor.Extract(resp.Output)
}

func (s *Shell) NewObject(template string) (string, error) {
	args := []string{}
	if template != "" {
		args = []string{template}
	}

	resp, err := s.newRequest("object/new", args...).Send(s.httpcli)
	if err != nil {
		return "", err
	}
	defer resp.Close()

	if resp.Error != nil {
		return "", resp.Error
	}

	var out object
	err = json.NewDecoder(resp.Output).Decode(&out)
	if err != nil {
		return "", err
	}

	return out.Hash, nil
}

func (s *Shell) ResolvePath(path string) (string, error) {
	resp, err := s.newRequest("object/stat", path).Send(s.httpcli)
	if err != nil {
		return "", err
	}
	defer resp.Close()

	if resp.Error != nil {
		return "", resp.Error
	}

	var out object
	err = json.NewDecoder(resp.Output).Decode(&out)
	if err != nil {
		return "", err
	}

	return out.Hash, nil
}

// returns ipfs version and commit sha
func (s *Shell) Version() (string, string, error) {
	resp, err := s.newRequest("version").Send(s.httpcli)
	if err != nil {
		return "", "", err
	}

	defer resp.Close()
	if resp.Error != nil {
		return "", "", resp.Error
	}

	ver := struct {
		Version string
		Commit  string
	}{}

	err = json.NewDecoder(resp.Output).Decode(&ver)
	if err != nil {
		return "", "", err
	}

	return ver.Version, ver.Commit, nil
}

func (s *Shell) IsUp() bool {
	_, err := s.ID()
	return err == nil
}
<commit_msg>add block stat<commit_after>// package shell implements a remote API interface for a running ipfs daemon
package shell

import (
	"bufio"
	"encoding/json"
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	gohttp "net/http"
	"os"

	files "github.com/whyrusleeping/go-multipart-files"
	tar "github.com/whyrusleeping/tar-utils"
)

type Shell struct {
	url     string
	httpcli *gohttp.Client
}

func NewShell(url string) *Shell {
	return &Shell{
		url: url,
		httpcli: &gohttp.Client{
			Transport: &gohttp.Transport{
				DisableKeepAlives: true,
			},
		},
	}
}

func (s *Shell) newRequest(command string, args ...string) *Request {
	return NewRequest(s.url, command, args...)
}

type IdOutput struct {
	ID              string
	PublicKey       string
	Addresses       []string
	AgentVersion    string
	ProtocolVersion string
}

// ID gets information about a given peer.  Arguments:
//
// peer: peer.ID of the node to look up.  If no peer is specified,
//   return information about the local peer.
func (s *Shell) ID(peer ...string) (*IdOutput, error) {
	if len(peer) > 1 {
		return nil, fmt.Errorf("Too many peer arguments")
	}

	resp, err := NewRequest(s.url, "id", peer...).Send(s.httpcli)
	if err != nil {
		return nil, err
	}

	defer resp.Close()
	if resp.Error != nil {
		return nil, resp.Error
	}

	decoder := json.NewDecoder(resp.Output)
	out := new(IdOutput)
	err = decoder.Decode(out)
	if err != nil {
		return nil, err
	}

	return out, nil
}

// Cat the content at the given path. Callers need to drain and close the returned reader after usage.
func (s *Shell) Cat(path string) (io.ReadCloser, error) {
	resp, err := NewRequest(s.url, "cat", path).Send(s.httpcli)
	if err != nil {
		return nil, err
	}
	if resp.Error != nil {
		return nil, resp.Error
	}

	return resp.Output, nil
}

type object struct {
	Hash string
}

// Add a file to ipfs from the given reader, returns the hash of the added file
func (s *Shell) Add(r io.Reader) (string, error) {
	var rc io.ReadCloser
	if rclose, ok := r.(io.ReadCloser); ok {
		rc = rclose
	} else {
		rc = ioutil.NopCloser(r)
	}

	// handler expects an array of files
	fr := files.NewReaderFile("", "", rc, nil)
	slf := files.NewSliceFile("", "", []files.File{fr})
	fileReader := files.NewMultiFileReader(slf, true)

	req := NewRequest(s.url, "add")
	req.Body = fileReader

	resp, err := req.Send(s.httpcli)
	if err != nil {
		return "", err
	}
	defer resp.Close()
	if resp.Error != nil {
		return "", resp.Error
	}

	var out object
	err = json.NewDecoder(resp.Output).Decode(&out)
	if err != nil {
		return "", err
	}

	return out.Hash, nil
}

func (s *Shell) AddLink(target string) (string, error) {
	link := files.NewLinkFile("", "", target, nil)
	slf := files.NewSliceFile("", "", []files.File{link})
	reader := files.NewMultiFileReader(slf, true)

	req := s.newRequest("add")
	req.Body = reader

	resp, err := req.Send(s.httpcli)
	if err != nil {
		return "", err
	}
	defer resp.Close()
	if resp.Error != nil {
		return "", resp.Error
	}

	var out object
	err = json.NewDecoder(resp.Output).Decode(&out)
	if err != nil {
		return "", err
	}

	return out.Hash, nil
}

// AddDir adds a directory recursively with all of the files under it
func (s *Shell) AddDir(dir string) (string, error) {
	stat, err := os.Lstat(dir)
	if err != nil {
		return "", err
	}

	sf, err := files.NewSerialFile("", dir, stat)
	if err != nil {
		return "", err
	}
	slf := files.NewSliceFile("", dir, []files.File{sf})
	reader := files.NewMultiFileReader(slf, true)

	req := NewRequest(s.url, "add")
	req.Opts["r"] = "true"
	req.Body = reader

	resp, err := req.Send(s.httpcli)
	if err != nil {
		return "", err
	}
	defer resp.Close()
	if resp.Error != nil {
		return "", resp.Error
	}

	dec := json.NewDecoder(resp.Output)
	var final string
	for {
		var out object
		err = dec.Decode(&out)
		if err != nil {
			if err == io.EOF {
				break
			}
			return "", err
		}
		final = out.Hash
	}

	if final == "" {
		return "", errors.New("no results received")
	}

	return final, nil
}

const (
	TRaw = iota
	TDirectory
	TFile
	TMetadata
	TSymlink
)

// List entries at the given path
func (s *Shell) List(path string) ([]*LsLink, error) {
	resp, err := NewRequest(s.url, "ls", path).Send(s.httpcli)
	if err != nil {
		return nil, err
	}
	defer resp.Close()

	if resp.Error != nil {
		return nil, resp.Error
	}

	var out struct{ Objects []LsObject }
	err = json.NewDecoder(resp.Output).Decode(&out)
	if err != nil {
		return nil, err
	}

	return out.Objects[0].Links, nil
}

type LsLink struct {
	Hash string
	Name string
	Size uint64
	Type int
}

type LsObject struct {
	Links []*LsLink
	LsLink
}

// Pin the given path
func (s *Shell) Pin(path string) error {
	req := NewRequest(s.url, "pin/add", path)
	req.Opts["r"] = "true"

	resp, err := req.Send(s.httpcli)
	if err != nil {
		return err
	}
	defer resp.Close()
	if resp.Error != nil {
		return resp.Error
	}

	return nil
}

// Unpin the given path
func (s *Shell) Unpin(path string) error {
	req := NewRequest(s.url, "pin/rm", path)
	req.Opts["r"] = "true"

	resp, err := req.Send(s.httpcli)
	if err != nil {
		return err
	}
	defer resp.Close()
	if resp.Error != nil {
		return resp.Error
	}

	return nil
}

type PeerInfo struct {
	Addrs []string
	ID    string
}

func (s *Shell) FindPeer(peer string) (*PeerInfo, error) {
	resp, err := s.newRequest("dht/findpeer", peer).Send(s.httpcli)
	if err != nil {
		return nil, err
	}
	defer resp.Close()

	if resp.Error != nil {
		return nil, resp.Error
	}

	str := struct{ Responses []PeerInfo }{}
	err = json.NewDecoder(resp.Output).Decode(&str)
	if err != nil {
		return nil, err
	}

	if len(str.Responses) == 0 {
		return nil, errors.New("peer not found")
	}

	return &str.Responses[0], nil
}

func (s *Shell) Refs(hash string, recursive bool) (<-chan string, error) {
	req := s.newRequest("refs", hash)
	if recursive {
		req.Opts["r"] = "true"
	}

	resp, err := req.Send(s.httpcli)
	if err != nil {
		return nil, err
	}

	if resp.Error != nil {
		return nil, resp.Error
	}

	out := make(chan string)
	go func() {
		defer resp.Close()
		scan := bufio.NewScanner(resp.Output)
		for scan.Scan() {
			if len(scan.Text()) > 0 {
				out <- scan.Text()
			}
		}
		close(out)
	}()

	return out, nil
}

func (s *Shell) Patch(root, action string, args ...string) (string, error) {
	cmdargs := append([]string{root, action}, args...)
	resp, err := s.newRequest("object/patch", cmdargs...).Send(s.httpcli)
	if err != nil {
		return "", err
	}
	defer resp.Close()

	if resp.Error != nil {
		return "", resp.Error
	}

	dec := json.NewDecoder(resp.Output)
	var out object
	err = dec.Decode(&out)
	if err != nil {
		return "", err
	}

	return out.Hash, nil
}

func (s *Shell) PatchLink(root, path, childhash string, create bool) (string, error) {
	cmdargs := []string{root, "add-link", path, childhash}

	req := s.newRequest("object/patch", cmdargs...)
	if create {
		req.Opts["create"] = "true"
	}

	resp, err := req.Send(s.httpcli)
	if err != nil {
		return "", err
	}
	defer resp.Close()

	if resp.Error != nil {
		return "", resp.Error
	}

	var out object
	err = json.NewDecoder(resp.Output).Decode(&out)
	if err != nil {
		return "", err
	}

	return out.Hash, nil
}

func (s *Shell) Get(hash, outdir string) error {
	resp, err := s.newRequest("get", hash).Send(s.httpcli)
	if err != nil {
		return err
	}
	defer resp.Close()

	if resp.Error != nil {
		return resp.Error
	}

	extractor := &tar.Extractor{Path: outdir}
	return extractor.Extract(resp.Output)
}

func (s *Shell) NewObject(template string) (string, error) {
	args := []string{}
	if template != "" {
		args = []string{template}
	}

	resp, err := s.newRequest("object/new", args...).Send(s.httpcli)
	if err != nil {
		return "", err
	}
	defer resp.Close()

	if resp.Error != nil {
		return "", resp.Error
	}

	var out object
	err = json.NewDecoder(resp.Output).Decode(&out)
	if err != nil {
		return "", err
	}

	return out.Hash, nil
}

func (s *Shell) ResolvePath(path string) (string, error) {
	resp, err := s.newRequest("object/stat", path).Send(s.httpcli)
	if err != nil {
		return "", err
	}
	defer resp.Close()

	if resp.Error != nil {
		return "", resp.Error
	}

	var out object
	err = json.NewDecoder(resp.Output).Decode(&out)
	if err != nil {
		return "", err
	}

	return out.Hash, nil
}

// returns ipfs version and commit sha
func (s *Shell) Version() (string, string, error) {
	resp, err := s.newRequest("version").Send(s.httpcli)
	if err != nil {
		return "", "", err
	}

	defer resp.Close()
	if resp.Error != nil {
		return "", "", resp.Error
	}

	ver := struct {
		Version string
		Commit  string
	}{}

	err = json.NewDecoder(resp.Output).Decode(&ver)
	if err != nil {
		return "", "", err
	}

	return ver.Version, ver.Commit, nil
}

func (s *Shell) IsUp() bool {
	_, err := s.ID()
	return err == nil
}

func (s *Shell) BlockStat(path string) (string, int, error) {
	resp, err := s.newRequest("block/stat", path).Send(s.httpcli)
	if err != nil {
		return "", 0, err
	}
	defer resp.Close()

	if resp.Error != nil {
		return "", 0, resp.Error
	}

	var inf struct {
		Key  string
		Size int
	}

	err = json.NewDecoder(resp.Output).Decode(&inf)
	if err != nil {
		return "", 0, err
	}

	return inf.Key, inf.Size, nil
}
<|endoftext|>


<commit_before>package main

import (
	"bufio"
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
	"strings"
)

func Command(cmd string) (output *exec.Cmd) {
	output = exec.Command("bash", "-c", cmd)
	return output
}

func Run(file string) {
	out, _ := Command(fmt.Sprintf(`go run %s`, file)).Output()
	fmt.Println(out)
}

func ExecuteCommands(commands string) {
	filename := "test-123.go"
	ioutil.WriteFile(filename, []byte(commands), 0x777)
	Run(filename)
}

func main() {
	commands := `package main;import "fmt"`

	var cmd string

	fmt.Println("Go in the Shell (type `exit` to finish)")
	reader := bufio.NewReader(os.Stdin)

	for cmd != "exit" {
		if cmd != "" {
			ExecuteCommands(commands)
		}
		fmt.Print(">>> ")
		cmd, _ = reader.ReadString('\n')
		cmd = strings.TrimSpace(cmd)
		commands += "\n" + cmd
	}
}
<commit_msg>Can print lines<commit_after>package main

import (
	"bufio"
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
	"strings"
)

func Command(cmd string) (output *exec.Cmd) {
	output = exec.Command("bash", "-c", cmd)
	return output
}

func Run(file string) (exitStatus int) {
	cmd := fmt.Sprintf(`go run %s`, file)
	command := Command(cmd)
	out, err := command.CombinedOutput()

	fmt.Println(string(out))
	if err != nil {
		return 1
	}
	return 0
}

func ExecuteCommands(commands string) (status int) {
	filename := "test-123.go"
	ioutil.WriteFile(filename, []byte(commands), os.ModeTemporary|os.ModePerm)
	return Run(filename)
}

func main() {
	commands := []string{`package main`, `import "fmt"`, `func main() {`}

	var cmd string

	fmt.Println("Go in the Shell (type `exit` to finish)")
	reader := bufio.NewReader(os.Stdin)

	for cmd != "exit" {
		if cmd != "" {
			status := ExecuteCommands(strings.Join(commands, "\n") + "\n" + cmd + "\n}")
			if status == 0 {
				commands = append(commands, cmd)
			}
		}
		fmt.Print(">>> ")
		cmd, _ = reader.ReadString('\n')
		cmd = strings.TrimSpace(cmd)
	}
}
<|endoftext|>


<commit_before>package shortstr

import (
	"reflect"

	"github.com/armon/go-radix"
)

// Short is a helper to return short, unique substrings when
// given a set of data to work with and the full value of
// the string to shorten. This can be useful to make indexes
// more human-friendly while still retaining their
// uniqueness and identifiability.
//
// A good example of where to use this library is with user-
// facing UUID's. It is often much easier to return a 6- or
// 7-character string and pass it around than it is to use
// the full 128-bit value.
type Short struct {
	tree *radix.Tree
}

// New creates a new shortener. It takes a slice of either
// strings or structs, and an optional field name. If using
// structs, the field name indicates which string field
// should be used.
func New(data interface{}, field string) *Short {
	v := reflect.ValueOf(data)
	if v.Kind() != reflect.Slice {
		panic("not a slice")
	}

	tree := radix.New()

	// Go over all of the data and insert our keys into
	// the tree.
	for i := 0; i < v.Len(); i++ {
		val := reflect.Indirect(v.Index(i))
		switch val.Kind() {
		case reflect.String:
			// No special handling required for strings

		case reflect.Struct:
			// If we have a struct, we need to attempt to
			// read the field value.
			val = val.FieldByName(field)
			if !val.IsValid() {
				panic("missing struct field")
			}

		default:
			panic("not a string or struct")
		}

		// Insert the value into the tree
		tree.Insert(val.String(), struct{}{})
	}

	return &Short{tree}
}

// min is the internal method used to retrieve the shortest
// possible string, given the length constraint.
func (s *Short) min(in string, l int) string {
	var result string
	for i := 0; ; i++ {
		// Add the next chunk of characters
		lidx := (i + 1) * l
		if lidx > len(in) {
			break
		}
		result += in[i*l : (i+1)*l]

		// Walk the tree. If anything is found by the given
		// result prefix, then the current result is ambiguous
		// and we need to add more characters.
		var ambiguous bool
		s.tree.WalkPrefix(result, func(s string, _ interface{}) bool {
			// If we find ourself in the tree, we can stop.
			// Uniqueness is not guaranteed in this case.
			if s == in {
				return true
			}

			ambiguous = true
			return true
		})
		if ambiguous {
			continue
		}

		// We got an unambiguous result, so return it
		return result
	}
	return ""
}

// MinChunk is used to return the shortest substring in the
// chunk size provided. This means the minimum returned length
// is l, and the max is a multiple thereof. This is useful
// for keeping churn rate low with a frequently changing
// data set.
func (s *Short) MinChunk(in string, l int) string {
	return s.min(in, l)
}

// Min is used to return the shortest possible unique match
// from the data set. If an empty string is returned, then
func (s *Short) Min(in string) string {
	return s.min(in, 1)
}
<commit_msg>Simplify code<commit_after>package shortstr

import (
	"reflect"

	"github.com/armon/go-radix"
)

// Short is a helper to return short, unique substrings when
// given a set of data to work with and the full value of
// the string to shorten. This can be useful to make indexes
// more human-friendly while still retaining their
// uniqueness and identifiability.
//
// A good example of where to use this library is with user-
// facing UUID's. It is often much easier to return a 6- or
// 7-character string and pass it around than it is to use
// the full 128-bit value.
type Short struct {
	tree *radix.Tree
}

// New creates a new shortener. It takes a slice of either
// strings or structs, and an optional field name. If using
// structs, the field name indicates which string field
// should be used.
func New(data interface{}, field string) *Short {
	v := reflect.ValueOf(data)
	if v.Kind() != reflect.Slice {
		panic("not a slice")
	}

	tree := radix.New()

	// Go over all of the data and insert our keys into
	// the tree.
	for i := 0; i < v.Len(); i++ {
		val := reflect.Indirect(v.Index(i))
		switch val.Kind() {
		case reflect.String:
			// No special handling required for strings

		case reflect.Struct:
			// If we have a struct, we need to attempt to
			// read the field value.
			val = val.FieldByName(field)
			if !val.IsValid() {
				panic("missing struct field")
			}

		default:
			panic("not a string or struct")
		}

		// Insert the value into the tree
		tree.Insert(val.String(), struct{}{})
	}

	return &Short{tree}
}

// min is the internal method used to retrieve the shortest
// possible string, given the length constraint.
func (s *Short) min(in string, l int) string {
	var result string
	for i := 0; ; i++ {
		// Add the next chunk of characters
		lidx := (i + 1) * l
		if lidx > len(in) {
			break
		}
		result += in[i*l : lidx]

		// Walk the tree. If anything is found by the given
		// result prefix, then the current result is ambiguous
		// and we need to add more characters.
		var ambiguous bool
		s.tree.WalkPrefix(result, func(s string, _ interface{}) bool {
			// If we find ourself in the tree, we can stop.
			// Uniqueness is not guaranteed in this case.
			ambiguous = (s != in)
			return true
		})
		if ambiguous {
			continue
		}

		// We got an unambiguous result, so return it
		return result
	}
	return ""
}

// MinChunk is used to return the shortest substring in the
// chunk size provided. This means the minimum returned length
// is l, and the max is a multiple thereof. This is useful
// for keeping churn rate low with a frequently changing
// data set.
func (s *Short) MinChunk(in string, l int) string {
	return s.min(in, l)
}

// Min is used to return the shortest possible unique match
// from the data set. If an empty string is returned, then
func (s *Short) Min(in string) string {
	return s.min(in, 1)
}

// Full is used to look up the full value of a given short
// string in the data set. Returns the full string (if found),
// and a bool indicator, which signals the compound condition
// of the key both existing and being unique.
func (s *Short) Full(in string) (string, bool) {
	var found, ambiguous bool
	var full string

	// Walk the prefix of the given short string. If a single
	// entry is found we can return safely, but if we find
	// more then the lookup cannot resolve.
	s.tree.WalkPrefix(in, func(s string, _ interface{}) bool {
		if found {
			ambiguous = true
			return true
		}
		found = true
		full = s
		return false
	})
	return full, found && !ambiguous
}
<|endoftext|>


<commit_before>package fsd

import (
	"errors"
	"fmt"
	"math/rand"
	"net"
	"time"
)

var (
	Instance *Fsd
)

type Fsd struct {
	outgoing chan string
	address  string
	conn     net.Conn
}

func init() {
	Start("127.0.0.1:8125")
}

func Start(address string) {
	Instance = &Fsd{address: address, outgoing: make(chan string, 100000)}
	Instance.connect()

	go Instance.processOutgoing()
}

func (fsd *Fsd) connect() error {
	conn, err := net.Dial("udp", fsd.address)
	if err != nil {
		return err
	}

	fsd.conn = conn
	return nil
}

func (fsd *Fsd) processOutgoing() {
	for outgoing := range fsd.outgoing {
		data := fmt.Sprintf("%s", outgoing)

		if _, err := fsd.conn.Write([]byte(data)); err != nil {
			fsd.connect()
		}
	}
}

// To read about the different semantics check out
// https://github.com/b/statsd_spec
// http://docs.datadoghq.com/guides/dogstatsd/

// Increment the page.views counter.
// page.views:1|c
func Count(name string, value float64) {
	CountL(name, value, 1.0)
}

func CountL(name string, value float64, rate float64) {
	payload := createPayload(name, value) + "|c"

	suffix, err := rateCheck(rate)
	if err != nil {
		return
	}

	payload = payload + suffix
	send(payload)
}

// Record the fuel tank is half-empty
// fuel.level:0.5|g
func Gauge(name string, value float64) {
	payload := createPayload(name, value) + "|g"
	send(payload)
}

// A request latency
// request.latency:320|ms
// Or a payload of a image
// image.size:2.3|ms
func Timer(name string, value float64) {
	TimerL(name, value, 1.0)
}

func TimerL(name string, value float64, rate float64) {
	payload := createPayload(name, value) + "|ms"

	suffix, err := rateCheck(rate)
	if err != nil {
		return
	}

	payload = payload + suffix
	send(payload)
}

func Time(name string, rate float64, lambda func()) {
	start := time.Now()
	lambda()
	Timer(name, float64(time.Now().Sub(start).Nanoseconds()/1000000), rate)
}

// Track a unique visitor id to the site.
// users.uniques:1234|s
func Set(name string, value float64) {
	payload := createPayload(name, value) + "|s"
	send(payload)
}

func createPayload(name string, value float64) (payload string) {
	payload = fmt.Sprintf("%s:%f", name, value)
	return payload
}

func rateCheck(rate float64) (suffix string, err error) {
	if rate < 1 {
		if rand.Float64() < rate {
			return fmt.Sprintf("|@%f", rate), nil
		}
	} else {
		return "", nil
	}

	return "", errors.New("Out of rate limit")
}

func send(payload string) {
	if float64(len(Instance.outgoing)) < float64(cap(Instance.outgoing))*0.9 {
		Instance.outgoing <- payload
	}
}
<commit_msg>Last fixes of rate limitation<commit_after>package fsd

import (
	"errors"
	"fmt"
	"math/rand"
	"net"
	"time"
)

var (
	Instance *Fsd
)

type Fsd struct {
	outgoing chan string
	address  string
	conn     net.Conn
}

func init() {
	Start("127.0.0.1:8125")
}

func Start(address string) {
	Instance = &Fsd{address: address, outgoing: make(chan string, 100000)}
	Instance.connect()

	go Instance.processOutgoing()
}

func (fsd *Fsd) connect() error {
	conn, err := net.Dial("udp", fsd.address)
	if err != nil {
		return err
	}

	fsd.conn = conn
	return nil
}

func (fsd *Fsd) processOutgoing() {
	for outgoing := range fsd.outgoing {
		data := fmt.Sprintf("%s", outgoing)

		if _, err := fsd.conn.Write([]byte(data)); err != nil {
			fsd.connect()
		}
	}
}

// To read about the different semantics check out
// https://github.com/b/statsd_spec
// http://docs.datadoghq.com/guides/dogstatsd/

// Increment the page.views counter.
// page.views:1|c
func Count(name string, value float64) {
	CountL(name, value, 1.0)
}

func CountL(name string, value float64, rate float64) {
	payload := createPayload(name, value) + "|c"

	suffix, err := rateCheck(rate)
	if err != nil {
		return
	}

	payload = payload + suffix
	send(payload)
}

// Record the fuel tank is half-empty
// fuel.level:0.5|g
func Gauge(name string, value float64) {
	payload := createPayload(name, value) + "|g"
	send(payload)
}

// A request latency
// request.latency:320|ms
// Or a payload of a image
// image.size:2.3|ms
func Timer(name string, value float64) {
	TimerL(name, value, 1.0)
}

func TimerL(name string, value float64, rate float64) {
	payload := createPayload(name, value) + "|ms"

	suffix, err := rateCheck(rate)
	if err != nil {
		return
	}

	payload = payload + suffix
	send(payload)
}

func Time(name string, lambda func()) {
	TimeL(name, 1.0, lambda)
}

func TimeL(name string, rate float64, lambda func()) {
	start := time.Now()
	lambda()
	TimerL(name, float64(time.Now().Sub(start).Nanoseconds()/1000000), rate)
}

// Track a unique visitor id to the site.
// users.uniques:1234|s
func Set(name string, value float64) {
	payload := createPayload(name, value) + "|s"
	send(payload)
}

func createPayload(name string, value float64) (payload string) {
	payload = fmt.Sprintf("%s:%f", name, value)
	return payload
}

func rateCheck(rate float64) (suffix string, err error) {
	if rate < 1 {
		if rand.Float64() < rate {
			return fmt.Sprintf("|@%f", rate), nil
		}
	} else {
		return "", nil
	}

	return "", errors.New("Out of rate limit")
}

func send(payload string) {
	if float64(len(Instance.outgoing)) < float64(cap(Instance.outgoing))*0.9 {
		Instance.outgoing <- payload
	}
}
<|endoftext|>


<commit_before>package importer

import (
	"code.google.com/p/go.tools/go/types"
	"fmt"
	"go/ast"
	"go/build"
	"go/parser"
	"go/token"
	"os"
)

type Importer struct {
	Imports map[string]*types.Package // All packages imported by Importer
}

func NewImporter() Importer {
	return Importer{
		Imports: make(map[string]*types.Package),
	}
}

// Import implements the Importer type from go/types.
func (imp Importer) Import(imports map[string]*types.Package, path string) (pkg *types.Package, err error) {
	// types.Importer does not seem to be designed for recursive
	// parsing like we're doing here. Specifically, each nested import
	// will maintain its own imports map. This will lead to duplicate
	// imports and in turn packages, which will lead to funny errors
	// such as "cannot pass argument ip (variable of type net.IP) to
	// variable of type net.IP"
	//
	// To work around this, we keep a global imports map, allImports,
	// to which we add all nested imports, and which we use as the
	// cache, instead of imports.
	//
	// Since all nested imports will also use this importer, there
	// should be no way to end up with duplicate imports.

	// We first try to use GcImport directly. This has the downside of
	// using possibly out-of-date packages, but it has the upside of
	// not having to parse most of the Go standard library.

	buildPkg, buildErr := build.Import(path, ".", 0)
	// If we found no build dir, assume we're dealing with installed
	// but no source. If we found a build dir, only use GcImport if
	// it's in GOROOT. This way we always use up-to-date code for
	// normal packages but avoid parsing the standard library.
	if (buildErr == nil && buildPkg.Goroot) || buildErr != nil {
		pkg, err = types.GcImport(imp.Imports, path)
		if err == nil {
			// We don't use imports, but per API we have to add the package.
			imports[pkg.Path()] = pkg
			imp.Imports[pkg.Path()] = pkg
			return pkg, nil
		}
	}

	// See if we already imported this package
	if pkg = imp.Imports[path]; pkg != nil && pkg.Complete() {
		return pkg, nil
	}

	// allImports failed, try to use go/build
	if buildErr != nil {
		return nil, fmt.Errorf("build.Import failed: %s", buildErr)
	}

	// TODO check if the .a file is up to date and use it instead
	fileSet := token.NewFileSet()

	isGoFile := func(d os.FileInfo) bool {
		allFiles := make([]string, 0, len(buildPkg.GoFiles)+len(buildPkg.CgoFiles))
		allFiles = append(allFiles, buildPkg.GoFiles...)
		allFiles = append(allFiles, buildPkg.CgoFiles...)

		for _, file := range allFiles {
			if file == d.Name() {
				return true
			}
		}
		return false
	}
	pkgs, err := parser.ParseDir(fileSet, buildPkg.Dir, isGoFile, 0)
	if err != nil {
		return nil, err
	}

	delete(pkgs, "documentation")
	var astPkg *ast.Package
	var name string
	for name, astPkg = range pkgs {
		// Use the first non-main package, or the only package we
		// found.
		//
		// NOTE(dh) I can't think of a reason why there should be
		// multiple packages in a single directory, but ParseDir
		// accommodates for that possibility.
		if len(pkgs) == 1 || name != "main" {
			break
		}
	}

	if astPkg == nil {
		return nil, fmt.Errorf("can't find import: %s", name)
	}

	var ff []*ast.File
	for _, f := range astPkg.Files {
		ff = append(ff, f)
	}

	context := types.Config{
		Import: imp.Import,
	}

	pkg, err = context.Check(name, fileSet, ff, nil)
	if err != nil {
		return pkg, err
	}
	if !pkg.Complete() {
		pkg = types.NewPackage(pkg.Pos(), pkg.Path(), pkg.Name(), pkg.Scope(), pkg.Imports(), true)
	}

	imports[path] = pkg
	imp.Imports[path] = pkg
	return pkg, nil
}
<commit_msg>Update to new upstream API<commit_after>package importer

import (
	"code.google.com/p/go.tools/go/types"
	"fmt"
	"go/ast"
	"go/build"
	"go/parser"
	"go/token"
	"os"
)

type Importer struct {
	Imports map[string]*types.Package // All packages imported by Importer
}

func NewImporter() Importer {
	return Importer{
		Imports: make(map[string]*types.Package),
	}
}

// Import implements the Importer type from go/types.
func (imp Importer) Import(imports map[string]*types.Package, path string) (pkg *types.Package, err error) {
	// types.Importer does not seem to be designed for recursive
	// parsing like we're doing here. Specifically, each nested import
	// will maintain its own imports map. This will lead to duplicate
	// imports and in turn packages, which will lead to funny errors
	// such as "cannot pass argument ip (variable of type net.IP) to
	// variable of type net.IP"
	//
	// To work around this, we keep a global imports map, allImports,
	// to which we add all nested imports, and which we use as the
	// cache, instead of imports.
	//
	// Since all nested imports will also use this importer, there
	// should be no way to end up with duplicate imports.

	// We first try to use GcImport directly. This has the downside of
	// using possibly out-of-date packages, but it has the upside of
	// not having to parse most of the Go standard library.

	buildPkg, buildErr := build.Import(path, ".", 0)
	// If we found no build dir, assume we're dealing with installed
	// but no source. If we found a build dir, only use GcImport if
	// it's in GOROOT. This way we always use up-to-date code for
	// normal packages but avoid parsing the standard library.
	if (buildErr == nil && buildPkg.Goroot) || buildErr != nil {
		pkg, err = types.GcImport(imp.Imports, path)
		if err == nil {
			// We don't use imports, but per API we have to add the package.
			imports[pkg.Path()] = pkg
			imp.Imports[pkg.Path()] = pkg
			return pkg, nil
		}
	}

	// See if we already imported this package
	if pkg = imp.Imports[path]; pkg != nil && pkg.Complete() {
		return pkg, nil
	}

	// allImports failed, try to use go/build
	if buildErr != nil {
		return nil, fmt.Errorf("build.Import failed: %s", buildErr)
	}

	// TODO check if the .a file is up to date and use it instead
	fileSet := token.NewFileSet()

	isGoFile := func(d os.FileInfo) bool {
		allFiles := make([]string, 0, len(buildPkg.GoFiles)+len(buildPkg.CgoFiles))
		allFiles = append(allFiles, buildPkg.GoFiles...)
		allFiles = append(allFiles, buildPkg.CgoFiles...)

		for _, file := range allFiles {
			if file == d.Name() {
				return true
			}
		}
		return false
	}
	pkgs, err := parser.ParseDir(fileSet, buildPkg.Dir, isGoFile, 0)
	if err != nil {
		return nil, err
	}

	delete(pkgs, "documentation")
	var astPkg *ast.Package
	var name string
	for name, astPkg = range pkgs {
		// Use the first non-main package, or the only package we
		// found.
		//
		// NOTE(dh) I can't think of a reason why there should be
		// multiple packages in a single directory, but ParseDir
		// accommodates for that possibility.
		if len(pkgs) == 1 || name != "main" {
			break
		}
	}

	if astPkg == nil {
		return nil, fmt.Errorf("can't find import: %s", name)
	}

	var ff []*ast.File
	for _, f := range astPkg.Files {
		ff = append(ff, f)
	}

	context := types.Config{
		Import: imp.Import,
	}

	pkg, err = context.Check(name, fileSet, ff, nil)
	if err != nil {
		return pkg, err
	}
	pkg.MarkComplete()

	imports[path] = pkg
	imp.Imports[path] = pkg
	return pkg, nil
}
<|endoftext|>


<commit_before>package extra

import (
	"bytes"
	"fmt"
	"testing"

	"github.com/elpinal/coco3/extra/ast"
	"github.com/elpinal/coco3/extra/typed"
	"github.com/elpinal/coco3/extra/types"
)

func TestEval(t *testing.T) {
	var buf bytes.Buffer
	prefix := "print: the argument is"
	printCommand := func(args []ast.Expr) error {
		_, err := fmt.Fprintln(&buf, prefix, args[0].(*ast.String).Lit)
		return err
	}
	e := Env{cmds: map[string]typed.Command{"print": {Params: []types.Type{types.String}, Fn: printCommand}}}
	err := e.Eval(&ast.Command{Name: "print", Args: []ast.Expr{&ast.String{Lit: "aaa"}}})
	if err != nil {
		t.Fatalf("Eval: %v", err)
	}
	got := buf.String()
	want := prefix + " aaa\n"
	if got != want {
		t.Errorf("Eval: want %q, but got %q", want, got)
	}
}
<commit_msg>Combine New & Bind<commit_after>package extra

import (
	"bytes"
	"fmt"
	"testing"

	"github.com/elpinal/coco3/extra/ast"
	"github.com/elpinal/coco3/extra/typed"
	"github.com/elpinal/coco3/extra/types"
)

func TestEval(t *testing.T) {
	var buf bytes.Buffer
	prefix := "print: the argument is"
	printCommand := func(args []ast.Expr) error {
		_, err := fmt.Fprintln(&buf, prefix, args[0].(*ast.String).Lit)
		return err
	}
	e := New()
	e.Bind("print", typed.Command{Params: []types.Type{types.String}, Fn: printCommand})
	err := e.Eval(&ast.Command{Name: "print", Args: []ast.Expr{&ast.String{Lit: "aaa"}}})
	if err != nil {
		t.Fatalf("Eval: %v", err)
	}
	got := buf.String()
	want := prefix + " aaa\n"
	if got != want {
		t.Errorf("Eval: want %q, but got %q", want, got)
	}
}
<|endoftext|>


<commit_before>
package ophion

import (
	"google.golang.org/grpc"
)

func OphionConnect(address string) (QueryClient, error) {
	conn, err := grpc.Dial(address, grpc.WithInsecure())
  if err != nil {
    return nil, err
  }
  out := NewQueryClient(conn)
  return out, err
}
<commit_msg>Starting to add client query builder<commit_after>
package ophion

import (
	"context"
	"google.golang.org/grpc"
)

func Connect(address string) (QueryClient, error) {
	conn, err := grpc.Dial(address, grpc.WithInsecure())
  if err != nil {
    return nil, err
  }
  out := NewQueryClient(conn)
  return out, err
}

type QueryBuilder struct {
	client QueryClient
	query  []*GraphStatement
}

func Query(client QueryClient) QueryBuilder {
	return QueryBuilder{client, []*GraphStatement{}}
}

func (q QueryBuilder) V(id ...string) QueryBuilder {
	if len(id) > 0 {
		return QueryBuilder{ q.client, append(q.query, &GraphStatement{&GraphStatement_V{id[0]}}) }
	} else {
		return QueryBuilder{ q.client, append(q.query, &GraphStatement{}) }
	}
}

func (q QueryBuilder) Execute() (chan *ResultRow, error) {
	tclient, err := q.client.Traversal(context.TODO(), &GraphQuery{q.query})
	if err != nil {
		return nil, err
	}
	out := make(chan *ResultRow, 100)
	go func() {
		defer close(out)
		for t, err := tclient.Recv(); err != nil; t, err = tclient.Recv() {
			out <- t
		}
	}()
	return out, nil
}
<|endoftext|>


<commit_before><commit_msg>2016-07-06:21:00:31<commit_after><|endoftext|>


<commit_before>// Copyright 2015 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.

package main

import (
	"fmt"
	"github.com/FactomProject/FactomCode/common"
	cp "github.com/FactomProject/FactomCode/controlpanel"
	"github.com/FactomProject/FactomCode/database"
	"github.com/FactomProject/FactomCode/database/ldb"
	"github.com/FactomProject/FactomCode/process"
	"github.com/FactomProject/FactomCode/util"
	"github.com/FactomProject/FactomCode/wsapi"
	"github.com/FactomProject/btcd"
	"github.com/FactomProject/btcd/limits"
	"github.com/FactomProject/btcd/wire"
	"os"
	"runtime"
	"strings"
	"time"
)

var (
	_               = fmt.Print
	cfg             *util.FactomdConfig
	shutdownChannel = make(chan struct{})
	ldbpath         = ""
	db              database.Db                           // database
	inMsgQueue      = make(chan wire.FtmInternalMsg, 100) //incoming message queue for factom application messages
	outMsgQueue     = make(chan wire.FtmInternalMsg, 100) //outgoing message queue for factom application messages
	inCtlMsgQueue   = make(chan wire.FtmInternalMsg, 100) //incoming message queue for factom application messages
	outCtlMsgQueue  = make(chan wire.FtmInternalMsg, 100) //outgoing message queue for factom application messages
	//	inRpcQueue      = make(chan wire.Message, 100) //incoming message queue for factom application messages
)

// winServiceMain is only invoked on Windows.  It detects when btcd is running
// as a service and reacts accordingly.
//var winServiceMain func() (bool, error)

func main() {
	ftmdLog.Info("//////////////////////// Copyright 2015 Factom Foundation")
	ftmdLog.Info("//////////////////////// Use of this source code is governed by the MIT")
	ftmdLog.Info("//////////////////////// license that can be found in the LICENSE file.")

	ftmdLog.Warning("Go compiler version: %s", runtime.Version())
	fmt.Println("Go compiler version: ", runtime.Version())
	cp.CP.AddUpdate("gocompiler",
		"system",
		fmt.Sprintln("Go compiler version: ", runtime.Version()),
		"",
		0)
	cp.CP.AddUpdate("copyright",
		"system",
		"Legal",
		"Copyright 2015 Factom Foundation\n"+
			"Use of this source code is governed by the MIT\n"+
			"license that can be found in the LICENSE file.",
		0)

	if !isCompilerVersionOK() {
		fmt.Println("\n\n === WARNING: unsupported compiler version !!! ===\n\n")
		time.Sleep(time.Second)
	}

	// Load configuration file and send settings to components
	loadConfigurations()

	// Initialize db
	initDB()

	// Use all processor cores.
	runtime.GOMAXPROCS(runtime.NumCPU())

	//Up some limits.
	if err := limits.SetLimits(); err != nil {
		os.Exit(1)
	}

	// Work around defer not working after os.Exit()
	if err := factomdMain(); err != nil {
		os.Exit(1)
	}

}

func factomdMain() error {

	// Start the processor module
	go process.Start_Processor(db, inMsgQueue, outMsgQueue, inCtlMsgQueue, outCtlMsgQueue)

	// Start the wsapi server module in a separate go-routine
	wsapi.Start(db, inMsgQueue)

	// wait till the initialization is complete in processor
	hash, _ := db.FetchDBHashByHeight(0)
	if hash != nil {
		for true {
			latestDirBlockHash, _, _ := db.FetchBlockHeightCache()
			if latestDirBlockHash == nil {
				ftmdLog.Info("Waiting for the processor to be initialized...")
				time.Sleep(2 * time.Second)
			} else {
				break
			}
		}
	}

	if len(os.Args) >= 2 {
		if os.Args[1] == "initializeonly" {
			time.Sleep(time.Second)
			fmt.Println("Initializing only.")
			os.Exit(0)
		}
	} else {
		fmt.Println("\n'factomd initializeonly' will do just that.  Initialize and stop.")
	}

	// Start the factoid (btcd) component and P2P component
	btcd.Start_btcd(db, inMsgQueue, outMsgQueue, inCtlMsgQueue, outCtlMsgQueue, process.FactomdUser, process.FactomdPass, common.SERVER_NODE != cfg.App.NodeMode)

	return nil
}

// Load settings from configuration file: factomd.conf
func loadConfigurations() {

	cfg = util.ReadConfig()

	ldbpath = cfg.App.LdbPath
	process.LoadConfigurations(cfg)

}

// Initialize the level db and share it with other components
func initDB() {

	//init db
	var err error
	db, err = ldb.OpenLevelDB(ldbpath, false)

	if err != nil {
		ftmdLog.Errorf("err opening db: %v\n", err)

	}

	if db == nil {
		ftmdLog.Info("Creating new db ...")
		db, err = ldb.OpenLevelDB(ldbpath, true)

		if err != nil {
			panic(err)
		}
	}
	ftmdLog.Info("Database started from: " + ldbpath)

}

func isCompilerVersionOK() bool {
	goodenough := false

	if strings.Contains(runtime.Version(), "1.3") {
		goodenough = true
	}

	if strings.Contains(runtime.Version(), "1.4") {
		goodenough = true
	}

	if strings.Contains(runtime.Version(), "1.5") {
		goodenough = true
	}

	if strings.Contains(runtime.Version(), "1.6") {
		goodenough = true
	}

	return goodenough
}
<commit_msg>enforce the proper go compiler version<commit_after>// Copyright 2015 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.

package main

import (
	"fmt"
	"github.com/FactomProject/FactomCode/common"
	cp "github.com/FactomProject/FactomCode/controlpanel"
	"github.com/FactomProject/FactomCode/database"
	"github.com/FactomProject/FactomCode/database/ldb"
	"github.com/FactomProject/FactomCode/process"
	"github.com/FactomProject/FactomCode/util"
	"github.com/FactomProject/FactomCode/wsapi"
	"github.com/FactomProject/btcd"
	"github.com/FactomProject/btcd/limits"
	"github.com/FactomProject/btcd/wire"
	"os"
	"runtime"
	"strings"
	"time"
)

var (
	_               = fmt.Print
	cfg             *util.FactomdConfig
	shutdownChannel = make(chan struct{})
	ldbpath         = ""
	db              database.Db                           // database
	inMsgQueue      = make(chan wire.FtmInternalMsg, 100) //incoming message queue for factom application messages
	outMsgQueue     = make(chan wire.FtmInternalMsg, 100) //outgoing message queue for factom application messages
	inCtlMsgQueue   = make(chan wire.FtmInternalMsg, 100) //incoming message queue for factom application messages
	outCtlMsgQueue  = make(chan wire.FtmInternalMsg, 100) //outgoing message queue for factom application messages
	//	inRpcQueue      = make(chan wire.Message, 100) //incoming message queue for factom application messages
)

// winServiceMain is only invoked on Windows.  It detects when btcd is running
// as a service and reacts accordingly.
//var winServiceMain func() (bool, error)

func main() {
	ftmdLog.Info("//////////////////////// Copyright 2015 Factom Foundation")
	ftmdLog.Info("//////////////////////// Use of this source code is governed by the MIT")
	ftmdLog.Info("//////////////////////// license that can be found in the LICENSE file.")

	ftmdLog.Warning("Go compiler version: %s", runtime.Version())
	fmt.Println("Go compiler version: ", runtime.Version())
	cp.CP.AddUpdate("gocompiler",
		"system",
		fmt.Sprintln("Go compiler version: ", runtime.Version()),
		"",
		0)
	cp.CP.AddUpdate("copyright",
		"system",
		"Legal",
		"Copyright 2015 Factom Foundation\n"+
			"Use of this source code is governed by the MIT\n"+
			"license that can be found in the LICENSE file.",
		0)

	if !isCompilerVersionOK() {
		for i := 0; i < 30; i++ {
			fmt.Println("=== ERROR: unsupported compiler version !!! ===")
		}
		time.Sleep(time.Second)
		os.Exit(1)
	}

	// Load configuration file and send settings to components
	loadConfigurations()

	// Initialize db
	initDB()

	// Use all processor cores.
	runtime.GOMAXPROCS(runtime.NumCPU())

	//Up some limits.
	if err := limits.SetLimits(); err != nil {
		os.Exit(1)
	}

	// Work around defer not working after os.Exit()
	if err := factomdMain(); err != nil {
		os.Exit(1)
	}

}

func factomdMain() error {

	// Start the processor module
	go process.Start_Processor(db, inMsgQueue, outMsgQueue, inCtlMsgQueue, outCtlMsgQueue)

	// Start the wsapi server module in a separate go-routine
	wsapi.Start(db, inMsgQueue)

	// wait till the initialization is complete in processor
	hash, _ := db.FetchDBHashByHeight(0)
	if hash != nil {
		for true {
			latestDirBlockHash, _, _ := db.FetchBlockHeightCache()
			if latestDirBlockHash == nil {
				ftmdLog.Info("Waiting for the processor to be initialized...")
				time.Sleep(2 * time.Second)
			} else {
				break
			}
		}
	}

	if len(os.Args) >= 2 {
		if os.Args[1] == "initializeonly" {
			time.Sleep(time.Second)
			fmt.Println("Initializing only.")
			os.Exit(0)
		}
	} else {
		fmt.Println("\n'factomd initializeonly' will do just that.  Initialize and stop.")
	}

	// Start the factoid (btcd) component and P2P component
	btcd.Start_btcd(db, inMsgQueue, outMsgQueue, inCtlMsgQueue, outCtlMsgQueue, process.FactomdUser, process.FactomdPass, common.SERVER_NODE != cfg.App.NodeMode)

	return nil
}

// Load settings from configuration file: factomd.conf
func loadConfigurations() {

	cfg = util.ReadConfig()

	ldbpath = cfg.App.LdbPath
	process.LoadConfigurations(cfg)

}

// Initialize the level db and share it with other components
func initDB() {

	//init db
	var err error
	db, err = ldb.OpenLevelDB(ldbpath, false)

	if err != nil {
		ftmdLog.Errorf("err opening db: %v\n", err)

	}

	if db == nil {
		ftmdLog.Info("Creating new db ...")
		db, err = ldb.OpenLevelDB(ldbpath, true)

		if err != nil {
			panic(err)
		}
	}
	ftmdLog.Info("Database started from: " + ldbpath)

}

func isCompilerVersionOK() bool {
	goodenough := false

	if strings.Contains(runtime.Version(), "1.3") {
		goodenough = true
	}

	if strings.Contains(runtime.Version(), "1.4") {
		goodenough = true
	}

	if strings.Contains(runtime.Version(), "1.5") {
		goodenough = true
	}

	if strings.Contains(runtime.Version(), "1.6") {
		goodenough = true
	}

	return goodenough
}
<|endoftext|>


<commit_before>package raft

import (
	"sync/atomic"
)

// RaftState captures the state of a Raft node: Follower, Candidate, Leader,
// or Shutdown.
type RaftState uint32

const (
	// Follower is the initial state of a Raft node.
	Follower RaftState = iota

	// Candidate is one of the valid states of a Raft node.
	Candidate

	// Leader is one of the valid states of a Raft node.
	Leader

	// Shutdown is the terminal state of a Raft node.
	Shutdown
)

func (s RaftState) String() string {
	switch s {
	case Follower:
		return "Follower"
	case Candidate:
		return "Candidate"
	case Leader:
		return "Leader"
	case Shutdown:
		return "Shutdown"
	default:
		return "Unknown"
	}
}

// raftState is used to maintain various state variables
// and provides an interface to set/get the variables in a
// thread safe manner.
type raftState struct {
	// The current term, cache of StableStore
	currentTerm uint64

	// Cache the latest log from LogStore
	LastLogIndex uint64
	LastLogTerm  uint64

	// Highest committed log entry
	commitIndex uint64

	// Last applied log to the FSM
	lastApplied uint64

	// Cache the latest snapshot index/term
	lastSnapshotIndex uint64
	lastSnapshotTerm  uint64

	// Tracks the number of live routines
	runningRoutines int32

	// The current state
	state RaftState
}

func (r *raftState) getState() RaftState {
	stateAddr := (*uint32)(&r.state)
	return RaftState(atomic.LoadUint32(stateAddr))
}

func (r *raftState) setState(s RaftState) {
	stateAddr := (*uint32)(&r.state)
	atomic.StoreUint32(stateAddr, uint32(s))
}

func (r *raftState) getCurrentTerm() uint64 {
	return atomic.LoadUint64(&r.currentTerm)
}

func (r *raftState) setCurrentTerm(term uint64) {
	atomic.StoreUint64(&r.currentTerm, term)
}

func (r *raftState) getLastLogIndex() uint64 {
	return atomic.LoadUint64(&r.LastLogIndex)
}

func (r *raftState) setLastLogIndex(term uint64) {
	atomic.StoreUint64(&r.LastLogIndex, term)
}

func (r *raftState) getLastLogTerm() uint64 {
	return atomic.LoadUint64(&r.LastLogTerm)
}

func (r *raftState) setLastLogTerm(term uint64) {
	atomic.StoreUint64(&r.LastLogTerm, term)
}

func (r *raftState) getCommitIndex() uint64 {
	return atomic.LoadUint64(&r.commitIndex)
}

func (r *raftState) setCommitIndex(term uint64) {
	atomic.StoreUint64(&r.commitIndex, term)
}

func (r *raftState) getLastApplied() uint64 {
	return atomic.LoadUint64(&r.lastApplied)
}

func (r *raftState) setLastApplied(term uint64) {
	atomic.StoreUint64(&r.lastApplied, term)
}

func (r *raftState) getLastSnapshotIndex() uint64 {
	return atomic.LoadUint64(&r.lastSnapshotIndex)
}

func (r *raftState) setLastSnapshotIndex(term uint64) {
	atomic.StoreUint64(&r.lastSnapshotIndex, term)
}

func (r *raftState) getLastSnapshotTerm() uint64 {
	return atomic.LoadUint64(&r.lastSnapshotTerm)
}

func (r *raftState) setLastSnapshotTerm(term uint64) {
	atomic.StoreUint64(&r.lastSnapshotTerm, term)
}

func (r *raftState) incrRoutines() {
	atomic.AddInt32(&r.runningRoutines, 1)
}

func (r *raftState) decrRoutines() {
	atomic.AddInt32(&r.runningRoutines, -1)
}

func (r *raftState) getRoutines() int32 {
	return atomic.LoadInt32(&r.runningRoutines)
}

// Start a goroutine and properly handle the race between a routine
// starting and incrementing, and exiting and decrementing.
func (r *raftState) goFunc(f func()) {
	r.incrRoutines()
	go func() {
		defer r.decrRoutines()
		f()
	}()
}

// getLastIndex returns the last index in stable storage.
// Either from the last log or from the last snapshot.
func (r *raftState) getLastIndex() uint64 {
	return max(r.getLastLogIndex(), r.getLastSnapshotIndex())
}

// getLastEntry returns the last index and term in stable storage.
// Either from the last log or from the last snapshot.
func (r *raftState) getLastEntry() (uint64, uint64) {
	if r.getLastLogIndex() >= r.getLastSnapshotIndex() {
		return r.getLastLogIndex(), r.getLastLogTerm()
	}
	return r.getLastSnapshotIndex(), r.getLastSnapshotTerm()
}
<commit_msg>state.go: Fix local index variables named 'term'<commit_after>package raft

import (
	"sync/atomic"
)

// RaftState captures the state of a Raft node: Follower, Candidate, Leader,
// or Shutdown.
type RaftState uint32

const (
	// Follower is the initial state of a Raft node.
	Follower RaftState = iota

	// Candidate is one of the valid states of a Raft node.
	Candidate

	// Leader is one of the valid states of a Raft node.
	Leader

	// Shutdown is the terminal state of a Raft node.
	Shutdown
)

func (s RaftState) String() string {
	switch s {
	case Follower:
		return "Follower"
	case Candidate:
		return "Candidate"
	case Leader:
		return "Leader"
	case Shutdown:
		return "Shutdown"
	default:
		return "Unknown"
	}
}

// raftState is used to maintain various state variables
// and provides an interface to set/get the variables in a
// thread safe manner.
type raftState struct {
	// The current term, cache of StableStore
	currentTerm uint64

	// Cache the latest log from LogStore
	LastLogIndex uint64
	LastLogTerm  uint64

	// Highest committed log entry
	commitIndex uint64

	// Last applied log to the FSM
	lastApplied uint64

	// Cache the latest snapshot index/term
	lastSnapshotIndex uint64
	lastSnapshotTerm  uint64

	// Tracks the number of live routines
	runningRoutines int32

	// The current state
	state RaftState
}

func (r *raftState) getState() RaftState {
	stateAddr := (*uint32)(&r.state)
	return RaftState(atomic.LoadUint32(stateAddr))
}

func (r *raftState) setState(s RaftState) {
	stateAddr := (*uint32)(&r.state)
	atomic.StoreUint32(stateAddr, uint32(s))
}

func (r *raftState) getCurrentTerm() uint64 {
	return atomic.LoadUint64(&r.currentTerm)
}

func (r *raftState) setCurrentTerm(term uint64) {
	atomic.StoreUint64(&r.currentTerm, term)
}

func (r *raftState) getLastLogIndex() uint64 {
	return atomic.LoadUint64(&r.LastLogIndex)
}

func (r *raftState) setLastLogIndex(index uint64) {
	atomic.StoreUint64(&r.LastLogIndex, index)
}

func (r *raftState) getLastLogTerm() uint64 {
	return atomic.LoadUint64(&r.LastLogTerm)
}

func (r *raftState) setLastLogTerm(term uint64) {
	atomic.StoreUint64(&r.LastLogTerm, term)
}

func (r *raftState) getCommitIndex() uint64 {
	return atomic.LoadUint64(&r.commitIndex)
}

func (r *raftState) setCommitIndex(index uint64) {
	atomic.StoreUint64(&r.commitIndex, index)
}

func (r *raftState) getLastApplied() uint64 {
	return atomic.LoadUint64(&r.lastApplied)
}

func (r *raftState) setLastApplied(index uint64) {
	atomic.StoreUint64(&r.lastApplied, index)
}

func (r *raftState) getLastSnapshotIndex() uint64 {
	return atomic.LoadUint64(&r.lastSnapshotIndex)
}

func (r *raftState) setLastSnapshotIndex(index uint64) {
	atomic.StoreUint64(&r.lastSnapshotIndex, index)
}

func (r *raftState) getLastSnapshotTerm() uint64 {
	return atomic.LoadUint64(&r.lastSnapshotTerm)
}

func (r *raftState) setLastSnapshotTerm(term uint64) {
	atomic.StoreUint64(&r.lastSnapshotTerm, term)
}

func (r *raftState) incrRoutines() {
	atomic.AddInt32(&r.runningRoutines, 1)
}

func (r *raftState) decrRoutines() {
	atomic.AddInt32(&r.runningRoutines, -1)
}

func (r *raftState) getRoutines() int32 {
	return atomic.LoadInt32(&r.runningRoutines)
}

// Start a goroutine and properly handle the race between a routine
// starting and incrementing, and exiting and decrementing.
func (r *raftState) goFunc(f func()) {
	r.incrRoutines()
	go func() {
		defer r.decrRoutines()
		f()
	}()
}

// getLastIndex returns the last index in stable storage.
// Either from the last log or from the last snapshot.
func (r *raftState) getLastIndex() uint64 {
	return max(r.getLastLogIndex(), r.getLastSnapshotIndex())
}

// getLastEntry returns the last index and term in stable storage.
// Either from the last log or from the last snapshot.
func (r *raftState) getLastEntry() (uint64, uint64) {
	if r.getLastLogIndex() >= r.getLastSnapshotIndex() {
		return r.getLastLogIndex(), r.getLastLogTerm()
	}
	return r.getLastSnapshotIndex(), r.getLastSnapshotTerm()
}
<|endoftext|>


<commit_before>package boardgame

import (
	"encoding/json"
)

//State represents the entire semantic state of a game at a given version. For
//your specific game, Game and Players will actually be concrete structs to
//your particular game. Games often define a top-level concreteStates()
//*myGameState, []*myPlayerState so at the top of methods that accept a State
//they can quickly get concrete, type-checked types with only a single
//conversion leap of faith at the top. States are intended to be read-only;
//methods where you are allowed to mutate the state (e.g. Move.Apply()) will
//take a MutableState instead as a signal that it is permissable to modify the
//state. That is why the states only return non-mutable states
//(PropertyReaders, not PropertyReadSetters, although realistically it is
//possible to cast them and modify directly. The MarshalJSON output of a State
//is appropriate for sending to a client or serializing a state to be put in
//storage. Given a blob serialized in that fashion, GameManager.StateFromBlob
//will return a state.
type State interface {
	//Game returns the GameState for this State
	Game() GameState
	//Players returns a slice of all PlayerStates for this State
	Players() []PlayerState
	//DynamicComponentValues returns a map of deck name to array of component
	//values, one per component in that deck.
	DynamicComponentValues() map[string][]DynamicComponentValues
	//Copy returns a deep copy of the State, including copied version of the Game
	//and Player States.
	Copy(sanitized bool) State
	//Diagram returns a basic, ascii rendering of the state for debug rendering.
	//It thunks out to Delegate.Diagram.
	Diagram() string
	//Santizied will return false if this is a full-fidelity State object, or
	//true if it has been sanitized, which means that some properties might be
	//hidden or otherwise altered. This should return true if the object was
	//created with Copy(true)
	Sanitized() bool
	//Computed returns the computed properties for this state.
	Computed() ComputedProperties
	//SanitizedForPlayer produces a copy state object that has been sanitized for
	//the player at the given index. The state object returned will have
	//Sanitized() return true. Will call GameDelegate.StateSanitizationPolicy to
	//retrieve the policy in place. See the package level comment for an overview
	//of how state sanitization works.
	SanitizedForPlayer(playerIndex int) State
}

//A MutableState is a state that is designed to be modified in place. These
//are passed to methods (instead of normal States) as a signal that
//modifications are intended to be done on the state.
type MutableState interface {
	//MutableState contains all of the methods of a read-only state.
	State
	//MutableGame is a reference to the MutableGameState for this MutableState.
	MutableGame() MutableGameState
	//MutablePlayers returns a slice of MutablePlayerStates for this MutableState.
	MutablePlayers() []MutablePlayerState
}

//state implements both State and MutableState, so it can always be passed for
//either, and what it's interpreted as is primarily a function of what the
//method signature is that it's passed to
type state struct {
	game                   MutableGameState
	players                []MutablePlayerState
	computed               *computedPropertiesImpl
	dynamicComponentValues map[string][]DynamicComponentValues
	sanitized              bool
	delegate               GameDelegate
}

func (s *state) MutableGame() MutableGameState {
	return s.game
}

func (s *state) MutablePlayers() []MutablePlayerState {
	return s.players
}

func (s *state) Game() GameState {
	return s.game
}

func (s *state) Players() []PlayerState {
	result := make([]PlayerState, len(s.players))
	for i := 0; i < len(s.players); i++ {
		result[i] = s.players[i]
	}
	return result
}

func (s *state) Copy(sanitized bool) State {
	return s.copy(sanitized)
}

func (s *state) copy(sanitized bool) *state {
	players := make([]MutablePlayerState, len(s.players))

	for i, player := range s.players {
		players[i] = player.MutableCopy()
	}

	result := &state{
		game:                   s.game.MutableCopy(),
		players:                players,
		dynamicComponentValues: make(map[string][]DynamicComponentValues),
		sanitized:              sanitized,
		delegate:               s.delegate,
	}

	//TODO: fix up stacks for component values

	//TODO: actually copy dynamic component states

	//FixUp stacks to make sure they point to this new state.
	if err := verifyReaderStacks(result.game.Reader(), result); err != nil {
		return nil
	}
	for _, player := range result.players {
		if err := verifyReaderStacks(player.Reader(), result); err != nil {
			return nil
		}
	}

	return result
}

func (s *state) MarshalJSON() ([]byte, error) {
	obj := map[string]interface{}{
		"Game":     s.game,
		"Players":  s.players,
		"Computed": s.Computed(),
	}

	dynamic := s.DynamicComponentValues()

	if dynamic != nil && len(dynamic) != 0 {
		obj["Components"] = dynamic
	}

	return json.Marshal(obj)
}

func (s *state) Diagram() string {
	return s.delegate.Diagram(s)
}

func (s *state) Sanitized() bool {
	return s.sanitized
}

func (s *state) DynamicComponentValues() map[string][]DynamicComponentValues {
	return s.dynamicComponentValues
}

func (s *state) Computed() ComputedProperties {
	if s.computed == nil {
		s.computed = newComputedPropertiesImpl(s.delegate.ComputedPropertiesConfig(), s)
	}
	return s.computed
}

func (s *state) SanitizedForPlayer(playerIndex int) State {

	//If the playerIndex isn't an actuall player's index, just return self.
	if playerIndex < 0 || playerIndex >= len(s.players) {
		return s
	}

	policy := s.delegate.StateSanitizationPolicy()

	if policy == nil {
		policy = &StatePolicy{}
	}

	sanitized := s.copy(true)

	sanitizeStateObj(sanitized.game.ReadSetter(), policy.Game, -1, playerIndex, PolicyVisible)

	playerStates := sanitized.players

	for i := 0; i < len(playerStates); i++ {
		sanitizeStateObj(playerStates[i].ReadSetter(), policy.Player, i, playerIndex, PolicyVisible)
	}

	return sanitized

}

//sanitizedWithExceptions will return a Sanitized() State where properties
//that are not in the passed policy are treated as PolicyRandom. Useful in
//computing properties.
func (s *state) sanitizedWithExceptions(policy *StatePolicy) State {

	sanitized := s.copy(true)

	sanitizeStateObj(sanitized.game.ReadSetter(), policy.Game, -1, -1, PolicyRandom)

	playerStates := sanitized.players

	for i := 0; i < len(playerStates); i++ {
		sanitizeStateObj(playerStates[i].ReadSetter(), policy.Player, -1, -1, PolicyRandom)
	}

	return sanitized

}

//BaseState is the interface that all state objects--PlayerStates and GameStates
//--implement.
type BaseState interface {
	Reader() PropertyReader
}

//MutableBaseState is the interface that Mutable{Game,Player}State's
//implement.
type MutableBaseState interface {
	ReadSetter() PropertyReadSetter
}

//PlayerState represents the state of a game associated with a specific user.
type PlayerState interface {
	//PlayerIndex encodes the index this user's state is in the containing
	//state object.
	PlayerIndex() int
	//Copy produces a copy of our current state. Be sure it's a deep copy that
	//makes a copy of any pointer arguments.
	Copy() PlayerState
	BaseState
}

//A MutablePlayerState is a PlayerState that is allowed to be mutated.
type MutablePlayerState interface {
	PlayerState
	MutableCopy() MutablePlayerState
	MutableBaseState
}

//GameState represents the state of a game that is not associated with a
//particular user. For example, the draw stack of cards, who the current
//player is, and other properites.
type GameState interface {
	//Copy returns a copy of our current state. Be sure it's a deep copy that
	//makes a copy of any pointer arguments.
	Copy() GameState
	BaseState
}

//A MutableGameState is a GameState that is allowed to be mutated.
type MutableGameState interface {
	GameState
	MutableCopy() MutableGameState
	MutableBaseState
}

//DefaultMarshalJSON is a simple wrapper around json.MarshalIndent, with the
//right defaults set. If your structs need to implement MarshaLJSON to output
//JSON, use this to encode it.
func DefaultMarshalJSON(obj interface{}) ([]byte, error) {
	return json.MarshalIndent(obj, "", "  ")
}
<commit_msg>state.Copy() actually copies dynamic component values<commit_after>package boardgame

import (
	"encoding/json"
)

//State represents the entire semantic state of a game at a given version. For
//your specific game, Game and Players will actually be concrete structs to
//your particular game. Games often define a top-level concreteStates()
//*myGameState, []*myPlayerState so at the top of methods that accept a State
//they can quickly get concrete, type-checked types with only a single
//conversion leap of faith at the top. States are intended to be read-only;
//methods where you are allowed to mutate the state (e.g. Move.Apply()) will
//take a MutableState instead as a signal that it is permissable to modify the
//state. That is why the states only return non-mutable states
//(PropertyReaders, not PropertyReadSetters, although realistically it is
//possible to cast them and modify directly. The MarshalJSON output of a State
//is appropriate for sending to a client or serializing a state to be put in
//storage. Given a blob serialized in that fashion, GameManager.StateFromBlob
//will return a state.
type State interface {
	//Game returns the GameState for this State
	Game() GameState
	//Players returns a slice of all PlayerStates for this State
	Players() []PlayerState
	//DynamicComponentValues returns a map of deck name to array of component
	//values, one per component in that deck.
	DynamicComponentValues() map[string][]DynamicComponentValues
	//Copy returns a deep copy of the State, including copied version of the Game
	//and Player States.
	Copy(sanitized bool) State
	//Diagram returns a basic, ascii rendering of the state for debug rendering.
	//It thunks out to Delegate.Diagram.
	Diagram() string
	//Santizied will return false if this is a full-fidelity State object, or
	//true if it has been sanitized, which means that some properties might be
	//hidden or otherwise altered. This should return true if the object was
	//created with Copy(true)
	Sanitized() bool
	//Computed returns the computed properties for this state.
	Computed() ComputedProperties
	//SanitizedForPlayer produces a copy state object that has been sanitized for
	//the player at the given index. The state object returned will have
	//Sanitized() return true. Will call GameDelegate.StateSanitizationPolicy to
	//retrieve the policy in place. See the package level comment for an overview
	//of how state sanitization works.
	SanitizedForPlayer(playerIndex int) State
}

//A MutableState is a state that is designed to be modified in place. These
//are passed to methods (instead of normal States) as a signal that
//modifications are intended to be done on the state.
type MutableState interface {
	//MutableState contains all of the methods of a read-only state.
	State
	//MutableGame is a reference to the MutableGameState for this MutableState.
	MutableGame() MutableGameState
	//MutablePlayers returns a slice of MutablePlayerStates for this MutableState.
	MutablePlayers() []MutablePlayerState
}

//state implements both State and MutableState, so it can always be passed for
//either, and what it's interpreted as is primarily a function of what the
//method signature is that it's passed to
type state struct {
	game                   MutableGameState
	players                []MutablePlayerState
	computed               *computedPropertiesImpl
	dynamicComponentValues map[string][]DynamicComponentValues
	sanitized              bool
	delegate               GameDelegate
}

func (s *state) MutableGame() MutableGameState {
	return s.game
}

func (s *state) MutablePlayers() []MutablePlayerState {
	return s.players
}

func (s *state) Game() GameState {
	return s.game
}

func (s *state) Players() []PlayerState {
	result := make([]PlayerState, len(s.players))
	for i := 0; i < len(s.players); i++ {
		result[i] = s.players[i]
	}
	return result
}

func (s *state) Copy(sanitized bool) State {
	return s.copy(sanitized)
}

func (s *state) copy(sanitized bool) *state {
	players := make([]MutablePlayerState, len(s.players))

	for i, player := range s.players {
		players[i] = player.MutableCopy()
	}

	result := &state{
		game:                   s.game.MutableCopy(),
		players:                players,
		dynamicComponentValues: make(map[string][]DynamicComponentValues),
		sanitized:              sanitized,
		delegate:               s.delegate,
	}

	//TODO: fix up stacks for component values

	for deckName, values := range s.dynamicComponentValues {
		arr := make([]DynamicComponentValues, len(values))
		for i := 0; i < len(values); i++ {
			arr[i] = values[i].Copy()
		}
		result.dynamicComponentValues[deckName] = arr
	}

	//FixUp stacks to make sure they point to this new state.
	if err := verifyReaderStacks(result.game.Reader(), result); err != nil {
		return nil
	}
	for _, player := range result.players {
		if err := verifyReaderStacks(player.Reader(), result); err != nil {
			return nil
		}
	}

	return result
}

func (s *state) MarshalJSON() ([]byte, error) {
	obj := map[string]interface{}{
		"Game":     s.game,
		"Players":  s.players,
		"Computed": s.Computed(),
	}

	dynamic := s.DynamicComponentValues()

	if dynamic != nil && len(dynamic) != 0 {
		obj["Components"] = dynamic
	}

	return json.Marshal(obj)
}

func (s *state) Diagram() string {
	return s.delegate.Diagram(s)
}

func (s *state) Sanitized() bool {
	return s.sanitized
}

func (s *state) DynamicComponentValues() map[string][]DynamicComponentValues {
	return s.dynamicComponentValues
}

func (s *state) Computed() ComputedProperties {
	if s.computed == nil {
		s.computed = newComputedPropertiesImpl(s.delegate.ComputedPropertiesConfig(), s)
	}
	return s.computed
}

func (s *state) SanitizedForPlayer(playerIndex int) State {

	//If the playerIndex isn't an actuall player's index, just return self.
	if playerIndex < 0 || playerIndex >= len(s.players) {
		return s
	}

	policy := s.delegate.StateSanitizationPolicy()

	if policy == nil {
		policy = &StatePolicy{}
	}

	sanitized := s.copy(true)

	sanitizeStateObj(sanitized.game.ReadSetter(), policy.Game, -1, playerIndex, PolicyVisible)

	playerStates := sanitized.players

	for i := 0; i < len(playerStates); i++ {
		sanitizeStateObj(playerStates[i].ReadSetter(), policy.Player, i, playerIndex, PolicyVisible)
	}

	return sanitized

}

//sanitizedWithExceptions will return a Sanitized() State where properties
//that are not in the passed policy are treated as PolicyRandom. Useful in
//computing properties.
func (s *state) sanitizedWithExceptions(policy *StatePolicy) State {

	sanitized := s.copy(true)

	sanitizeStateObj(sanitized.game.ReadSetter(), policy.Game, -1, -1, PolicyRandom)

	playerStates := sanitized.players

	for i := 0; i < len(playerStates); i++ {
		sanitizeStateObj(playerStates[i].ReadSetter(), policy.Player, -1, -1, PolicyRandom)
	}

	return sanitized

}

//BaseState is the interface that all state objects--PlayerStates and GameStates
//--implement.
type BaseState interface {
	Reader() PropertyReader
}

//MutableBaseState is the interface that Mutable{Game,Player}State's
//implement.
type MutableBaseState interface {
	ReadSetter() PropertyReadSetter
}

//PlayerState represents the state of a game associated with a specific user.
type PlayerState interface {
	//PlayerIndex encodes the index this user's state is in the containing
	//state object.
	PlayerIndex() int
	//Copy produces a copy of our current state. Be sure it's a deep copy that
	//makes a copy of any pointer arguments.
	Copy() PlayerState
	BaseState
}

//A MutablePlayerState is a PlayerState that is allowed to be mutated.
type MutablePlayerState interface {
	PlayerState
	MutableCopy() MutablePlayerState
	MutableBaseState
}

//GameState represents the state of a game that is not associated with a
//particular user. For example, the draw stack of cards, who the current
//player is, and other properites.
type GameState interface {
	//Copy returns a copy of our current state. Be sure it's a deep copy that
	//makes a copy of any pointer arguments.
	Copy() GameState
	BaseState
}

//A MutableGameState is a GameState that is allowed to be mutated.
type MutableGameState interface {
	GameState
	MutableCopy() MutableGameState
	MutableBaseState
}

//DefaultMarshalJSON is a simple wrapper around json.MarshalIndent, with the
//right defaults set. If your structs need to implement MarshaLJSON to output
//JSON, use this to encode it.
func DefaultMarshalJSON(obj interface{}) ([]byte, error) {
	return json.MarshalIndent(obj, "", "  ")
}
<|endoftext|>


<commit_before>package jobsrunner

import (
	"context"
	"fmt"
	"log"
	"sync"
	"time"
)

// State of jobrunner.
type State struct {
	Conf Config
	//Logger log.Logger

	ctx context.Context
}

// NewFromFile creates State with Config parsed from the specified file.
func NewFromFile(filename string) (State, error) {
	conf, err := NewConfigFromFile(filename)
	if err != nil {
		return State{}, fmt.Errorf("can't create config: %s", err)
	}
	return State{
		Conf: conf,
	}, nil
}

// Run the configured jobs.
func (s *State) Run(ctx context.Context) {
	var wg sync.WaitGroup
	s.ctx = ctx
	for i, job := range s.Conf.Jobs {
		wg.Add(1)
		go func() {
			s.startJob(job)
			// TODO: Logger dependency.
			log.Printf("Job #%d finished", i)
			wg.Done()
		}()
	}
	wg.Wait()
}

func (s *State) startJob(job ConfigJob) {
	// TODO: Run CMD and output the response in case of a non-zero exit code.
	// TODO: Logger dependency.
	log.Println(job.Cmd)
	for {
		select {
		case <-s.ctx.Done():
			return
		case <-time.Tick(time.Duration(job.Interval)):
			// TODO: Run CMD and output the response in case of a non-zero exit code.
			// TODO: Logger dependency.
			log.Println(job.Cmd)
		}
	}
}
<commit_msg>Renamed State to Runtime<commit_after>package jobsrunner

import (
	"context"
	"fmt"
	"log"
	"sync"
	"time"
)

// Runtime of jobrunner.
type Runtime struct {
	Conf Config
	//Logger log.Logger

	ctx context.Context
}

// NewFromFile creates Runtime with Config parsed from the specified file.
func NewFromFile(filename string) (Runtime, error) {
	conf, err := NewConfigFromFile(filename)
	if err != nil {
		return Runtime{}, fmt.Errorf("can't create config: %s", err)
	}
	return Runtime{
		Conf: conf,
	}, nil
}

// Run the configured jobs.
func (r *Runtime) Run(ctx context.Context) {
	var wg sync.WaitGroup
	r.ctx = ctx
	for i, job := range r.Conf.Jobs {
		wg.Add(1)
		go func() {
			r.startJob(job)
			// TODO: Logger dependency.
			log.Printf("Job #%d finished", i)
			wg.Done()
		}()
	}
	wg.Wait()
}

func (r *Runtime) startJob(job ConfigJob) {
	// TODO: Run CMD and output the response in case of a non-zero exit code.
	// TODO: Logger dependency.
	log.Println(job.Cmd)
	for {
		select {
		case <-r.ctx.Done():
			return
		case <-time.Tick(time.Duration(job.Interval)):
			// TODO: Run CMD and output the response in case of a non-zero exit code.
			// TODO: Logger dependency.
			log.Println(job.Cmd)
		}
	}
}
<|endoftext|>


<commit_before>package templar

import (
	"fmt"
	"github.com/amir/raidman"
	"net/http"
	"strings"
	"time"
)

type DebugStats struct{}

func (d *DebugStats) StartRequest(req *http.Request) {
	fmt.Printf("[%s] S %s %s\n", time.Now().Format(time.RFC3339Nano), req.Method, req.URL)
}

func (d *DebugStats) Emit(req *http.Request, dur time.Duration) {
	fmt.Printf("[%s] E %s %s (%s)\n", time.Now().Format(time.RFC3339Nano), req.Method, req.URL, dur)
}

func (d *DebugStats) RequestTimeout(req *http.Request, timeout time.Duration) {
	fmt.Printf("[%s] T %s %s (%s)\n", time.Now().Format(time.RFC3339Nano), req.Method, req.URL, timeout)
}

var _ = Stats(&DebugStats{})

type StatsdOutput struct {
	client StatsdClient
}

var _ = Stats(&StatsdOutput{})

func NewStatsdOutput(client StatsdClient) *StatsdOutput {
	return &StatsdOutput{client}
}

func (s *StatsdOutput) url(req *http.Request) string {
	return req.Host + strings.Replace(req.URL.Path, "/", "-", -1)
}

func (s *StatsdOutput) StartRequest(req *http.Request) {
	s.client.Incr("templar.request.method."+req.Method, 1)
	s.client.Incr("templar.request.host."+req.Host, 1)
	s.client.Incr("templar.request.url."+s.url(req), 1)
	s.client.GaugeDelta("templar.requests.active", 1)
}

func (s *StatsdOutput) Emit(req *http.Request, delta time.Duration) {
	s.client.GaugeDelta("templar.requests.active", -1)
	s.client.PrecisionTiming("templar.request.url."+s.url(req), delta)
}

func (s *StatsdOutput) RequestTimeout(req *http.Request, timeout time.Duration) {
	s.client.Incr("templar.timeout.host."+req.Host, 1)
	s.client.Incr("templar.timeout.url."+s.url(req), 1)
}

type RiemannOutput struct {
	client RiemannClient
}

func NewRiemannOutput(client RiemannClient) *RiemannOutput {
	return &RiemannOutput{client}
}

func (r *RiemannOutput) StartRequest(req *http.Request) {
	attributes := make(map[string]string)
	attributes["method"] = req.Method
	attributes["host"] = req.Host
	attributes["path"] = req.URL.Path
	var event = &raidman.Event{
		State:      "ok",
		Service:    "templar request",
		Metric:     1,
		Attributes: attributes,
	}
	r.client.Send(event)
}

func (r *RiemannOutput) Emit(req *http.Request, delta time.Duration) {
	attributes := make(map[string]string)
	attributes["method"] = req.Method
	attributes["host"] = req.Host
	attributes["path"] = req.URL.Path
	var event = &raidman.Event{
		State:      "ok",
		Service:    "templar response",
		Metric:     1000.0 * delta.Seconds(),
		Attributes: attributes,
	}
	r.client.Send(event)
}

func (r *RiemannOutput) RequestTimeout(req *http.Request, timeout time.Duration) {
	attributes := make(map[string]string)
	attributes["method"] = req.Method
	attributes["host"] = req.Host
	attributes["path"] = req.URL.Path
	var event = &raidman.Event{
		State:      "warning",
		Service:    "templar timeout",
		Metric:     timeout.Seconds() * 1000.0,
		Attributes: attributes,
	}
	r.client.Send(event)
}

type MultiStats []Stats

var _ = Stats(MultiStats{})

func (m MultiStats) StartRequest(req *http.Request) {
	for _, s := range m {
		s.StartRequest(req)
	}
}

func (m MultiStats) Emit(req *http.Request, t time.Duration) {
	for _, s := range m {
		s.Emit(req, t)
	}
}

func (m MultiStats) RequestTimeout(req *http.Request, timeout time.Duration) {
	for _, s := range m {
		s.RequestTimeout(req, timeout)
	}
}
<commit_msg>gofmt<commit_after>package templar

import (
	"fmt"
	"net/http"
	"strings"
	"time"

	"github.com/amir/raidman"
)

type DebugStats struct{}

func (d *DebugStats) StartRequest(req *http.Request) {
	fmt.Printf("[%s] S %s %s\n", time.Now().Format(time.RFC3339Nano), req.Method, req.URL)
}

func (d *DebugStats) Emit(req *http.Request, dur time.Duration) {
	fmt.Printf("[%s] E %s %s (%s)\n", time.Now().Format(time.RFC3339Nano), req.Method, req.URL, dur)
}

func (d *DebugStats) RequestTimeout(req *http.Request, timeout time.Duration) {
	fmt.Printf("[%s] T %s %s (%s)\n", time.Now().Format(time.RFC3339Nano), req.Method, req.URL, timeout)
}

var _ = Stats(&DebugStats{})

type StatsdOutput struct {
	client StatsdClient
}

var _ = Stats(&StatsdOutput{})

func NewStatsdOutput(client StatsdClient) *StatsdOutput {
	return &StatsdOutput{client}
}

func (s *StatsdOutput) url(req *http.Request) string {
	return req.Host + strings.Replace(req.URL.Path, "/", "-", -1)
}

func (s *StatsdOutput) StartRequest(req *http.Request) {
	s.client.Incr("templar.request.method."+req.Method, 1)
	s.client.Incr("templar.request.host."+req.Host, 1)
	s.client.Incr("templar.request.url."+s.url(req), 1)
	s.client.GaugeDelta("templar.requests.active", 1)
}

func (s *StatsdOutput) Emit(req *http.Request, delta time.Duration) {
	s.client.GaugeDelta("templar.requests.active", -1)
	s.client.PrecisionTiming("templar.request.url."+s.url(req), delta)
}

func (s *StatsdOutput) RequestTimeout(req *http.Request, timeout time.Duration) {
	s.client.Incr("templar.timeout.host."+req.Host, 1)
	s.client.Incr("templar.timeout.url."+s.url(req), 1)
}

type RiemannOutput struct {
	client RiemannClient
}

func NewRiemannOutput(client RiemannClient) *RiemannOutput {
	return &RiemannOutput{client}
}

func (r *RiemannOutput) StartRequest(req *http.Request) {
	attributes := make(map[string]string)
	attributes["method"] = req.Method
	attributes["host"] = req.Host
	attributes["path"] = req.URL.Path
	var event = &raidman.Event{
		State:      "ok",
		Service:    "templar request",
		Metric:     1,
		Attributes: attributes,
	}
	r.client.Send(event)
}

func (r *RiemannOutput) Emit(req *http.Request, delta time.Duration) {
	attributes := make(map[string]string)
	attributes["method"] = req.Method
	attributes["host"] = req.Host
	attributes["path"] = req.URL.Path
	var event = &raidman.Event{
		State:      "ok",
		Service:    "templar response",
		Metric:     1000.0 * delta.Seconds(),
		Attributes: attributes,
	}
	r.client.Send(event)
}

func (r *RiemannOutput) RequestTimeout(req *http.Request, timeout time.Duration) {
	attributes := make(map[string]string)
	attributes["method"] = req.Method
	attributes["host"] = req.Host
	attributes["path"] = req.URL.Path
	var event = &raidman.Event{
		State:      "warning",
		Service:    "templar timeout",
		Metric:     timeout.Seconds() * 1000.0,
		Attributes: attributes,
	}
	r.client.Send(event)
}

type MultiStats []Stats

var _ = Stats(MultiStats{})

func (m MultiStats) StartRequest(req *http.Request) {
	for _, s := range m {
		s.StartRequest(req)
	}
}

func (m MultiStats) Emit(req *http.Request, t time.Duration) {
	for _, s := range m {
		s.Emit(req, t)
	}
}

func (m MultiStats) RequestTimeout(req *http.Request, timeout time.Duration) {
	for _, s := range m {
		s.RequestTimeout(req, timeout)
	}
}
<|endoftext|>