<commit_before>package puppetserver

import (
	"github.com/mitchellh/packer/packer"
	"io/ioutil"
	"os"
	"testing"
)

func testConfig() map[string]interface{} {
	tf, err := ioutil.TempFile("", "packer")
	if err != nil {
		panic(err)
	}

	return map[string]interface{}{
		"puppet_server": tf.Name(),
	}
}

func TestProvisioner_Impl(t *testing.T) {
	var raw interface{}
	raw = &Provisioner{}
	if _, ok := raw.(packer.Provisioner); !ok {
		t.Fatalf("must be a Provisioner")
	}
}

func TestProvisionerPrepare_puppetBinDir(t *testing.T) {
	config := testConfig()

	delete(config, "puppet_bin_dir")
	p := new(Provisioner)
	err := p.Prepare(config)
	if err != nil {
		t.Fatalf("err: %s", err)
	}

	// Test with a good one
	tf, err := ioutil.TempFile("", "packer")
	if err != nil {
		t.Fatalf("error tempfile: %s", err)
	}
	defer os.Remove(tf.Name())

	config["puppet_bin_dir"] = tf.Name()
	p = new(Provisioner)
	err = p.Prepare(config)
	if err != nil {
		t.Fatalf("err: %s", err)
	}
}

func TestProvisionerPrepare_clientPrivateKeyPath(t *testing.T) {
	config := testConfig()

	delete(config, "client_private_key_path")
	p := new(Provisioner)
	err := p.Prepare(config)
	if err != nil {
		t.Fatalf("err: %s", err)
	}

	// Test with bad paths
	config["client_private_key_path"] = "i-should-not-exist"
	p = new(Provisioner)
	err = p.Prepare(config)
	if err == nil {
		t.Fatal("should be an error")
	}

	// Test with a good one
	td, err := ioutil.TempDir("", "packer")
	if err != nil {
		t.Fatalf("error: %s", err)
	}
	defer os.RemoveAll(td)

	config["client_private_key_path"] = td
	p = new(Provisioner)
	err = p.Prepare(config)
	if err != nil {
		t.Fatalf("err: %s", err)
	}
}

func TestProvisionerPrepare_clientCertPath(t *testing.T) {
	config := testConfig()

	delete(config, "client_cert_path")
	p := new(Provisioner)
	err := p.Prepare(config)
	if err != nil {
		t.Fatalf("err: %s", err)
	}

	// Test with bad paths
	config["client_cert_path"] = "i-should-not-exist"
	p = new(Provisioner)
	err = p.Prepare(config)
	if err == nil {
		t.Fatal("should be an error")
	}

	// Test with a good one
	td, err := ioutil.TempDir("", "packer")
	if err != nil {
		t.Fatalf("error: %s", err)
	}
	defer os.RemoveAll(td)

	config["client_cert_path"] = td
	p = new(Provisioner)
	err = p.Prepare(config)
	if err != nil {
		t.Fatalf("err: %s", err)
	}
}

func TestProvisionerPrepare_facterFacts(t *testing.T) {
	config := testConfig()

	delete(config, "facter")
	p := new(Provisioner)
	err := p.Prepare(config)
	if err != nil {
		t.Fatalf("err: %s", err)
	}

	// Test with malformed fact
	config["facter"] = "fact=stringified"
	p = new(Provisioner)
	err = p.Prepare(config)
	if err == nil {
		t.Fatal("should be an error")
	}

	// Test with a good one
	td, err := ioutil.TempDir("", "packer")
	if err != nil {
		t.Fatalf("error: %s", err)
	}
	defer os.RemoveAll(td)

	facts := make(map[string]string)
	facts["fact_name"] = "fact_value"
	config["facter"] = facts

	p = new(Provisioner)
	err = p.Prepare(config)
	if err != nil {
		t.Fatalf("err: %s", err)
	}

	// Make sure the default facts are present
	delete(config, "facter")
	p = new(Provisioner)
	err = p.Prepare(config)
	if p.config.Facter == nil {
		t.Fatalf("err: Default facts are not set in the Puppet provisioner!")
	}

	if _, ok := p.config.Facter["packer_build_name"]; !ok {
		t.Fatalf("err: packer_build_name fact not set in the Puppet provisioner!")
	}

	if _, ok := p.config.Facter["packer_builder_type"]; !ok {
		t.Fatalf("err: packer_builder_type fact not set in the Puppet provisioner!")
	}
}
<commit_msg>Add additional unit tests for puppet-server provisioner<commit_after>package puppetserver

import (
	"github.com/mitchellh/packer/packer"
	"io/ioutil"
	"os"
	"testing"
)

func testConfig() map[string]interface{} {
	tf, err := ioutil.TempFile("", "packer")
	if err != nil {
		panic(err)
	}

	return map[string]interface{}{
		"puppet_server": tf.Name(),
	}
}

func TestProvisioner_Impl(t *testing.T) {
	var raw interface{}
	raw = &Provisioner{}
	if _, ok := raw.(packer.Provisioner); !ok {
		t.Fatalf("must be a Provisioner")
	}
}

func TestProvisionerPrepare_puppetBinDir(t *testing.T) {
	config := testConfig()

	delete(config, "puppet_bin_dir")
	p := new(Provisioner)
	err := p.Prepare(config)
	if err != nil {
		t.Fatalf("err: %s", err)
	}

	// Test with a good one
	tf, err := ioutil.TempFile("", "packer")
	if err != nil {
		t.Fatalf("error tempfile: %s", err)
	}
	defer os.Remove(tf.Name())

	config["puppet_bin_dir"] = tf.Name()
	p = new(Provisioner)
	err = p.Prepare(config)
	if err != nil {
		t.Fatalf("err: %s", err)
	}
}

func TestProvisionerPrepare_clientPrivateKeyPath(t *testing.T) {
	config := testConfig()

	delete(config, "client_private_key_path")
	p := new(Provisioner)
	err := p.Prepare(config)
	if err != nil {
		t.Fatalf("err: %s", err)
	}

	// Test with bad paths
	config["client_private_key_path"] = "i-should-not-exist"
	p = new(Provisioner)
	err = p.Prepare(config)
	if err == nil {
		t.Fatal("should be an error")
	}

	// Test with a good one
	td, err := ioutil.TempDir("", "packer")
	if err != nil {
		t.Fatalf("error: %s", err)
	}
	defer os.RemoveAll(td)

	config["client_private_key_path"] = td
	p = new(Provisioner)
	err = p.Prepare(config)
	if err != nil {
		t.Fatalf("err: %s", err)
	}
}

func TestProvisionerPrepare_clientCertPath(t *testing.T) {
	config := testConfig()

	delete(config, "client_cert_path")
	p := new(Provisioner)
	err := p.Prepare(config)
	if err != nil {
		t.Fatalf("err: %s", err)
	}

	// Test with bad paths
	config["client_cert_path"] = "i-should-not-exist"
	p = new(Provisioner)
	err = p.Prepare(config)
	if err == nil {
		t.Fatal("should be an error")
	}

	// Test with a good one
	td, err := ioutil.TempDir("", "packer")
	if err != nil {
		t.Fatalf("error: %s", err)
	}
	defer os.RemoveAll(td)

	config["client_cert_path"] = td
	p = new(Provisioner)
	err = p.Prepare(config)
	if err != nil {
		t.Fatalf("err: %s", err)
	}
}

func TestProvisionerPrepare_executeCommand(t *testing.T) {
	config := testConfig()

	delete(config, "execute_command")
	p := new(Provisioner)
	err := p.Prepare(config)
	if err != nil {
		t.Fatalf("err: %s", err)
	}
}

func TestProvisionerPrepare_facterFacts(t *testing.T) {
	config := testConfig()

	delete(config, "facter")
	p := new(Provisioner)
	err := p.Prepare(config)
	if err != nil {
		t.Fatalf("err: %s", err)
	}

	// Test with malformed fact
	config["facter"] = "fact=stringified"
	p = new(Provisioner)
	err = p.Prepare(config)
	if err == nil {
		t.Fatal("should be an error")
	}

	// Test with a good one
	td, err := ioutil.TempDir("", "packer")
	if err != nil {
		t.Fatalf("error: %s", err)
	}
	defer os.RemoveAll(td)

	facts := make(map[string]string)
	facts["fact_name"] = "fact_value"
	config["facter"] = facts

	p = new(Provisioner)
	err = p.Prepare(config)
	if err != nil {
		t.Fatalf("err: %s", err)
	}

	// Make sure the default facts are present
	delete(config, "facter")
	p = new(Provisioner)
	err = p.Prepare(config)
	if p.config.Facter == nil {
		t.Fatalf("err: Default facts are not set in the Puppet provisioner!")
	}

	if _, ok := p.config.Facter["packer_build_name"]; !ok {
		t.Fatalf("err: packer_build_name fact not set in the Puppet provisioner!")
	}

	if _, ok := p.config.Facter["packer_builder_type"]; !ok {
		t.Fatalf("err: packer_builder_type fact not set in the Puppet provisioner!")
	}
}

func TestProvisionerPrepare_stagingDir(t *testing.T) {
	config := testConfig()

	delete(config, "staging_dir")
	p := new(Provisioner)
	err := p.Prepare(config)
	if err != nil {
		t.Fatalf("err: %s", err)
	}

	// Make sure the default staging directory is correct
	if p.config.StagingDir != "/tmp/packer-puppet-server" {
		t.Fatalf("err: Default staging_dir is not set in the Puppet provisioner!")
	}

	// Make sure default staging directory can be overridden
	config["staging_dir"] = "/tmp/override"
	p = new(Provisioner)
	err = p.Prepare(config)
	if err != nil {
		t.Fatalf("err: %s", err)
	}

	if p.config.StagingDir != "/tmp/override" {
		t.Fatalf("err: Overridden staging_dir is not set correctly in the Puppet provisioner!")
	}
}
<|endoftext|>


<commit_before>package main

import (
	"bytes"
	"log"
	"net/http"
	"os/exec"
	"strconv"
	"strings"
	"time"
)

func AddReminder() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		thenDay := r.URL.Query().Get("day")
		thenTime := r.URL.Query().Get("time")
		if thenTime == "" {
			http.Error(w, "Example: provide a `time=1330` param", http.StatusBadRequest)
			return
		}
		delay, err := timeToSleepFor(thenDay, thenTime)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		w.WriteHeader(http.StatusOK)
		w.Write([]byte("Your command has been added to the queue. Thank you."))
		go func(time.Duration) {
			select {
			case <-time.After(delay):
				execute()
			}
		}(delay)
	})
}

func timeToSleepFor(thenDay, thenTime string) (time.Duration, error) {
	then, err := thenAsUnix(thenDay, thenTime)
	if err != nil {
		return 0, err
	}
	now, err := nowAsUnix()
	if err != nil {
		return 0, err
	}
	return (time.Duration(then-now) * time.Second), nil
}

func nowAsUnix() (int64, error) {
	cmd := exec.Command("date", "+%s")
	var cmdResult bytes.Buffer
	cmd.Stdout = &cmdResult
	err := cmd.Run()
	if err != nil {
		return 0, err
	}
	now, err := cmdResultToInt(cmdResult)
	if err != nil {
		return 0, err
	}
	return now, nil
}

func thenAsUnix(thenDay, thenTime string) (int64, error) {
	cmd := exec.Command("date", "--date", thenDay+" "+thenTime, "+%s")
	var cmdResult bytes.Buffer
	cmd.Stdout = &cmdResult
	err := cmd.Run()
	if err != nil {
		return 0, err
	}
	then, err := cmdResultToInt(cmdResult)
	if err != nil {
		return 0, err
	}
	return then, nil
}

func cmdResultToInt(cmdResult bytes.Buffer) (int64, error) {
	result, err := cmdResult.ReadString('\n')
	if err != nil {
		return 0, err
	}
	resultAsUnixTime, err := strconv.Atoi(strings.TrimSuffix(result, "\n"))
	if err != nil {
		return 0, err
	}
	return int64(resultAsUnixTime), nil

}

func execute() {
	cmd := exec.Command("~/scripts/pushover.sh")
	var cmdResult bytes.Buffer
	cmd.Stdout = &cmdResult
	err := cmd.Run()
	if err != nil {
		log.Fatal(err)
	}
	return
}
<commit_msg>Add a message to a reminder<commit_after>package main

import (
	"bytes"
	"log"
	"net/http"
	"os/exec"
	"strconv"
	"strings"
	"time"
)

func AddReminder() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		thenDay := r.URL.Query().Get("day")
		thenTime := r.URL.Query().Get("time")
		message := r.URL.Query().Get("message")
		if thenTime == "" {
			http.Error(w, "Example: provide a `time=1330` param", http.StatusBadRequest)
			return
		}
		if message == "" {
			http.Error(w, "Example: provide a `message=buy-beer` param", http.StatusBadRequest)
			return
		}
		delay, err := timeToSleepFor(thenDay, thenTime)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		w.WriteHeader(http.StatusOK)
		w.Write([]byte("Your command has been added to the queue. Thank you."))
		go func(time.Duration, string) {
			select {
			case <-time.After(delay):
				execute(message)
			}
		}(delay, message)
	})
}

func timeToSleepFor(thenDay, thenTime string) (time.Duration, error) {
	then, err := thenAsUnix(thenDay, thenTime)
	if err != nil {
		return 0, err
	}
	now, err := nowAsUnix()
	if err != nil {
		return 0, err
	}
	return (time.Duration(then-now) * time.Second), nil
}

func nowAsUnix() (int64, error) {
	cmd := exec.Command("date", "+%s")
	var cmdResult bytes.Buffer
	cmd.Stdout = &cmdResult
	err := cmd.Run()
	if err != nil {
		return 0, err
	}
	now, err := cmdResultToInt(cmdResult)
	if err != nil {
		return 0, err
	}
	return now, nil
}

func thenAsUnix(thenDay, thenTime string) (int64, error) {
	cmd := exec.Command("date", "--date", thenDay+" "+thenTime, "+%s")
	var cmdResult bytes.Buffer
	cmd.Stdout = &cmdResult
	err := cmd.Run()
	if err != nil {
		return 0, err
	}
	then, err := cmdResultToInt(cmdResult)
	if err != nil {
		return 0, err
	}
	return then, nil
}

func cmdResultToInt(cmdResult bytes.Buffer) (int64, error) {
	result, err := cmdResult.ReadString('\n')
	if err != nil {
		return 0, err
	}
	resultAsUnixTime, err := strconv.Atoi(strings.TrimSuffix(result, "\n"))
	if err != nil {
		return 0, err
	}
	return int64(resultAsUnixTime), nil

}

func execute(msg string) {
	cmd := exec.Command("~/scripts/pushover.sh", msg)
	var cmdResult bytes.Buffer
	cmd.Stdout = &cmdResult
	err := cmd.Run()
	if err != nil {
		log.Fatal(err)
	}
	return
}
<|endoftext|>


<commit_before>package goboots

import (
	"labix.org/v2/mgo/bson"
	"log"
	"reflect"
	"time"
)

const (
	QueryOpEq                  = "$eq"
	QueryOpLessOrEq            = "$lte"
	QueryOpGreaterOrEq         = "$gte"
	QueryOpLess                = "$lt"
	QueryOpGreater             = "$gt"
	QueryOpNotEqual            = "$ne"
	QueryOpInsideSlice         = "$in"
	QueryOpContainsAllElements = "$all"
	QueryOpNotContainsElements = "$nin"
)

var (
	//typeBinary         = reflect.TypeOf(Binary{})
	//typeObjectId       = reflect.TypeOf(ObjectId(""))
	//typeSymbol         = reflect.TypeOf(Symbol(""))
	//typeMongoTimestamp = reflect.TypeOf(MongoTimestamp(0))
	//typeOrderKey       = reflect.TypeOf(MinKey)
	//typeDocElem        = reflect.TypeOf(DocElem{})
	//typeRaw            = reflect.TypeOf(Raw{})
	//typeURL            = reflect.TypeOf(url.URL{})
	typeTime = reflect.TypeOf(time.Time{})
)

type M2 bson.M

type IModel interface {
	GetId() bson.ObjectId
	GetCollectionName() string
}

type Model struct {
	Id bson.ObjectId `json:"id",bson:"_id,omitempty"`
}

func (m *Model) GetId() bson.ObjectId {
	return m.Id
}

func (m *Model) SetIdHex(idHex string) error {
	if !bson.IsObjectIdHex(idHex) {
		return &AppError{-1, "ObjectId Hex '" + idHex + "' is not valid!"}
	}
	m.Id = bson.ObjectIdHex(idHex)
	return nil
}

func (m *Model) GenerateId() {
	m.Id = bson.NewObjectId()
}

func (m *Model) RemoveById() {
	log.Println("catson"+reflect.TypeOf(m).Name(), reflect.TypeOf(m))
}

func (m *Model) FindOne(input interface{}, output IModel, omitEmpty bool) error {
	bs, _ := bson.Marshal(input)
	mp := bson.M{}
	bson.Unmarshal(bs, mp)
	if omitEmpty {
		for k, v := range mp {
			vv := reflect.ValueOf(v)
			if isZero(vv) {
				delete(mp, k)
			}
		}
	}
	return DB.C(output.GetCollectionName()).Find(mp).One(output)
}

func (m *Model) Query(input IModel, omitEmpty bool) *M2 {
	bs, _ := bson.Marshal(input)
	mp := M2{}
	bson.Unmarshal(bs, mp)
	if omitEmpty {
		for k, v := range mp {
			vv := reflect.ValueOf(v)
			if isZero(vv) {
				delete(mp, k)
			}
		}
	}
	mp["_8cname9__"] = input.GetCollectionName()
	return &mp
}

func (m *M2) Where(name string, signal string, value interface{}) *M2 {
	mm := *m
	mm[name] = bson.M{signal: value}
	return &mm
}

func (m *M2) One(result IModel) error {
	mm := *m
	cname, _ := mm["_8cname9__"].(string)
	delete(mm, "_8cname9__")
	return DB.C(cname).Find(mm).One(result)
}

func (m *M2) All(result interface{}) error {
	mm := *m
	cname, _ := mm["_8cname9__"].(string)
	delete(mm, "_8cname9__")
	return DB.C(cname).Find(mm).All(result)
}

func isZero(v reflect.Value) bool {
	switch v.Kind() {
	case reflect.String:
		return len(v.String()) == 0
	case reflect.Ptr, reflect.Interface:
		return v.IsNil()
	case reflect.Slice:
		return v.Len() == 0
	case reflect.Map:
		return v.Len() == 0
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		return v.Int() == 0
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
		return v.Uint() == 0
	case reflect.Float32, reflect.Float64:
		return v.Float() == 0
	case reflect.Bool:
		return !v.Bool()
	case reflect.Struct:
		if v.Type() == typeTime {
			return v.Interface().(time.Time).IsZero()
		}
	}
	return false
}
<commit_msg>Model bson/json<commit_after>package goboots

import (
	"labix.org/v2/mgo/bson"
	"log"
	"reflect"
	"time"
)

const (
	QueryOpEq                  = "$eq"
	QueryOpLessOrEq            = "$lte"
	QueryOpGreaterOrEq         = "$gte"
	QueryOpLess                = "$lt"
	QueryOpGreater             = "$gt"
	QueryOpNotEqual            = "$ne"
	QueryOpInsideSlice         = "$in"
	QueryOpContainsAllElements = "$all"
	QueryOpNotContainsElements = "$nin"
)

var (
	//typeBinary         = reflect.TypeOf(Binary{})
	//typeObjectId       = reflect.TypeOf(ObjectId(""))
	//typeSymbol         = reflect.TypeOf(Symbol(""))
	//typeMongoTimestamp = reflect.TypeOf(MongoTimestamp(0))
	//typeOrderKey       = reflect.TypeOf(MinKey)
	//typeDocElem        = reflect.TypeOf(DocElem{})
	//typeRaw            = reflect.TypeOf(Raw{})
	//typeURL            = reflect.TypeOf(url.URL{})
	typeTime = reflect.TypeOf(time.Time{})
)

type M2 bson.M

type IModel interface {
	GetId() bson.ObjectId
	GetCollectionName() string
}

type Model struct {
	Id bson.ObjectId `json:"id";bson:"_id,omitempty"`
}

func (m *Model) GetId() bson.ObjectId {
	return m.Id
}

func (m *Model) SetIdHex(idHex string) error {
	if !bson.IsObjectIdHex(idHex) {
		return &AppError{-1, "ObjectId Hex '" + idHex + "' is not valid!"}
	}
	m.Id = bson.ObjectIdHex(idHex)
	return nil
}

func (m *Model) GenerateId() {
	m.Id = bson.NewObjectId()
}

func (m *Model) RemoveById() {
	log.Println("catson"+reflect.TypeOf(m).Name(), reflect.TypeOf(m))
}

func (m *Model) FindOne(input interface{}, output IModel, omitEmpty bool) error {
	bs, _ := bson.Marshal(input)
	mp := bson.M{}
	bson.Unmarshal(bs, mp)
	if omitEmpty {
		for k, v := range mp {
			vv := reflect.ValueOf(v)
			if isZero(vv) {
				delete(mp, k)
			}
		}
	}
	return DB.C(output.GetCollectionName()).Find(mp).One(output)
}

func (m *Model) Query(input IModel, omitEmpty bool) *M2 {
	bs, _ := bson.Marshal(input)
	mp := M2{}
	bson.Unmarshal(bs, mp)
	if omitEmpty {
		for k, v := range mp {
			vv := reflect.ValueOf(v)
			if isZero(vv) {
				delete(mp, k)
			}
		}
	}
	mp["_8cname9__"] = input.GetCollectionName()
	return &mp
}

func (m *M2) Where(name string, signal string, value interface{}) *M2 {
	mm := *m
	mm[name] = bson.M{signal: value}
	return &mm
}

func (m *M2) One(result IModel) error {
	mm := *m
	cname, _ := mm["_8cname9__"].(string)
	delete(mm, "_8cname9__")
	return DB.C(cname).Find(mm).One(result)
}

func (m *M2) All(result interface{}) error {
	mm := *m
	cname, _ := mm["_8cname9__"].(string)
	delete(mm, "_8cname9__")
	return DB.C(cname).Find(mm).All(result)
}

func isZero(v reflect.Value) bool {
	switch v.Kind() {
	case reflect.String:
		return len(v.String()) == 0
	case reflect.Ptr, reflect.Interface:
		return v.IsNil()
	case reflect.Slice:
		return v.Len() == 0
	case reflect.Map:
		return v.Len() == 0
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		return v.Int() == 0
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
		return v.Uint() == 0
	case reflect.Float32, reflect.Float64:
		return v.Float() == 0
	case reflect.Bool:
		return !v.Bool()
	case reflect.Struct:
		if v.Type() == typeTime {
			return v.Interface().(time.Time).IsZero()
		}
	}
	return false
}
<|endoftext|>


<commit_before>package irc

import (
	"bufio"
	"log"
	"math/rand"
	"net"
	"sync"
	"time"
)

const delim byte = '\n'
const endline string = "\r\n"

type Connection struct {
	Network        string
	Nick           string
	User           string
	RealName       string
	Input          chan Message
	Output         chan Message
	reader         *bufio.Reader
	writer         *bufio.Writer
	dispatcher     func(chan struct{}, chan Message, chan Message)
	conn           net.Conn
	reconnect      chan struct{}
	Quit           chan struct{}
	quitsend       chan struct{}
	quitrecv       chan struct{}
	quitdispatcher chan struct{}
	l              sync.Mutex
}

func (c *Connection) Sender() {
	log.Println(c.Network, "spawned Sender")
	for {
		select {
		case msg := <-c.Input:
			c.writer.WriteString(msg.String() + endline)
			log.Println(c.Network, "-->", msg.String())
			c.writer.Flush()
		case <-c.quitsend:
			log.Println(c.Network, "closing Sender")
			return
		}
	}
}

func (c *Connection) Receiver() {
	log.Println(c.Network, "spawned Receiver")
	for {
		raw, err := c.reader.ReadString(delim)
		if err != nil {
			log.Println(c.Network, "error reading message", err.Error())
			log.Println(c.Network, "closing Receiver")
			c.Quit <- struct{}{}
			log.Println(c.Network, "sent quit message from Receiver")
			return
		}
		msg, err := ParseMessage(raw)
		if err != nil {
			log.Println(c.Network, "error decoding message", err.Error())
			log.Println(c.Network, "closing Receiver")
			c.Quit <- struct{}{}
			log.Println(c.Network, "sent quit message from Receiver")
			return
		} else {
			log.Println(c.Network, "<--", msg.String())
		}
		select {
		case c.Output <- *msg:
		case <-c.quitrecv:
			log.Println(c.Network, "closing Receiver")
			return
		}
	}
}

func (c *Connection) Cleaner() {
	log.Println(c.Network, "spawned Cleaner")
	for {
		<-c.Quit
		log.Println(c.Network, "ceceived quit message")
		c.l.Lock()
		log.Println(c.Network, "cleaning up!")
		c.quitsend <- struct{}{}
		c.quitrecv <- struct{}{}
		c.reconnect <- struct{}{}
		c.conn.Close()
		log.Println(c.Network, "closing Cleaner")
		c.l.Unlock()
	}
}

func (c *Connection) Keeper(servers []string) {
	log.Println(c.Network, "spawned Keeper")
	for {
		<-c.reconnect
		c.l.Lock()
		if c.Input != nil {
			close(c.Input)
			close(c.Output)
			close(c.quitsend)
			close(c.quitrecv)
		}
		c.Input = make(chan Message, 1)
		c.Output = make(chan Message, 1)
		c.quitsend = make(chan struct{}, 1)
		c.quitrecv = make(chan struct{}, 1)
		server := servers[rand.Intn(len(servers))]
		log.Println(c.Network, "connecting to", server)
		c.Dial(server)
		c.l.Unlock()

		go c.Sender()
		go c.Receiver()
		go c.dispatcher(c.quitdispatcher, c.Input, c.Output)

		log.Println(c.Network, "Initializing IRC connection")
		c.Input <- Message{
			Command:  "NICK",
			Trailing: c.Nick,
		}
		c.Input <- Message{
			Command:  "USER",
			Params:   []string{c.User, "0", "*"},
			Trailing: c.RealName,
		}

	}
}

func (c *Connection) Setup(dispatcher func(chan struct{}, chan Message, chan Message), network string, servers []string, nick string, user string, realname string) {
	rand.Seed(time.Now().UnixNano())

	c.reconnect = make(chan struct{}, 1)
	c.Quit = make(chan struct{}, 1)
	c.Nick = nick
	c.User = user
	c.RealName = realname
	c.Network = network
	c.dispatcher = dispatcher

	c.reconnect <- struct{}{}
	go c.Keeper(servers)
	go c.Cleaner()
	return
}

func (c *Connection) Dial(server string) error {

	conn, err := net.Dial("tcp", server)
	if err != nil {
		log.Println(c.Network, "Cannot connect to", server, "error:", err.Error())
		return err
	}
	log.Println(c.Network, "Connected to", server)
	c.writer = bufio.NewWriter(conn)
	c.reader = bufio.NewReader(conn)
	c.conn = conn

	return nil
}
<commit_msg>Make sure we close the dispatcher.<commit_after>package irc

import (
	"bufio"
	"log"
	"math/rand"
	"net"
	"sync"
	"time"
)

const delim byte = '\n'
const endline string = "\r\n"

type Connection struct {
	Network        string
	Nick           string
	User           string
	RealName       string
	Input          chan Message
	Output         chan Message
	reader         *bufio.Reader
	writer         *bufio.Writer
	dispatcher     func(chan struct{}, chan Message, chan Message)
	conn           net.Conn
	reconnect      chan struct{}
	Quit           chan struct{}
	quitsend       chan struct{}
	quitrecv       chan struct{}
	quitdispatcher chan struct{}
	l              sync.Mutex
}

func (c *Connection) Sender() {
	log.Println(c.Network, "spawned Sender")
	for {
		select {
		case msg := <-c.Input:
			c.writer.WriteString(msg.String() + endline)
			log.Println(c.Network, "-->", msg.String())
			c.writer.Flush()
		case <-c.quitsend:
			log.Println(c.Network, "closing Sender")
			return
		}
	}
}

func (c *Connection) Receiver() {
	log.Println(c.Network, "spawned Receiver")
	for {
		raw, err := c.reader.ReadString(delim)
		if err != nil {
			log.Println(c.Network, "error reading message", err.Error())
			log.Println(c.Network, "closing Receiver")
			c.Quit <- struct{}{}
			log.Println(c.Network, "sent quit message from Receiver")
			return
		}
		msg, err := ParseMessage(raw)
		if err != nil {
			log.Println(c.Network, "error decoding message", err.Error())
			log.Println(c.Network, "closing Receiver")
			c.Quit <- struct{}{}
			log.Println(c.Network, "sent quit message from Receiver")
			return
		} else {
			log.Println(c.Network, "<--", msg.String())
		}
		select {
		case c.Output <- *msg:
		case <-c.quitrecv:
			log.Println(c.Network, "closing Receiver")
			return
		}
	}
}

func (c *Connection) Cleaner() {
	log.Println(c.Network, "spawned Cleaner")
	for {
		<-c.Quit
		log.Println(c.Network, "ceceived quit message")
		c.l.Lock()
		log.Println(c.Network, "cleaning up!")
		c.quitsend <- struct{}{}
		c.quitrecv <- struct{}{}
        c.quitdispatcher <- struct{}{}
		c.reconnect <- struct{}{}
		c.conn.Close()
		log.Println(c.Network, "closing Cleaner")
		c.l.Unlock()
	}
}

func (c *Connection) Keeper(servers []string) {
	log.Println(c.Network, "spawned Keeper")
	for {
		<-c.reconnect
		c.l.Lock()
		if c.Input != nil {
			close(c.Input)
			close(c.Output)
			close(c.quitsend)
			close(c.quitrecv)
		}
		c.Input = make(chan Message, 1)
		c.Output = make(chan Message, 1)
		c.quitsend = make(chan struct{}, 1)
		c.quitrecv = make(chan struct{}, 1)
		server := servers[rand.Intn(len(servers))]
		log.Println(c.Network, "connecting to", server)
		c.Dial(server)
		c.l.Unlock()

		go c.Sender()
		go c.Receiver()
		go c.dispatcher(c.quitdispatcher, c.Input, c.Output)

		log.Println(c.Network, "Initializing IRC connection")
		c.Input <- Message{
			Command:  "NICK",
			Trailing: c.Nick,
		}
		c.Input <- Message{
			Command:  "USER",
			Params:   []string{c.User, "0", "*"},
			Trailing: c.RealName,
		}

	}
}

func (c *Connection) Setup(dispatcher func(chan struct{}, chan Message, chan Message), network string, servers []string, nick string, user string, realname string) {
	rand.Seed(time.Now().UnixNano())

	c.reconnect = make(chan struct{}, 1)
	c.Quit = make(chan struct{}, 1)
	c.Nick = nick
	c.User = user
	c.RealName = realname
	c.Network = network
	c.dispatcher = dispatcher

	c.reconnect <- struct{}{}
	go c.Keeper(servers)
	go c.Cleaner()
	return
}

func (c *Connection) Dial(server string) error {

	conn, err := net.Dial("tcp", server)
	if err != nil {
		log.Println(c.Network, "Cannot connect to", server, "error:", err.Error())
		return err
	}
	log.Println(c.Network, "Connected to", server)
	c.writer = bufio.NewWriter(conn)
	c.reader = bufio.NewReader(conn)
	c.conn = conn

	return nil
}
<|endoftext|>


<commit_before>package sms

import (
	"encoding/xml"
	"fmt"
	"log"
	"os"

	xco "github.com/mndrix/go-xco"
)

// xmppProcess is the piece which interacts with the XMPP network and
// converts those communications into values which the rest of
// sms-over-xmpp can understand.
type xmppProcess struct {
	// where to connect to the XMPP server
	host string
	port int

	// credentials for XMPP auth
	name   string
	secret string

	// channels for communicating with the Gateway process
	gatewayRx chan<- *xco.Message
	gatewayTx <-chan *xco.Message

	// contacted records whether the local and remote JIDs,
	// respectively, have contacted each other during the life of this
	// process.
	contacted map[xco.Address]map[xco.Address]bool
}

// runXmppComponent creates a goroutine for sending and receiving XMPP
// stanzas.  it returns a channel for monitoring the goroutine's
// health.  if that channel closes, the XMPP process has died.
func (x *xmppProcess) run() <-chan struct{} {
	opts := xco.Options{
		Name:         x.name,
		SharedSecret: x.secret,
		Address:      fmt.Sprintf("%s:%d", x.host, x.port),
		Logger:       log.New(os.Stderr, "", log.LstdFlags),
	}

	healthCh := make(chan struct{})
	go x.loop(opts, healthCh)
	return healthCh
}

func (x *xmppProcess) loop(opts xco.Options, healthCh chan<- struct{}) {
	defer func() { close(healthCh) }()

	c, err := xco.NewComponent(opts)
	if err != nil {
		log.Printf("can't create internal XMPP component: %s", err)
		return
	}

	tx, rx, errx := c.RunAsync()
	for {
		select {
		case st := <-rx:
			switch stanza := st.(type) {
			case *xco.Message:
				log.Printf("Message: %+v", stanza)
				if stanza.Body == "" {
					log.Printf("  ignoring message with empty body")
					break
				}
				local := &stanza.Header.To
				remote := &stanza.Header.From
				if x.isFirstContact(local, remote) {
					x.hadContact(local, remote)
					p := x.requestSubscription(local, remote)
					go func() { tx <- p }()
				}
				go func() { x.gatewayRx <- stanza }()
			case *xco.Iq:
				if stanza.IsDiscoInfo() {
					log.Printf("Disco: %+v", stanza)
					ids, features := x.describeService()
					stanza, err = stanza.DiscoInfoReply(ids, features)
					if err == nil {
						go func() { tx <- stanza }()
					}
				} else {
					log.Printf("Iq: %+v", stanza)
				}
			case *xco.Presence:
				log.Printf("Presence: %+v", stanza)
				switch stanza.Type {
				case "probe":
					stanza = x.presenceAvailable(stanza)
					go func() { tx <- stanza }()
				case "subscribe", "unsubscribe":
					stanzas := x.handleSubscribeUnsubscribe(stanza)
					go func() {
						for _, stanza := range stanzas {
							tx <- stanza
						}
					}()
				}
			case *xml.StartElement:
				log.Printf("Unknown: %+v", stanza)
			default:
				panic(fmt.Sprintf("Unexpected stanza type: %#v", st))
			}
		case stanza := <-x.gatewayTx:
			local := &stanza.Header.From
			remote := &stanza.Header.To
			if x.isFirstContact(local, remote) {
				x.hadContact(local, remote)
				//p := x.requestSubscription(local, remote)
				go func() {
					//tx <- p
					tx <- stanza
				}()
			} else {
				go func() { tx <- stanza }()
			}
		case err = <-errx:
		}

		if err != nil {
			break
		}
	}

	log.Printf("lost XMPP connection: %s", err)
}

func (x *xmppProcess) describeService() ([]xco.DiscoIdentity, []xco.DiscoFeature) {
	ids := []xco.DiscoIdentity{
		{
			Category: "gateway",
			Type:     "sms",
			Name:     "SMS over XMPP",
		},
	}
	features := []xco.DiscoFeature{
		{
			Var: "urn:xmpp:receipts",
		},
	}
	return ids, features
}

// isFirstContact returns true if the entities local and remote have
// not had any contact during the life of this process.  Contact
// includes both messages and subscriptions.
func (x *xmppProcess) isFirstContact(local, remote *xco.Address) bool {
	if x.contacted == nil {
		return true
	}

	local = local.Bare()
	remotes := x.contacted[*local]
	if remotes == nil {
		return true
	}

	remote = remote.Bare()
	return !remotes[*remote]
}

// hadContact records the fact that local and remote have contacted
// each other.  It could be for the first time or any subsequent time.
func (x *xmppProcess) hadContact(local, remote *xco.Address) {
	local = local.Bare()
	remote = remote.Bare()
	if x.contacted == nil {
		x.contacted = make(map[xco.Address]map[xco.Address]bool)
	}
	remotes := x.contacted[*local]
	if remotes == nil {
		remotes = make(map[xco.Address]bool)
		x.contacted[*local] = remotes
	}
	remotes[*remote] = true
}

func (x *xmppProcess) presenceAvailable(p *xco.Presence) *xco.Presence {
	stanza := &xco.Presence{
		Header: xco.Header{
			From: p.Header.To,
			To:   p.Header.From,
			ID:   NewId(),
		},
	}
	return stanza
}

func (x *xmppProcess) handleSubscribeUnsubscribe(p *xco.Presence) []*xco.Presence {
	// RFC says to use bare JIDs
	p.Header.To.ResourcePart = ""
	p.Header.From.ResourcePart = ""

	stanza := &xco.Presence{
		Header: xco.Header{
			From: p.Header.To,
			To:   p.Header.From,
			ID:   NewId(),
		},
	}
	switch p.Type {
	case "subscribe":
		stanza.Type = "subscribed"
		local := &p.Header.To
		remote := &p.Header.From
		stanzas := []*xco.Presence{
			stanza,
			x.presenceAvailable(p),
		}
		if x.isFirstContact(local, remote) {
			x.hadContact(local, remote)
			//stanzas = append(stanzas, x.requestSubscription(local, remote))
		}
		return stanzas
	case "unsubscribe":
		stanza.Type = "unavailable"
		return []*xco.Presence{stanza}
	}

	return nil
}

func (x *xmppProcess) requestSubscription(local, remote *xco.Address) *xco.Presence {
	stanza := &xco.Presence{
		Header: xco.Header{
			From: *local,
			To:   *remote,
			ID:   NewId(),
		},
		Type: "subscribe",
	}
	return stanza
}
<commit_msg>Use Bare() for forcing bare JIDs<commit_after>package sms

import (
	"encoding/xml"
	"fmt"
	"log"
	"os"

	xco "github.com/mndrix/go-xco"
)

// xmppProcess is the piece which interacts with the XMPP network and
// converts those communications into values which the rest of
// sms-over-xmpp can understand.
type xmppProcess struct {
	// where to connect to the XMPP server
	host string
	port int

	// credentials for XMPP auth
	name   string
	secret string

	// channels for communicating with the Gateway process
	gatewayRx chan<- *xco.Message
	gatewayTx <-chan *xco.Message

	// contacted records whether the local and remote JIDs,
	// respectively, have contacted each other during the life of this
	// process.
	contacted map[xco.Address]map[xco.Address]bool
}

// runXmppComponent creates a goroutine for sending and receiving XMPP
// stanzas.  it returns a channel for monitoring the goroutine's
// health.  if that channel closes, the XMPP process has died.
func (x *xmppProcess) run() <-chan struct{} {
	opts := xco.Options{
		Name:         x.name,
		SharedSecret: x.secret,
		Address:      fmt.Sprintf("%s:%d", x.host, x.port),
		Logger:       log.New(os.Stderr, "", log.LstdFlags),
	}

	healthCh := make(chan struct{})
	go x.loop(opts, healthCh)
	return healthCh
}

func (x *xmppProcess) loop(opts xco.Options, healthCh chan<- struct{}) {
	defer func() { close(healthCh) }()

	c, err := xco.NewComponent(opts)
	if err != nil {
		log.Printf("can't create internal XMPP component: %s", err)
		return
	}

	tx, rx, errx := c.RunAsync()
	for {
		select {
		case st := <-rx:
			switch stanza := st.(type) {
			case *xco.Message:
				log.Printf("Message: %+v", stanza)
				if stanza.Body == "" {
					log.Printf("  ignoring message with empty body")
					break
				}
				local := &stanza.Header.To
				remote := &stanza.Header.From
				if x.isFirstContact(local, remote) {
					x.hadContact(local, remote)
					p := x.requestSubscription(local, remote)
					go func() { tx <- p }()
				}
				go func() { x.gatewayRx <- stanza }()
			case *xco.Iq:
				if stanza.IsDiscoInfo() {
					log.Printf("Disco: %+v", stanza)
					ids, features := x.describeService()
					stanza, err = stanza.DiscoInfoReply(ids, features)
					if err == nil {
						go func() { tx <- stanza }()
					}
				} else {
					log.Printf("Iq: %+v", stanza)
				}
			case *xco.Presence:
				log.Printf("Presence: %+v", stanza)
				switch stanza.Type {
				case "probe":
					stanza = x.presenceAvailable(stanza)
					go func() { tx <- stanza }()
				case "subscribe", "unsubscribe":
					stanzas := x.handleSubscribeUnsubscribe(stanza)
					go func() {
						for _, stanza := range stanzas {
							tx <- stanza
						}
					}()
				}
			case *xml.StartElement:
				log.Printf("Unknown: %+v", stanza)
			default:
				panic(fmt.Sprintf("Unexpected stanza type: %#v", st))
			}
		case stanza := <-x.gatewayTx:
			local := &stanza.Header.From
			remote := &stanza.Header.To
			if x.isFirstContact(local, remote) {
				x.hadContact(local, remote)
				//p := x.requestSubscription(local, remote)
				go func() {
					//tx <- p
					tx <- stanza
				}()
			} else {
				go func() { tx <- stanza }()
			}
		case err = <-errx:
		}

		if err != nil {
			break
		}
	}

	log.Printf("lost XMPP connection: %s", err)
}

func (x *xmppProcess) describeService() ([]xco.DiscoIdentity, []xco.DiscoFeature) {
	ids := []xco.DiscoIdentity{
		{
			Category: "gateway",
			Type:     "sms",
			Name:     "SMS over XMPP",
		},
	}
	features := []xco.DiscoFeature{
		{
			Var: "urn:xmpp:receipts",
		},
	}
	return ids, features
}

// isFirstContact returns true if the entities local and remote have
// not had any contact during the life of this process.  Contact
// includes both messages and subscriptions.
func (x *xmppProcess) isFirstContact(local, remote *xco.Address) bool {
	if x.contacted == nil {
		return true
	}

	local = local.Bare()
	remotes := x.contacted[*local]
	if remotes == nil {
		return true
	}

	remote = remote.Bare()
	return !remotes[*remote]
}

// hadContact records the fact that local and remote have contacted
// each other.  It could be for the first time or any subsequent time.
func (x *xmppProcess) hadContact(local, remote *xco.Address) {
	local = local.Bare()
	remote = remote.Bare()
	if x.contacted == nil {
		x.contacted = make(map[xco.Address]map[xco.Address]bool)
	}
	remotes := x.contacted[*local]
	if remotes == nil {
		remotes = make(map[xco.Address]bool)
		x.contacted[*local] = remotes
	}
	remotes[*remote] = true
}

func (x *xmppProcess) presenceAvailable(p *xco.Presence) *xco.Presence {
	stanza := &xco.Presence{
		Header: xco.Header{
			From: p.Header.To,
			To:   p.Header.From,
			ID:   NewId(),
		},
	}
	return stanza
}

func (x *xmppProcess) handleSubscribeUnsubscribe(p *xco.Presence) []*xco.Presence {
	// RFC says to use bare JIDs
	local := (&p.Header.To).Bare()
	remote := (&p.Header.From).Bare()

	stanza := &xco.Presence{
		Header: xco.Header{
			From: *local,
			To:   *remote,
			ID:   NewId(),
		},
	}
	switch p.Type {
	case "subscribe":
		stanza.Type = "subscribed"
		stanzas := []*xco.Presence{
			stanza,
			x.presenceAvailable(p),
		}
		if x.isFirstContact(local, remote) {
			x.hadContact(local, remote)
			//stanzas = append(stanzas, x.requestSubscription(local, remote))
		}
		return stanzas
	case "unsubscribe":
		stanza.Type = "unavailable"
		return []*xco.Presence{stanza}
	}

	return nil
}

func (x *xmppProcess) requestSubscription(local, remote *xco.Address) *xco.Presence {
	stanza := &xco.Presence{
		Header: xco.Header{
			From: *local,
			To:   *remote,
			ID:   NewId(),
		},
		Type: "subscribe",
	}
	return stanza
}
<|endoftext|>


<commit_before>package emil

import (
	"encoding/json"
	"errors"
	"fmt"
	"io/ioutil"
	"time"
)

var IN_TEST = false
var errNowNewAnalysis = errors.New("errNowNewAnalysis")

// EndGameDb to query for mate in 1,2, etc.
type EndGameDb struct {
	Start       time.Time            `json:"startTime"`
	Duration    time.Duration        `json:"duration"`
	AnalysisMap map[string]*Analysis `json:"analysis"`
	dtmDb       []map[string]bool    `json:"dtms"`
}

func (db *EndGameDb) Find(p *position) (bestMove *Move) {
	if DEBUG {
		fmt.Printf("Find:\n%s\n", p.board)
	}
	a := db.AnalysisMap[p.board.String()]
	if DEBUG {
		fmt.Printf("Found: AnalysisMap with dtms %v\n", a.DTMs(p.player))
	}
	return a.BestMove(p.player)
}

func (db *EndGameDb) FindMatesIn(dtm int) (as []*Analysis) {
	if dtm == -1 {
		for _, a := range db.AnalysisMap {
			if a.playerHaveDTMs() {
				as = append(as, a)
			}
		}
	} else {
		for str := range db.dtmDb[dtm] {
			as = append(as, db.AnalysisMap[str])
		}
	}
	return as
}

func (db *EndGameDb) FindMates() (as []*Analysis) {
	return db.FindMatesIn(0)
}

func (db *EndGameDb) FindMate(piece, square int) (boards []*Board) {
	for str := range db.dtmDb[0] {
		a := db.AnalysisMap[str]
		if a.Board.squares[square] == piece {
			boards = append(boards, a.Board)
		}
	}
	return boards
}

func (db *EndGameDb) addPosition(board *Board) {
	a := &Analysis{
		DtmWhite: make([]*DTM, 0),
		DtmBlack: make([]*DTM, 0),
		Board:    board,
		moves:    make(map[string]bool)}
	db.AnalysisMap[a.Board.String()] = a
}

func (db *EndGameDb) addAnalysis(board *Board, dtm int, move *Move) {
	a := db.AnalysisMap[board.String()]
	if move != nil {
		a.addDTM(move.reverse(), dtm)
	}
	if dtm >= 0 {
		if move != nil {
			playerForStep := playerForStepN(dtm)
			if playerForStep != move.player {
				panic("playerForStep != move.player")
			}
		}
		db.dtmDb[dtm][board.String()] = true
	}
}

func (db *EndGameDb) positions() int {
	return len(db.AnalysisMap)
}

// find positions where black is checkmate
func (db *EndGameDb) retrogradeAnalysisStep1() {
	db.dtmDb = append(db.dtmDb, make(map[string]bool))

	start := time.Now()

	player := BLACK
	for boardStr, a := range db.AnalysisMap {
		// mate only on border square
		blackKingSquare := BoardSquares[a.Board.blackKing]
		if !blackKingSquare.isBorder {
			continue
		}
		// mate only with help from king
		if squaresDistances[a.Board.blackKing][a.Board.whiteKing] > 2 {
			continue
		}

		p := NewPosition(a.Board, player)

		move := Search(p)
		if move == nil {
			if isKingInCheck(p) {
				db.addAnalysis(a.Board, 0, nil)
				if DEBUG {
					fmt.Printf("mate:\n%s\n", boardStr)
				}
			}
		}
	}
	end := time.Now()
	if DEBUG {
		fmt.Printf("db.dtmDb[0] %d\n", len(db.dtmDb[0]))
		fmt.Printf("duration %v\n\n\n", end.Sub(start))
	}
}
func playerForStepN(dtm int) (player int) {
	if dtm%2 == 0 {
		return BLACK
	}
	return WHITE
}

func (db *EndGameDb) retrogradeAnalysisStepN(dtm int) (noError error) {
	start := time.Now()
	db.dtmDb = append(db.dtmDb, make(map[string]bool))

	player := playerForStepN(dtm)

	positions := 0
	if player == WHITE {
		if DEBUG {
			fmt.Printf("WHITE Start positions %d\n", len(db.dtmDb[dtm-1]))
		}
		for str := range db.dtmDb[dtm-1] {
			a := db.AnalysisMap[str]
			p := NewPosition(a.Board, player)
			list := generateMoves(p)
			moves := filterKingCaptures(p, list)
			moves = filterKingCaptures(NewPosition(a.Board, otherPlayer(player)), list)

			for _, m := range moves {
				newBoard := a.Board.doMove(m)
				db.addAnalysis(newBoard, dtm, m)
			}
		}
	} else {
		for _, a := range db.AnalysisMap {
			if db.isMateIn0246(a.Board, dtm) >= 0 {
				positions++
			}
		}
		if DEBUG {
			fmt.Printf("BLACK Start positions %d\n", len(db.AnalysisMap)-positions)
		}
		for _, a := range db.AnalysisMap {
			if db.isMateIn0246(a.Board, dtm) >= 0 {
				continue
			}
			p := NewPosition(a.Board, player)
			moves := GenerateMoves(p)

			found := 0
			maxDTM := -1
			for _, m := range moves {
				newBoard := a.Board.doMove(m)
				newDtm := db.isMateIn1357(newBoard, dtm)
				if newDtm > maxDTM {
					maxDTM = newDtm
				}
				if db.isMateIn1357(newBoard, dtm) >= 0 {
					found++
				}
			}

			if found == len(moves) {
				for _, m := range moves {
					db.addAnalysis(a.Board, maxDTM+1, m)
				}
			}
		}
	}
	end := time.Now()

	if DEBUG {
		fmt.Printf("db.dtmDb[%d] %d\n", dtm, len(db.dtmDb[dtm]))
		fmt.Printf("duration %v\n\n\n", end.Sub(start))
	}

	if len(db.dtmDb[dtm]) == 0 {
		return errNowNewAnalysis
	}
	return noError
}
func (db *EndGameDb) isMateIn0246(board *Board, maxDtm int) int {
	for dtm := 0; dtm < maxDtm; dtm += 2 {
		_, ok := db.dtmDb[dtm][board.String()]
		if ok {
			return dtm
		}
	}
	return -1
}
func (db *EndGameDb) isMateIn1357(board *Board, maxDtm int) int {
	for dtm := 1; dtm < maxDtm; dtm += 2 {
		_, ok := db.dtmDb[dtm][board.String()]
		if ok {
			return dtm
		}
	}
	return -1
}

func (db *EndGameDb) MaxDtm() int {
	return len(db.dtmDb)
}

func (db *EndGameDb) retrogradeAnalysis() {
	// find positions where black is checkmate
	db.retrogradeAnalysisStep1()
	dtm := 1
	for {
		err := db.retrogradeAnalysisStepN(dtm)
		if err != nil {
			break
		}
		if IN_TEST {
			return
		}
		dtm++
	}
}
func GenerateMoves(p *position) (list []*Move) {
	for _, m := range generateMoves(p) {
		b := p.board.DoMove(m)
		if !IsTheKingInCheck(NewPosition(b, WHITE)) {
			list = append(list, m)
		}
	}
	return list
}
func generateMoves(p *position) (list []*Move) {
	for src, piece := range p.board.squares {
		if isOwnPiece(p.player, piece) {
			switch abs(piece) {
			case kingValue:
				for _, dst := range kingDestinationsFrom(src) {
					capture := p.board.squares[dst]
					if isOtherKing(p.player, capture) {
						continue
					}
					if capture == Empty {
						list = append(list, newSilentMove(p.player, piece, src, dst))
					} else if !isOwnPiece(p.player, capture) {
						list = append(list, newCaptureMove(p.player, piece, capture, src, dst))
					}
				}
			case rockValue:
				for _, dsts := range rockDestinationsFrom(src) {
					for _, dst := range dsts {
						capture := p.board.squares[dst]
						if isOtherKing(p.player, capture) {
							break
						}
						if capture == Empty {
							list = append(list, newSilentMove(p.player, piece, src, dst))
						} else if !isOwnPiece(p.player, capture) {
							list = append(list, newCaptureMove(p.player, piece, capture, src, dst))
							break
						} else {
							break // onOwnPiece
						}
					}
				}
			}
		}
	}
	return list
}

// SaveEndGameDb saves the an end game DB for KRK to file
func SaveEndGameDb(db *EndGameDb) error {
	filename := "EndGameDb.json"
	fmt.Println("WriteDataToFile: ", filename)

	var data EndGameDb
	data = *db

	b, err := json.MarshalIndent(data, "", "  ")
	if err != nil {
		return err
	}

	return ioutil.WriteFile(filename, b, 0666)
}

// NewEndGameDb generates an end game DB for KRK
func NewEndGameDb() *EndGameDb {
	var err error

	endGames := &EndGameDb{
		Start:       time.Now(),
		AnalysisMap: make(map[string]*Analysis),
		dtmDb:       make([]map[string]bool, 0)}

	for wk := A1; wk <= H8; wk++ {
		//for wk := E3; wk <= E3; wk++ {
		if DEBUG {
			fmt.Printf("White king on %s\n", BoardSquares[wk])
		}
		for wr := A1; wr <= H8; wr++ {
			for bk := A1; bk <= H8; bk++ {

				board := NewBoard()

				err = board.Setup(WhiteKing, wk)
				if err != nil {
					continue
				}

				err = board.Setup(WhiteRock, wr)
				if err != nil {
					continue
				}

				err = board.Setup(BlackKing, bk)
				if err != nil {
					continue
				}

				err = board.kingsToClose()
				if err != nil {
					continue
				}

				endGames.addPosition(board)
			}
		}
	}
	end := time.Now()
	endGames.Duration = end.Sub(endGames.Start)
	if DEBUG {
		fmt.Printf("all positions %d\n", 64*63*62)
		fmt.Printf("endGames.positions() %d\n", endGames.positions())
		fmt.Printf("difference %d\n", 64*63*62-endGames.positions())
		fmt.Printf("duration %v\n", endGames.Duration)
	}
	endGames.retrogradeAnalysis()

	return endGames
}
<commit_msg>fix endgame_db.go<commit_after>package emil

import (
	"encoding/json"
	"errors"
	"fmt"
	"io/ioutil"
	"time"
)

var IN_TEST = false
var errNowNewAnalysis = errors.New("errNowNewAnalysis")

// EndGameDb to query for mate in 1,2, etc.
type EndGameDb struct {
	Start       time.Time            `json:"startTime"`
	Duration    time.Duration        `json:"duration"`
	AnalysisMap map[string]*Analysis `json:"analysis"`
	dtmDb       []map[string]bool    `json:"dtms"`
}

func (db *EndGameDb) Find(p *position) (bestMove *Move) {
	if DEBUG {
		fmt.Printf("Find:\n%s\n", p.board)
	}
	a := db.AnalysisMap[p.board.String()]
	if DEBUG {
		fmt.Printf("Found: AnalysisMap with dtms %v\n", a.DTMs(p.player))
	}
	return a.BestMove(p.player)
}

func (db *EndGameDb) FindMatesIn(dtm int) (as []*Analysis) {
	if dtm == -1 {
		for _, a := range db.AnalysisMap {
			if a.playerHaveDTMs() {
				as = append(as, a)
			}
		}
	} else {
		for str := range db.dtmDb[dtm] {
			as = append(as, db.AnalysisMap[str])
		}
	}
	return as
}

func (db *EndGameDb) FindMates() (as []*Analysis) {
	return db.FindMatesIn(0)
}

func (db *EndGameDb) FindMate(piece, square int) (boards []*Board) {
	for str := range db.dtmDb[0] {
		a := db.AnalysisMap[str]
		if a.Board.Squares[square] == piece {
			boards = append(boards, a.Board)
		}
	}
	return boards
}

func (db *EndGameDb) addPosition(board *Board) {
	a := &Analysis{
		DtmWhite: make([]*DTM, 0),
		DtmBlack: make([]*DTM, 0),
		Board:    board,
		moves:    make(map[string]bool)}
	db.AnalysisMap[a.Board.String()] = a
}

func (db *EndGameDb) addAnalysis(board *Board, dtm int, move *Move) {
	a := db.AnalysisMap[board.String()]
	if move != nil {
		a.addDTM(move.reverse(), dtm)
	}
	if dtm >= 0 {
		if move != nil {
			playerForStep := playerForStepN(dtm)
			if playerForStep != move.player {
				panic("playerForStep != move.player")
			}
		}
		db.dtmDb[dtm][board.String()] = true
	}
}

func (db *EndGameDb) positions() int {
	return len(db.AnalysisMap)
}

// find positions where black is checkmate
func (db *EndGameDb) retrogradeAnalysisStep1() {
	db.dtmDb = append(db.dtmDb, make(map[string]bool))

	start := time.Now()

	player := BLACK
	for boardStr, a := range db.AnalysisMap {
		// mate only on border square
		blackKingSquare := BoardSquares[a.Board.blackKing]
		if !blackKingSquare.isBorder {
			continue
		}
		// mate only with help from king
		if squaresDistances[a.Board.blackKing][a.Board.whiteKing] > 2 {
			continue
		}

		p := NewPosition(a.Board, player)

		move := Search(p)
		if move == nil {
			if isKingInCheck(p) {
				db.addAnalysis(a.Board, 0, nil)
				if DEBUG {
					fmt.Printf("mate:\n%s\n", boardStr)
				}
			}
		}
	}
	end := time.Now()
	if DEBUG {
		fmt.Printf("db.dtmDb[0] %d\n", len(db.dtmDb[0]))
		fmt.Printf("duration %v\n\n\n", end.Sub(start))
	}
}
func playerForStepN(dtm int) (player int) {
	if dtm%2 == 0 {
		return BLACK
	}
	return WHITE
}

func (db *EndGameDb) retrogradeAnalysisStepN(dtm int) (noError error) {
	start := time.Now()
	db.dtmDb = append(db.dtmDb, make(map[string]bool))

	player := playerForStepN(dtm)

	positions := 0
	if player == WHITE {
		if DEBUG {
			fmt.Printf("WHITE Start positions %d\n", len(db.dtmDb[dtm-1]))
		}
		for str := range db.dtmDb[dtm-1] {
			a := db.AnalysisMap[str]
			p := NewPosition(a.Board, player)
			list := generateMoves(p)
			moves := filterKingCaptures(p, list)
			moves = filterKingCaptures(NewPosition(a.Board, otherPlayer(player)), list)

			for _, m := range moves {
				newBoard := a.Board.doMove(m)
				db.addAnalysis(newBoard, dtm, m)
			}
		}
	} else {
		for _, a := range db.AnalysisMap {
			if db.isMateIn0246(a.Board, dtm) >= 0 {
				positions++
			}
		}
		if DEBUG {
			fmt.Printf("BLACK Start positions %d\n", len(db.AnalysisMap)-positions)
		}
		for _, a := range db.AnalysisMap {
			if db.isMateIn0246(a.Board, dtm) >= 0 {
				continue
			}
			p := NewPosition(a.Board, player)
			moves := GenerateMoves(p)

			found := 0
			maxDTM := -1
			for _, m := range moves {
				newBoard := a.Board.doMove(m)
				newDtm := db.isMateIn1357(newBoard, dtm)
				if newDtm > maxDTM {
					maxDTM = newDtm
				}
				if db.isMateIn1357(newBoard, dtm) >= 0 {
					found++
				}
			}

			if found == len(moves) {
				for _, m := range moves {
					db.addAnalysis(a.Board, maxDTM+1, m)
				}
			}
		}
	}
	end := time.Now()

	if DEBUG {
		fmt.Printf("db.dtmDb[%d] %d\n", dtm, len(db.dtmDb[dtm]))
		fmt.Printf("duration %v\n\n\n", end.Sub(start))
	}

	if len(db.dtmDb[dtm]) == 0 {
		return errNowNewAnalysis
	}
	return noError
}
func (db *EndGameDb) isMateIn0246(board *Board, maxDtm int) int {
	for dtm := 0; dtm < maxDtm; dtm += 2 {
		_, ok := db.dtmDb[dtm][board.String()]
		if ok {
			return dtm
		}
	}
	return -1
}
func (db *EndGameDb) isMateIn1357(board *Board, maxDtm int) int {
	for dtm := 1; dtm < maxDtm; dtm += 2 {
		_, ok := db.dtmDb[dtm][board.String()]
		if ok {
			return dtm
		}
	}
	return -1
}

func (db *EndGameDb) MaxDtm() int {
	return len(db.dtmDb)
}

func (db *EndGameDb) retrogradeAnalysis() {
	// find positions where black is checkmate
	db.retrogradeAnalysisStep1()
	dtm := 1
	for {
		err := db.retrogradeAnalysisStepN(dtm)
		if err != nil {
			break
		}
		if IN_TEST {
			return
		}
		dtm++
	}
}
func GenerateMoves(p *position) (list []*Move) {
	for _, m := range generateMoves(p) {
		b := p.board.DoMove(m)
		if !IsTheKingInCheck(NewPosition(b, WHITE)) {
			list = append(list, m)
		}
	}
	return list
}
func generateMoves(p *position) (list []*Move) {
	for src, piece := range p.board.Squares {
		if isOwnPiece(p.player, piece) {
			switch abs(piece) {
			case kingValue:
				for _, dst := range kingDestinationsFrom(src) {
					capture := p.board.Squares[dst]
					if isOtherKing(p.player, capture) {
						continue
					}
					if capture == Empty {
						list = append(list, newSilentMove(p.player, piece, src, dst))
					} else if !isOwnPiece(p.player, capture) {
						list = append(list, newCaptureMove(p.player, piece, capture, src, dst))
					}
				}
			case rockValue:
				for _, dsts := range rockDestinationsFrom(src) {
					for _, dst := range dsts {
						capture := p.board.Squares[dst]
						if isOtherKing(p.player, capture) {
							break
						}
						if capture == Empty {
							list = append(list, newSilentMove(p.player, piece, src, dst))
						} else if !isOwnPiece(p.player, capture) {
							list = append(list, newCaptureMove(p.player, piece, capture, src, dst))
							break
						} else {
							break // onOwnPiece
						}
					}
				}
			}
		}
	}
	return list
}

// SaveEndGameDb saves the an end game DB for KRK to file
func SaveEndGameDb(db *EndGameDb) error {
	filename := "EndGameDb.json"
	fmt.Println("WriteDataToFile: ", filename)

	var data EndGameDb
	data = *db

	b, err := json.MarshalIndent(data, "", "  ")
	if err != nil {
		return err
	}

	return ioutil.WriteFile(filename, b, 0666)
}

// NewEndGameDb generates an end game DB for KRK
func NewEndGameDb() *EndGameDb {
	var err error

	endGames := &EndGameDb{
		Start:       time.Now(),
		AnalysisMap: make(map[string]*Analysis),
		dtmDb:       make([]map[string]bool, 0)}

	for wk := A1; wk <= H8; wk++ {
		//for wk := E3; wk <= E3; wk++ {
		if DEBUG {
			fmt.Printf("White king on %s\n", BoardSquares[wk])
		}
		for wr := A1; wr <= H8; wr++ {
			for bk := A1; bk <= H8; bk++ {

				board := NewBoard()

				err = board.Setup(WhiteKing, wk)
				if err != nil {
					continue
				}

				err = board.Setup(WhiteRock, wr)
				if err != nil {
					continue
				}

				err = board.Setup(BlackKing, bk)
				if err != nil {
					continue
				}

				err = board.kingsToClose()
				if err != nil {
					continue
				}

				endGames.addPosition(board)
			}
		}
	}
	end := time.Now()
	endGames.Duration = end.Sub(endGames.Start)
	if DEBUG {
		fmt.Printf("all positions %d\n", 64*63*62)
		fmt.Printf("endGames.positions() %d\n", endGames.positions())
		fmt.Printf("difference %d\n", 64*63*62-endGames.positions())
		fmt.Printf("duration %v\n", endGames.Duration)
	}
	endGames.retrogradeAnalysis()

	return endGames
}
<|endoftext|>


<commit_before>package docindexing

import (
    "bufio"
    "log"
    "errors"
    "os"
    "strings"
    "regexp"
    "container/list"
    "unicode"
)

var supportedExtensions = map[string]int{
  "txt" : 0,
  "py" : 0,
  "c" : 0,
  "cpp" : 0,
  "go" : 0,
  "java" : 0,
}

// List of stop words to ignore when parsing
var stopWords = map[string]int{
  "a" : 0,
  "about" : 0,
  "above" : 0,
  "after" : 0,
  "again" : 0,
  "against" : 0,
  "all" : 0,
  "am" : 0,
  "an" : 0,
  "and" : 0,
  "any" : 0,
  "are" : 0,
  "arent" : 0,
  "as" : 0,
  "at" : 0,
  "be" : 0,
  "because" : 0,
  "been" : 0,
  "before" : 0,
  "being" : 0,
  "below" : 0,
  "between" : 0,
  "both" : 0,
  "but" : 0,
  "by" : 0,
  "cant" : 0,
  "cannot" : 0,
  "could" : 0,
  "couldnt" : 0,
  "did" : 0,
  "didnt" : 0,
  "do" : 0,
  "does" : 0,
  "doesnt" : 0,
  "doing" : 0,
  "dont" : 0,
  "down" : 0,
  "during" : 0,
  "each" : 0,
  "few" : 0,
  "for" : 0,
  "from" : 0,
  "further" : 0,
  "had" : 0,
  "hadnt" : 0,
  "has" : 0,
  "hasnt" : 0,
  "have" : 0,
  "havent" : 0,
  "having" : 0,
  "he" : 0,
  "hed" : 0,
  "hell" : 0,
  "hes" : 0,
  "her" : 0,
  "here" : 0,
  "heres" : 0,
  "hers" : 0,
  "herself" : 0,
  "him" : 0,
  "himself" : 0,
  "his" : 0,
  "how" : 0,
  "hows" : 0,
  "i" : 0,
  "id" : 0,
  "ill" : 0,
  "im" : 0,
  "ive" : 0,
  "if" : 0,
  "in" : 0,
  "into" : 0,
  "is" : 0,
  "isnt" : 0,
  "it" : 0,
  "its" : 0,
  "itself" : 0,
  "lets" : 0,
  "me" : 0,
  "more" : 0,
  "most" : 0,
  "mustnt" : 0,
  "my" : 0,
  "myself" : 0,
  "no" : 0,
  "nor" : 0,
  "not" : 0,
  "of" : 0,
  "off" : 0,
  "on" : 0,
  "once" : 0,
  "only" : 0,
  "or" : 0,
  "other" : 0,
  "ought" : 0,
  "our" : 0,
  "ours" : 0,
  "ourselves" : 0,
  "out" : 0,
  "over" : 0,
  "own" : 0,
  "same" : 0,
  "shant" : 0,
  "she" : 0,
  "shed" : 0,
  "shell" : 0,
  "shes" : 0,
  "should" : 0,
  "shouldnt" : 0,
  "so" : 0,
  "some" : 0,
  "such" : 0,
  "than" : 0,
  "that" : 0,
  "thats" : 0,
  "the" : 0,
  "their" : 0,
  "theirs" : 0,
  "them" : 0,
  "themselves" : 0,
  "then" : 0,
  "there" : 0,
  "theres" : 0,
  "these" : 0,
  "they" : 0,
  "theyd" : 0,
  "theyll" : 0,
  "theyre" : 0,
  "theyve" : 0,
  "this" : 0,
  "those" : 0,
  "through" : 0,
  "to" : 0,
  "too" : 0,
  "under" : 0,
  "until" : 0,
  "up" : 0,
  "very" : 0,
  "was" : 0,
  "wasnt" : 0,
  "we" : 0,
  "wed" : 0,
  "well" : 0,
  "weve" : 0,
  "were" : 0,
  "werent" : 0,
  "what" : 0,
  "whats" : 0,
  "when" : 0,
  "whens" : 0,
  "where" : 0,
  "wheres" : 0,
  "which" : 0,
  "while" : 0,
  "who" : 0,
  "whos" : 0,
  "whom" : 0,
  "why" : 0,
  "whys" : 0,
  "with" : 0,
  "wont" : 0,
  "would" : 0,
  "wouldnt" : 0,
  "you" : 0,
  "youd" : 0,
  "youll" : 0,
  "youre" : 0,
  "youve" : 0,
  "your" : 0,
  "yours" : 0,
  "yourself" : 0,
  "yourselves" : 0,
}

// Validates and opens file, returns a scanner to read file
func OpenFile(path string) (*os.File, os.FileInfo, *bufio.Scanner, error) {
  // Open file
  file, err := os.Open(path)
  if err != nil {
    return nil, nil, nil, err
  }

  // Read file line by line
  scanner := bufio.NewScanner(file)

  // Get file metadata
  fileInfo, err := file.Stat()
  if err != nil {
    return nil, nil, nil, err
  }

  // Validate file type
  if valid, err := validFile(path, fileInfo); !valid {
    return nil, nil, nil, err
  }

  return file, fileInfo, scanner, nil
}

// Reads file line by line
func ReadFile(path string) (map[string]*DocumentEntry, error) {
  log.Printf("Working on file %s", path)
  // Open file for reading
  file, fileInfo, scanner, err := OpenFile(path)
  defer file.Close()

  // Something went wrong in opening the file
  if err != nil {
    return nil, err
  }

  // Get file metadata
  fileName := fileInfo.Name()
  fileSize := fileInfo.Size()

  // To store resulting term frequencies
  termCounts := make(map[string]*DocumentEntry)
  position := 0

  // Format each line and then update frequencies
  for scanner.Scan() {
    terms := formatTerms(strings.Fields(scanner.Text()))
    for _, term := range terms {
      if _, present := termCounts[term]; !present {
        termCounts[term] = &DocumentEntry{fileName, path, fileSize, 0, list.New()}
      } else {
        termCounts[term].Frequency++
        termCounts[term].Positions.PushBack(position)
      }
      position++
    }
  }

  // Make sure the scanner didn't fail
  if err := scanner.Err(); err != nil {
    return termCounts, err
  }

  return termCounts, nil
}

// Validates that file is parseable
func validFile(path string, fileInfo os.FileInfo) (bool, error) {

  // Check for regular file
  if fileInfo.IsDir() {
      return false, errors.New("Cannot parse directory")
  }

  // Get file extension
  pathSplit := strings.Split(path, ".")
  if len(pathSplit) == 0 {
    return false, errors.New("File has no extension")
  }

  // Check that file type is supported
  extension := pathSplit[len(pathSplit) - 1]
  if _,present := supportedExtensions[extension]; !present {
    return false, errors.New("Unsupported file type")
  }

  return true, nil
}

// Formats words for term construction
func formatTerms(words []string) []string {
    reg, err := regexp.Compile("[^A-Za-z]+")
    if err != nil {
        panic(err)
    }

  // To store formatted strings
    newWords := make([]string, 0)

  for _, word := range words {
    // Check if a stop word
    if _, inMap := stopWords[strings.ToLower(word)]; !inMap {
      // Strip non alphabetic characters and spaces
      newWord := clearSpaces(reg.ReplaceAllString(word, ""))

      // Make sure there are remaining characters
      if(len(newWord) > 0) {
        newWords = append(newWords, newWord)
      }
    }
  }

  return newWords
}

// Remove all spaces from string
func clearSpaces(str string) string {
    return strings.Map(func(r rune) rune {
        if unicode.IsSpace(r) {
            return -1
        }
        return r
    }, str)
}
<commit_msg>Fixed spacing<commit_after>package docindexing

import (
    "bufio"
    "log"
    "errors"
    "os"
    "strings"
    "regexp"
    "container/list"
    "unicode"
)

var supportedExtensions = map[string]int{
  "txt" : 0,
  "py" : 0,
  "c" : 0,
  "cpp" : 0,
  "go" : 0,
  "java" : 0,
}

// List of stop words to ignore when parsing
var stopWords = map[string]int{
  "a" : 0,
  "about" : 0,
  "above" : 0,
  "after" : 0,
  "again" : 0,
  "against" : 0,
  "all" : 0,
  "am" : 0,
  "an" : 0,
  "and" : 0,
  "any" : 0,
  "are" : 0,
  "arent" : 0,
  "as" : 0,
  "at" : 0,
  "be" : 0,
  "because" : 0,
  "been" : 0,
  "before" : 0,
  "being" : 0,
  "below" : 0,
  "between" : 0,
  "both" : 0,
  "but" : 0,
  "by" : 0,
  "cant" : 0,
  "cannot" : 0,
  "could" : 0,
  "couldnt" : 0,
  "did" : 0,
  "didnt" : 0,
  "do" : 0,
  "does" : 0,
  "doesnt" : 0,
  "doing" : 0,
  "dont" : 0,
  "down" : 0,
  "during" : 0,
  "each" : 0,
  "few" : 0,
  "for" : 0,
  "from" : 0,
  "further" : 0,
  "had" : 0,
  "hadnt" : 0,
  "has" : 0,
  "hasnt" : 0,
  "have" : 0,
  "havent" : 0,
  "having" : 0,
  "he" : 0,
  "hed" : 0,
  "hell" : 0,
  "hes" : 0,
  "her" : 0,
  "here" : 0,
  "heres" : 0,
  "hers" : 0,
  "herself" : 0,
  "him" : 0,
  "himself" : 0,
  "his" : 0,
  "how" : 0,
  "hows" : 0,
  "i" : 0,
  "id" : 0,
  "ill" : 0,
  "im" : 0,
  "ive" : 0,
  "if" : 0,
  "in" : 0,
  "into" : 0,
  "is" : 0,
  "isnt" : 0,
  "it" : 0,
  "its" : 0,
  "itself" : 0,
  "lets" : 0,
  "me" : 0,
  "more" : 0,
  "most" : 0,
  "mustnt" : 0,
  "my" : 0,
  "myself" : 0,
  "no" : 0,
  "nor" : 0,
  "not" : 0,
  "of" : 0,
  "off" : 0,
  "on" : 0,
  "once" : 0,
  "only" : 0,
  "or" : 0,
  "other" : 0,
  "ought" : 0,
  "our" : 0,
  "ours" : 0,
  "ourselves" : 0,
  "out" : 0,
  "over" : 0,
  "own" : 0,
  "same" : 0,
  "shant" : 0,
  "she" : 0,
  "shed" : 0,
  "shell" : 0,
  "shes" : 0,
  "should" : 0,
  "shouldnt" : 0,
  "so" : 0,
  "some" : 0,
  "such" : 0,
  "than" : 0,
  "that" : 0,
  "thats" : 0,
  "the" : 0,
  "their" : 0,
  "theirs" : 0,
  "them" : 0,
  "themselves" : 0,
  "then" : 0,
  "there" : 0,
  "theres" : 0,
  "these" : 0,
  "they" : 0,
  "theyd" : 0,
  "theyll" : 0,
  "theyre" : 0,
  "theyve" : 0,
  "this" : 0,
  "those" : 0,
  "through" : 0,
  "to" : 0,
  "too" : 0,
  "under" : 0,
  "until" : 0,
  "up" : 0,
  "very" : 0,
  "was" : 0,
  "wasnt" : 0,
  "we" : 0,
  "wed" : 0,
  "well" : 0,
  "weve" : 0,
  "were" : 0,
  "werent" : 0,
  "what" : 0,
  "whats" : 0,
  "when" : 0,
  "whens" : 0,
  "where" : 0,
  "wheres" : 0,
  "which" : 0,
  "while" : 0,
  "who" : 0,
  "whos" : 0,
  "whom" : 0,
  "why" : 0,
  "whys" : 0,
  "with" : 0,
  "wont" : 0,
  "would" : 0,
  "wouldnt" : 0,
  "you" : 0,
  "youd" : 0,
  "youll" : 0,
  "youre" : 0,
  "youve" : 0,
  "your" : 0,
  "yours" : 0,
  "yourself" : 0,
  "yourselves" : 0,
}

// Validates and opens file, returns a scanner to read file
func OpenFile(path string) (*os.File, os.FileInfo, *bufio.Scanner, error) {
  // Open file
  file, err := os.Open(path)
  if err != nil {
    return nil, nil, nil, err
  }

  // Read file line by line
  scanner := bufio.NewScanner(file)

  // Get file metadata
  fileInfo, err := file.Stat()
  if err != nil {
    return nil, nil, nil, err
  }

  // Validate file type
  if valid, err := validFile(path, fileInfo); !valid {
    return nil, nil, nil, err
  }

  return file, fileInfo, scanner, nil
}

// Reads file line by line
func ReadFile(path string) (map[string]*DocumentEntry, error) {
  log.Printf("Working on file %s", path)
  // Open file for reading
  file, fileInfo, scanner, err := OpenFile(path)
  defer file.Close()

  // Something went wrong in opening the file
  if err != nil {
    return nil, err
  }

  // Get file metadata
  fileName := fileInfo.Name()
  fileSize := fileInfo.Size()

  // To store resulting term frequencies
  termCounts := make(map[string]*DocumentEntry)
  position := 0

  // Format each line and then update frequencies
  for scanner.Scan() {
    terms := formatTerms(strings.Fields(scanner.Text()))
    for _, term := range terms {
      if _, present := termCounts[term]; !present {
        termCounts[term] = &DocumentEntry{fileName, path, fileSize, 0, list.New()}
      } else {
        termCounts[term].Frequency++
        termCounts[term].Positions.PushBack(position)
      }
      position++
    }
  }

  // Make sure the scanner didn't fail
  if err := scanner.Err(); err != nil {
    return termCounts, err
  }

  return termCounts, nil
}

// Validates that file is parseable
func validFile(path string, fileInfo os.FileInfo) (bool, error) {

  // Check for regular file
  if fileInfo.IsDir() {
      return false, errors.New("Cannot parse directory")
  }

  // Get file extension
  pathSplit := strings.Split(path, ".")
  if len(pathSplit) == 0 {
    return false, errors.New("File has no extension")
  }

  // Check that file type is supported
  extension := pathSplit[len(pathSplit) - 1]
  if _,present := supportedExtensions[extension]; !present {
    return false, errors.New("Unsupported file type")
  }

  return true, nil
}

// Formats words for term construction
func formatTerms(words []string) []string {
    reg, err := regexp.Compile("[^A-Za-z]+")
    if err != nil {
        panic(err)
    }

  // To store formatted strings
  newWords := make([]string, 0)

  for _, word := range words {
    // Check if a stop word
    if _, inMap := stopWords[strings.ToLower(word)]; !inMap {
      // Strip non alphabetic characters and spaces
      newWord := clearSpaces(reg.ReplaceAllString(word, ""))

      // Make sure there are remaining characters
      if(len(newWord) > 0) {
        newWords = append(newWords, newWord)
      }
    }
  }

  return newWords
}

// Remove all spaces from string
func clearSpaces(str string) string {
    return strings.Map(func(r rune) rune {
        if unicode.IsSpace(r) {
            return -1
        }
        return r
    }, str)
}
<|endoftext|>


<commit_before>package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"gopkg.in/dougEfresh/gtoggl.v8"
	"gopkg.in/dougEfresh/gworkspace.v8"
	"gopkg.in/dougEfresh/gclient.v8"
	"gopkg.in/dougEfresh/gproject.v8"
	"os"
	"strconv"
)

type debugger struct {
	debug bool
}

func (l *debugger) Printf(format string, v ...interface{}) {
	if l.debug {
		fmt.Printf(format, v)
	}
}

func main() {
	var debug = flag.Bool("d", false, "Debug")
	var token = flag.String("t", "", "Toggl API token: https://www.toggl.com/app/profile")
	var command = flag.String("c", "workspace", "command: workspace,client,project...etc ")
	flag.Parse()
	tc, err := gtoggl.NewClient(*token, gtoggl.SetTraceLogger(&debugger{debug: *debug}))
	if err != nil {
		fmt.Fprint(os.Stderr, "A token is required\n")
		flag.Usage()
		os.Exit(-1)
	}
	if *command == "workspace" {
		workspace(tc, flag.Args())
	}
	if *command == "client" {
		client(tc, flag.Args())
	}
	if *command == "user" {
		user(tc, flag.Args())
	}
}

func handleError(error error) {
	if error != nil {
		fmt.Fprintln(os.Stderr, error)
		os.Exit(-1)
	}
}

func client(tc *gtoggl.TogglHttpClient, args []string) {
	c, err := gclient.NewClient(tc)
	var client gclient.Client
	handleError(err)
	if len(args) == 0 || args[0] == "list" {
		clients, err := c.List()
		handleError(err)
		fmt.Printf("%+v\n", clients)
	}

	if args[0] == "create" && len(args) > 1 {
		err = json.Unmarshal([]byte(args[1]), &client)
		handleError(err)
		nClient, err := c.Create(&client)
		handleError(err)
		fmt.Printf("%+v\n", nClient)
	}

	if args[0] == "update" && len(args) > 1 {
		err = json.Unmarshal([]byte(args[1]), &client)
		handleError(err)
		_, err = c.Get(client.Id)
		handleError(err)
		nClient, err := c.Update(&client)
		handleError(err)
		fmt.Printf("%+v\n", nClient)
	}

	if args[0] == "get" && len(args) > 1 {
		i, err := strconv.ParseUint(args[1], 0, 64)
		handleError(err)
		nClient, err := c.Get(i)
		handleError(err)
		fmt.Printf("%+v\n", nClient)
	}

	if args[0] == "delete" && len(args) > 1 {
		i, err := strconv.ParseUint(args[1], 0, 64)
		handleError(err)
		err = c.Delete(i)
		handleError(err)
		fmt.Printf("%+v  deleted\n", i)
	}

}

func workspace(tc *gtoggl.TogglHttpClient, args []string) {
	wsc, err := gworkspace.NewClient(tc)
	handleError(err)
	if len(args) == 0 || args[0] == "list" {
		w, err := wsc.List()
		handleError(err)
		fmt.Printf("%+v\n", w)
		return
	}

	if args[0] == "get" && len(args) > 1 {
		i, err := strconv.ParseUint(args[1], 0, 64)
		handleError(err)
		w, err := wsc.Get(i)
		fmt.Printf("%+v\n", w)
		return
	}

	if args[0] == "update" && len(args) > 1 {
		var uWs gworkspace.Workspace
		handleError(err)
		err = json.Unmarshal([]byte(args[1]), &uWs)
		handleError(err)
		_, err := wsc.Get(uWs.Id)
		handleError(err)
		nWs, err := wsc.Update(&uWs)
		handleError(err)
		fmt.Printf("%+v\n", nWs)
		return
	}
}

func user(tc *gtoggl.TogglHttpClient, args []string) {
	wsc, err := guser.NewClient(tc)
	handleError(err)
	if len(args) == 0 || args[0] == "get" {
		w, err := wsc.Get(false)
		handleError(err)
		js, err := json.Marshal(w)
		handleError(err)
		fmt.Printf("%+s\n", js)
		return
	}

	if args[0] == "reset" {
		w, err := wsc.ResetToken()
		handleError(err)
		fmt.Printf("%+v\n", w)
		return
	}

	if args[0] == "update" && len(args) > 1 {
		var uWs guser.User
		handleError(err)
		err = json.Unmarshal([]byte(args[1]), &uWs)
		handleError(err)
		_, err := wsc.Get(true)
		handleError(err)
		nWs, err := wsc.Update(&uWs)
		handleError(err)
		fmt.Printf("%+v\n", nWs)
		return
	}
	if args[0] == "create" && len(args) > 2 {
		var uWs *guser.User
		uWs, err = wsc.Create(args[1], args[2], args[3])
		handleError(err)
		js, err := json.Marshal(uWs)
		handleError(err)
		fmt.Printf("%+s\n", js)
		return
	}

}
<commit_msg>fixing import<commit_after>package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"gopkg.in/dougEfresh/gtoggl.v8"
	"gopkg.in/dougEfresh/gworkspace.v8"
	"gopkg.in/dougEfresh/gclient.v8"
	"gopkg.in/dougEfresh/guser.v8"
	"os"
	"strconv"
)

type debugger struct {
	debug bool
}

func (l *debugger) Printf(format string, v ...interface{}) {
	if l.debug {
		fmt.Printf(format, v)
	}
}

func main() {
	var debug = flag.Bool("d", false, "Debug")
	var token = flag.String("t", "", "Toggl API token: https://www.toggl.com/app/profile")
	var command = flag.String("c", "workspace", "command: workspace,client,project...etc ")
	flag.Parse()
	tc, err := gtoggl.NewClient(*token, gtoggl.SetTraceLogger(&debugger{debug: *debug}))
	if err != nil {
		fmt.Fprint(os.Stderr, "A token is required\n")
		flag.Usage()
		os.Exit(-1)
	}
	if *command == "workspace" {
		workspace(tc, flag.Args())
	}
	if *command == "client" {
		client(tc, flag.Args())
	}
	if *command == "user" {
		user(tc, flag.Args())
	}
}

func handleError(error error) {
	if error != nil {
		fmt.Fprintln(os.Stderr, error)
		os.Exit(-1)
	}
}

func client(tc *gtoggl.TogglHttpClient, args []string) {
	c, err := gclient.NewClient(tc)
	var client gclient.Client
	handleError(err)
	if len(args) == 0 || args[0] == "list" {
		clients, err := c.List()
		handleError(err)
		fmt.Printf("%+v\n", clients)
	}

	if args[0] == "create" && len(args) > 1 {
		err = json.Unmarshal([]byte(args[1]), &client)
		handleError(err)
		nClient, err := c.Create(&client)
		handleError(err)
		fmt.Printf("%+v\n", nClient)
	}

	if args[0] == "update" && len(args) > 1 {
		err = json.Unmarshal([]byte(args[1]), &client)
		handleError(err)
		_, err = c.Get(client.Id)
		handleError(err)
		nClient, err := c.Update(&client)
		handleError(err)
		fmt.Printf("%+v\n", nClient)
	}

	if args[0] == "get" && len(args) > 1 {
		i, err := strconv.ParseUint(args[1], 0, 64)
		handleError(err)
		nClient, err := c.Get(i)
		handleError(err)
		fmt.Printf("%+v\n", nClient)
	}

	if args[0] == "delete" && len(args) > 1 {
		i, err := strconv.ParseUint(args[1], 0, 64)
		handleError(err)
		err = c.Delete(i)
		handleError(err)
		fmt.Printf("%+v  deleted\n", i)
	}

}

func workspace(tc *gtoggl.TogglHttpClient, args []string) {
	wsc, err := gworkspace.NewClient(tc)
	handleError(err)
	if len(args) == 0 || args[0] == "list" {
		w, err := wsc.List()
		handleError(err)
		fmt.Printf("%+v\n", w)
		return
	}

	if args[0] == "get" && len(args) > 1 {
		i, err := strconv.ParseUint(args[1], 0, 64)
		handleError(err)
		w, err := wsc.Get(i)
		fmt.Printf("%+v\n", w)
		return
	}

	if args[0] == "update" && len(args) > 1 {
		var uWs gworkspace.Workspace
		handleError(err)
		err = json.Unmarshal([]byte(args[1]), &uWs)
		handleError(err)
		_, err := wsc.Get(uWs.Id)
		handleError(err)
		nWs, err := wsc.Update(&uWs)
		handleError(err)
		fmt.Printf("%+v\n", nWs)
		return
	}
}

func user(tc *gtoggl.TogglHttpClient, args []string) {
	wsc, err := guser.NewClient(tc)
	handleError(err)
	if len(args) == 0 || args[0] == "get" {
		w, err := wsc.Get(false)
		handleError(err)
		js, err := json.Marshal(w)
		handleError(err)
		fmt.Printf("%+s\n", js)
		return
	}

	if args[0] == "reset" {
		w, err := wsc.ResetToken()
		handleError(err)
		fmt.Printf("%+v\n", w)
		return
	}

	if args[0] == "update" && len(args) > 1 {
		var uWs guser.User
		handleError(err)
		err = json.Unmarshal([]byte(args[1]), &uWs)
		handleError(err)
		_, err := wsc.Get(true)
		handleError(err)
		nWs, err := wsc.Update(&uWs)
		handleError(err)
		fmt.Printf("%+v\n", nWs)
		return
	}
	if args[0] == "create" && len(args) > 2 {
		var uWs *guser.User
		uWs, err = wsc.Create(args[1], args[2], args[3])
		handleError(err)
		js, err := json.Marshal(uWs)
		handleError(err)
		fmt.Printf("%+s\n", js)
		return
	}

}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"io"
	"log"
	"net"
	"os"
	"strconv"
	"strings"
	"syscall"
	"time"

	"github.com/rneugeba/virtsock/go/hvsock"
	"github.com/rneugeba/virtsock/go/vsock"
)

var (
	portstr   string
	sock      string
	detach    bool
	useHVsock bool
)

type vConn interface {
	net.Conn
	CloseRead() error
	CloseWrite() error
}

func init() {
	flag.StringVar(&portstr, "port", "2376", "vsock port to forward")
	flag.StringVar(&sock, "sock", "/var/run/docker.sock", "path of the local Unix domain socket to forward to")
	flag.BoolVar(&detach, "detach", false, "detach from terminal")
}

func main() {
	log.SetFlags(log.LstdFlags)
	flag.Parse()

	if detach {
		logFile, err := os.Create("/var/log/vsudd.log")
		if err != nil {
			log.Fatalln("Failed to open log file", err)
		}
		log.SetOutput(logFile)
		null, err := os.OpenFile("/dev/null", os.O_RDWR, 0)
		if err != nil {
			log.Fatalln("Failed to open /dev/null", err)
		}
		fd := null.Fd()
		syscall.Dup2(int(fd), int(os.Stdin.Fd()))
		syscall.Dup2(int(fd), int(os.Stdout.Fd()))
		syscall.Dup2(int(fd), int(os.Stderr.Fd()))
	}

	var l net.Listener
	if strings.Contains(portstr, "-") {
		svcid, err := hvsock.GuidFromString(portstr)
		if err != nil {
			log.Fatalln("Failed to parse GUID", portstr, err)
		}
		l, err = hvsock.Listen(hvsock.HypervAddr{VmId: hvsock.GUID_WILDCARD, ServiceId: svcid})
		if err != nil {
			log.Fatalf("Failed to bind to hvsock port: %#v", err)
		}
		log.Printf("Listening on ServiceId %s", svcid)
		useHVsock = true
	} else {
		port, err := strconv.ParseUint(portstr, 10, 32)
		if err != nil {
			log.Fatalln("Can't convert %s to a uint.", portstr, err)
		}
		l, err = vsock.Listen(uint(port))
		if err != nil {
			log.Fatalf("Failed to bind to vsock port %u: %#v", port, err)
		}
		log.Printf("Listening on port %u", port)
		useHVsock = false
	}

	connid := 0
	for {
		connid++
		conn, err := l.Accept()
		if err != nil {
			log.Printf("Error accepting connection: %#v", err)
			return // no more listening
		}
		log.Printf("Connection %d from: %s\n", connid, conn.RemoteAddr())

		go handleOne(connid, conn.(vConn))
	}
}

func handleOne(connid int, conn vConn) {
	defer func() {
		if err := conn.Close(); err != nil {
			// On windows we get an EINVAL when the other end already closed
			// Don't bother spilling this into the logs
			if !(useHVsock && err == syscall.EINVAL) {
				log.Println(connid, "Error closing", conn, ":", err)
			}
		}
	}()

	var docker *net.UnixConn
	var err error

	// Cope with the server socket appearing up to 10s later
	for i := 0; i < 200; i++ {
		docker, err = net.DialUnix("unix", nil, &net.UnixAddr{sock, "unix"})
		if err == nil {
			break
		}
		time.Sleep(50 * time.Millisecond)
	}
	if err != nil {
		// If the forwarding program has broken then close and continue
		log.Println(connid, "Failed to connect to Unix domain socket after 10s", sock, err)
		return
	}
	defer func() {
		if err := docker.Close(); err != nil {
			log.Println(connid, "Error closing", docker, ":", err)
		}
	}()

	w := make(chan int64)
	go func() {
		n, err := io.Copy(conn, docker)
		if err != nil {
			log.Println(connid, "error copying from docker to vsock:", err)
		}

		err = docker.CloseRead()
		if err != nil {
			log.Println(connid, "error CloseRead on docker socket:", err)
		}
		err = conn.CloseWrite()
		if err != nil {
			log.Println(connid, "error CloseWrite on vsock:", err)
		}
		w <- n
	}()

	n, err := io.Copy(docker, conn)
	if err != nil {
		log.Println(connid, "error copying from vsock to docker:", err)
	}
	totalRead := n

	err = docker.CloseWrite()
	if err != nil {
		log.Println(connid, "error CloseWrite on docker socket:", err)
	}
	err = conn.CloseRead()
	if err != nil {
		log.Println(connid, "error CloseRead on vsock:", err)
	}

	totalWritten := <-w
	log.Println(connid, "Done. read:", totalRead, "written:", totalWritten)
}
<commit_msg>vsudd: Correctly format go Error's with %s<commit_after>package main

import (
	"flag"
	"io"
	"log"
	"net"
	"os"
	"strconv"
	"strings"
	"syscall"
	"time"

	"github.com/rneugeba/virtsock/go/hvsock"
	"github.com/rneugeba/virtsock/go/vsock"
)

var (
	portstr   string
	sock      string
	detach    bool
	useHVsock bool
)

type vConn interface {
	net.Conn
	CloseRead() error
	CloseWrite() error
}

func init() {
	flag.StringVar(&portstr, "port", "2376", "vsock port to forward")
	flag.StringVar(&sock, "sock", "/var/run/docker.sock", "path of the local Unix domain socket to forward to")
	flag.BoolVar(&detach, "detach", false, "detach from terminal")
}

func main() {
	log.SetFlags(log.LstdFlags)
	flag.Parse()

	if detach {
		logFile, err := os.Create("/var/log/vsudd.log")
		if err != nil {
			log.Fatalln("Failed to open log file", err)
		}
		log.SetOutput(logFile)
		null, err := os.OpenFile("/dev/null", os.O_RDWR, 0)
		if err != nil {
			log.Fatalln("Failed to open /dev/null", err)
		}
		fd := null.Fd()
		syscall.Dup2(int(fd), int(os.Stdin.Fd()))
		syscall.Dup2(int(fd), int(os.Stdout.Fd()))
		syscall.Dup2(int(fd), int(os.Stderr.Fd()))
	}

	var l net.Listener
	if strings.Contains(portstr, "-") {
		svcid, err := hvsock.GuidFromString(portstr)
		if err != nil {
			log.Fatalln("Failed to parse GUID", portstr, err)
		}
		l, err = hvsock.Listen(hvsock.HypervAddr{VmId: hvsock.GUID_WILDCARD, ServiceId: svcid})
		if err != nil {
			log.Fatalf("Failed to bind to hvsock port: %s", err)
		}
		log.Printf("Listening on ServiceId %s", svcid)
		useHVsock = true
	} else {
		port, err := strconv.ParseUint(portstr, 10, 32)
		if err != nil {
			log.Fatalln("Can't convert %s to a uint.", portstr, err)
		}
		l, err = vsock.Listen(uint(port))
		if err != nil {
			log.Fatalf("Failed to bind to vsock port %u: %s", port, err)
		}
		log.Printf("Listening on port %u", port)
		useHVsock = false
	}

	connid := 0
	for {
		connid++
		conn, err := l.Accept()
		if err != nil {
			log.Printf("Error accepting connection: %s", err)
			return // no more listening
		}
		log.Printf("Connection %d from: %s\n", connid, conn.RemoteAddr())

		go handleOne(connid, conn.(vConn))
	}
}

func handleOne(connid int, conn vConn) {
	defer func() {
		if err := conn.Close(); err != nil {
			// On windows we get an EINVAL when the other end already closed
			// Don't bother spilling this into the logs
			if !(useHVsock && err == syscall.EINVAL) {
				log.Println(connid, "Error closing", conn, ":", err)
			}
		}
	}()

	var docker *net.UnixConn
	var err error

	// Cope with the server socket appearing up to 10s later
	for i := 0; i < 200; i++ {
		docker, err = net.DialUnix("unix", nil, &net.UnixAddr{sock, "unix"})
		if err == nil {
			break
		}
		time.Sleep(50 * time.Millisecond)
	}
	if err != nil {
		// If the forwarding program has broken then close and continue
		log.Println(connid, "Failed to connect to Unix domain socket after 10s", sock, err)
		return
	}
	defer func() {
		if err := docker.Close(); err != nil {
			log.Println(connid, "Error closing", docker, ":", err)
		}
	}()

	w := make(chan int64)
	go func() {
		n, err := io.Copy(conn, docker)
		if err != nil {
			log.Println(connid, "error copying from docker to vsock:", err)
		}

		err = docker.CloseRead()
		if err != nil {
			log.Println(connid, "error CloseRead on docker socket:", err)
		}
		err = conn.CloseWrite()
		if err != nil {
			log.Println(connid, "error CloseWrite on vsock:", err)
		}
		w <- n
	}()

	n, err := io.Copy(docker, conn)
	if err != nil {
		log.Println(connid, "error copying from vsock to docker:", err)
	}
	totalRead := n

	err = docker.CloseWrite()
	if err != nil {
		log.Println(connid, "error CloseWrite on docker socket:", err)
	}
	err = conn.CloseRead()
	if err != nil {
		log.Println(connid, "error CloseRead on vsock:", err)
	}

	totalWritten := <-w
	log.Println(connid, "Done. read:", totalRead, "written:", totalWritten)
}
<|endoftext|>


<commit_before>package analysis

import (
	"fmt"
	"strings"

	"github.com/skypies/geo"
	"github.com/skypies/geo/sfo"
	"github.com/skypies/util/date"

	fdb "github.com/skypies/flightdb2"
	"github.com/skypies/flightdb2/report"
)

func init() {
	report.HandleReport("approachsignature", ApproachSignature, "Signature for SFO approaches")
}

func ApproachSignature(r *report.Report, f *fdb.Flight, tis []fdb.TrackIntersection) (report.FlightReportOutcome, error){
	r.I["[C] Flights considered for signatures"]++

	// Maybe make this configurable later
	equipPrefix := "B73"
	dest := "SFO"
	reqWaypoints := []string{"EPICK","EDDYY","SWELS"}
	sigDistNMs := []float64{41.1, 37.5, 34.5, 33.5}

	if f.Destination != dest {
		r.I[fmt.Sprintf("[D] dest not %s", dest)]++
		return report.RejectedByReport, nil
	}
	if ! strings.HasPrefix(f.EquipmentType, equipPrefix) {
		//if ! regexp.MustCompile("^").MatchString(f.EquipmentType) {
		r.I[fmt.Sprintf("[D] equip didn't have prefix %s", equipPrefix)]++
		return report.RejectedByReport, nil
	}
	for _,wpName := range reqWaypoints {
		if ! f.HasWaypoint(wpName) {
			r.I[fmt.Sprintf("[D] didn't hit waypoint %s", wpName)]++
			return report.RejectedByReport, nil
		}
	}

	trackName,track := f.PreferredTrack([]string{"ADSB", "MLAT", "FOIA"})
	if trackName == "" {
		r.I["[D] Skipped, no ADSB, MLAT or FOIA track avail"]++
		return report.RejectedByReport,nil
	}

	r.I["[D] flight accepted"]++

	track.PostProcess()
	track.AdjustAltitudes(&r.Archive)

	sigDistKMs := []float64{}
	for _,v := range sigDistNMs { sigDistKMs = append(sigDistKMs, geo.NM2KM(v)) }
	results := track.IndicesAtDistKMsFrom(sfo.KAirports["KSFO"], sigDistKMs)

	tEpick := date.InPdt(f.Waypoints["EPICK"])
	htmlRow := []string{
		r.Links(f),
		"" + f.IdentString() + "",
		"" + f.EquipmentType + "",
		trackName,
		tEpick.Format("15:04:05"),
//		fmt.Sprintf("%v", results),
	}
	textRow := []string{
		f.IdentString(),
		f.EquipmentType,
		trackName,
		tEpick.Format("2006/01/02"),
		tEpick.Format("15:04:05"),
	}

	for i,result := range results {
		tp := track[result]

		dist := tp.DistNM(sfo.KAirports["KSFO"])
		track[result].AnalysisMapIcon = "red-large"

		track[result].AnalysisAnnotation += fmt.Sprintf("* Signature at %.1fNM, from %v\n",
			dist, sigDistNMs)

		tpVals := []string{
			fmt.Sprintf("{alt,pressurealt,angle,accel}@%.1fNM", sigDistNMs[i]),
			fmt.Sprintf("%.0f", tp.IndicatedAltitude),
			fmt.Sprintf("%.0f", tp.Altitude),
			fmt.Sprintf("%.2f", tp.AngleOfInclination),
			fmt.Sprintf("%.2f", tp.GroundAccelerationKPS),
		}
		htmlRow = append(htmlRow, tpVals...)
		textRow = append(textRow, tpVals...)
	}

	r.AddRow(&htmlRow, &textRow)

	return report.Accepted, nil
}
<commit_msg>Add timestamps to the per-trackpoint output<commit_after>package analysis

import (
	"fmt"
	"strings"

	"github.com/skypies/geo"
	"github.com/skypies/geo/sfo"
	"github.com/skypies/util/date"

	fdb "github.com/skypies/flightdb2"
	"github.com/skypies/flightdb2/report"
)

func init() {
	report.HandleReport("approachsignature", ApproachSignature, "Signature for SFO approaches")
}

func ApproachSignature(r *report.Report, f *fdb.Flight, tis []fdb.TrackIntersection) (report.FlightReportOutcome, error){
	r.I["[C] Flights considered for signatures"]++

	// Maybe make this configurable later
	equipPrefix := "B73"
	dest := "SFO"
	reqWaypoints := []string{"EPICK","EDDYY","SWELS"}
	sigDistNMs := []float64{41.1, 37.5, 34.5, 33.5}

	if f.Destination != dest {
		r.I[fmt.Sprintf("[D] dest not %s", dest)]++
		return report.RejectedByReport, nil
	}
	if ! strings.HasPrefix(f.EquipmentType, equipPrefix) {
		//if ! regexp.MustCompile("^").MatchString(f.EquipmentType) {
		r.I[fmt.Sprintf("[D] equip didn't have prefix %s", equipPrefix)]++
		return report.RejectedByReport, nil
	}
	for _,wpName := range reqWaypoints {
		if ! f.HasWaypoint(wpName) {
			r.I[fmt.Sprintf("[D] didn't hit waypoint %s", wpName)]++
			return report.RejectedByReport, nil
		}
	}

	trackName,track := f.PreferredTrack([]string{"ADSB", "MLAT", "FOIA"})
	if trackName == "" {
		r.I["[D] Skipped, no ADSB, MLAT or FOIA track avail"]++
		return report.RejectedByReport,nil
	}

	r.I["[D] flight accepted"]++

	track.PostProcess()
	track.AdjustAltitudes(&r.Archive)

	sigDistKMs := []float64{}
	for _,v := range sigDistNMs { sigDistKMs = append(sigDistKMs, geo.NM2KM(v)) }
	results := track.IndicesAtDistKMsFrom(sfo.KAirports["KSFO"], sigDistKMs)

	tEpick := date.InPdt(f.Waypoints["EPICK"])
	htmlRow := []string{
		r.Links(f),
		"" + f.IdentString() + "",
		"" + f.EquipmentType + "",
		trackName,
		tEpick.Format("15:04:05"),
//		fmt.Sprintf("%v", results),
	}
	textRow := []string{
		f.IdentString(),
		f.EquipmentType,
		trackName,
		tEpick.Format("2006/01/02"),
		tEpick.Format("15:04:05"),
	}

	for i,result := range results {
		tp := track[result]

		dist := tp.DistNM(sfo.KAirports["KSFO"])
		track[result].AnalysisMapIcon = "red-large"

		track[result].AnalysisAnnotation += fmt.Sprintf("* Signature at %.1fNM, from %v\n",
			dist, sigDistNMs)

		tpVals := []string{
			fmt.Sprintf("{time,alt,pressurealt,angle,accel}@%.1fNM", sigDistNMs[i]),
			date.InPdt(tp.TimestampUTC).Format("15:04:05"),
			fmt.Sprintf("%.0f", tp.IndicatedAltitude),
			fmt.Sprintf("%.0f", tp.Altitude),
			fmt.Sprintf("%.2f", tp.AngleOfInclination),
			fmt.Sprintf("%.2f", tp.GroundAccelerationKPS),
		}
		htmlRow = append(htmlRow, tpVals...)
		textRow = append(textRow, tpVals...)
	}

	r.AddRow(&htmlRow, &textRow)

	return report.Accepted, nil
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"ironman/build"
	"ironman/callbacks"
	"ironman/config"
	"ironman/logging"
	"log"
	"net/http"
)

var cfgFlag = flag.String("config", "ironman.json", "JSON formatted config")

func main() {
	flag.Parse()

	c := config.FromFile(*cfgFlag)

	jobs := make(chan logging.Job, 100)
	b := logging.Buildlog{}

	go build.Build(jobs, &c, &b)

	http.HandleFunc("/callback/", func(w http.ResponseWriter, r *http.Request) {
		callbacks.Callback(w, r, jobs)
	})
	log.Fatal(http.ListenAndServe(":8082", nil))

	log.Println("Ironman up an running!")
}
<commit_msg>move start print up so it is actually printed<commit_after>package main

import (
	"flag"
	"ironman/build"
	"ironman/callbacks"
	"ironman/config"
	"ironman/logging"
	"log"
	"net/http"
)

var cfgFlag = flag.String("config", "ironman.json", "JSON formatted config")

func main() {
	flag.Parse()

	c := config.FromFile(*cfgFlag)

	jobs := make(chan logging.Job, 100)
	b := logging.Buildlog{}

	go build.Build(jobs, &c, &b)

	log.Println("Ironman up an running!")

	http.HandleFunc("/callback/", func(w http.ResponseWriter, r *http.Request) {
		callbacks.Callback(w, r, jobs)
	})
	log.Fatal(http.ListenAndServe(":8082", nil))
}
<|endoftext|>


<commit_before>package gojenkins

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
)

type Auth struct {
	Username string
	ApiToken string
}

type Jenkins struct {
	auth    *Auth
	baseUrl string
}

func NewJenkins(auth *Auth, baseUrl string) *Jenkins {
	return &Jenkins{
		auth:    auth,
		baseUrl: baseUrl,
	}
}

func (jenkins *Jenkins) buildUrl(path string, params url.Values) (requestUrl string) {
	requestUrl = jenkins.baseUrl + path + "/api/json"
	if params != nil {
		queryString := params.Encode()
		if queryString != "" {
			requestUrl = requestUrl + "?" + queryString
		}
	}

	return
}

func (jenkins *Jenkins) sendRequest(req *http.Request) (*http.Response, error) {
	req.SetBasicAuth(jenkins.auth.Username, jenkins.auth.ApiToken)
	return http.DefaultClient.Do(req)
}

func (jenkins *Jenkins) parseResponse(resp *http.Response, body interface{}) (err error) {
	defer resp.Body.Close()

	if body == nil {
		return
	}

	data, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return
	}

	return json.Unmarshal(data, body)
}

func (jenkins *Jenkins) get(path string, params url.Values, body interface{}) (err error) {
	requestUrl := jenkins.buildUrl(path, params)
	req, err := http.NewRequest("GET", requestUrl, nil)
	if err != nil {
		return
	}

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return
	}

	return jenkins.parseResponse(resp, body)
}

func (jenkins *Jenkins) post(path string, params url.Values, body interface{}) (err error) {
	requestUrl := jenkins.buildUrl(path, params)
	req, err := http.NewRequest("POST", requestUrl, nil)
	if err != nil {
		return
	}

	resp, err := jenkins.sendRequest(req)
	if err != nil {
		return
	}

	return jenkins.parseResponse(resp, body)
}

func (jenkins *Jenkins) GetJobs() (jobs []Job, err error) {
	var payload = struct {
		Jobs []Job `json:"jobs"`
	}{
		Jobs: jobs,
	}
	err = jenkins.get("", nil, &payload)
	return
}

func (jenkins *Jenkins) GetJob(name string) (job Job, err error) {
	err = jenkins.get(fmt.Sprintf("/job/%s", name), nil, &job)
	return
}

func (jenkins *Jenkins) GetBuild(job Job, number int) (build Build, err error) {
	err = jenkins.get(fmt.Sprintf("/job/%s/%d", job.Name, number), nil, &build)
	return
}

// Create a new build for this job.
// Params can be nil.
func (jenkins *Jenkins) Build(job Job, params url.Values) error {
	if params == nil {
		return jenkins.post(fmt.Sprintf("/job/%s/buildWithParameters", job.Name), params, nil)
	} else {
		return jenkins.post(fmt.Sprintf("/job/%s/build", job.Name), params, nil)
	}
}
<commit_msg>fix buildWithParameters URLs - logic was reversed<commit_after>package gojenkins

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
)

type Auth struct {
	Username string
	ApiToken string
}

type Jenkins struct {
	auth    *Auth
	baseUrl string
}

func NewJenkins(auth *Auth, baseUrl string) *Jenkins {
	return &Jenkins{
		auth:    auth,
		baseUrl: baseUrl,
	}
}

func (jenkins *Jenkins) buildUrl(path string, params url.Values) (requestUrl string) {
	requestUrl = jenkins.baseUrl + path + "/api/json"
	if params != nil {
		queryString := params.Encode()
		if queryString != "" {
			requestUrl = requestUrl + "?" + queryString
		}
	}

	return
}

func (jenkins *Jenkins) sendRequest(req *http.Request) (*http.Response, error) {
	req.SetBasicAuth(jenkins.auth.Username, jenkins.auth.ApiToken)
	return http.DefaultClient.Do(req)
}

func (jenkins *Jenkins) parseResponse(resp *http.Response, body interface{}) (err error) {
	defer resp.Body.Close()

	if body == nil {
		return
	}

	data, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return
	}

	return json.Unmarshal(data, body)
}

func (jenkins *Jenkins) get(path string, params url.Values, body interface{}) (err error) {
	requestUrl := jenkins.buildUrl(path, params)
	req, err := http.NewRequest("GET", requestUrl, nil)
	if err != nil {
		return
	}

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return
	}

	return jenkins.parseResponse(resp, body)
}

func (jenkins *Jenkins) post(path string, params url.Values, body interface{}) (err error) {
	requestUrl := jenkins.buildUrl(path, params)
	req, err := http.NewRequest("POST", requestUrl, nil)
	if err != nil {
		return
	}

	resp, err := jenkins.sendRequest(req)
	if err != nil {
		return
	}

	return jenkins.parseResponse(resp, body)
}

func (jenkins *Jenkins) GetJobs() (jobs []Job, err error) {
	var payload = struct {
		Jobs []Job `json:"jobs"`
	}{
		Jobs: jobs,
	}
	err = jenkins.get("", nil, &payload)
	return
}

func (jenkins *Jenkins) GetJob(name string) (job Job, err error) {
	err = jenkins.get(fmt.Sprintf("/job/%s", name), nil, &job)
	return
}

func (jenkins *Jenkins) GetBuild(job Job, number int) (build Build, err error) {
	err = jenkins.get(fmt.Sprintf("/job/%s/%d", job.Name, number), nil, &build)
	return
}

// Create a new build for this job.
// Params can be nil.
func (jenkins *Jenkins) Build(job Job, params url.Values) error {
	if params == nil {
		return jenkins.post(fmt.Sprintf("/job/%s/build", job.Name), params, nil)
	} else {
		return jenkins.post(fmt.Sprintf("/job/%s/buildWithParameters", job.Name), params, nil)
	}
}
<|endoftext|>