<commit_before>package tradier

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

const (
	libraryVersion = "0.0.1"
	defaultBaseURL = "https://api.tradier.com/v1/"
	userAgent      = "go-tradier/" + libraryVersion
)

// Client takes care of managing communication to the Tradier API.
type Client struct {
	clientMu sync.Mutex
	client   *http.Client

	BaseURL *url.URL

	UserAgent string

	common service

	User       *UserService
	Account    *AccountService
	Order      *OrderService
	Watchlists *WatchlistsService
	Markets    *MarketsService
}

type service struct {
	client *Client
}

// NewClient creates a new Tradier API client.
// To use API methods that require authentication, an http.Client that performs proper authentication must be provided, such as one created with the golang.org/x/oauth2 library.
func NewClient(httpClient *http.Client) *Client {
	if httpClient == nil {
		httpClient = http.DefaultClient
	}

	baseURL, _ := url.Parse(defaultBaseURL)

	c := &Client{
		client:    httpClient,
		BaseURL:   baseURL,
		UserAgent: userAgent,
	}

	c.common.client = c
	c.User = (*UserService)(&c.common)
	c.Account = (*AccountService)(&c.common) // FIXME: should be AccountsService
	c.Order = (*OrderService)(&c.common)     // FIXME: should be OrdersService
	c.Watchlists = (*WatchlistsService)(&c.common)
	c.Markets = (*MarketsService)(&c.common)

	return c
}

// NewRequest creates an API request.
func (c *Client) NewRequest(method, urlStr string, body interface{}) (*http.Request, error) {
	rel, err := url.Parse(urlStr)
	if err != nil {
		return nil, err
	}

	u := c.BaseURL.ResolveReference(rel)

	// If the body is not empty, assume it's a form data
	var buf io.ReadWriter
	if body != nil {
		buf = bytes.NewBufferString(body.(string))
	}

	req, err := http.NewRequest(method, u.String(), buf)
	if err != nil {
		return nil, err
	}

	// Always request JSON
	req.Header.Set("Accept", "application/json")

	if body != nil {
		req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	}

	if c.UserAgent != "" {
		req.Header.Set("User-Agent", c.UserAgent)
	}
	return req, nil
}

// Response is the Tradier API response. It wraps the standard http.Response from Traider along with rate limits from the header so that it can be more easily accessed
type Response struct {
	*http.Response
	// Rate
}

func newResponse(r *http.Response) *Response {
	response := &Response{Response: r}
	// response.Rate = parseRate(r)
	return response
}

// Do sends an API request and returns the response. It accepts an empty interface{} used to unmarshal the response into that object instance.
func (c *Client) Do(req *http.Request, v interface{}) (*Response, error) {
	// TODO: Do rate limit checking

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

	defer func() {
		// Drain up to 512 bytes and close the body to let the Transport reuse the connection
		io.CopyN(ioutil.Discard, resp.Body, 512)
		resp.Body.Close()
	}()

	response := newResponse(resp)
	err = checkResponse(resp)
	if err != nil {
		return response, err
	}

	// Write to interface that implements io.Writer if one is provided
	if v != nil {
		if w, ok := v.(io.Writer); ok {
			io.Copy(w, resp.Body)
		} else {
			err = json.NewDecoder(resp.Body).Decode(v)
			if err == io.EOF {
				err = nil // ignore EOF errors caused by empty response body
			}
		}
	}

	return response, err
}

func checkResponse(r *http.Response) error {
	if c := r.StatusCode; 200 <= c && c <= 299 {
		return nil
	}

	// Read response body
	data, err := ioutil.ReadAll(r.Body)
	if err != nil && data != nil {
		return err
	}

	switch {
	case r.StatusCode == http.StatusUnauthorized:
		return fmt.Errorf("%s", data)
	}

	return nil
}

// Bool is a helper routine that allocates a new bool value
// to store v and returns a pointer to it.
func Bool(v bool) *bool { return &v }

// Int is a helper routine that allocates a new int value
// to store v and returns a pointer to it.
func Int(v int) *int { return &v }

// Int64 is a helper routine that allocates a new int64 value
// to store v and returns a pointer to it.
func Int64(v int64) *int64 { return &v }

// String is a helper routine that allocates a new string value
// to store v and returns a pointer to it.
func String(v string) *string { return &v }

// Float64 is a helper routine that allocates a new float64 value
// to store v and returns a pointer to it.
func Float64(v float64) *float64 { return &v }
<commit_msg>checkResponse now properly returns response error<commit_after>package tradier

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

const (
	libraryVersion = "0.0.1"
	defaultBaseURL = "https://api.tradier.com/v1/"
	userAgent      = "go-tradier/" + libraryVersion
)

// Client takes care of managing communication to the Tradier API.
type Client struct {
	clientMu sync.Mutex
	client   *http.Client

	BaseURL *url.URL

	UserAgent string

	common service

	User       *UserService
	Account    *AccountService
	Order      *OrderService
	Watchlists *WatchlistsService
	Markets    *MarketsService
}

type service struct {
	client *Client
}

// NewClient creates a new Tradier API client.
// To use API methods that require authentication, an http.Client that performs proper authentication must be provided, such as one created with the golang.org/x/oauth2 library.
func NewClient(httpClient *http.Client) *Client {
	if httpClient == nil {
		httpClient = http.DefaultClient
	}

	baseURL, _ := url.Parse(defaultBaseURL)

	c := &Client{
		client:    httpClient,
		BaseURL:   baseURL,
		UserAgent: userAgent,
	}

	c.common.client = c
	c.User = (*UserService)(&c.common)
	c.Account = (*AccountService)(&c.common) // FIXME: should be AccountsService
	c.Order = (*OrderService)(&c.common)     // FIXME: should be OrdersService
	c.Watchlists = (*WatchlistsService)(&c.common)
	c.Markets = (*MarketsService)(&c.common)

	return c
}

// NewRequest creates an API request.
func (c *Client) NewRequest(method, urlStr string, body interface{}) (*http.Request, error) {
	rel, err := url.Parse(urlStr)
	if err != nil {
		return nil, err
	}

	u := c.BaseURL.ResolveReference(rel)

	// If the body is not empty, assume it's a form data
	var buf io.ReadWriter
	if body != nil {
		buf = bytes.NewBufferString(body.(string))
	}

	req, err := http.NewRequest(method, u.String(), buf)
	if err != nil {
		return nil, err
	}

	// Always request JSON
	req.Header.Set("Accept", "application/json")

	if body != nil {
		req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	}

	if c.UserAgent != "" {
		req.Header.Set("User-Agent", c.UserAgent)
	}
	return req, nil
}

// Response is the Tradier API response. It wraps the standard http.Response from Traider along with rate limits from the header so that it can be more easily accessed
type Response struct {
	*http.Response
	// Rate
}

func newResponse(r *http.Response) *Response {
	response := &Response{Response: r}
	// response.Rate = parseRate(r)
	return response
}

// Do sends an API request and returns the response. It accepts an empty interface{} used to unmarshal the response into that object instance.
func (c *Client) Do(req *http.Request, v interface{}) (*Response, error) {
	// TODO: Do rate limit checking

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

	defer func() {
		// Drain up to 512 bytes and close the body to let the Transport reuse the connection
		io.CopyN(ioutil.Discard, resp.Body, 512)
		resp.Body.Close()
	}()

	response := newResponse(resp)
	err = checkResponse(resp)
	if err != nil {
		return response, err
	}

	// Write to interface that implements io.Writer if one is provided
	if v != nil {
		if w, ok := v.(io.Writer); ok {
			io.Copy(w, resp.Body)
		} else {
			err = json.NewDecoder(resp.Body).Decode(v)
			if err == io.EOF {
				err = nil // ignore EOF errors caused by empty response body
			}
		}
	}

	return response, err
}

func checkResponse(r *http.Response) error {
	if c := r.StatusCode; 200 <= c && c <= 299 {
		return nil
	}

	// Read response body
	data, err := ioutil.ReadAll(r.Body)
	if err != nil && data != nil {
		return err
	}

	// NOTE: The switch is to stuff error information into an ErrorResponse struct, yet to be implemented
	switch {
	case r.StatusCode == http.StatusUnauthorized:
		return fmt.Errorf("%s", data)
	}

	return fmt.Errorf("%s", data)
}

// Bool is a helper routine that allocates a new bool value
// to store v and returns a pointer to it.
func Bool(v bool) *bool { return &v }

// Int is a helper routine that allocates a new int value
// to store v and returns a pointer to it.
func Int(v int) *int { return &v }

// Int64 is a helper routine that allocates a new int64 value
// to store v and returns a pointer to it.
func Int64(v int64) *int64 { return &v }

// String is a helper routine that allocates a new string value
// to store v and returns a pointer to it.
func String(v string) *string { return &v }

// Float64 is a helper routine that allocates a new float64 value
// to store v and returns a pointer to it.
func Float64(v float64) *float64 { return &v }
<|endoftext|>


<commit_before>// Copyright Roger Booth (roger.booth@gmail.com)
// In the event that you find this code useful, feel free to provide attribution :)

package main

import (
	"container/ring"
	"fmt"
	"time"
)

type Color string

var colors = [...]Color{"white", "blue", "red", "yellow", "orange", "green"}
var intForColor = map[Color]int {
	"white": 0,
	"blue": 1,
	"red": 2,
	"yellow": 3,
	"orange": 4,
	"green": 5
}
var edgesForFace = map[Color][]Color{
	"white":  {"red", "green", "orange", "blue"},
	"blue":   {"white", "orange", "yellow", "red"},
	"red":    {"blue", "yellow", "green", "white"},
	"yellow": {"green", "red", "blue", "orange"},
	"orange": {"yellow", "blue", "white", "green"},
	"green":  {"orange", "white", "red", "yellow"},
}

// Based on the coordinate system I discovered in 1984
var edgePos = [...]int{0, 7, 6, 4, 3, 2, 6, 5, 4, 2, 1, 0}

// Pair cube faces by front-to-front projection
// Eight cubes X six faces
var straightProjection = [...][...]int {
	{1,3,4,1,4,3},
	{0,2,5,0,5,2},
	{3,1,6,3,6,1},
	{2,0,7,2,7,0},
	{5,7,0,5,0,7},
	{4,6,1,4,1,6},
	{7,5,2,7,2,5},
	{6,4,3,6,3,4}
}

type Face [8]Color

type Edge [12]*Color

type Cube struct {
	faceMap map[Color]*Face
	edgeMap map[Color]Edge
}

type Entanglement [8]*Cube

func NewCube() (*Cube, error) {
	newFaceMap := make(map[Color]*Face)
	newEdgeMap := make(map[Color]Edge)
	for _, color := range colors {
		newFaceMap[color] = &Face{color, color, color, color, color, color, color, color}
	}
	i := 0
	for _, faceColor := range colors {
		var newEdge Edge
		for _, edgeColor := range edgesForFace[faceColor] {
		        //fmt.Println(faceColor)
		        //fmt.Println(i)
			newEdge[i] = &newFaceMap[edgeColor][edgePos[i]]
			newEdge[i+1] = &newFaceMap[edgeColor][edgePos[i+1]]
			newEdge[i+2] = &newFaceMap[edgeColor][edgePos[i+2]]
			i += 3
			if i == 12 {
			    i = 0
			}
		}
		newEdgeMap[faceColor] = newEdge
	}
	return &Cube{newFaceMap, newEdgeMap}, nil
}

func NewEntanglement() (*Entanglement, error) {
        var newEntanglement Entanglement
	for i:=0; i<8; i++{
		newEntanglement[i], _ = NewCube();
	}
	return &newEntanglement, nil
}

type ThreeDTransformer struct {
	faceRing *ring.Ring
	edgeRing *ring.Ring
}

func ThreeDRotate(op ThreeDOperation) error {

        newFaceRing := ring.New(8)
        newEdgeRing := ring.New(12)
        trx := ThreeDTransformer{
		newFaceRing,newEdgeRing}
	for _, faceColor := range op.ent[op.cubeId].faceMap[op.face] {
		trx.faceRing.Value = faceColor
		trx.faceRing = trx.faceRing.Next()
	}
	for _, edgeColorPtr := range op.ent[op.cubeId].edgeMap[op.face] {
		trx.edgeRing.Value = *edgeColorPtr
		trx.edgeRing = trx.edgeRing.Next()
	}

	trx.faceRing = trx.faceRing.Move(2*op.direction)
	trx.edgeRing = trx.edgeRing.Move(3*op.direction)

	for i, _ := range ent[op.cubeId].faceMap[op.face] {
	        if v,ok := trx.faceRing.Value.(Color); ok {
		    op.ent[op.cubeId].faceMap[op.face][i] = v
		}
		trx.faceRing = trx.faceRing.Next()
	}
	for i, _ := range ent[op.cubeId].edgeMap[op.face] {
	        if v,ok := trx.edgeRing.Value.(Color); ok {
		    *op.ent[cubeId].edgeMap[face][i] = v
		}
		trx.edgeRing = trx.edgeRing.Next()
	}

    return nil
}

func Sister(cubeId int, face Color) (sisterCubeId int, sisterFace Color){
	sisterCubeId = straightProjection[cubeId][intForColor[Color]]
	sisterFace = face
}

type ThreeDOperation struct {
	ent *Entanglement
	cubeId int
	face Color
	direction int
	primary bool
}

func SplitMessage(op ThreeDOperation, opchan channel ThreeDOperation) error {
	sisterCubeId, sisterFace = Sister(op.CubeId, op.face)
	op.primary = false
	opchan <- op
	var sisterOp = ThreeDOperation {op.ent, sisterCubeId, sisterFace,op.direction, false}
	opchan <- sisterOp
}

func countDown(count chan int) {
	for i := 20 ; i >= 0 ; i-- {
		count <- i
		time.Sleep(1000000000)
	}
}

func takeSample(ent *Entanglement) {
	fmt.Println(ent[0].faceMap["red"][1])
	fmt.Println(ent[0].faceMap["red"][2])
	fmt.Println(*ent[0].edgeMap["red"][2])
	fmt.Println(*ent[0].edgeMap["red"][3])
	fmt.Println(*ent[0].edgeMap["red"][8])
	fmt.Println(*ent[0].edgeMap["red"][11])
}

func main() {
	entanglement1,_ := NewEntanglement()
	operations := make(chan ThreeDOperation)
	count := make(chan int)
        go player1(operations)
	go player2(operations)
	go countDown(count)
	for {
		select {
			case o := <- operations:
			    if o.primary {
					SplitMessage(o, operations)
				} else {
					ThreeDRotate(o)
				}

			case i := <- count:
				if 0 == i {
					takeSample(entanglement1)
					return
				}
				fmt.Printf("%d seconds remaining\n", i)
		}
	}
}
<commit_msg>Introduce a couple "sims" for testing.<commit_after>// Copyright Roger Booth (roger.booth@gmail.com)
// In the event that you find this code useful, feel free to provide attribution :)

package main

import (
	"container/ring"
	"fmt"
	"time"
)

type Color string

var colors = [...]Color{"white", "blue", "red", "yellow", "orange", "green"}
var intForColor = map[Color]int {
	"white": 0,
	"blue": 1,
	"red": 2,
	"yellow": 3,
	"orange": 4,
	"green": 5
}
var edgesForFace = map[Color][]Color{
	"white":  {"red", "green", "orange", "blue"},
	"blue":   {"white", "orange", "yellow", "red"},
	"red":    {"blue", "yellow", "green", "white"},
	"yellow": {"green", "red", "blue", "orange"},
	"orange": {"yellow", "blue", "white", "green"},
	"green":  {"orange", "white", "red", "yellow"},
}

// Based on the coordinate system I discovered in 1984
var edgePos = [...]int{0, 7, 6, 4, 3, 2, 6, 5, 4, 2, 1, 0}

// Pair cube faces by front-to-front projection
// Eight cubes X six faces
var straightProjection = [...][...]int {
	{1,3,4,1,4,3},
	{0,2,5,0,5,2},
	{3,1,6,3,6,1},
	{2,0,7,2,7,0},
	{5,7,0,5,0,7},
	{4,6,1,4,1,6},
	{7,5,2,7,2,5},
	{6,4,3,6,3,4}
}

type Face [8]Color

type Edge [12]*Color

type Cube struct {
	faceMap map[Color]*Face
	edgeMap map[Color]Edge
}

type Entanglement [8]*Cube

func NewCube() (*Cube, error) {
	newFaceMap := make(map[Color]*Face)
	newEdgeMap := make(map[Color]Edge)
	for _, color := range colors {
		newFaceMap[color] = &Face{color, color, color, color, color, color, color, color}
	}
	i := 0
	for _, faceColor := range colors {
		var newEdge Edge
		for _, edgeColor := range edgesForFace[faceColor] {
		        //fmt.Println(faceColor)
		        //fmt.Println(i)
			newEdge[i] = &newFaceMap[edgeColor][edgePos[i]]
			newEdge[i+1] = &newFaceMap[edgeColor][edgePos[i+1]]
			newEdge[i+2] = &newFaceMap[edgeColor][edgePos[i+2]]
			i += 3
			if i == 12 {
			    i = 0
			}
		}
		newEdgeMap[faceColor] = newEdge
	}
	return &Cube{newFaceMap, newEdgeMap}, nil
}

func NewEntanglement() (*Entanglement, error) {
        var newEntanglement Entanglement
	for i:=0; i<8; i++{
		newEntanglement[i], _ = NewCube();
	}
	return &newEntanglement, nil
}

type ThreeDTransformer struct {
	faceRing *ring.Ring
	edgeRing *ring.Ring
}

func ThreeDRotate(op ThreeDOperation) error {

        newFaceRing := ring.New(8)
        newEdgeRing := ring.New(12)
        trx := ThreeDTransformer{
		newFaceRing,newEdgeRing}
	for _, faceColor := range op.ent[op.cubeId].faceMap[op.face] {
		trx.faceRing.Value = faceColor
		trx.faceRing = trx.faceRing.Next()
	}
	for _, edgeColorPtr := range op.ent[op.cubeId].edgeMap[op.face] {
		trx.edgeRing.Value = *edgeColorPtr
		trx.edgeRing = trx.edgeRing.Next()
	}

	trx.faceRing = trx.faceRing.Move(2*op.direction)
	trx.edgeRing = trx.edgeRing.Move(3*op.direction)

	for i, _ := range ent[op.cubeId].faceMap[op.face] {
	        if v,ok := trx.faceRing.Value.(Color); ok {
		    op.ent[op.cubeId].faceMap[op.face][i] = v
		}
		trx.faceRing = trx.faceRing.Next()
	}
	for i, _ := range ent[op.cubeId].edgeMap[op.face] {
	        if v,ok := trx.edgeRing.Value.(Color); ok {
		    *op.ent[cubeId].edgeMap[face][i] = v
		}
		trx.edgeRing = trx.edgeRing.Next()
	}

    return nil
}

func Sister(cubeId int, face Color) (sisterCubeId int, sisterFace Color){
	sisterCubeId = straightProjection[cubeId][intForColor[Color]]
	sisterFace = face
}

type ThreeDOperation struct {
	ent *Entanglement
	cubeId int
	face Color
	direction int
	primary bool
}

func SplitMessage(op ThreeDOperation, opchan channel ThreeDOperation) error {
	sisterCubeId, sisterFace = Sister(op.CubeId, op.face)
	op.primary = false
	opchan <- op
	var sisterOp = ThreeDOperation {op.ent, sisterCubeId, sisterFace,op.direction, false}
	opchan <- sisterOp
}

func countDown(count chan int) {
	for i := 20 ; i >= 0 ; i-- {
		count <- i
		time.Sleep(1000000000)
	}
}

func takeSample(ent *Entanglement) {
	fmt.Println(ent[0].faceMap["red"][1])
	fmt.Println(ent[0].faceMap["red"][2])
	fmt.Println(*ent[0].edgeMap["red"][2])
	fmt.Println(*ent[0].edgeMap["red"][3])
	fmt.Println(*ent[0].edgeMap["red"][8])
	fmt.Println(*ent[0].edgeMap["red"][11])
}

func player1(blab chan ThreeDOperation) {
	for i := range sim1 {
		blab <- sim1[i]
		time.Sleep(1000000)
	}
}

func player2(blab chan ThreeDOperation) {
	for i := range sim2 {
		blab <- sim2[i]
		time.Sleep(1000000)
	}
}

func main() {
	entanglement1,_ := NewEntanglement()
	operations := make(chan ThreeDOperation)
	count := make(chan int)
        go player1(operations)
	go player2(operations)
	go countDown(count)
	for {
		select {
			case o := <- operations:
			    if o.primary {
					SplitMessage(o, operations)
				} else {
					ThreeDRotate(o)
				}

			case i := <- count:
				if 0 == i {
					takeSample(entanglement1)
					return
				}
				fmt.Printf("%d seconds remaining\n", i)
		}
	}
}
<|endoftext|>


<commit_before>// Copyright 2016 The Gosl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package utl

import (
	"math/rand"
	"testing"

	"github.com/cpmech/gosl/io"
)

var ___benchmarking_strindexsmall___ []string
var ___benchmarking_strindexmap___ map[string]int
var ___benchmarking_result___ int

func init() {
	rand.Seed(13)
	answers := []string{"66", "644", "666", "653", "10", "0", "1", "1", "1", "9", "A", ""}
	//n := 1000000
	//m := 500000
	//n := 100
	//m := 50
	n := 20
	m := 10
	___benchmarking_strindexsmall___ = make([]string, n)
	___benchmarking_strindexmap___ = make(map[string]int)
	for i := 0; i < n; i++ {
		j := rand.Intn(len(answers))
		___benchmarking_strindexsmall___[i] = answers[j]
		___benchmarking_strindexmap___[io.Sf("%s_%d", answers[j], i)] = i
		if i == m {
			___benchmarking_strindexsmall___[i] = "user"
			___benchmarking_strindexmap___["user"] = i
		}
	}
	//io.Pforan("%v\n", ___benchmarking_strindexsmall___)
	//io.Pfcyan("%v\n", ___benchmarking_strindexmap___)
}

func BenchmarkStrIndexSmall(b *testing.B) {
	var idx int
	for i := 0; i < b.N; i++ {
		idx = StrIndexSmall(___benchmarking_strindexsmall___, "user")
	}
	___benchmarking_result___ = idx
}

func BenchmarkStrIndexMap(b *testing.B) {
	var idx int
	for i := 0; i < b.N; i++ {
		idx = ___benchmarking_strindexmap___["user"]
	}
	___benchmarking_result___ = idx
}
<commit_msg>Rename vars<commit_after>// Copyright 2016 The Gosl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package utl

import (
	"math/rand"
	"testing"

	"github.com/cpmech/gosl/io"
)

var (
	benchStrindexsmall []string
	benchStrindexmap   map[string]int
	benchResult        int
)

func init() {
	rand.Seed(13)
	answers := []string{"66", "644", "666", "653", "10", "0", "1", "1", "1", "9", "A", ""}
	//n := 1000000
	//m := 500000
	//n := 100
	//m := 50
	n := 20
	m := 10
	benchStrindexsmall = make([]string, n)
	benchStrindexmap = make(map[string]int)
	for i := 0; i < n; i++ {
		j := rand.Intn(len(answers))
		benchStrindexsmall[i] = answers[j]
		benchStrindexmap[io.Sf("%s_%d", answers[j], i)] = i
		if i == m {
			benchStrindexsmall[i] = "user"
			benchStrindexmap["user"] = i
		}
	}
	//io.Pforan("%v\n", ___benchmarking_strindexsmall___)
	//io.Pfcyan("%v\n", ___benchmarking_strindexmap___)
}

func BenchmarkStrIndexSmall(b *testing.B) {
	var idx int
	for i := 0; i < b.N; i++ {
		idx = StrIndexSmall(benchStrindexsmall, "user")
	}
	benchResult = idx
}

func BenchmarkStrIndexMap(b *testing.B) {
	var idx int
	for i := 0; i < b.N; i++ {
		idx = benchStrindexmap["user"]
	}
	benchResult = idx
}
<|endoftext|>


<commit_before>package colonycore

import (
	"github.com/eaciit/dbox"
	"github.com/eaciit/orm/v1"
)

const (
	CONF_DB_ACL string = "db_acl"
)

type Configuration struct {
	orm.ModelBase
	Key   string `json:"_id",bson:"_id"`
	Value interface{}
}

func (a *Configuration) TableName() string {
	return "configurations"
}

func (a *Configuration) RecordID() interface{} {
	return a.Key
}

func GetConfig(key string) interface{} {
	cursor, err := Find(new(Configuration), dbox.Eq("_id", key))
	if err != nil {
		return nil
	}

	if cursor.Count() == 0 {
		return nil
	}

	data := Configuration{}
	err = cursor.Fetch(&data, 1, false)
	if err != nil {
		return nil
	}

	return data.Value
}

func SetConfig(key string, value interface{}) {
	o := new(Configuration)
	o.Key = key
	o.Value = value
	Save(o)
}
<commit_msg>no message<commit_after>package colonycore

import (
	"github.com/eaciit/dbox"
	"github.com/eaciit/orm/v1"
)

const (
	CONF_DB_ACL string = "db_acl"
)

type Configuration struct {
	orm.ModelBase
	Key   string `json:"_id",bson:"_id"`
	Value interface{}
}

func (a *Configuration) TableName() string {
	return "configurations"
}

func (a *Configuration) RecordID() interface{} {
	return a.Key
}

func GetConfig(key string, args ...string) interface{} {
	var res interface{} = nil
	if len(args) > 0 {
		res = args[0]
	}

	cursor, err := Find(new(Configuration), dbox.Eq("_id", key))
	if err != nil {
		return res
	}

	if cursor.Count() == 0 {
		return res
	}

	data := Configuration{}
	err = cursor.Fetch(&data, 1, false)
	if err != nil {
		return res
	}

	return data.Value
}

func SetConfig(key string, value interface{}) {
	o := new(Configuration)
	o.Key = key
	o.Value = value
	Save(o)
}
<|endoftext|>


<commit_before><commit_msg>Ensure we catch errors earlier with --shell<commit_after><|endoftext|>


<commit_before>package rain

import (
	"crypto/rand"
	"net"
	"runtime"
	"sync"

	"github.com/cenkalti/log"

	"github.com/cenkalti/rain/internal/connection"
	"github.com/cenkalti/rain/internal/logger"
	"github.com/cenkalti/rain/internal/protocol"
	"github.com/cenkalti/rain/internal/torrent"
)

// Limits
const (
	maxPeerServe      = 200
	maxPeerPerTorrent = 50
	downloadSlots     = 4
	uploadSlots       = 4
)

// http://www.bittorrent.org/beps/bep_0020.html
var peerIDPrefix = []byte("-RN" + build + "-")
var build = "0001"

func SetLogLevel(l log.Level) { logger.DefaultHandler.SetLevel(l) }

type Rain struct {
	config        *Config
	peerID        protocol.PeerID
	listener      *net.TCPListener
	transfers     map[protocol.InfoHash]*transfer // all active transfers
	transfersSKey map[[20]byte]*transfer          // for encryption
	transfersM    sync.Mutex
	log           logger.Logger
}

// New returns a pointer to new Rain BitTorrent client.
func New(c *Config) (*Rain, error) {
	peerID, err := generatePeerID()
	if err != nil {
		return nil, err
	}
	return &Rain{
		config:        c,
		peerID:        peerID,
		transfers:     make(map[protocol.InfoHash]*transfer),
		transfersSKey: make(map[[20]byte]*transfer),
		log:           logger.New("rain"),
	}, nil
}

func generatePeerID() (protocol.PeerID, error) {
	var id protocol.PeerID
	copy(id[:], peerIDPrefix)
	_, err := rand.Read(id[len(peerIDPrefix):])
	return id, err
}

func (r *Rain) PeerID() protocol.PeerID { return r.peerID }

// Listen peer port and accept incoming peer connections.
func (r *Rain) Listen() error {
	var err error
	addr := &net.TCPAddr{Port: int(r.config.Port)}
	r.listener, err = net.ListenTCP("tcp4", addr)
	if err != nil {
		return err
	}
	r.log.Notice("Listening peers on tcp://" + r.listener.Addr().String())
	go r.accepter()
	return nil
}

// Port returns the port number that the client is listening.
// If the client does not listen any port, returns 0.
func (r *Rain) Port() uint16 {
	if r.listener != nil {
		return uint16(r.listener.Addr().(*net.TCPAddr).Port)
	}
	return 0
}

func (r *Rain) Close() error { return r.listener.Close() }

func (r *Rain) accepter() {
	limit := make(chan struct{}, maxPeerServe)
	for {
		conn, err := r.listener.Accept()
		if err != nil {
			r.log.Error(err)
			return
		}
		limit <- struct{}{}
		go func(c net.Conn) {
			defer func() {
				if err := recover(); err != nil {
					buf := make([]byte, 10000)
					r.log.Critical(err, "\n", string(buf[:runtime.Stack(buf, false)]))
				}
				c.Close()
				<-limit
			}()
			r.servePeer(c)
		}(conn)
	}
}

func (r *Rain) servePeer(conn net.Conn) {
	getSKey := func(sKeyHash [20]byte) (sKey []byte) {
		r.transfersM.Lock()
		t, ok := r.transfersSKey[sKeyHash]
		r.transfersM.Unlock()
		if ok {
			sKey = t.torrent.Info.Hash[:]
		}
		return
	}

	hasInfoHash := func(ih protocol.InfoHash) bool {
		r.transfersM.Lock()
		_, ok := r.transfers[ih]
		r.transfersM.Unlock()
		return ok
	}

	encConn, _, _, ih, _, err := connection.Accept(conn, getSKey, r.config.Encryption.ForceIncoming, hasInfoHash, [8]byte{}, r.peerID)
	if err != nil {
		if err == connection.ErrOwnConnection {
			r.log.Debug(err)
		} else {
			r.log.Error(err)
		}
		return
	}

	p := newPeer(encConn, incoming)
	p.log.Info("Connection accepted")

	r.transfersM.Lock()
	t, ok := r.transfers[ih]
	r.transfersM.Unlock()
	if !ok {
		r.log.Debug("Transfer is removed during incoming handshake")
		return
	}

	p.log.Debugln("servePeerConn: Handshake completed")
	p.Serve(t)
}

func (r *Rain) Add(torrentPath, where string) (*transfer, error) {
	torrent, err := torrent.New(torrentPath)
	if err != nil {
		return nil, err
	}
	r.log.Debugf("Parsed torrent file: %#v", torrent)
	return r.newTransfer(torrent, where)
}

func (r *Rain) AddMagnet(url, where string) (*transfer, error) { panic("not implemented") }

func (r *Rain) Start(t *transfer)  { go t.Run() }
func (r *Rain) Stop(t *transfer)   { panic("not implemented") }
func (r *Rain) Remove(t *transfer) { panic("not implemented") }
<commit_msg>fix where<commit_after>package rain

import (
	"crypto/rand"
	"net"
	"runtime"
	"sync"

	"github.com/cenkalti/log"

	"github.com/cenkalti/rain/internal/connection"
	"github.com/cenkalti/rain/internal/logger"
	"github.com/cenkalti/rain/internal/protocol"
	"github.com/cenkalti/rain/internal/torrent"
)

// Limits
const (
	maxPeerServe      = 200
	maxPeerPerTorrent = 50
	downloadSlots     = 4
	uploadSlots       = 4
)

// http://www.bittorrent.org/beps/bep_0020.html
var peerIDPrefix = []byte("-RN" + build + "-")
var build = "0001"

func SetLogLevel(l log.Level) { logger.DefaultHandler.SetLevel(l) }

type Rain struct {
	config        *Config
	peerID        protocol.PeerID
	listener      *net.TCPListener
	transfers     map[protocol.InfoHash]*transfer // all active transfers
	transfersSKey map[[20]byte]*transfer          // for encryption
	transfersM    sync.Mutex
	log           logger.Logger
}

// New returns a pointer to new Rain BitTorrent client.
func New(c *Config) (*Rain, error) {
	peerID, err := generatePeerID()
	if err != nil {
		return nil, err
	}
	return &Rain{
		config:        c,
		peerID:        peerID,
		transfers:     make(map[protocol.InfoHash]*transfer),
		transfersSKey: make(map[[20]byte]*transfer),
		log:           logger.New("rain"),
	}, nil
}

func generatePeerID() (protocol.PeerID, error) {
	var id protocol.PeerID
	copy(id[:], peerIDPrefix)
	_, err := rand.Read(id[len(peerIDPrefix):])
	return id, err
}

func (r *Rain) PeerID() protocol.PeerID { return r.peerID }

// Listen peer port and accept incoming peer connections.
func (r *Rain) Listen() error {
	var err error
	addr := &net.TCPAddr{Port: int(r.config.Port)}
	r.listener, err = net.ListenTCP("tcp4", addr)
	if err != nil {
		return err
	}
	r.log.Notice("Listening peers on tcp://" + r.listener.Addr().String())
	go r.accepter()
	return nil
}

// Port returns the port number that the client is listening.
// If the client does not listen any port, returns 0.
func (r *Rain) Port() uint16 {
	if r.listener != nil {
		return uint16(r.listener.Addr().(*net.TCPAddr).Port)
	}
	return 0
}

func (r *Rain) Close() error { return r.listener.Close() }

func (r *Rain) accepter() {
	limit := make(chan struct{}, maxPeerServe)
	for {
		conn, err := r.listener.Accept()
		if err != nil {
			r.log.Error(err)
			return
		}
		limit <- struct{}{}
		go func(c net.Conn) {
			defer func() {
				if err := recover(); err != nil {
					buf := make([]byte, 10000)
					r.log.Critical(err, "\n", string(buf[:runtime.Stack(buf, false)]))
				}
				c.Close()
				<-limit
			}()
			r.servePeer(c)
		}(conn)
	}
}

func (r *Rain) servePeer(conn net.Conn) {
	getSKey := func(sKeyHash [20]byte) (sKey []byte) {
		r.transfersM.Lock()
		t, ok := r.transfersSKey[sKeyHash]
		r.transfersM.Unlock()
		if ok {
			sKey = t.torrent.Info.Hash[:]
		}
		return
	}

	hasInfoHash := func(ih protocol.InfoHash) bool {
		r.transfersM.Lock()
		_, ok := r.transfers[ih]
		r.transfersM.Unlock()
		return ok
	}

	encConn, _, _, ih, _, err := connection.Accept(conn, getSKey, r.config.Encryption.ForceIncoming, hasInfoHash, [8]byte{}, r.peerID)
	if err != nil {
		if err == connection.ErrOwnConnection {
			r.log.Debug(err)
		} else {
			r.log.Error(err)
		}
		return
	}

	p := newPeer(encConn, incoming)
	p.log.Info("Connection accepted")

	r.transfersM.Lock()
	t, ok := r.transfers[ih]
	r.transfersM.Unlock()
	if !ok {
		r.log.Debug("Transfer is removed during incoming handshake")
		return
	}

	p.log.Debugln("servePeerConn: Handshake completed")
	p.Serve(t)
}

func (r *Rain) Add(torrentPath, where string) (*transfer, error) {
	torrent, err := torrent.New(torrentPath)
	if err != nil {
		return nil, err
	}
	r.log.Debugf("Parsed torrent file: %#v", torrent)
	if where == "" {
		where = r.config.DownloadDir
	}
	return r.newTransfer(torrent, where)
}

func (r *Rain) AddMagnet(url, where string) (*transfer, error) { panic("not implemented") }

func (r *Rain) Start(t *transfer)  { go t.Run() }
func (r *Rain) Stop(t *transfer)   { panic("not implemented") }
func (r *Rain) Remove(t *transfer) { panic("not implemented") }
<|endoftext|>


<commit_before>// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package flate

import (
	"testing"
)

func TestForwardCopy(t *testing.T) {
	testCases := []struct {
		dst0, dst1 int
		src0, src1 int
		want       string
	}{
		{0, 9, 0, 9, "012345678"},
		{0, 5, 4, 9, "45678"},
		{4, 9, 0, 5, "01230"},
		{1, 6, 3, 8, "34567"},
		{3, 8, 1, 6, "12121"},
		{0, 9, 3, 6, "345"},
		{3, 6, 0, 9, "012"},
		{1, 6, 0, 9, "00000"},
		{0, 4, 7, 8, "7"},
		{0, 1, 6, 8, "6"},
		{4, 4, 6, 9, ""},
		{2, 8, 6, 6, ""},
		{0, 0, 0, 0, ""},
	}
	for _, tc := range testCases {
		b := []byte("012345678")
		dst := b[tc.dst0:tc.dst1]
		src := b[tc.src0:tc.src1]
		n := forwardCopy(dst, src)
		got := string(dst[:n])
		if got != tc.want {
			t.Errorf("dst=b[%d:%d], src=b[%d:%d]: got %q, want %q",
				tc.dst0, tc.dst1, tc.src0, tc.src1, got, tc.want)
		}
	}
}
<commit_msg>compress/flate: add a copy overrun test.<commit_after>// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package flate

import (
	"testing"
)

func TestForwardCopy(t *testing.T) {
	testCases := []struct {
		dst0, dst1 int
		src0, src1 int
		want       string
	}{
		{0, 9, 0, 9, "012345678"},
		{0, 5, 4, 9, "45678"},
		{4, 9, 0, 5, "01230"},
		{1, 6, 3, 8, "34567"},
		{3, 8, 1, 6, "12121"},
		{0, 9, 3, 6, "345"},
		{3, 6, 0, 9, "012"},
		{1, 6, 0, 9, "00000"},
		{0, 4, 7, 8, "7"},
		{0, 1, 6, 8, "6"},
		{4, 4, 6, 9, ""},
		{2, 8, 6, 6, ""},
		{0, 0, 0, 0, ""},
	}
	for _, tc := range testCases {
		b := []byte("0123456789")
		dst := b[tc.dst0:tc.dst1]
		src := b[tc.src0:tc.src1]
		n := forwardCopy(dst, src)
		got := string(dst[:n])
		if got != tc.want {
			t.Errorf("dst=b[%d:%d], src=b[%d:%d]: got %q, want %q",
				tc.dst0, tc.dst1, tc.src0, tc.src1, got, tc.want)
		}
		// Check that the bytes outside of dst[:n] were not modified.
		for i, x := range b {
			if i >= tc.dst0 && i < tc.dst0+n {
				continue
			}
			if int(x) != '0'+i {
				t.Errorf("dst=b[%d:%d], src=b[%d:%d]: copy overrun at b[%d]: got '%c', want '%c'",
					tc.dst0, tc.dst1, tc.src0, tc.src1, i, x, '0'+i)
			}
		}
	}
}
<|endoftext|>


<commit_before>// Copyright 2009 The Go Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package gosym

import (
	"debug/elf"
	"os"
	"testing"
	"syscall"
)

func dotest() bool {
	// For now, only works on ELF platforms.
	return syscall.OS == "linux" && os.Getenv("GOARCH") == "amd64"
}

func getTable(t *testing.T) *Table {
	f, tab := crack(os.Args[0], t)
	f.Close()
	return tab
}

func crack(file string, t *testing.T) (*elf.File, *Table) {
	// Open self
	f, err := elf.Open(file)
	if err != nil {
		t.Fatal(err)
	}
	return parse(file, f, t)
}

func parse(file string, f *elf.File, t *testing.T) (*elf.File, *Table) {
	symdat, err := f.Section(".gosymtab").Data()
	if err != nil {
		f.Close()
		t.Fatalf("reading %s gosymtab: %v", file, err)
	}
	pclndat, err := f.Section(".gopclntab").Data()
	if err != nil {
		f.Close()
		t.Fatalf("reading %s gopclntab: %v", file, err)
	}

	pcln := NewLineTable(pclndat, f.Section(".text").Addr)
	tab, err := NewTable(symdat, pcln)
	if err != nil {
		f.Close()
		t.Fatalf("parsing %s gosymtab: %v", file, err)
	}

	return f, tab
}

var goarch = os.Getenv("O")

func TestLineFromAline(t *testing.T) {
	if !dotest() {
		return
	}

	tab := getTable(t)

	// Find the sym package
	pkg := tab.LookupFunc("debug/gosym.TestLineFromAline").Obj
	if pkg == nil {
		t.Fatalf("nil pkg")
	}

	// Walk every absolute line and ensure that we hit every
	// source line monotonically
	lastline := make(map[string]int)
	final := -1
	for i := 0; i < 10000; i++ {
		path, line := pkg.lineFromAline(i)
		// Check for end of object
		if path == "" {
			if final == -1 {
				final = i - 1
			}
			continue
		} else if final != -1 {
			t.Fatalf("reached end of package at absolute line %d, but absolute line %d mapped to %s:%d", final, i, path, line)
		}
		// It's okay to see files multiple times (e.g., sys.a)
		if line == 1 {
			lastline[path] = 1
			continue
		}
		// Check that the is the next line in path
		ll, ok := lastline[path]
		if !ok {
			t.Errorf("file %s starts on line %d", path, line)
		} else if line != ll+1 {
			t.Errorf("expected next line of file %s to be %d, got %d", path, ll+1, line)
		}
		lastline[path] = line
	}
	if final == -1 {
		t.Errorf("never reached end of object")
	}
}

func TestLineAline(t *testing.T) {
	if !dotest() {
		return
	}

	tab := getTable(t)

	for _, o := range tab.Files {
		// A source file can appear multiple times in a
		// object.  alineFromLine will always return alines in
		// the first file, so track which lines we've seen.
		found := make(map[string]int)
		for i := 0; i < 1000; i++ {
			path, line := o.lineFromAline(i)
			if path == "" {
				break
			}

			// cgo files are full of 'Z' symbols, which we don't handle
			if len(path) > 4 && path[len(path)-4:] == ".cgo" {
				continue
			}

			if minline, ok := found[path]; path != "" && ok {
				if minline >= line {
					// We've already covered this file
					continue
				}
			}
			found[path] = line

			a, err := o.alineFromLine(path, line)
			if err != nil {
				t.Errorf("absolute line %d in object %s maps to %s:%d, but mapping that back gives error %s", i, o.Paths[0].Name, path, line, err)
			} else if a != i {
				t.Errorf("absolute line %d in object %s maps to %s:%d, which maps back to absolute line %d\n", i, o.Paths[0].Name, path, line, a)
			}
		}
	}
}

// gotest: if [ "$(uname)-$(uname -m)" = Linux-x86_64 ]; then
// gotest:    mkdir -p _test && $AS pclinetest.s && $LD -E main -l -o _test/pclinetest pclinetest.$O
// gotest: fi
func TestPCLine(t *testing.T) {
	if !dotest() {
		return
	}

	f, tab := crack("_test/pclinetest", t)
	text := f.Section(".text")
	textdat, err := text.Data()
	if err != nil {
		t.Fatalf("reading .text: %v", err)
	}

	// Test PCToLine
	sym := tab.LookupFunc("linefrompc")
	wantLine := 0
	for pc := sym.Entry; pc < sym.End; pc++ {
		file, line, fn := tab.PCToLine(pc)
		off := pc - text.Addr // TODO(rsc): should not need off; bug in 8g
		wantLine += int(textdat[off])
		if fn == nil {
			t.Errorf("failed to get line of PC %#x", pc)
		} else if len(file) < 12 || file[len(file)-12:] != "pclinetest.s" || line != wantLine || fn != sym {
			t.Errorf("expected %s:%d (%s) at PC %#x, got %s:%d (%s)", "pclinetest.s", wantLine, sym.Name, pc, file, line, fn.Name)
		}
	}

	// Test LineToPC
	sym = tab.LookupFunc("pcfromline")
	lookupline := -1
	wantLine = 0
	off := uint64(0) // TODO(rsc): should not need off; bug in 8g
	for pc := sym.Value; pc < sym.End; pc += 2 + uint64(textdat[off]) {
		file, line, fn := tab.PCToLine(pc)
		off = pc - text.Addr
		wantLine += int(textdat[off])
		if line != wantLine {
			t.Errorf("expected line %d at PC %#x in pcfromline, got %d", wantLine, pc, line)
			off = pc + 1 - text.Addr
			continue
		}
		if lookupline == -1 {
			lookupline = line
		}
		for ; lookupline <= line; lookupline++ {
			pc2, fn2, err := tab.LineToPC(file, lookupline)
			if lookupline != line {
				// Should be nothing on this line
				if err == nil {
					t.Errorf("expected no PC at line %d, got %#x (%s)", lookupline, pc2, fn2.Name)
				}
			} else if err != nil {
				t.Errorf("failed to get PC of line %d: %s", lookupline, err)
			} else if pc != pc2 {
				t.Errorf("expected PC %#x (%s) at line %d, got PC %#x (%s)", pc, fn.Name, line, pc2, fn2.Name)
			}
		}
		off = pc + 1 - text.Addr
	}
}
<commit_msg>debug/gosym: fix test for new 6l<commit_after>// Copyright 2009 The Go Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package gosym

import (
	"debug/elf"
	"os"
	"testing"
	"syscall"
)

func dotest() bool {
	// For now, only works on ELF platforms.
	return syscall.OS == "linux" && os.Getenv("GOARCH") == "amd64"
}

func getTable(t *testing.T) *Table {
	f, tab := crack(os.Args[0], t)
	f.Close()
	return tab
}

func crack(file string, t *testing.T) (*elf.File, *Table) {
	// Open self
	f, err := elf.Open(file)
	if err != nil {
		t.Fatal(err)
	}
	return parse(file, f, t)
}

func parse(file string, f *elf.File, t *testing.T) (*elf.File, *Table) {
	symdat, err := f.Section(".gosymtab").Data()
	if err != nil {
		f.Close()
		t.Fatalf("reading %s gosymtab: %v", file, err)
	}
	pclndat, err := f.Section(".gopclntab").Data()
	if err != nil {
		f.Close()
		t.Fatalf("reading %s gopclntab: %v", file, err)
	}

	pcln := NewLineTable(pclndat, f.Section(".text").Addr)
	tab, err := NewTable(symdat, pcln)
	if err != nil {
		f.Close()
		t.Fatalf("parsing %s gosymtab: %v", file, err)
	}

	return f, tab
}

var goarch = os.Getenv("O")

func TestLineFromAline(t *testing.T) {
	if !dotest() {
		return
	}

	tab := getTable(t)

	// Find the sym package
	pkg := tab.LookupFunc("debug/gosym.TestLineFromAline").Obj
	if pkg == nil {
		t.Fatalf("nil pkg")
	}

	// Walk every absolute line and ensure that we hit every
	// source line monotonically
	lastline := make(map[string]int)
	final := -1
	for i := 0; i < 10000; i++ {
		path, line := pkg.lineFromAline(i)
		// Check for end of object
		if path == "" {
			if final == -1 {
				final = i - 1
			}
			continue
		} else if final != -1 {
			t.Fatalf("reached end of package at absolute line %d, but absolute line %d mapped to %s:%d", final, i, path, line)
		}
		// It's okay to see files multiple times (e.g., sys.a)
		if line == 1 {
			lastline[path] = 1
			continue
		}
		// Check that the is the next line in path
		ll, ok := lastline[path]
		if !ok {
			t.Errorf("file %s starts on line %d", path, line)
		} else if line != ll+1 {
			t.Errorf("expected next line of file %s to be %d, got %d", path, ll+1, line)
		}
		lastline[path] = line
	}
	if final == -1 {
		t.Errorf("never reached end of object")
	}
}

func TestLineAline(t *testing.T) {
	if !dotest() {
		return
	}

	tab := getTable(t)

	for _, o := range tab.Files {
		// A source file can appear multiple times in a
		// object.  alineFromLine will always return alines in
		// the first file, so track which lines we've seen.
		found := make(map[string]int)
		for i := 0; i < 1000; i++ {
			path, line := o.lineFromAline(i)
			if path == "" {
				break
			}

			// cgo files are full of 'Z' symbols, which we don't handle
			if len(path) > 4 && path[len(path)-4:] == ".cgo" {
				continue
			}

			if minline, ok := found[path]; path != "" && ok {
				if minline >= line {
					// We've already covered this file
					continue
				}
			}
			found[path] = line

			a, err := o.alineFromLine(path, line)
			if err != nil {
				t.Errorf("absolute line %d in object %s maps to %s:%d, but mapping that back gives error %s", i, o.Paths[0].Name, path, line, err)
			} else if a != i {
				t.Errorf("absolute line %d in object %s maps to %s:%d, which maps back to absolute line %d\n", i, o.Paths[0].Name, path, line, a)
			}
		}
	}
}

// gotest: if [ "$(uname)-$(uname -m)" = Linux-x86_64 ]; then
// gotest:    mkdir -p _test && $AS pclinetest.s && $LD -E main -o _test/pclinetest pclinetest.$O
// gotest: fi
func TestPCLine(t *testing.T) {
	if !dotest() {
		return
	}

	f, tab := crack("_test/pclinetest", t)
	text := f.Section(".text")
	textdat, err := text.Data()
	if err != nil {
		t.Fatalf("reading .text: %v", err)
	}

	// Test PCToLine
	sym := tab.LookupFunc("linefrompc")
	wantLine := 0
	for pc := sym.Entry; pc < sym.End; pc++ {
		file, line, fn := tab.PCToLine(pc)
		off := pc - text.Addr // TODO(rsc): should not need off; bug in 8g
		wantLine += int(textdat[off])
		if fn == nil {
			t.Errorf("failed to get line of PC %#x", pc)
		} else if len(file) < 12 || file[len(file)-12:] != "pclinetest.s" || line != wantLine || fn != sym {
			t.Errorf("expected %s:%d (%s) at PC %#x, got %s:%d (%s)", "pclinetest.s", wantLine, sym.Name, pc, file, line, fn.Name)
		}
	}

	// Test LineToPC
	sym = tab.LookupFunc("pcfromline")
	lookupline := -1
	wantLine = 0
	off := uint64(0) // TODO(rsc): should not need off; bug in 8g
	for pc := sym.Value; pc < sym.End; pc += 2 + uint64(textdat[off]) {
		file, line, fn := tab.PCToLine(pc)
		off = pc - text.Addr
		wantLine += int(textdat[off])
		if line != wantLine {
			t.Errorf("expected line %d at PC %#x in pcfromline, got %d", wantLine, pc, line)
			off = pc + 1 - text.Addr
			continue
		}
		if lookupline == -1 {
			lookupline = line
		}
		for ; lookupline <= line; lookupline++ {
			pc2, fn2, err := tab.LineToPC(file, lookupline)
			if lookupline != line {
				// Should be nothing on this line
				if err == nil {
					t.Errorf("expected no PC at line %d, got %#x (%s)", lookupline, pc2, fn2.Name)
				}
			} else if err != nil {
				t.Errorf("failed to get PC of line %d: %s", lookupline, err)
			} else if pc != pc2 {
				t.Errorf("expected PC %#x (%s) at line %d, got PC %#x (%s)", pc, fn.Name, line, pc2, fn2.Name)
			}
		}
		off = pc + 1 - text.Addr
	}
}
<|endoftext|>


<commit_before>// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package debug

import (
	"strings"
	"testing"
)

type T int

func (t *T) ptrmethod() []byte {
	return Stack()
}
func (t T) method() []byte {
	return t.ptrmethod()
}

/*
	The traceback should look something like this, modulo line numbers and hex constants.
	Don't worry much about the base levels, but check the ones in our own package.

		/Users/r/go/src/pkg/runtime/debug/stack_test.go:15 (0x13878)
			(*T).ptrmethod: return Stack()
		/Users/r/go/src/pkg/runtime/debug/stack_test.go:18 (0x138dd)
			T.method: return t.ptrmethod()
		/Users/r/go/src/pkg/runtime/debug/stack_test.go:23 (0x13920)
			TestStack: b := T(0).method()
		/Users/r/go/src/pkg/testing/testing.go:132 (0x14a7a)
			tRunner: test.F(t)
		/Users/r/go/src/pkg/runtime/proc.c:145 (0xc970)
			???: runtime·unlock(&runtime·sched);
*/
func TestStack(t *testing.T) {
	b := T(0).method()
	lines := strings.Split(string(b), "\n")
	if len(lines) <= 6 {
		t.Fatal("too few lines")
	}
	n := 0
	frame := func(line, code string) {
		check(t, lines[n], line)
		n++
		// The source might not be available while running the test.
		if strings.HasPrefix(lines[n], "\t") {
			check(t, lines[n], code)
			n++
		}
	}
	frame("src/pkg/runtime/debug/stack_test.go", "\t(*T).ptrmethod: return Stack()")
	frame("src/pkg/runtime/debug/stack_test.go", "\tT.method: return t.ptrmethod()")
	frame("src/pkg/runtime/debug/stack_test.go", "\tTestStack: b := T(0).method()")
	frame("src/pkg/testing/testing.go", "")
}

func check(t *testing.T, line, has string) {
	if strings.Index(line, has) < 0 {
		t.Errorf("expected %q in %q", has, line)
	}
}
<commit_msg>runtime/debug: fix the test If source are not available, then the stack looks like: stack_test.go:40: 	/tmp/gobuilder/linux-amd64-race-72b15c5d6f65/go/src/pkg/runtime/debug/bla-bla-bla/src/pkg/runtime/debug/stack_test.go:15 (0x43fb11) stack_test.go:40: 	/tmp/gobuilder/linux-amd64-race-72b15c5d6f65/go/src/pkg/runtime/debug/bla-bla-bla/src/pkg/runtime/debug/stack_test.go:18 (0x43fb7a) stack_test.go:40: 	/tmp/gobuilder/linux-amd64-race-72b15c5d6f65/go/src/pkg/runtime/debug/bla-bla-bla/src/pkg/runtime/debug/stack_test.go:37 (0x43fbf4) stack_test.go:40: 	/tmp/gobuilder/linux-amd64-race-72b15c5d6f65/go/src/pkg/testing/bla-bla-bla/src/pkg/testing/testing.go:301 (0x43b5ba) stack_test.go:40: 	/tmp/gobuilder/linux-amd64-race-72b15c5d6f65/go/src/pkg/runtime/bla-bla-bla/src/pkg/runtime/proc.c:276 (0x410670) stack_test.go:40: which is 6 lines.<commit_after>// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package debug

import (
	"strings"
	"testing"
)

type T int

func (t *T) ptrmethod() []byte {
	return Stack()
}
func (t T) method() []byte {
	return t.ptrmethod()
}

/*
	The traceback should look something like this, modulo line numbers and hex constants.
	Don't worry much about the base levels, but check the ones in our own package.

		/Users/r/go/src/pkg/runtime/debug/stack_test.go:15 (0x13878)
			(*T).ptrmethod: return Stack()
		/Users/r/go/src/pkg/runtime/debug/stack_test.go:18 (0x138dd)
			T.method: return t.ptrmethod()
		/Users/r/go/src/pkg/runtime/debug/stack_test.go:23 (0x13920)
			TestStack: b := T(0).method()
		/Users/r/go/src/pkg/testing/testing.go:132 (0x14a7a)
			tRunner: test.F(t)
		/Users/r/go/src/pkg/runtime/proc.c:145 (0xc970)
			???: runtime·unlock(&runtime·sched);
*/
func TestStack(t *testing.T) {
	b := T(0).method()
	lines := strings.Split(string(b), "\n")
	if len(lines) < 6 {
		t.Fatal("too few lines")
	}
	n := 0
	frame := func(line, code string) {
		check(t, lines[n], line)
		n++
		// The source might not be available while running the test.
		if strings.HasPrefix(lines[n], "\t") {
			check(t, lines[n], code)
			n++
		}
	}
	frame("src/pkg/runtime/debug/stack_test.go", "\t(*T).ptrmethod: return Stack()")
	frame("src/pkg/runtime/debug/stack_test.go", "\tT.method: return t.ptrmethod()")
	frame("src/pkg/runtime/debug/stack_test.go", "\tTestStack: b := T(0).method()")
	frame("src/pkg/testing/testing.go", "")
}

func check(t *testing.T, line, has string) {
	if strings.Index(line, has) < 0 {
		t.Errorf("expected %q in %q", has, line)
	}
}
<|endoftext|>


<commit_before>package traversal

import (
	"fmt"
)

type Stats struct {
	// Count of (probably) distinct addresses we've sent traversal queries to. Accessed with atomic.
	NumAddrsTried uint32
	// Number of responses we received to queries related to this traversal. Accessed with atomic.
	NumResponses uint32
}

func (me Stats) String() string {
	return fmt.Sprintf("%#v", me)
}
<commit_msg>traversal.Stats: Unimplement Stringer<commit_after>package traversal

type Stats struct {
	// Count of (probably) distinct addresses we've sent traversal queries to. Accessed with atomic.
	NumAddrsTried uint32
	// Number of responses we received to queries related to this traversal. Accessed with atomic.
	NumResponses uint32
}
<|endoftext|>


<commit_before>// Package globus implements Globus Online Nexus authentication
//(code is modified from github.com/MG-RAST/Shock/shock-server/auth/globus)
package globus

import (
	"crypto/tls"
	"encoding/json"
	"errors"
	"github.com/MG-RAST/AWE/lib/auth/basic"
	"github.com/MG-RAST/AWE/lib/conf"
	e "github.com/MG-RAST/AWE/lib/errors"
	"github.com/MG-RAST/AWE/lib/logger"
	"github.com/MG-RAST/AWE/lib/user"
	"io/ioutil"
	"net/http"
	"strings"
)

// Token response struct
type token struct {
	AccessToken     string      `json:"access_token"`
	AccessTokenHash string      `json:"access_token_hash"`
	ClientId        string      `json:"client_id"`
	ExpiresIn       int         `json:"expires_in"`
	Expiry          int         `json:"expiry"`
	IssuedOn        int         `json:"issued_on"`
	Lifetime        int         `json:"lifetime"`
	Scopes          interface{} `json:"scopes"`
	TokenId         string      `json:"token_id"`
	TokeType        string      `json:"token_type"`
	UserName        string      `json:"user_name"`
}

func authHeaderType(header string) string {
	tmp := strings.Split(header, " ")
	if len(tmp) > 1 {
		return strings.ToLower(tmp[0])
	}
	return ""
}

// Auth takes the request authorization header and returns
// user
func Auth(header string) (usr *user.User, err error) {
	switch authHeaderType(header) {
	case "globus-goauthtoken", "oauth":
		return fetchProfile(strings.Split(header, " ")[1])
	case "basic":
		if username, password, err := basic.DecodeHeader(header); err == nil {
			if t, err := fetchToken(username, password); err == nil {
				return fetchProfile(t.AccessToken)
			} else {
				return nil, err
			}
		} else {
			return nil, err
		}
	default:
		return nil, errors.New("Invalid authentication header.")
	}
	return nil, errors.New("Invalid authentication header.")
}

// fetchToken takes username and password and then retrieves user token
func fetchToken(u string, p string) (t *token, err error) {
	client := &http.Client{
		Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}},
	}
	req, err := http.NewRequest("GET", conf.GLOBUS_TOKEN_URL, nil)
	if err != nil {
		return nil, err
	}
	req.SetBasicAuth(u, p)
	if resp, err := client.Do(req); err == nil {
		defer resp.Body.Close()
		if resp.StatusCode == http.StatusCreated {
			if body, err := ioutil.ReadAll(resp.Body); err == nil {
				if err = json.Unmarshal(body, &t); err != nil {
					return nil, err
				}
			}
		} else {
			return nil, errors.New("Authentication failed: Unexpected response status: " + resp.Status)
		}
	} else {
		return nil, err
	}
	return
}

// fetchProfile validiates token by using it to fetch user profile
func fetchProfile(t string) (u *user.User, err error) {
	client := &http.Client{
		Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}},
	}
	req, err := http.NewRequest("GET", conf.GLOBUS_PROFILE_URL+"/"+clientId(t), nil)
	if err != nil {
		return nil, err
	}
	req.Header.Add("Authorization", "Globus-Goauthtoken "+t)
	if resp, err := client.Do(req); err == nil {
		defer resp.Body.Close()
		if resp.StatusCode == http.StatusOK {
			if body, err := ioutil.ReadAll(resp.Body); err == nil {
				u = &user.User{}
				if err = json.Unmarshal(body, &u); err != nil {
					return nil, err
				} else {
					if err = u.SetMongoInfo(); err != nil {
						return nil, err
					}
				}
			}
		} else if resp.StatusCode == http.StatusForbidden {
			return nil, errors.New(e.InvalidAuth)
		} else {
			err_str := "Authentication failed: Unexpected response status: " + resp.Status
			logger.Error(err_str)
			return nil, errors.New(err_str)
		}
	} else {
		return nil, err
	}
	return
}

func clientId(t string) string {
	for _, part := range strings.Split(t, "|") {
		if kv := strings.Split(part, "="); kv[0] == "client_id" {
			return kv[1]
		}
	}
	return ""
}
<commit_msg>patch to do lookup against API instead of token parsing<commit_after>// Package globus implements Globus Online Nexus authentication
//(code is modified from github.com/MG-RAST/Shock/shock-server/auth/globus)
package globus

import (
	"crypto/tls"
	"encoding/json"
	"errors"
	"github.com/MG-RAST/AWE/lib/auth/basic"
	"github.com/MG-RAST/AWE/lib/conf"
	e "github.com/MG-RAST/AWE/lib/errors"
	"github.com/MG-RAST/AWE/lib/logger"
	"github.com/MG-RAST/AWE/lib/user"
	"io/ioutil"
	"net/http"
	"strings"
)

// Token response struct
type token struct {
	AccessToken     string      `json:"access_token"`
	AccessTokenHash string      `json:"access_token_hash"`
	ClientId        string      `json:"client_id"`
	ExpiresIn       int         `json:"expires_in"`
	Expiry          int         `json:"expiry"`
	IssuedOn        int         `json:"issued_on"`
	Lifetime        int         `json:"lifetime"`
	Scopes          interface{} `json:"scopes"`
	TokenId         string      `json:"token_id"`
	TokeType        string      `json:"token_type"`
	UserName        string      `json:"user_name"`
}

func authHeaderType(header string) string {
	tmp := strings.Split(header, " ")
	if len(tmp) > 1 {
		return strings.ToLower(tmp[0])
	}
	return ""
}

// Auth takes the request authorization header and returns
// user
func Auth(header string) (usr *user.User, err error) {
	switch authHeaderType(header) {
	case "globus-goauthtoken", "oauth":
		return fetchProfile(strings.Split(header, " ")[1])
	case "basic":
		if username, password, err := basic.DecodeHeader(header); err == nil {
			if t, err := fetchToken(username, password); err == nil {
				return fetchProfile(t.AccessToken)
			} else {
				return nil, err
			}
		} else {
			return nil, err
		}
	default:
		return nil, errors.New("Invalid authentication header.")
	}
	return nil, errors.New("Invalid authentication header.")
}

// fetchToken takes username and password and then retrieves user token
func fetchToken(u string, p string) (t *token, err error) {
	client := &http.Client{
		Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}},
	}
	req, err := http.NewRequest("GET", conf.GLOBUS_TOKEN_URL, nil)
	if err != nil {
		return nil, err
	}
	req.SetBasicAuth(u, p)
	if resp, err := client.Do(req); err == nil {
		defer resp.Body.Close()
		if resp.StatusCode == http.StatusCreated {
			if body, err := ioutil.ReadAll(resp.Body); err == nil {
				if err = json.Unmarshal(body, &t); err != nil {
					return nil, err
				}
			}
		} else {
			return nil, errors.New("Authentication failed: Unexpected response status: " + resp.Status)
		}
	} else {
		return nil, err
	}
	return
}

// fetchProfile validiates token by using it to fetch user profile
func fetchProfile(t string) (u *user.User, err error) {
	client := &http.Client{
		Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}},
	}
	req, err := http.NewRequest("GET", conf.GLOBUS_PROFILE_URL+"/"+clientId(t), nil)
	if err != nil {
		return nil, err
	}
	req.Header.Add("Authorization", "Globus-Goauthtoken "+t)
	if resp, err := client.Do(req); err == nil {
		defer resp.Body.Close()
		if resp.StatusCode == http.StatusOK {
			if body, err := ioutil.ReadAll(resp.Body); err == nil {
				u = &user.User{}
				if err = json.Unmarshal(body, &u); err != nil {
					return nil, err
				} else {
					if err = u.SetMongoInfo(); err != nil {
						return nil, err
					}
				}
			}
		} else if resp.StatusCode == http.StatusForbidden {
			return nil, errors.New(e.InvalidAuth)
		} else {
			err_str := "Authentication failed: Unexpected response status: " + resp.Status
			logger.Error(err_str)
			return nil, errors.New(err_str)
		}
	} else {
		return nil, err
	}
	return
}

func clientId(t string) string {
	//for _, part := range strings.Split(t, "|") {
	//	if kv := strings.Split(part, "="); kv[0] == "client_id" {
	//		return kv[1]
	//	}
	//}
	//return ""
        client := &http.Client{
                Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}},
        }
        req, err := http.NewRequest("GET", conf.AUTH_GLOBUS_TOKEN_URL, nil)
        if err != nil {
                return ""
        }
        req.Header.Add("X-Globus-Goauthtoken", t)
        if resp, err := client.Do(req); err == nil {
                defer resp.Body.Close()
                if resp.StatusCode == http.StatusCreated {
                        if body, err := ioutil.ReadAll(resp.Body); err == nil {
                                var dat map[string]interface{}
                                if err = json.Unmarshal(body, &dat); err != nil {
                                        return ""
                                } else {
                                        return dat["client_id"].(string)
                                }
                        }
                } else if resp.StatusCode == http.StatusForbidden {
                        return ""
                } else {
                        //err_str := "Authentication failed: Unexpected response status: " + resp.Status
                        return ""
                }
        } else {
                return ""
        }

        return ""

}
<|endoftext|>


<commit_before><commit_msg>add string method to peer.Source<commit_after><|endoftext|>


<commit_before><commit_msg>Add IUIAutomationElement.SetFocus<commit_after><|endoftext|>


<commit_before>package rtnetlink

import (
	"errors"
	"fmt"
	"net"

	"github.com/mdlayher/netlink"
	"github.com/mdlayher/netlink/nlenc"
)

var (
	// errInvalidLinkMessage is returned when a LinkMessage is malformed.
	errInvalidLinkMessage = errors.New("rtnetlink LinkMessage is invalid or too short")

	// errInvalidLinkMessageAttr is returned when link attributes are malformed.
	errInvalidLinkMessageAttr = errors.New("rtnetlink LinkMessage has a wrong attribute data length")
)

var _ Message = &LinkMessage{}

// A LinkMessage is a route netlink link message.
type LinkMessage struct {
	// Always set to AF_UNSPEC (0)
	Family uint16

	// Device Type
	Type uint16

	// Unique interface index, using a nonzero value with
	// NewLink will instruct the kernel to create a
	// device with the given index (kernel 3.7+ required)
	Index uint32

	// Contains device flags, see netdevice(7)
	Flags uint32

	// Change Flags, reserved for future use and should
	// always be 0xffffffff
	Change uint32

	// Attributes List
	Attributes LinkAttributes
}

const linkMessageLength = 16

// MarshalBinary marshals a LinkMessage into a byte slice.
func (m *LinkMessage) MarshalBinary() ([]byte, error) {
	b := make([]byte, linkMessageLength)

	b[0] = 0 //Family
	b[1] = 0 //reserved
	nlenc.PutUint16(b[2:4], m.Type)
	nlenc.PutUint32(b[4:8], m.Index)
	nlenc.PutUint32(b[8:12], m.Flags)
	nlenc.PutUint32(b[12:16], 0) //Change, reserved

	a, err := m.Attributes.MarshalBinary()
	if err != nil {
		return nil, err
	}

	return append(b, a...), nil
}

// UnmarshalBinary unmarshals the contents of a byte slice into a LinkMessage.
func (m *LinkMessage) UnmarshalBinary(b []byte) error {
	l := len(b)
	if l < linkMessageLength {
		return errInvalidLinkMessage
	}

	m.Family = nlenc.Uint16(b[0:2])
	m.Type = nlenc.Uint16(b[2:4])
	m.Index = nlenc.Uint32(b[4:8])
	m.Flags = nlenc.Uint32(b[8:12])
	m.Change = nlenc.Uint32(b[12:16])

	if l > linkMessageLength {
		m.Attributes = LinkAttributes{}
		err := m.Attributes.UnmarshalBinary(b[16:])
		if err != nil {
			return err
		}
	}

	return nil
}

// rtMessage is an empty method to sattisfy the Message interface.
func (*LinkMessage) rtMessage() {}

// LinkService is used to retrieve rtnetlink family information.
type LinkService struct {
	c *Conn
}

// Constants used to request information from rtnetlink links.
const (
	RTNLGRP_LINK = 0x1

	RTM_NEWLINK = 0x10
	RTM_DELLINK = 0x11
	RTM_GETLINK = 0x12
	RTM_SETLINK = 0x13
)

// New creates a new interface using the LinkMessage information.
func (l *LinkService) New(req *LinkMessage) error {
	flags := netlink.HeaderFlagsRequest
	_, err := l.c.Send(req, RTM_SETLINK, flags)
	if err != nil {
		return err
	}

	return nil
}

// Delete removes an interface by index.
func (l *LinkService) Delete(index uint32) error {
	req := &LinkMessage{
		Index: index,
	}

	flags := netlink.HeaderFlagsRequest
	_, err := l.c.Send(req, RTM_DELLINK, flags)
	if err != nil {
		return err
	}

	return nil
}

// Get retrieves interface information by index.
func (l *LinkService) Get(index uint32) (LinkMessage, error) {
	req := &LinkMessage{
		Index: index,
	}

	flags := netlink.HeaderFlagsRequest | netlink.HeaderFlagsDumpFiltered
	msg, err := l.c.Execute(req, RTM_GETLINK, flags)
	if err != nil {
		return LinkMessage{}, err
	}

	if len(msg) != 1 {
		return LinkMessage{}, fmt.Errorf("too many/little matches, expected 1")
	}

	link := (msg[0]).(*LinkMessage)
	return *link, nil
}

// Set sets interface attributes according to the LinkMessage information.
func (l *LinkService) Set(req *LinkMessage) error {
	flags := netlink.HeaderFlagsRequest
	_, err := l.c.Send(req, RTM_SETLINK, flags)
	if err != nil {
		return err
	}

	return nil
}

// List retrieves all interfaces.
func (l *LinkService) List() ([]LinkMessage, error) {
	req := &LinkMessage{}

	flags := netlink.HeaderFlagsRequest | netlink.HeaderFlagsDump
	msgs, err := l.c.Execute(req, RTM_GETLINK, flags)
	if err != nil {
		return nil, err
	}

	links := make([]LinkMessage, 0, len(msgs))
	for _, m := range msgs {
		link := (m).(*LinkMessage)
		links = append(links, *link)
	}

	return links, nil
}

// LinkAttributes contains all attributes for an interface.
type LinkAttributes struct {
	Address   net.HardwareAddr // Interface L2 address
	Broadcast net.HardwareAddr // L2 broadcast address
	Name      string           // Device name
	MTU       uint32           // MTU of the device
	Type      uint32           // Link type
	QueueDisc string           // Queueing discipline
	Stats     *LinkStats       // Interface Statistics
}

// Attribute IDs mapped to specific LinkAttribute fields.
const (
	iflaUnspec uint16 = iota
	iflaAddress
	iflaBroadcast
	iflaIfname
	iflaMTU
	iflaLink
	iflaQdisc
	iflaStats
)

// UnmarshalBinary unmarshals the contents of a byte slice into a LinkMessage.
func (a *LinkAttributes) UnmarshalBinary(b []byte) error {
	attrs, err := netlink.UnmarshalAttributes(b)
	if err != nil {
		return err
	}

	for _, attr := range attrs {
		switch attr.Type {
		case iflaUnspec:
			//unused attribute
		case iflaAddress:
			l := len(attr.Data)
			if l < 4 || l > 32 {
				return errInvalidLinkMessageAttr
			}
			a.Address = attr.Data
		case iflaBroadcast:
			l := len(attr.Data)
			if l < 4 || l > 32 {
				return errInvalidLinkMessageAttr
			}
			a.Broadcast = attr.Data
		case iflaIfname:
			a.Name = nlenc.String(attr.Data)
		case iflaMTU:
			if len(attr.Data) != 4 {
				return errInvalidLinkMessageAttr
			}
			a.MTU = nlenc.Uint32(attr.Data)
		case iflaLink:
			if len(attr.Data) != 4 {
				return errInvalidLinkMessageAttr
			}
			a.Type = nlenc.Uint32(attr.Data)
		case iflaQdisc:
			a.QueueDisc = nlenc.String(attr.Data)
		case iflaStats:
			a.Stats = &LinkStats{}
			err := a.Stats.UnmarshalBinary(attr.Data)
			if err != nil {
				return err
			}
		}
	}

	return nil
}

// MarshalBinary marshals a LinkAttributes into a byte slice.
func (a *LinkAttributes) MarshalBinary() ([]byte, error) {
	return netlink.MarshalAttributes([]netlink.Attribute{
		{
			Type: iflaUnspec,
			Data: nlenc.Uint16Bytes(0),
		},
		{
			Type: iflaAddress,
			Data: a.Address,
		},
		{
			Type: iflaBroadcast,
			Data: a.Broadcast,
		},
		{
			Type: iflaIfname,
			Data: nlenc.Bytes(a.Name),
		},
		{
			Type: iflaMTU,
			Data: nlenc.Uint32Bytes(a.MTU),
		},
		{
			Type: iflaLink,
			Data: nlenc.Uint32Bytes(a.Type),
		},
		{
			Type: iflaQdisc,
			Data: nlenc.Bytes(a.QueueDisc),
		},
		/*
			{
				Type: iflaStats,
				Data: nlenc.Bytes(name),
			},
		*/
	})
}

//LinkStats contains packet statistics
type LinkStats struct {
	RXPackets  uint32 // total packets received
	TXPackets  uint32 // total packets transmitted
	RXBytes    uint32 // total bytes received
	TXBytes    uint32 // total bytes transmitted
	RXErrors   uint32 // bad packets received
	TXErrors   uint32 // packet transmit problems
	RXDropped  uint32 // no space in linux buffers
	TXDropped  uint32 // no space available in linux
	Multicast  uint32 // multicast packets received
	Collisions uint32

	// detailed rx_errors:
	RXLengthErrors uint32
	RXOverErrors   uint32 // receiver ring buff overflow
	RXCRCErrors    uint32 // recved pkt with crc error
	RXFrameErrors  uint32 // recv'd frame alignment error
	RXFIFOErrors   uint32 // recv'r fifo overrun
	RXMissedErrors uint32 // receiver missed packet

	// detailed tx_errors
	TXAbortedErrors   uint32
	TXCarrierErrors   uint32
	TXFIFOErrors      uint32
	TXHeartbeatErrors uint32
	TXWindowErrors    uint32

	// for cslip etc
	RXCompressed uint32
	TXCompressed uint32

	RXNoHandler uint32 // dropped, no handler found
}

// UnmarshalBinary unmarshals the contents of a byte slice into a LinkMessage.
func (a *LinkStats) UnmarshalBinary(b []byte) error {
	if len(b) != 96 && len(b) != 104 {
		return fmt.Errorf("incorrect size, want: 96 or 104, got: %d", len(b))
	}

	a.RXPackets = nlenc.Uint32(b[0:4])
	a.TXPackets = nlenc.Uint32(b[4:8])
	a.RXBytes = nlenc.Uint32(b[8:12])
	a.TXBytes = nlenc.Uint32(b[12:16])
	a.RXErrors = nlenc.Uint32(b[16:20])
	a.TXErrors = nlenc.Uint32(b[20:24])
	a.RXDropped = nlenc.Uint32(b[24:28])
	a.TXDropped = nlenc.Uint32(b[28:32])
	a.Multicast = nlenc.Uint32(b[32:36])
	a.Collisions = nlenc.Uint32(b[36:40])

	a.RXLengthErrors = nlenc.Uint32(b[40:44])
	a.RXOverErrors = nlenc.Uint32(b[44:48])
	a.RXCRCErrors = nlenc.Uint32(b[48:52])
	a.RXFrameErrors = nlenc.Uint32(b[52:56])
	a.RXFIFOErrors = nlenc.Uint32(b[56:60])
	a.RXMissedErrors = nlenc.Uint32(b[60:64])

	a.TXAbortedErrors = nlenc.Uint32(b[68:72])
	a.TXCarrierErrors = nlenc.Uint32(b[76:80])
	a.TXFIFOErrors = nlenc.Uint32(b[80:84])
	a.TXHeartbeatErrors = nlenc.Uint32(b[84:88])
	a.TXWindowErrors = nlenc.Uint32(b[88:92])

	if len(b) == 96 {
		a.RXNoHandler = nlenc.Uint32(b[92:96])
	}

	if len(b) == 104 {
		a.RXCompressed = nlenc.Uint32(b[92:96])
		a.TXCompressed = nlenc.Uint32(b[96:100])
		a.RXNoHandler = nlenc.Uint32(b[100:104])
	}

	return nil
}
<commit_msg>fix(LinkService): execute to get errors from netlink (#9)<commit_after>package rtnetlink

import (
	"errors"
	"fmt"
	"net"

	"github.com/mdlayher/netlink"
	"github.com/mdlayher/netlink/nlenc"
)

var (
	// errInvalidLinkMessage is returned when a LinkMessage is malformed.
	errInvalidLinkMessage = errors.New("rtnetlink LinkMessage is invalid or too short")

	// errInvalidLinkMessageAttr is returned when link attributes are malformed.
	errInvalidLinkMessageAttr = errors.New("rtnetlink LinkMessage has a wrong attribute data length")
)

var _ Message = &LinkMessage{}

// A LinkMessage is a route netlink link message.
type LinkMessage struct {
	// Always set to AF_UNSPEC (0)
	Family uint16

	// Device Type
	Type uint16

	// Unique interface index, using a nonzero value with
	// NewLink will instruct the kernel to create a
	// device with the given index (kernel 3.7+ required)
	Index uint32

	// Contains device flags, see netdevice(7)
	Flags uint32

	// Change Flags, reserved for future use and should
	// always be 0xffffffff
	Change uint32

	// Attributes List
	Attributes LinkAttributes
}

const linkMessageLength = 16

// MarshalBinary marshals a LinkMessage into a byte slice.
func (m *LinkMessage) MarshalBinary() ([]byte, error) {
	b := make([]byte, linkMessageLength)

	b[0] = 0 //Family
	b[1] = 0 //reserved
	nlenc.PutUint16(b[2:4], m.Type)
	nlenc.PutUint32(b[4:8], m.Index)
	nlenc.PutUint32(b[8:12], m.Flags)
	nlenc.PutUint32(b[12:16], 0) //Change, reserved

	a, err := m.Attributes.MarshalBinary()
	if err != nil {
		return nil, err
	}

	return append(b, a...), nil
}

// UnmarshalBinary unmarshals the contents of a byte slice into a LinkMessage.
func (m *LinkMessage) UnmarshalBinary(b []byte) error {
	l := len(b)
	if l < linkMessageLength {
		return errInvalidLinkMessage
	}

	m.Family = nlenc.Uint16(b[0:2])
	m.Type = nlenc.Uint16(b[2:4])
	m.Index = nlenc.Uint32(b[4:8])
	m.Flags = nlenc.Uint32(b[8:12])
	m.Change = nlenc.Uint32(b[12:16])

	if l > linkMessageLength {
		m.Attributes = LinkAttributes{}
		err := m.Attributes.UnmarshalBinary(b[16:])
		if err != nil {
			return err
		}
	}

	return nil
}

// rtMessage is an empty method to sattisfy the Message interface.
func (*LinkMessage) rtMessage() {}

// LinkService is used to retrieve rtnetlink family information.
type LinkService struct {
	c *Conn
}

// Constants used to request information from rtnetlink links.
const (
	RTNLGRP_LINK = 0x1

	RTM_NEWLINK = 0x10
	RTM_DELLINK = 0x11
	RTM_GETLINK = 0x12
	RTM_SETLINK = 0x13
)

// New creates a new interface using the LinkMessage information.
func (l *LinkService) New(req *LinkMessage) error {
	flags := netlink.HeaderFlagsRequest | netlink.HeaderFlagsCreate | netlink.HeaderFlagsAcknowledge | netlink.HeaderFlagsExcl
	_, err := l.c.Execute(req, RTM_NEWLINK, flags)
	if err != nil {
		return err
	}

	return nil
}

// Delete removes an interface by index.
func (l *LinkService) Delete(index uint32) error {
	req := &LinkMessage{
		Index: index,
	}

	flags := netlink.HeaderFlagsRequest | netlink.HeaderFlagsAcknowledge
	_, err := l.c.Execute(req, RTM_DELLINK, flags)
	if err != nil {
		return err
	}

	return nil
}

// Get retrieves interface information by index.
func (l *LinkService) Get(index uint32) (LinkMessage, error) {
	req := &LinkMessage{
		Index: index,
	}

	flags := netlink.HeaderFlagsRequest | netlink.HeaderFlagsDumpFiltered
	msg, err := l.c.Execute(req, RTM_GETLINK, flags)
	if err != nil {
		return LinkMessage{}, err
	}

	if len(msg) != 1 {
		return LinkMessage{}, fmt.Errorf("too many/little matches, expected 1")
	}

	link := (msg[0]).(*LinkMessage)
	return *link, nil
}

// Set sets interface attributes according to the LinkMessage information.
func (l *LinkService) Set(req *LinkMessage) error {
	flags := netlink.HeaderFlagsRequest | netlink.HeaderFlagsAcknowledge
	_, err := l.c.Execute(req, RTM_SETLINK, flags)
	if err != nil {
		return err
	}

	return nil
}

// List retrieves all interfaces.
func (l *LinkService) List() ([]LinkMessage, error) {
	req := &LinkMessage{}

	flags := netlink.HeaderFlagsRequest | netlink.HeaderFlagsDump
	msgs, err := l.c.Execute(req, RTM_GETLINK, flags)
	if err != nil {
		return nil, err
	}

	links := make([]LinkMessage, 0, len(msgs))
	for _, m := range msgs {
		link := (m).(*LinkMessage)
		links = append(links, *link)
	}

	return links, nil
}

// LinkAttributes contains all attributes for an interface.
type LinkAttributes struct {
	Address   net.HardwareAddr // Interface L2 address
	Broadcast net.HardwareAddr // L2 broadcast address
	Name      string           // Device name
	MTU       uint32           // MTU of the device
	Type      uint32           // Link type
	QueueDisc string           // Queueing discipline
	Stats     *LinkStats       // Interface Statistics
}

// Attribute IDs mapped to specific LinkAttribute fields.
const (
	iflaUnspec uint16 = iota
	iflaAddress
	iflaBroadcast
	iflaIfname
	iflaMTU
	iflaLink
	iflaQdisc
	iflaStats
)

// UnmarshalBinary unmarshals the contents of a byte slice into a LinkMessage.
func (a *LinkAttributes) UnmarshalBinary(b []byte) error {
	attrs, err := netlink.UnmarshalAttributes(b)
	if err != nil {
		return err
	}

	for _, attr := range attrs {
		switch attr.Type {
		case iflaUnspec:
			//unused attribute
		case iflaAddress:
			l := len(attr.Data)
			if l < 4 || l > 32 {
				return errInvalidLinkMessageAttr
			}
			a.Address = attr.Data
		case iflaBroadcast:
			l := len(attr.Data)
			if l < 4 || l > 32 {
				return errInvalidLinkMessageAttr
			}
			a.Broadcast = attr.Data
		case iflaIfname:
			a.Name = nlenc.String(attr.Data)
		case iflaMTU:
			if len(attr.Data) != 4 {
				return errInvalidLinkMessageAttr
			}
			a.MTU = nlenc.Uint32(attr.Data)
		case iflaLink:
			if len(attr.Data) != 4 {
				return errInvalidLinkMessageAttr
			}
			a.Type = nlenc.Uint32(attr.Data)
		case iflaQdisc:
			a.QueueDisc = nlenc.String(attr.Data)
		case iflaStats:
			a.Stats = &LinkStats{}
			err := a.Stats.UnmarshalBinary(attr.Data)
			if err != nil {
				return err
			}
		}
	}

	return nil
}

// MarshalBinary marshals a LinkAttributes into a byte slice.
func (a *LinkAttributes) MarshalBinary() ([]byte, error) {
	return netlink.MarshalAttributes([]netlink.Attribute{
		{
			Type: iflaUnspec,
			Data: nlenc.Uint16Bytes(0),
		},
		{
			Type: iflaAddress,
			Data: a.Address,
		},
		{
			Type: iflaBroadcast,
			Data: a.Broadcast,
		},
		{
			Type: iflaIfname,
			Data: nlenc.Bytes(a.Name),
		},
		{
			Type: iflaMTU,
			Data: nlenc.Uint32Bytes(a.MTU),
		},
		{
			Type: iflaLink,
			Data: nlenc.Uint32Bytes(a.Type),
		},
		{
			Type: iflaQdisc,
			Data: nlenc.Bytes(a.QueueDisc),
		},
		/*
			{
				Type: iflaStats,
				Data: nlenc.Bytes(name),
			},
		*/
	})
}

//LinkStats contains packet statistics
type LinkStats struct {
	RXPackets  uint32 // total packets received
	TXPackets  uint32 // total packets transmitted
	RXBytes    uint32 // total bytes received
	TXBytes    uint32 // total bytes transmitted
	RXErrors   uint32 // bad packets received
	TXErrors   uint32 // packet transmit problems
	RXDropped  uint32 // no space in linux buffers
	TXDropped  uint32 // no space available in linux
	Multicast  uint32 // multicast packets received
	Collisions uint32

	// detailed rx_errors:
	RXLengthErrors uint32
	RXOverErrors   uint32 // receiver ring buff overflow
	RXCRCErrors    uint32 // recved pkt with crc error
	RXFrameErrors  uint32 // recv'd frame alignment error
	RXFIFOErrors   uint32 // recv'r fifo overrun
	RXMissedErrors uint32 // receiver missed packet

	// detailed tx_errors
	TXAbortedErrors   uint32
	TXCarrierErrors   uint32
	TXFIFOErrors      uint32
	TXHeartbeatErrors uint32
	TXWindowErrors    uint32

	// for cslip etc
	RXCompressed uint32
	TXCompressed uint32

	RXNoHandler uint32 // dropped, no handler found
}

// UnmarshalBinary unmarshals the contents of a byte slice into a LinkMessage.
func (a *LinkStats) UnmarshalBinary(b []byte) error {
	if len(b) != 96 && len(b) != 104 {
		return fmt.Errorf("incorrect size, want: 96 or 104, got: %d", len(b))
	}

	a.RXPackets = nlenc.Uint32(b[0:4])
	a.TXPackets = nlenc.Uint32(b[4:8])
	a.RXBytes = nlenc.Uint32(b[8:12])
	a.TXBytes = nlenc.Uint32(b[12:16])
	a.RXErrors = nlenc.Uint32(b[16:20])
	a.TXErrors = nlenc.Uint32(b[20:24])
	a.RXDropped = nlenc.Uint32(b[24:28])
	a.TXDropped = nlenc.Uint32(b[28:32])
	a.Multicast = nlenc.Uint32(b[32:36])
	a.Collisions = nlenc.Uint32(b[36:40])

	a.RXLengthErrors = nlenc.Uint32(b[40:44])
	a.RXOverErrors = nlenc.Uint32(b[44:48])
	a.RXCRCErrors = nlenc.Uint32(b[48:52])
	a.RXFrameErrors = nlenc.Uint32(b[52:56])
	a.RXFIFOErrors = nlenc.Uint32(b[56:60])
	a.RXMissedErrors = nlenc.Uint32(b[60:64])

	a.TXAbortedErrors = nlenc.Uint32(b[68:72])
	a.TXCarrierErrors = nlenc.Uint32(b[76:80])
	a.TXFIFOErrors = nlenc.Uint32(b[80:84])
	a.TXHeartbeatErrors = nlenc.Uint32(b[84:88])
	a.TXWindowErrors = nlenc.Uint32(b[88:92])

	if len(b) == 96 {
		a.RXNoHandler = nlenc.Uint32(b[92:96])
	}

	if len(b) == 104 {
		a.RXCompressed = nlenc.Uint32(b[92:96])
		a.TXCompressed = nlenc.Uint32(b[96:100])
		a.RXNoHandler = nlenc.Uint32(b[100:104])
	}

	return nil
}
<|endoftext|>


<commit_before>// Copyright 2011 Aaron Jacobs. All Rights Reserved.
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package oglematchers

import (
	"strings"
)

// AllOf accepts a set of matchers S and returns a matcher that follows the
// algorithm below when considering a candidate c:
//
//  1. Return MATCH_TRUE if for every Matcher m in S, m matches c.
//
//  2. Otherwise, if there is a matcher m in S such that m returns
//     MATCH_UNDEFINED for c, return MATCH_UNDEFINED with that matcher's error
//     message.
//
//  3. Otherwise, return  MATCH_FALSE.
//
// This is akin to a logical AND operation for matchers.
func AllOf(matchers ...Matcher) Matcher {
	return &allOfMatcher{matchers}
}

type allOfMatcher struct {
	wrappedMatchers []Matcher
}

func (m *allOfMatcher) Description() string {
	// Special case: the empty set.
	if len(m.wrappedMatchers) == 0 {
		return "is anything"
	}

	// Join the descriptions for the wrapped matchers.
	wrappedDescs := make([]string, len(m.wrappedMatchers))
	for i, wrappedMatcher := range m.wrappedMatchers {
		wrappedDescs[i] = wrappedMatcher.Description()
	}

	return strings.Join(wrappedDescs, ", and ")
}

func (m *allOfMatcher) Matches(c interface{}) (res MatchResult, err error) {
	res = MATCH_TRUE
	for _, wrappedMatcher := range m.wrappedMatchers {
		wrappedRes, wrappedErr := wrappedMatcher.Matches(c)

		switch wrappedRes {
		case MATCH_UNDEFINED:
			// Return immediately.
			res = MATCH_UNDEFINED
			err = wrappedErr
			return

		case MATCH_FALSE:
			// Note the failure.
			res = MATCH_FALSE
			err = wrappedErr
		}
	}

	return
}
<commit_msg>Fixed up all_of.go for #20.<commit_after>// Copyright 2011 Aaron Jacobs. All Rights Reserved.
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package oglematchers

import (
	"strings"
)

// AllOf accepts a set of matchers S and returns a matcher that follows the
// algorithm below when considering a candidate c:
//
//  1. Return true if for every Matcher m in S, m matches c.
//
//  2. Otherwise, if there is a matcher m in S such that m returns a fatal
//     error for c, return that matcher's error message.
//
//  3. Otherwise, return false with the error from some wrapped matcher.
//
// This is akin to a logical AND operation for matchers.
func AllOf(matchers ...Matcher) Matcher {
	return &allOfMatcher{matchers}
}

type allOfMatcher struct {
	wrappedMatchers []Matcher
}

func (m *allOfMatcher) Description() string {
	// Special case: the empty set.
	if len(m.wrappedMatchers) == 0 {
		return "is anything"
	}

	// Join the descriptions for the wrapped matchers.
	wrappedDescs := make([]string, len(m.wrappedMatchers))
	for i, wrappedMatcher := range m.wrappedMatchers {
		wrappedDescs[i] = wrappedMatcher.Description()
	}

	return strings.Join(wrappedDescs, ", and ")
}

func (m *allOfMatcher) Matches(c interface{}) (res bool, err error) {
	res = true

	for _, wrappedMatcher := range m.wrappedMatchers {
		wrappedRes, wrappedErr := wrappedMatcher.Matches(c)

		if !wrappedRes {
			res = false
			err = wrappedErr

			// If the error is fatal, return immediately with this error.
			_, ok := wrappedErr.(*FatalError)
			if ok {
				return
			}
		}
	}

	return
}
<|endoftext|>


<commit_before>package main

import "fmt"

// ListCommand specifies options for the list subcommand.
type ListCommand struct {
	Source string `short:"s" long:"source" description:"Only look for manifests in this source"`
}

var listCommand ListCommand

// Listing utilities
func (x *ListCommand) Execute(args []string) error {
	manifestFinder, err := NewManifestFinder()
	if err != nil {
		return err
	}

	return manifestFinder.List(listCommand.Source)
}

func init() {
	_, err := parser.AddCommand("list",
		"List utilities.",
		"",
		&listCommand)

	if err != nil {
		fmt.Println(err)
	}
}
<commit_msg>add ls as alias for list<commit_after>package main

import "fmt"

// ListCommand specifies options for the list subcommand.
type ListCommand struct {
	Source string `short:"s" long:"source" description:"Only look for manifests in this source"`
}

var listCommand ListCommand

// Listing utilities
func (x *ListCommand) Execute(args []string) error {
	manifestFinder, err := NewManifestFinder()
	if err != nil {
		return err
	}

	return manifestFinder.List(listCommand.Source)
}

func init() {
	cmd, err := parser.AddCommand("list",
		"List utilities.",
		"",
		&listCommand)

	cmd.Aliases = append(cmd.Aliases, "ls")

	if err != nil {
		fmt.Println(err)
	}
}
<|endoftext|>


<commit_before>// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package utils

import (
	"net/http"
	"net/url"

	"github.com/mattermost/platform/model"
)

const (
	DIAGNOSTIC_URL = "https://d7zmvsa9e04kk.cloudfront.net"

	PROP_DIAGNOSTIC_ID                = "id"
	PROP_DIAGNOSTIC_CATEGORY          = "c"
	VAL_DIAGNOSTIC_CATEGORY_DEFAULT   = "d"
	PROP_DIAGNOSTIC_BUILD             = "b"
	PROP_DIAGNOSTIC_DATABASE          = "db"
	PROP_DIAGNOSTIC_OS                = "os"
	PROP_DIAGNOSTIC_USER_COUNT        = "uc"
	PROP_DIAGNOSTIC_ACTIVE_USER_COUNT = "auc"
)

func SendDiagnostic(values url.Values) {
	if *Cfg.ServiceSettings.EnableSecurityFixAlert {

		res, err := http.Get(DIAGNOSTIC_URL + "/i?" + values.Encode())
		if err != nil {
			return
		}

		res.Body.Close()
	}
}
<commit_msg>Fixing break<commit_after>// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package utils

import (
	"net/http"
	"net/url"
)

const (
	DIAGNOSTIC_URL = "https://d7zmvsa9e04kk.cloudfront.net"

	PROP_DIAGNOSTIC_ID                = "id"
	PROP_DIAGNOSTIC_CATEGORY          = "c"
	VAL_DIAGNOSTIC_CATEGORY_DEFAULT   = "d"
	PROP_DIAGNOSTIC_BUILD             = "b"
	PROP_DIAGNOSTIC_DATABASE          = "db"
	PROP_DIAGNOSTIC_OS                = "os"
	PROP_DIAGNOSTIC_USER_COUNT        = "uc"
	PROP_DIAGNOSTIC_ACTIVE_USER_COUNT = "auc"
)

func SendDiagnostic(values url.Values) {
	if *Cfg.ServiceSettings.EnableSecurityFixAlert {

		res, err := http.Get(DIAGNOSTIC_URL + "/i?" + values.Encode())
		if err != nil {
			return
		}

		res.Body.Close()
	}
}
<|endoftext|>


<commit_before>package pool

import (
	"errors"
	"sync"
)

var (
	// errEmptyTaskList = errors.New("task list is empty")
	errNilTask = errors.New("task is nil")
)

type taskList struct {
	m   sync.RWMutex
	len int
	val []Task
}

func (t *taskList) put(task Task) {
	t.m.Lock()
	t.val = append(t.val, task)
	t.len++
	t.m.Unlock()
}

func (t *taskList) get() (Task, bool) {
	t.m.Lock()
	var task Task
	if t.len > 0 {
		task = t.val[0]
		t.len--
		t.val = t.val[1:]
		t.m.Unlock()
		return task, true
	}
	t.m.Unlock()
	return task, false
}

// func (t *taskList) length() int {
// 	t.m.RLock()
// 	len := t.len
// 	t.m.RUnlock()
// 	return len
// }
<commit_msg>remove length<commit_after>package pool

import (
	"errors"
	"sync"
)

var (
	// errEmptyTaskList = errors.New("task list is empty")
	errNilTask = errors.New("task is nil")
)

type taskList struct {
	m   sync.Mutex
	len int
	val []Task
}

func (t *taskList) put(task Task) {
	t.m.Lock()
	t.val = append(t.val, task)
	t.len++
	t.m.Unlock()
}

func (t *taskList) get() (Task, bool) {
	t.m.Lock()
	var task Task
	if t.len > 0 {
		task = t.val[0]
		t.len--
		t.val = t.val[1:]
		t.m.Unlock()
		return task, true
	}
	t.m.Unlock()
	return task, false
}
<|endoftext|>


<commit_before>package client

import (
	"crypto/tls"
	"encoding/json"
	"errors"
	"fmt"
	"io/ioutil"
	"net/http"
	"time"

	"github.com/ninjasphere/go-ninja/config"
)

type Node struct {
	ID     string `json:"node_id"`
	SiteID string `json:"site_id"`
}

type Site struct {
	ID           string `json:"site_id"`
	MasterNodeID string `json:"master_node_id"`
	UserID       string `json:"user_id"`
	Updated      nTime  `json:"updated"`
}

var errorUnauthorised = errors.New("Unauthorised token")

type nTime time.Time

func (jt *nTime) UnmarshalJSON(data []byte) error {
	var s string
	if err := json.Unmarshal(data, &s); err != nil {
		return err
	}
	t, err := time.Parse(time.RFC3339, s)
	if err != nil {
		return err
	}

	*jt = nTime(t)
	return nil
}

type restError struct {
	Type    string `json:"type"`
	Code    int    `json:"code"`
	Message int    `json:"message"`
}

type restResponse struct {
	Type string          `json:"type"`
	Data json.RawMessage `json:"data"`
}

func getNodes() (map[string]Node, error) {
	var data []Node
	err := req(config.MustString("cloud", "nodes"), &data)

	m := make(map[string]Node)
	for _, n := range data {
		m[n.ID] = n
	}
	return m, err
}

func getSites() (map[string]Site, error) {
	var data []Site
	err := req(config.MustString("cloud", "sites"), &data)
	m := make(map[string]Site)
	for _, s := range data {
		m[s.ID] = s
	}
	return m, err
}

func req(url string, data interface{}) error {

	client := &http.Client{
		Timeout: time.Second * 30,
	}

	if config.Bool(false, "cloud", "allowSelfSigned") {
		log.Warningf("Allowing self-signed cerificate (should only be used to connect to development cloud)")
		client.Transport = &http.Transport{
			TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
		}
	}

	resp, err := client.Get(fmt.Sprintf(url, config.MustString("token")))
	if err != nil {
		return err
	}

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

	var response restResponse

	err = json.Unmarshal(body, &response)
	if err != nil {
		return err
	}

	if resp.StatusCode != http.StatusOK || response.Type == "error" {
		var data restError
		err = json.Unmarshal(response.Data, &data)

		if data.Type == "authentication_invalid_token" {
			return errorUnauthorised
		}

		if err != nil {
			return err
		}

		return fmt.Errorf("Error from cloud: %s (%s)", data.Message, data.Type)
	}

	return json.Unmarshal(response.Data, data)
}
<commit_msg>Log the cloud calls to debug<commit_after>package client

import (
	"crypto/tls"
	"encoding/json"
	"errors"
	"fmt"
	"io/ioutil"
	"net/http"
	"time"

	"github.com/ninjasphere/go-ninja/config"
)

type Node struct {
	ID     string `json:"node_id"`
	SiteID string `json:"site_id"`
}

type Site struct {
	ID           string `json:"site_id"`
	MasterNodeID string `json:"master_node_id"`
	UserID       string `json:"user_id"`
	Updated      nTime  `json:"updated"`
}

var errorUnauthorised = errors.New("Unauthorised token")

type nTime time.Time

func (jt *nTime) UnmarshalJSON(data []byte) error {
	var s string
	if err := json.Unmarshal(data, &s); err != nil {
		return err
	}
	t, err := time.Parse(time.RFC3339, s)
	if err != nil {
		return err
	}

	*jt = nTime(t)
	return nil
}

type restError struct {
	Type    string `json:"type"`
	Code    int    `json:"code"`
	Message int    `json:"message"`
}

type restResponse struct {
	Type string          `json:"type"`
	Data json.RawMessage `json:"data"`
}

func getNodes() (map[string]Node, error) {
	var data []Node
	err := req(config.MustString("cloud", "nodes"), &data)
	log.Debugf("Fetched nodes: %+v", data)

	m := make(map[string]Node)
	for _, n := range data {
		m[n.ID] = n
	}

	return m, err
}

func getSites() (map[string]Site, error) {
	var data []Site
	err := req(config.MustString("cloud", "sites"), &data)
	log.Debugf("Fetched sites: %+v", data)

	m := make(map[string]Site)
	for _, s := range data {
		m[s.ID] = s
	}

	return m, err
}

func req(url string, data interface{}) error {

	client := &http.Client{
		Timeout: time.Second * 30,
	}

	if config.Bool(false, "cloud", "allowSelfSigned") {
		log.Warningf("Allowing self-signed cerificate (should only be used to connect to development cloud)")
		client.Transport = &http.Transport{
			TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
		}
	}

	resp, err := client.Get(fmt.Sprintf(url, config.MustString("token")))
	if err != nil {
		return err
	}

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

	var response restResponse

	err = json.Unmarshal(body, &response)
	if err != nil {
		return err
	}

	if resp.StatusCode != http.StatusOK || response.Type == "error" {
		var data restError
		err = json.Unmarshal(response.Data, &data)

		if data.Type == "authentication_invalid_token" {
			return errorUnauthorised
		}

		if err != nil {
			return err
		}

		return fmt.Errorf("Error from cloud: %s (%s)", data.Message, data.Type)
	}

	return json.Unmarshal(response.Data, data)
}
<|endoftext|>


<commit_before>package file

import (
	"os"
	"strconv"
)

// Parses a file into the correct FTP-defined string.
func ParseFile(file os.FileInfo, parseType string) string {
	fileName := file.Name()
	bytes := strconv.FormatInt(file.Size(), 10)

	switch parseType {
	// ASCII data format type
	case "A":

		//      drwx------   3 slacker    users         104 Jul 27 01:45 public_html"   <- example
		return "drwx------   3 slacker    users         " + bytes + " Jul 27 01:45 " + fileName
	default:
		return "HELLO THERE"
	}
}<commit_msg>Yet another derp<commit_after>package utils

import (
	"os"
	"strconv"
)

// Parses a file into the correct FTP-defined string.
func ParseFile(file os.FileInfo, parseType string) string {
	fileName := file.Name()
	bytes := strconv.FormatInt(file.Size(), 10)

	switch parseType {
	// ASCII data format type
	case "A":

		//      drwx------   3 slacker    users         104 Jul 27 01:45 public_html"   <- example
		return "drwx------   3 slacker    users         " + bytes + " Jul 27 01:45 " + fileName
	default:
		return "HELLO THERE"
	}
}<|endoftext|>


<commit_before>package lib

import (
	"database/sql"
	"log"
)

// Check that select to a table works.  Return an error if we get a failure.
func CheckTableAccess(dbh *sql.DB, table_name string) error {
	sql_select := "SELECT 1 FROM " + table_name + " LIMIT 1"

	var one int
	err := dbh.QueryRow(sql_select).Scan(&one)
	switch {
	case err == sql.ErrNoRows:
		log.Println("No setting with that variable_name", one)
	case err != nil:
		log.Fatal(err)
	default:
		// we don't care if there's no error
	}

	return err
}
<commit_msg>CheckTableAccess(): respond correctly to zero rows being returned<commit_after>package lib

import (
	"database/sql"
	"log"
)

// Check that select to a table works.  Return an error if we get a failure.
func CheckTableAccess(dbh *sql.DB, table_name string) error {
	sql_select := "SELECT 1 FROM " + table_name + " LIMIT 1"

	var one int
	err := dbh.QueryRow(sql_select).Scan(&one)
	switch {
	case err == sql.ErrNoRows:
		// no rows is unlikely except on a recently started server so take it into account.
		err = nil
	case err != nil:
		log.Fatal("Unable to SELECT FROM " + table_name + ":", err)
	default:
		// we don't care if there's no error
	}

	return err
}
<|endoftext|>


<commit_before>// Copyright 2020 The LUCI Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package serviceaccountsv2

import (
	"context"

	"github.com/golang/protobuf/ptypes/empty"
	"google.golang.org/grpc"
	"google.golang.org/grpc/codes"

	"go.chromium.org/luci/config/validation"
	"go.chromium.org/luci/tokenserver/api/admin/v1"
)

// ImportProjectOwnedAccountsConfigsRPC implements the corresponding method.
type ImportProjectOwnedAccountsConfigsRPC struct {
	MappingCache *MappingCache // usually GlobalMappingCache, but replaced in tests
}

// ImportServiceAccountsConfigs fetches configs from luci-config right now.
func (r *ImportProjectOwnedAccountsConfigsRPC) ImportProjectOwnedAccountsConfigs(c context.Context, _ *empty.Empty) (*admin.ImportedConfigs, error) {
	// TODO
	return nil, grpc.Errorf(codes.Unimplemented, "not implemented yet")
}

// SetupConfigValidation registers the config validation rules.
func (r *ImportProjectOwnedAccountsConfigsRPC) SetupConfigValidation(rules *validation.RuleSet) {
	r.MappingCache.SetupConfigValidation(rules)
}
<commit_msg>[tokenserver] Really implement ImportProjectOwnedAccountsConfigs.<commit_after>// Copyright 2020 The LUCI Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package serviceaccountsv2

import (
	"context"

	"github.com/golang/protobuf/ptypes/empty"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"

	"go.chromium.org/luci/common/logging"
	"go.chromium.org/luci/config/validation"
	"go.chromium.org/luci/tokenserver/api/admin/v1"
)

// ImportProjectOwnedAccountsConfigsRPC implements the corresponding method.
type ImportProjectOwnedAccountsConfigsRPC struct {
	MappingCache *MappingCache // usually GlobalMappingCache, but replaced in tests
}

// ImportProjectOwnedAccountsConfigs fetches configs from luci-config right now.
func (r *ImportProjectOwnedAccountsConfigsRPC) ImportProjectOwnedAccountsConfigs(ctx context.Context, _ *empty.Empty) (*admin.ImportedConfigs, error) {
	rev, err := r.MappingCache.ImportConfigs(ctx)
	if err != nil {
		logging.WithError(err).Errorf(ctx, "Failed to fetch service accounts configs")
		return nil, status.Errorf(codes.Internal, "%s", err.Error())
	}
	return &admin.ImportedConfigs{Revision: rev}, nil
}

// SetupConfigValidation registers the config validation rules.
func (r *ImportProjectOwnedAccountsConfigsRPC) SetupConfigValidation(rules *validation.RuleSet) {
	r.MappingCache.SetupConfigValidation(rules)
}
<|endoftext|>