<commit_before>package eos

/*
#cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework Cocoa -framework EDSDK
#define __MACOS__ 1
#include <EDSDK/EDSDK.h>
#include <EDSDK/EDSDKTypes.h>
#include <stdlib.h>
*/
import (
	"C"
)
import (
	"errors"
	"fmt"
	"unsafe"
)

type EOSClient struct{}

func NewEOSClient() *EOSClient {
	return &EOSClient{}
}

// Initialize a new EOSClient instance
func (e *EOSClient) Initialize() error {
	eosError := C.EdsInitializeSDK()
	if eosError != C.EDS_ERR_OK {
		return errors.New("Error when initializing Canon SDK")
	}
	return nil
}

// Release the EOSClient, must be called on termination
func (p *EOSClient) Release() error {
	eosError := C.EdsTerminateSDK()
	if eosError != C.EDS_ERR_OK {
		return errors.New("Error when terminating Canon SDK")
	}
	return nil
}

// Get an array representing the cameras currently connected.  Each CameraModel
// instance must be released by invoking the Release function once no longer
// needed.
func (e *EOSClient) GetCameraModels() ([]CameraModel, error) {
	var eosCameraList *C.EdsCameraListRef
	var eosError C.EdsError
	var cameraCount int

	// get a reference to the cameras list record
	if eosError = C.EdsGetCameraList((*C.EdsCameraListRef)(unsafe.Pointer(&eosCameraList))); eosError != C.EDS_ERR_OK {
		return nil, errors.New(fmt.Sprintf("Error when obtaining reference to list of cameras (code=%d)", eosError))
	}
	defer C.EdsRelease((*C.struct___EdsObject)(unsafe.Pointer(&eosCameraList)))

	// get the number of cameras connected
	if eosError = C.EdsGetChildCount((*C.struct___EdsObject)(unsafe.Pointer(eosCameraList)), (*C.EdsUInt32)(unsafe.Pointer(&cameraCount))); eosError != C.EDS_ERR_OK {
		return nil, errors.New(fmt.Sprintf("Error when obtaining count of connected cameras (code=%d)", eosError))
	}

	// get details for each camera detected
	cameras := make([]CameraModel, 0)
	for i := 0; i < cameraCount; i++ {
		var eosCameraRef *C.EdsCameraRef
		if eosError = C.EdsGetChildAtIndex((*C.struct___EdsObject)(unsafe.Pointer(eosCameraList)), (C.EdsInt32)(i), (*C.EdsBaseRef)(unsafe.Pointer(&eosCameraRef))); eosError != C.EDS_ERR_OK {
			return nil, errors.New(fmt.Sprintf("Error when obtaining reference to camera (code=%d)", eosError))
		}

		var eosCameraDeviceInfo C.EdsDeviceInfo
		if eosError = C.EdsGetDeviceInfo((*C.struct___EdsObject)(unsafe.Pointer(eosCameraRef)), (*C.EdsDeviceInfo)(unsafe.Pointer(&eosCameraDeviceInfo))); eosError != C.EDS_ERR_OK {
			return nil, errors.New(fmt.Sprintf("Error when obtaining camera device info (code=%d)", eosError))
		}

		// instantiate new CameraModel with the camera reference and model details
		camera := CameraModel{
			camera:              eosCameraRef,
			szPortName:          C.GoString((*_Ctype_char)(&eosCameraDeviceInfo.szPortName[0])),
			szDeviceDescription: C.GoString((*_Ctype_char)(&eosCameraDeviceInfo.szDeviceDescription[0])),
			deviceSubType:       (uint32)(eosCameraDeviceInfo.deviceSubType),
			reserved:            (uint32)(eosCameraDeviceInfo.reserved),
		}
		cameras = append(cameras, camera)
	}

	return cameras, nil
}

type CameraModel struct {
	camera         *C.EdsCameraRef
	sessionOpen    bool
	liveViewActive bool

	szPortName          string
	szDeviceDescription string
	deviceSubType       uint32
	reserved            uint32
}

// Releases reference to the camera
func (c *CameraModel) Release() {
	C.EdsRelease((*C.struct___EdsObject)(unsafe.Pointer(&c.camera)))
}

// Open a session with the camera for sending commands
func (c *CameraModel) OpenSession() error {
	eosError := C.EdsOpenSession((*C.struct___EdsObject)(unsafe.Pointer(c.camera)))
	if eosError != C.EDS_ERR_OK {
		return errors.New(fmt.Sprintf("Error when opening session with camera (code=%d)", eosError))
	}
	c.sessionOpen = true
	return nil
}

// Close an existing camera session
func (c *CameraModel) CloseSession() {
	if c.sessionOpen == false {
		return
	}
	c.sessionOpen = false
	C.EdsCloseSession((*C.struct___EdsObject)(unsafe.Pointer(&c.camera)))
}

// Take a picture
func (c *CameraModel) TakePicture() error {
	if c.sessionOpen == false {
		return errors.New("Session is not open, must call OpenSession first")
	}
	eosError := C.EdsSendCommand((*C.struct___EdsObject)(unsafe.Pointer(c.camera)), C.kEdsCameraCommand_TakePicture, 0)
	if eosError != C.EDS_ERR_OK {
		return errors.New(fmt.Sprintf("Error when taking picture (code=%d)", eosError))
	}
	return nil
}

// Start LiveView on the PC
func (c *CameraModel) StartLiveView() error {
	if c.sessionOpen == false {
		return errors.New("Session is not open, must call OpenSession first")
	}

	if c.liveViewActive == true {
		return errors.New("LiveView is already active, cannot start")
	}

	var device int
	eosError := C.EdsGetPropertyData((*C.struct___EdsObject)(unsafe.Pointer(c.camera)), C.kEdsPropID_Evf_OutputDevice, 0, (C.EdsUInt32)(unsafe.Sizeof(device)), unsafe.Pointer(&device))
	if eosError != C.EDS_ERR_OK {
		return errors.New(fmt.Sprintf("Error getting output device property when activating LiveMode (code=%d)", eosError))
	}

	// connect PC to Live View output device
	device |= C.kEdsEvfOutputDevice_PC
	eosError = C.EdsSetPropertyData((*C.struct___EdsObject)(unsafe.Pointer(c.camera)), C.kEdsPropID_Evf_OutputDevice, 0, (C.EdsUInt32)(unsafe.Sizeof(device)), unsafe.Pointer(&device))
	if eosError != C.EDS_ERR_OK {
		return errors.New(fmt.Sprintf("Error setting output device property when activating LiveMode (code=%d)", eosError))
	}
	c.liveViewActive = true
	return nil
}

// Stop LiveView on the PC
func (c *CameraModel) StopLiveView() error {
	if c.sessionOpen == false {
		return errors.New("Session is not open, must call OpenSession first")
	}

	if c.liveViewActive == false {
		return errors.New("LiveView is already inactive, cannot stop")
	}

	var device int
	eosError := C.EdsGetPropertyData((*C.struct___EdsObject)(unsafe.Pointer(c.camera)), C.kEdsPropID_Evf_OutputDevice, 0, (C.EdsUInt32)(unsafe.Sizeof(device)), unsafe.Pointer(&device))
	if eosError != C.EDS_ERR_OK {
		return errors.New(fmt.Sprintf("Error getting output device property when stopping LiveMode (code=%d)", eosError))
	}

	// connect PC to Live View output device
	device &= ^C.kEdsEvfOutputDevice_PC
	eosError = C.EdsSetPropertyData((*C.struct___EdsObject)(unsafe.Pointer(c.camera)), C.kEdsPropID_Evf_OutputDevice, 0, (C.EdsUInt32)(unsafe.Sizeof(device)), unsafe.Pointer(&device))
	if eosError != C.EDS_ERR_OK {
		return errors.New(fmt.Sprintf("Error setting output device property when stopping LiveMode (code=%d)", eosError))
	}
	c.liveViewActive = false
	return nil
}

// Toggle the LiveView state of the camera
func (c *CameraModel) ToggleLiveView() error {
	if c.liveViewActive {
		return c.StopLiveView()
	} else {
		return c.StartLiveView()
	}
}
<commit_msg>Add function to set the LiveView device to use<commit_after>package eos

/*
#cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework Cocoa -framework EDSDK
#define __MACOS__ 1
#include <EDSDK/EDSDK.h>
#include <EDSDK/EDSDKTypes.h>
#include <stdlib.h>
*/
import (
	"C"
)
import (
	"errors"
	"fmt"
	"unsafe"
)

type LiveViewOutputDevice int

const (
	TFT LiveViewOutputDevice = iota
	PC
)

type EOSClient struct{}

func NewEOSClient() *EOSClient {
	return &EOSClient{}
}

// Initialize a new EOSClient instance
func (e *EOSClient) Initialize() error {
	eosError := C.EdsInitializeSDK()
	if eosError != C.EDS_ERR_OK {
		return errors.New("Error when initializing Canon SDK")
	}
	return nil
}

// Release the EOSClient, must be called on termination
func (p *EOSClient) Release() error {
	eosError := C.EdsTerminateSDK()
	if eosError != C.EDS_ERR_OK {
		return errors.New("Error when terminating Canon SDK")
	}
	return nil
}

// Get an array representing the cameras currently connected.  Each CameraModel
// instance must be released by invoking the Release function once no longer
// needed.
func (e *EOSClient) GetCameraModels() ([]CameraModel, error) {
	var eosCameraList *C.EdsCameraListRef
	var eosError C.EdsError
	var cameraCount int

	// get a reference to the cameras list record
	if eosError = C.EdsGetCameraList((*C.EdsCameraListRef)(unsafe.Pointer(&eosCameraList))); eosError != C.EDS_ERR_OK {
		return nil, errors.New(fmt.Sprintf("Error when obtaining reference to list of cameras (code=%d)", eosError))
	}
	defer C.EdsRelease((*C.struct___EdsObject)(unsafe.Pointer(&eosCameraList)))

	// get the number of cameras connected
	if eosError = C.EdsGetChildCount((*C.struct___EdsObject)(unsafe.Pointer(eosCameraList)), (*C.EdsUInt32)(unsafe.Pointer(&cameraCount))); eosError != C.EDS_ERR_OK {
		return nil, errors.New(fmt.Sprintf("Error when obtaining count of connected cameras (code=%d)", eosError))
	}

	// get details for each camera detected
	cameras := make([]CameraModel, 0)
	for i := 0; i < cameraCount; i++ {
		var eosCameraRef *C.EdsCameraRef
		if eosError = C.EdsGetChildAtIndex((*C.struct___EdsObject)(unsafe.Pointer(eosCameraList)), (C.EdsInt32)(i), (*C.EdsBaseRef)(unsafe.Pointer(&eosCameraRef))); eosError != C.EDS_ERR_OK {
			return nil, errors.New(fmt.Sprintf("Error when obtaining reference to camera (code=%d)", eosError))
		}

		var eosCameraDeviceInfo C.EdsDeviceInfo
		if eosError = C.EdsGetDeviceInfo((*C.struct___EdsObject)(unsafe.Pointer(eosCameraRef)), (*C.EdsDeviceInfo)(unsafe.Pointer(&eosCameraDeviceInfo))); eosError != C.EDS_ERR_OK {
			return nil, errors.New(fmt.Sprintf("Error when obtaining camera device info (code=%d)", eosError))
		}

		// instantiate new CameraModel with the camera reference and model details
		camera := CameraModel{
			camera:              eosCameraRef,
			szPortName:          C.GoString((*_Ctype_char)(&eosCameraDeviceInfo.szPortName[0])),
			szDeviceDescription: C.GoString((*_Ctype_char)(&eosCameraDeviceInfo.szDeviceDescription[0])),
			deviceSubType:       (uint32)(eosCameraDeviceInfo.deviceSubType),
			reserved:            (uint32)(eosCameraDeviceInfo.reserved),
		}
		cameras = append(cameras, camera)
	}

	return cameras, nil
}

type CameraModel struct {
	camera         *C.EdsCameraRef
	sessionOpen    bool
	liveViewActive bool
	liveViewDevice int

	szPortName          string
	szDeviceDescription string
	deviceSubType       uint32
	reserved            uint32
}

// Releases reference to the camera
func (c *CameraModel) Release() {
	C.EdsRelease((*C.struct___EdsObject)(unsafe.Pointer(&c.camera)))
}

// Open a session with the camera for sending commands
func (c *CameraModel) OpenSession() error {
	eosError := C.EdsOpenSession((*C.struct___EdsObject)(unsafe.Pointer(c.camera)))
	if eosError != C.EDS_ERR_OK {
		return errors.New(fmt.Sprintf("Error when opening session with camera (code=%d)", eosError))
	}
	c.sessionOpen = true
	return nil
}

// Close an existing camera session
func (c *CameraModel) CloseSession() {
	if c.sessionOpen == false {
		return
	}
	c.sessionOpen = false
	C.EdsCloseSession((*C.struct___EdsObject)(unsafe.Pointer(&c.camera)))
}

// Take a picture
func (c *CameraModel) TakePicture() error {
	if c.sessionOpen == false {
		return errors.New("Session is not open, must call OpenSession first")
	}
	eosError := C.EdsSendCommand((*C.struct___EdsObject)(unsafe.Pointer(c.camera)), C.kEdsCameraCommand_TakePicture, 0)
	if eosError != C.EDS_ERR_OK {
		return errors.New(fmt.Sprintf("Error when taking picture (code=%d)", eosError))
	}
	return nil
}

// Start LiveView on the device configured with SetLiveViewOutputDevice
func (c *CameraModel) StartLiveView() error {
	if c.sessionOpen == false {
		return errors.New("Session is not open, must call OpenSession first")
	}

	if c.liveViewActive == true {
		return errors.New("LiveView is already active, cannot start")
	}

	var device int
	eosError := C.EdsGetPropertyData((*C.struct___EdsObject)(unsafe.Pointer(c.camera)), C.kEdsPropID_Evf_OutputDevice, 0, (C.EdsUInt32)(unsafe.Sizeof(device)), unsafe.Pointer(&device))
	if eosError != C.EDS_ERR_OK {
		return errors.New(fmt.Sprintf("Error getting output device property when activating LiveMode (code=%d)", eosError))
	}

	// connect Live View output device
	device |= c.liveViewDevice
	eosError = C.EdsSetPropertyData((*C.struct___EdsObject)(unsafe.Pointer(c.camera)), C.kEdsPropID_Evf_OutputDevice, 0, (C.EdsUInt32)(unsafe.Sizeof(device)), unsafe.Pointer(&device))
	if eosError != C.EDS_ERR_OK {
		return errors.New(fmt.Sprintf("Error setting output device property when activating LiveMode (code=%d)", eosError))
	}
	c.liveViewActive = true
	return nil
}

// Stop LiveView on the device configured with SetLiveViewOutputDevice
func (c *CameraModel) StopLiveView() error {
	if c.sessionOpen == false {
		return errors.New("Session is not open, must call OpenSession first")
	}

	if c.liveViewActive == false {
		return errors.New("LiveView is already inactive, cannot stop")
	}

	var device int
	eosError := C.EdsGetPropertyData((*C.struct___EdsObject)(unsafe.Pointer(c.camera)), C.kEdsPropID_Evf_OutputDevice, 0, (C.EdsUInt32)(unsafe.Sizeof(device)), unsafe.Pointer(&device))
	if eosError != C.EDS_ERR_OK {
		return errors.New(fmt.Sprintf("Error getting output device property when stopping LiveMode (code=%d)", eosError))
	}

	// disconnect Live View output device
	device &= ^c.liveViewDevice
	eosError = C.EdsSetPropertyData((*C.struct___EdsObject)(unsafe.Pointer(c.camera)), C.kEdsPropID_Evf_OutputDevice, 0, (C.EdsUInt32)(unsafe.Sizeof(device)), unsafe.Pointer(&device))
	if eosError != C.EDS_ERR_OK {
		return errors.New(fmt.Sprintf("Error setting output device property when stopping LiveMode (code=%d)", eosError))
	}
	c.liveViewActive = false
	return nil
}

// Toggle the LiveView state of the camera
func (c *CameraModel) ToggleLiveView() error {
	if c.liveViewActive {
		return c.StopLiveView()
	} else {
		return c.StartLiveView()
	}
}

// Set the device to use with LiveView.  Will stop LiveView if already active on a device
func (c *CameraModel) SetLiveViewOutputDevice(device LiveViewOutputDevice) error {
	if c.liveViewActive {
		if err := c.StopLiveView(); err != nil {
			return err
		}
	}

	switch device {
	case TFT:
		c.liveViewDevice = C.kEdsEvfOutputDevice_TFT
		break
	case PC:
		c.liveViewDevice = C.kEdsEvfOutputDevice_PC
		break
	default:
		return errors.New("Unrecognized LiveView device supplied")
	}
	return nil
}
<|endoftext|>


<commit_before>package sqlstore

import (
	"context"
	"database/sql"
	"database/sql/driver"
	"time"

	"github.com/gchaincl/sqlhooks"
	"github.com/go-sql-driver/mysql"
	"github.com/grafana/grafana/pkg/infra/log"
	"github.com/grafana/grafana/pkg/services/sqlstore/migrator"
	"github.com/lib/pq"
	"github.com/mattn/go-sqlite3"
	"github.com/prometheus/client_golang/prometheus"
	"xorm.io/core"
)

var (
	databaseQueryCounter   *prometheus.CounterVec
	databaseQueryHistogram *prometheus.HistogramVec
)

func init() {
	databaseQueryCounter = prometheus.NewCounterVec(prometheus.CounterOpts{
		Namespace: "grafana",
		Name:      "database_queries_total",
		Help:      "The total amount of Database queries",
	}, []string{"status"})

	databaseQueryHistogram = prometheus.NewHistogramVec(prometheus.HistogramOpts{
		Namespace: "grafana",
		Name:      "database_queries_duration_seconds",
		Help:      "Database query histogram",
		Buckets:   prometheus.ExponentialBuckets(0.0001, 4, 9),
	}, []string{"status"})

	prometheus.MustRegister(databaseQueryCounter, databaseQueryHistogram)
}

// WrapDatabaseDriverWithHooks creates a fake database driver that
// executes pre and post functions which we use to gather metrics about
// database queries.
func WrapDatabaseDriverWithHooks(dbType string) string {
	drivers := map[string]driver.Driver{
		migrator.SQLITE:   &sqlite3.SQLiteDriver{},
		migrator.MYSQL:    &mysql.MySQLDriver{},
		migrator.POSTGRES: &pq.Driver{},
	}

	d, exist := drivers[dbType]
	if !exist {
		return dbType
	}

	driverWithHooks := dbType + "WithHooks"
	sql.Register(driverWithHooks, sqlhooks.Wrap(d, &databaseQueryWrapper{log: log.New("sqlstore.metrics")}))
	core.RegisterDriver(driverWithHooks, &databaseQueryWrapperParser{dbType: dbType})
	return driverWithHooks
}

// databaseQueryWrapper satisfies the sqlhook.databaseQueryWrapper interface
// which allow us to wrap all SQL queries with a `Before` & `After` hook.
type databaseQueryWrapper struct {
	log log.Logger
}

// databaseQueryWrapperKey is used as key to save values in `context.Context`
type databaseQueryWrapperKey struct{}

// Before hook will print the query with its args and return the context with the timestamp
func (h *databaseQueryWrapper) Before(ctx context.Context, query string, args ...interface{}) (context.Context, error) {
	return context.WithValue(ctx, databaseQueryWrapperKey{}, time.Now()), nil
}

// After hook will get the timestamp registered on the Before hook and print the elapsed time
func (h *databaseQueryWrapper) After(ctx context.Context, query string, args ...interface{}) (context.Context, error) {
	begin := ctx.Value(databaseQueryWrapperKey{}).(time.Time)
	elapsed := time.Since(begin)
	databaseQueryCounter.WithLabelValues("success").Inc()
	databaseQueryHistogram.WithLabelValues("success").Observe(elapsed.Seconds())
	h.log.Debug("query finished", "status", "success", "elapsed time", elapsed, "sql", query)
	return ctx, nil
}

// OnError will be called if any error happens
func (h *databaseQueryWrapper) OnError(ctx context.Context, err error, query string, args ...interface{}) error {
	status := "error"
	// https://golang.org/pkg/database/sql/driver/#ErrSkip
	if err == nil || err == driver.ErrSkip {
		status = "success"
	}

	begin := ctx.Value(databaseQueryWrapperKey{}).(time.Time)
	elapsed := time.Since(begin)
	databaseQueryCounter.WithLabelValues(status).Inc()
	databaseQueryHistogram.WithLabelValues(status).Observe(elapsed.Seconds())
	h.log.Debug("query finished", "status", status, "elapsed time", elapsed, "sql", query, "error", err)
	return err
}

type databaseQueryWrapperParser struct {
	dbType string
}

func (hp *databaseQueryWrapperParser) Parse(string, string) (*core.Uri, error) {
	return &core.Uri{
		DbType: core.DbType(hp.dbType),
	}, nil
}
<commit_msg>remove status label from histogram (#28387)<commit_after>package sqlstore

import (
	"context"
	"database/sql"
	"database/sql/driver"
	"time"

	"github.com/gchaincl/sqlhooks"
	"github.com/go-sql-driver/mysql"
	"github.com/grafana/grafana/pkg/infra/log"
	"github.com/grafana/grafana/pkg/services/sqlstore/migrator"
	"github.com/lib/pq"
	"github.com/mattn/go-sqlite3"
	"github.com/prometheus/client_golang/prometheus"
	"xorm.io/core"
)

var (
	databaseQueryCounter   *prometheus.CounterVec
	databaseQueryHistogram prometheus.Histogram
)

func init() {
	databaseQueryCounter = prometheus.NewCounterVec(prometheus.CounterOpts{
		Namespace: "grafana",
		Name:      "database_queries_total",
		Help:      "The total amount of Database queries",
	}, []string{"status"})

	databaseQueryHistogram = prometheus.NewHistogram(prometheus.HistogramOpts{
		Namespace: "grafana",
		Name:      "database_queries_duration_seconds",
		Help:      "Database query histogram",
		Buckets:   prometheus.ExponentialBuckets(0.0001, 4, 9),
	})

	prometheus.MustRegister(databaseQueryCounter, databaseQueryHistogram)
}

// WrapDatabaseDriverWithHooks creates a fake database driver that
// executes pre and post functions which we use to gather metrics about
// database queries.
func WrapDatabaseDriverWithHooks(dbType string) string {
	drivers := map[string]driver.Driver{
		migrator.SQLITE:   &sqlite3.SQLiteDriver{},
		migrator.MYSQL:    &mysql.MySQLDriver{},
		migrator.POSTGRES: &pq.Driver{},
	}

	d, exist := drivers[dbType]
	if !exist {
		return dbType
	}

	driverWithHooks := dbType + "WithHooks"
	sql.Register(driverWithHooks, sqlhooks.Wrap(d, &databaseQueryWrapper{log: log.New("sqlstore.metrics")}))
	core.RegisterDriver(driverWithHooks, &databaseQueryWrapperParser{dbType: dbType})
	return driverWithHooks
}

// databaseQueryWrapper satisfies the sqlhook.databaseQueryWrapper interface
// which allow us to wrap all SQL queries with a `Before` & `After` hook.
type databaseQueryWrapper struct {
	log log.Logger
}

// databaseQueryWrapperKey is used as key to save values in `context.Context`
type databaseQueryWrapperKey struct{}

// Before hook will print the query with its args and return the context with the timestamp
func (h *databaseQueryWrapper) Before(ctx context.Context, query string, args ...interface{}) (context.Context, error) {
	return context.WithValue(ctx, databaseQueryWrapperKey{}, time.Now()), nil
}

// After hook will get the timestamp registered on the Before hook and print the elapsed time
func (h *databaseQueryWrapper) After(ctx context.Context, query string, args ...interface{}) (context.Context, error) {
	begin := ctx.Value(databaseQueryWrapperKey{}).(time.Time)
	elapsed := time.Since(begin)
	databaseQueryCounter.WithLabelValues("success").Inc()
	databaseQueryHistogram.Observe(elapsed.Seconds())
	h.log.Debug("query finished", "status", "success", "elapsed time", elapsed, "sql", query)
	return ctx, nil
}

// OnError will be called if any error happens
func (h *databaseQueryWrapper) OnError(ctx context.Context, err error, query string, args ...interface{}) error {
	status := "error"
	// https://golang.org/pkg/database/sql/driver/#ErrSkip
	if err == nil || err == driver.ErrSkip {
		status = "success"
	}

	begin := ctx.Value(databaseQueryWrapperKey{}).(time.Time)
	elapsed := time.Since(begin)
	databaseQueryCounter.WithLabelValues(status).Inc()
	databaseQueryHistogram.Observe(elapsed.Seconds())
	h.log.Debug("query finished", "status", status, "elapsed time", elapsed, "sql", query, "error", err)
	return err
}

type databaseQueryWrapperParser struct {
	dbType string
}

func (hp *databaseQueryWrapperParser) Parse(string, string) (*core.Uri, error) {
	return &core.Uri{
		DbType: core.DbType(hp.dbType),
	}, nil
}
<|endoftext|>


<commit_before>// Package errdefs defines the common errors used throughout containerd
// packages.
//
// Use with errors.Wrap and error.Wrapf to add context to an error.
//
// To detect an error class, use the IsXXX functions to tell whether an error
// is of a certain type.
//
// The functions ToGRPC and FromGRPC can be used to map server-side and
// client-side errors to the correct types.
package errdefs

import "github.com/pkg/errors"

// Definitions of common error types used throughout containerd. All containerd
// errors returned by most packages will map into one of these errors classes.
// Packages should return errors of these types when they want to instruct a
// client to take a particular action.
//
// For the most part, we just try to provide local grpc errors. Most conditions
// map very well to those defined by grpc.
var (
	ErrUnknown            = errors.New("unknown") // used internally to represent a missed mapping.
	ErrInvalidArgument    = errors.New("invalid")
	ErrNotFound           = errors.New("not found")
	ErrAlreadyExists      = errors.New("already exists")
	ErrFailedPrecondition = errors.New("failed precondition")
	ErrUnavailable        = errors.New("unavailable")
)

func IsInvalidArgument(err error) bool {
	return errors.Cause(err) == ErrInvalidArgument
}

// IsNotFound returns true if the error is due to a missing object
func IsNotFound(err error) bool {
	return errors.Cause(err) == ErrNotFound
}

// IsAlreadyExists returns true if the error is due to an already existing
// metadata item
func IsAlreadyExists(err error) bool {
	return errors.Cause(err) == ErrAlreadyExists
}

// IsFailedPrecondition returns true if an operation could not proceed to the
// lack of a particular condition.
func IsFailedPrecondition(err error) bool {
	return errors.Cause(err) == ErrFailedPrecondition
}

func IsUnavailable(err error) bool {
	return errors.Cause(err) == ErrUnavailable
}
<commit_msg>Update errdefs.ErrInvalidArgument message<commit_after>// Package errdefs defines the common errors used throughout containerd
// packages.
//
// Use with errors.Wrap and error.Wrapf to add context to an error.
//
// To detect an error class, use the IsXXX functions to tell whether an error
// is of a certain type.
//
// The functions ToGRPC and FromGRPC can be used to map server-side and
// client-side errors to the correct types.
package errdefs

import "github.com/pkg/errors"

// Definitions of common error types used throughout containerd. All containerd
// errors returned by most packages will map into one of these errors classes.
// Packages should return errors of these types when they want to instruct a
// client to take a particular action.
//
// For the most part, we just try to provide local grpc errors. Most conditions
// map very well to those defined by grpc.
var (
	ErrUnknown            = errors.New("unknown") // used internally to represent a missed mapping.
	ErrInvalidArgument    = errors.New("invalid argument")
	ErrNotFound           = errors.New("not found")
	ErrAlreadyExists      = errors.New("already exists")
	ErrFailedPrecondition = errors.New("failed precondition")
	ErrUnavailable        = errors.New("unavailable")
)

func IsInvalidArgument(err error) bool {
	return errors.Cause(err) == ErrInvalidArgument
}

// IsNotFound returns true if the error is due to a missing object
func IsNotFound(err error) bool {
	return errors.Cause(err) == ErrNotFound
}

// IsAlreadyExists returns true if the error is due to an already existing
// metadata item
func IsAlreadyExists(err error) bool {
	return errors.Cause(err) == ErrAlreadyExists
}

// IsFailedPrecondition returns true if an operation could not proceed to the
// lack of a particular condition.
func IsFailedPrecondition(err error) bool {
	return errors.Cause(err) == ErrFailedPrecondition
}

func IsUnavailable(err error) bool {
	return errors.Cause(err) == ErrUnavailable
}
<|endoftext|>


<commit_before>package ansicolor_test

import (
	"bytes"
	"testing"

	"github.com/shiena/ansicolor"
)

func TestNewAnsiColor1(t *testing.T) {
	inner := bytes.NewBufferString("")
	w := ansicolor.NewAnsiColorWriter(inner)
	if w == inner {
		t.Errorf("Get %#v, want %#v", w, inner)
	}
}

func TestNewAnsiColor2(t *testing.T) {
	inner := bytes.NewBufferString("")
	w1 := ansicolor.NewAnsiColorWriter(inner)
	w2 := ansicolor.NewAnsiColorWriter(w1)
	if w1 != w2 {
		t.Errorf("Get %#v, want %#v", w1, w2)
	}
}
<commit_msg>Add LICENSE comment<commit_after>// Copyright 2015 shiena Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package ansicolor_test

import (
	"bytes"
	"testing"

	"github.com/shiena/ansicolor"
)

func TestNewAnsiColor1(t *testing.T) {
	inner := bytes.NewBufferString("")
	w := ansicolor.NewAnsiColorWriter(inner)
	if w == inner {
		t.Errorf("Get %#v, want %#v", w, inner)
	}
}

func TestNewAnsiColor2(t *testing.T) {
	inner := bytes.NewBufferString("")
	w1 := ansicolor.NewAnsiColorWriter(inner)
	w2 := ansicolor.NewAnsiColorWriter(w1)
	if w1 != w2 {
		t.Errorf("Get %#v, want %#v", w1, w2)
	}
}
<|endoftext|>


<commit_before>//go:generate bitfanDoc
package file

import (
	"fmt"
	"os"
	"path/filepath"
	"sync"
	"time"

	"github.com/ShowMax/go-fqdn"
	zglob "github.com/mattn/go-zglob"
	"github.com/vjeantet/bitfan/codecs"
	"github.com/vjeantet/bitfan/processors"
)

func New() processors.Processor {
	return &processor{opt: &options{}}
}

type options struct {
	// If this filter is successful, add any arbitrary fields to this event.
	Add_field map[string]interface{}

	// If this filter is successful, add arbitrary tags to the event. Tags can be dynamic
	// and include parts of the event using the %{field} syntax.
	Tags []string

	// Add a type field to all events handled by this input
	Type string

	// The codec used for input data. Input codecs are a convenient method for decoding
	// your data before it enters the input, without needing a separate filter in your bitfan pipeline
	// @Default "plain"
	// @Type Codec
	Codec codecs.Codec `mapstructure:"codec"`

	// How many seconds a file should stay unmodified to be read
	// use this to prevent reading a file while another process is writing into.
	ReadOlder int `mapstructure:"read_older"`

	// How often (in seconds) we expand the filename patterns in the path option
	// to discover new files to watch. Default value is 15
	DiscoverInterval int `mapstructure:"discover_interval"`

	// Exclusions (matched against the filename, not full path).
	// Filename patterns are valid here, too.
	Exclude []string `mapstructure:"exclude"`

	// When the file input discovers a file that was last modified before the
	// specified timespan in seconds, the file is ignored.
	// After it’s discovery, if an ignored file is modified it is no longer ignored
	// and any new data is read.
	// Default value is 86400 (i.e. 24 hours)
	IgnoreOlder int `mapstructure:"ignore_older"`

	// What is the maximum number of file_handles that this input consumes at any one time.
	// Use close_older to close some files if you need to process more files than this number.
	MaxOpenFiles int `mapstructure:"max_open_files"`

	// The path(s) to the file(s) to use as an input.
	// You can use filename patterns here, such as /var/log/*.log.
	// If you use a pattern like /var/log/**/*.log, a recursive search of /var/log
	// will be done for all *.log files.
	// Paths must be absolute and cannot be relative.
	// You may also configure multiple paths.
	Path []string `mapstructure:"path" validate:"required"`

	// Path of the sincedb database file
	// The sincedb database keeps track of the current position of monitored
	// log files that will be written to disk.
	SincedbPath string `mapstructure:"sincedb_path"`
}

type processor struct {
	processors.Base

	opt  *options
	q    chan bool
	q2   chan bool
	host string

	filestoWatch chan string

	sinceDBInfos        *sinceDBInfo
	sinceDBLastSaveTime time.Time
	sinceDBInfosMutex   *sync.Mutex
}

func (p *processor) Configure(ctx processors.ProcessorContext, conf map[string]interface{}) error {
	defaults := options{
		MaxOpenFiles:     5,
		DiscoverInterval: 15,
		ReadOlder:        5,
		SincedbPath:      ".sincedb-readfile.json",
		Codec:            codecs.New("plain", nil, ctx.Log(), ctx.ConfigWorkingLocation()),
	}

	p.opt = &defaults
	p.host = fqdn.Get()

	var err error
	err = p.ConfigureAndValidate(ctx, conf, p.opt)

	if false == filepath.IsAbs(p.opt.SincedbPath) {
		p.opt.SincedbPath = filepath.Join(p.DataLocation, p.opt.SincedbPath)
	}
	p.Logger.Debugf("sincedb=", p.opt.SincedbPath)

	// Fix relative paths
	fixedPaths := []string{}
	for _, path := range p.opt.Path {
		if !filepath.IsAbs(path) {
			path = filepath.Join(p.ConfigWorkingLocation, path)
		}
		fixedPaths = append(fixedPaths, path)
	}
	p.opt.Path = fixedPaths

	return err

}

func (p *processor) MaxConcurent() int { return 1 }

func (p *processor) Start(e processors.IPacket) error {

	p.loadSinceDBInfos()
	p.q2 = make(chan bool)
	p.q = make(chan bool)

	p.filestoWatch = make(chan string, p.opt.MaxOpenFiles)
	p.Logger.Debug("Start discovering file looper -> towatch ")
	// Start discovering file looper -> towatch
	if p.opt.DiscoverInterval > 0 {
		go func() {
			err := p.discoverFilesToRead()
			if err != nil {
				p.Logger.Debugf("discover files to read : %s", err)
			}
		}()
	}

	p.Logger.Debug("Start file reader <- towatch")
	// Start file reader <- towatch
	go func() {

		for {
			select {
			case <-p.q2:
				close(p.q)
				return
			case filepath := <-p.filestoWatch:
				p.Logger.Debugf("found file %s", filepath)
				p.readfile(filepath)
			}
		}

	}()

	return nil
}

func (p *processor) Receive(e processors.IPacket) error {
	// read files
	var matches []string

	// find files
	p.Logger.Debugln("p.opt.Path = ", p.opt.Path)
	for _, currentPath := range p.opt.Path {
		if currentMatches, err := zglob.Glob(currentPath); err == nil {
			// if currentMatches, err := filepath.Glob(currentPath); err == nil {
			matches = append(matches, currentMatches...)
			continue
		}
		return fmt.Errorf("glob(%q) failed", currentPath)
	}
	p.Logger.Debugln("files = ", matches)

	// ignore excluded
	if len(p.opt.Exclude) > 0 {
		var matches_tmp []string
		for _, pattern := range p.opt.Exclude {
			for _, name := range matches {
				if match, _ := filepath.Match(pattern, name); match == false {
					matches_tmp = append(matches_tmp, name)
				}
			}
		}
		matches = matches_tmp
	}

	// ignore already seen files
	var matches_tmp []string
	for _, name := range matches {
		if !p.sinceDBInfos.has(name) {
			matches_tmp = append(matches_tmp, name)
		}
	}
	matches = matches_tmp

	matches_tmp = []string{}
	for _, name := range matches {
		info, err := os.Stat(name)
		if err != nil {
			p.Logger.Warnf("Error while stating " + name)
			break
		}
		duration := time.Since(info.ModTime()).Seconds()
		// ignore modified to soon
		if duration > float64(p.opt.ReadOlder) {
			// ignore  too old file
			if p.opt.IgnoreOlder > 0 && duration < float64(p.opt.IgnoreOlder) {
			} else {
				matches_tmp = append(matches_tmp, name)
			}
		}
	}
	matches = matches_tmp
	// send to watchChan
	for _, name := range matches {
		p.readfile(name)
	}

	return nil
}

func (p *processor) discoverFilesToRead() error {
	for {
		var matches []string

		// find files
		for _, currentPath := range p.opt.Path {
			if currentMatches, err := zglob.Glob(currentPath); err == nil {
				// if currentMatches, err := filepath.Glob(currentPath); err == nil {
				matches = append(matches, currentMatches...)
				continue
			}
			return fmt.Errorf("glob(%q) failed", currentPath)
		}

		// ignore excluded
		if len(p.opt.Exclude) > 0 {
			var matches_tmp []string
			for _, pattern := range p.opt.Exclude {
				for _, name := range matches {
					if match, _ := filepath.Match(pattern, name); match == false {
						matches_tmp = append(matches_tmp, name)
					}
				}
			}
			matches = matches_tmp
		}

		// ignore already seen files
		var matches_tmp []string
		for _, name := range matches {
			if !p.sinceDBInfos.has(name) {
				matches_tmp = append(matches_tmp, name)
			}
		}
		matches = matches_tmp

		matches_tmp = []string{}
		for _, name := range matches {
			info, err := os.Stat(name)
			if err != nil {
				p.Logger.Warnf("Error while stating " + name)
				break
			}
			duration := time.Since(info.ModTime()).Seconds()
			// ignore modified to soon
			if duration > float64(p.opt.ReadOlder) {
				// ignore  too old file
				if p.opt.IgnoreOlder > 0 && duration < float64(p.opt.IgnoreOlder) {
				} else {
					matches_tmp = append(matches_tmp, name)
				}
			}
		}
		matches = matches_tmp

		// send to watchChan
		for _, name := range matches {
			p.filestoWatch <- name
		}

		select {
		case <-time.NewTicker(time.Second * time.Duration(p.opt.DiscoverInterval)).C:
			p.saveSinceDBInfos()
			continue
		case <-p.q2:
			return nil
		}
	}
	return nil
}

func (p *processor) readfile(pathfile string) error {

	p.Logger.Debug("reading " + pathfile)

	// Create a reader
	f, err := os.Open(pathfile)
	if err != nil {
		p.Logger.Errorf("Error while opening %s : %s", pathfile, err)
		return err
	}
	defer f.Close()

	var dec codecs.Decoder

	if dec, err = p.opt.Codec.NewDecoder(f); err != nil {
		p.Logger.Errorln("decoder error : ", err.Error())
		return err
	}

	for dec.More() {

		var record interface{}
		if err := dec.Decode(&record); err != nil {
			return err
			break
		} else {
			var e processors.IPacket
			switch v := record.(type) {
			case string:
				e = p.NewPacket(v, map[string]interface{}{
					"host":     p.host,
					"basename": filepath.Base(pathfile),
					"path":     pathfile,
				})
			case map[string]interface{}:
				e = p.NewPacket("", v)
				e.Fields().SetValueForPath(p.host, "host")
				e.Fields().SetValueForPath(filepath.Base(pathfile), "basename")
				e.Fields().SetValueForPath(pathfile, "path")
			case []interface{}:
				e = p.NewPacket("", map[string]interface{}{
					"host":     p.host,
					"basename": filepath.Base(pathfile),
					"path":     pathfile,
					"data":     v,
				})
			default:
				p.Logger.Errorf("Unknow structure %#v", v)
			}

			processors.ProcessCommonFields(e.Fields(), p.opt.Add_field, p.opt.Tags, p.opt.Type)
			p.Send(e)
		}

		select {
		case <-p.q2:
			return nil // file will not be marked as read :(
		default:
		}
	}

	// mark file read on sincedb
	p.markFileReaded(pathfile)
	return nil
}

func (p *processor) Stop(e processors.IPacket) error {
	close(p.q2)
	<-p.q
	p.saveSinceDBInfos()
	return nil
}
<commit_msg>processor : file - fix a logging message<commit_after>//go:generate bitfanDoc
package file

import (
	"fmt"
	"os"
	"path/filepath"
	"sync"
	"time"

	"github.com/ShowMax/go-fqdn"
	zglob "github.com/mattn/go-zglob"
	"github.com/vjeantet/bitfan/codecs"
	"github.com/vjeantet/bitfan/processors"
)

func New() processors.Processor {
	return &processor{opt: &options{}}
}

type options struct {
	// If this filter is successful, add any arbitrary fields to this event.
	Add_field map[string]interface{}

	// If this filter is successful, add arbitrary tags to the event. Tags can be dynamic
	// and include parts of the event using the %{field} syntax.
	Tags []string

	// Add a type field to all events handled by this input
	Type string

	// The codec used for input data. Input codecs are a convenient method for decoding
	// your data before it enters the input, without needing a separate filter in your bitfan pipeline
	// @Default "plain"
	// @Type Codec
	Codec codecs.Codec `mapstructure:"codec"`

	// How many seconds a file should stay unmodified to be read
	// use this to prevent reading a file while another process is writing into.
	ReadOlder int `mapstructure:"read_older"`

	// How often (in seconds) we expand the filename patterns in the path option
	// to discover new files to watch. Default value is 15
	DiscoverInterval int `mapstructure:"discover_interval"`

	// Exclusions (matched against the filename, not full path).
	// Filename patterns are valid here, too.
	Exclude []string `mapstructure:"exclude"`

	// When the file input discovers a file that was last modified before the
	// specified timespan in seconds, the file is ignored.
	// After it’s discovery, if an ignored file is modified it is no longer ignored
	// and any new data is read.
	// Default value is 86400 (i.e. 24 hours)
	IgnoreOlder int `mapstructure:"ignore_older"`

	// What is the maximum number of file_handles that this input consumes at any one time.
	// Use close_older to close some files if you need to process more files than this number.
	MaxOpenFiles int `mapstructure:"max_open_files"`

	// The path(s) to the file(s) to use as an input.
	// You can use filename patterns here, such as /var/log/*.log.
	// If you use a pattern like /var/log/**/*.log, a recursive search of /var/log
	// will be done for all *.log files.
	// Paths must be absolute and cannot be relative.
	// You may also configure multiple paths.
	Path []string `mapstructure:"path" validate:"required"`

	// Path of the sincedb database file
	// The sincedb database keeps track of the current position of monitored
	// log files that will be written to disk.
	SincedbPath string `mapstructure:"sincedb_path"`
}

type processor struct {
	processors.Base

	opt  *options
	q    chan bool
	q2   chan bool
	host string

	filestoWatch chan string

	sinceDBInfos        *sinceDBInfo
	sinceDBLastSaveTime time.Time
	sinceDBInfosMutex   *sync.Mutex
}

func (p *processor) Configure(ctx processors.ProcessorContext, conf map[string]interface{}) error {
	defaults := options{
		MaxOpenFiles:     5,
		DiscoverInterval: 15,
		ReadOlder:        5,
		SincedbPath:      ".sincedb-readfile.json",
		Codec:            codecs.New("plain", nil, ctx.Log(), ctx.ConfigWorkingLocation()),
	}

	p.opt = &defaults
	p.host = fqdn.Get()

	var err error
	err = p.ConfigureAndValidate(ctx, conf, p.opt)

	if false == filepath.IsAbs(p.opt.SincedbPath) {
		p.opt.SincedbPath = filepath.Join(p.DataLocation, p.opt.SincedbPath)
	}
	p.Logger.Debugf("sincedb=%s", p.opt.SincedbPath)

	// Fix relative paths
	fixedPaths := []string{}
	for _, path := range p.opt.Path {
		if !filepath.IsAbs(path) {
			path = filepath.Join(p.ConfigWorkingLocation, path)
		}
		fixedPaths = append(fixedPaths, path)
	}
	p.opt.Path = fixedPaths

	return err

}

func (p *processor) MaxConcurent() int { return 1 }

func (p *processor) Start(e processors.IPacket) error {

	p.loadSinceDBInfos()
	p.q2 = make(chan bool)
	p.q = make(chan bool)

	p.filestoWatch = make(chan string, p.opt.MaxOpenFiles)
	p.Logger.Debug("Start discovering file looper -> towatch ")
	// Start discovering file looper -> towatch
	if p.opt.DiscoverInterval > 0 {
		go func() {
			err := p.discoverFilesToRead()
			if err != nil {
				p.Logger.Debugf("discover files to read : %s", err)
			}
		}()
	}

	p.Logger.Debug("Start file reader <- towatch")
	// Start file reader <- towatch
	go func() {

		for {
			select {
			case <-p.q2:
				close(p.q)
				return
			case filepath := <-p.filestoWatch:
				p.Logger.Debugf("found file %s", filepath)
				p.readfile(filepath)
			}
		}

	}()

	return nil
}

func (p *processor) Receive(e processors.IPacket) error {
	// read files
	var matches []string

	// find files
	p.Logger.Debugln("p.opt.Path = ", p.opt.Path)
	for _, currentPath := range p.opt.Path {
		if currentMatches, err := zglob.Glob(currentPath); err == nil {
			// if currentMatches, err := filepath.Glob(currentPath); err == nil {
			matches = append(matches, currentMatches...)
			continue
		}
		return fmt.Errorf("glob(%q) failed", currentPath)
	}
	p.Logger.Debugln("files = ", matches)

	// ignore excluded
	if len(p.opt.Exclude) > 0 {
		var matches_tmp []string
		for _, pattern := range p.opt.Exclude {
			for _, name := range matches {
				if match, _ := filepath.Match(pattern, name); match == false {
					matches_tmp = append(matches_tmp, name)
				}
			}
		}
		matches = matches_tmp
	}

	// ignore already seen files
	var matches_tmp []string
	for _, name := range matches {
		if !p.sinceDBInfos.has(name) {
			matches_tmp = append(matches_tmp, name)
		}
	}
	matches = matches_tmp

	matches_tmp = []string{}
	for _, name := range matches {
		info, err := os.Stat(name)
		if err != nil {
			p.Logger.Warnf("Error while stating " + name)
			break
		}
		duration := time.Since(info.ModTime()).Seconds()
		// ignore modified to soon
		if duration > float64(p.opt.ReadOlder) {
			// ignore  too old file
			if p.opt.IgnoreOlder > 0 && duration < float64(p.opt.IgnoreOlder) {
			} else {
				matches_tmp = append(matches_tmp, name)
			}
		}
	}
	matches = matches_tmp
	// send to watchChan
	for _, name := range matches {
		p.readfile(name)
	}

	return nil
}

func (p *processor) discoverFilesToRead() error {
	for {
		var matches []string

		// find files
		for _, currentPath := range p.opt.Path {
			if currentMatches, err := zglob.Glob(currentPath); err == nil {
				// if currentMatches, err := filepath.Glob(currentPath); err == nil {
				matches = append(matches, currentMatches...)
				continue
			}
			return fmt.Errorf("glob(%q) failed", currentPath)
		}

		// ignore excluded
		if len(p.opt.Exclude) > 0 {
			var matches_tmp []string
			for _, pattern := range p.opt.Exclude {
				for _, name := range matches {
					if match, _ := filepath.Match(pattern, name); match == false {
						matches_tmp = append(matches_tmp, name)
					}
				}
			}
			matches = matches_tmp
		}

		// ignore already seen files
		var matches_tmp []string
		for _, name := range matches {
			if !p.sinceDBInfos.has(name) {
				matches_tmp = append(matches_tmp, name)
			}
		}
		matches = matches_tmp

		matches_tmp = []string{}
		for _, name := range matches {
			info, err := os.Stat(name)
			if err != nil {
				p.Logger.Warnf("Error while stating " + name)
				break
			}
			duration := time.Since(info.ModTime()).Seconds()
			// ignore modified to soon
			if duration > float64(p.opt.ReadOlder) {
				// ignore  too old file
				if p.opt.IgnoreOlder > 0 && duration < float64(p.opt.IgnoreOlder) {
				} else {
					matches_tmp = append(matches_tmp, name)
				}
			}
		}
		matches = matches_tmp

		// send to watchChan
		for _, name := range matches {
			p.filestoWatch <- name
		}

		select {
		case <-time.NewTicker(time.Second * time.Duration(p.opt.DiscoverInterval)).C:
			p.saveSinceDBInfos()
			continue
		case <-p.q2:
			return nil
		}
	}
	return nil
}

func (p *processor) readfile(pathfile string) error {

	p.Logger.Debug("reading " + pathfile)

	// Create a reader
	f, err := os.Open(pathfile)
	if err != nil {
		p.Logger.Errorf("Error while opening %s : %s", pathfile, err)
		return err
	}
	defer f.Close()

	var dec codecs.Decoder

	if dec, err = p.opt.Codec.NewDecoder(f); err != nil {
		p.Logger.Errorln("decoder error : ", err.Error())
		return err
	}

	for dec.More() {

		var record interface{}
		if err := dec.Decode(&record); err != nil {
			return err
			break
		} else {
			var e processors.IPacket
			switch v := record.(type) {
			case string:
				e = p.NewPacket(v, map[string]interface{}{
					"host":     p.host,
					"basename": filepath.Base(pathfile),
					"path":     pathfile,
				})
			case map[string]interface{}:
				e = p.NewPacket("", v)
				e.Fields().SetValueForPath(p.host, "host")
				e.Fields().SetValueForPath(filepath.Base(pathfile), "basename")
				e.Fields().SetValueForPath(pathfile, "path")
			case []interface{}:
				e = p.NewPacket("", map[string]interface{}{
					"host":     p.host,
					"basename": filepath.Base(pathfile),
					"path":     pathfile,
					"data":     v,
				})
			default:
				p.Logger.Errorf("Unknow structure %#v", v)
			}

			processors.ProcessCommonFields(e.Fields(), p.opt.Add_field, p.opt.Tags, p.opt.Type)
			p.Send(e)
		}

		select {
		case <-p.q2:
			return nil // file will not be marked as read :(
		default:
		}
	}

	// mark file read on sincedb
	p.markFileReaded(pathfile)
	return nil
}

func (p *processor) Stop(e processors.IPacket) error {
	close(p.q2)
	<-p.q
	p.saveSinceDBInfos()
	return nil
}
<|endoftext|>


<commit_before>package irmaserver

import (
	//TODO: use redigo instead of redis-go v8?
	"context"
	"encoding/json"
	"fmt"
	"strings"
	"sync"
	"time"

	"github.com/alexandrevicenzi/go-sse"
	"github.com/go-redis/redis/v8"
	"github.com/privacybydesign/gabi"
	"github.com/privacybydesign/gabi/big"
	irma "github.com/privacybydesign/irmago"
	"github.com/privacybydesign/irmago/internal/common"
	"github.com/privacybydesign/irmago/server"

	"github.com/sirupsen/logrus"
)

type session struct {
	//TODO: check if we can get rid of this Mutex for Redis
	sync.Mutex `json:-`
	//TODO: note somewhere that state with redis will not support sse for the moment
	sse      *sse.Server
	locked   bool
	sessions sessionStore
	conf     *server.Configuration
	request  irma.SessionRequest

	sessionData
}

type sessionData struct {
	Action             irma.Action
	Token              string
	ClientToken        string
	Version            *irma.ProtocolVersion `json:",omitempty"`
	Rrequest           irma.RequestorRequest
	LegacyCompatible   bool // if the request is convertible to pre-condiscon format
	ImplicitDisclosure irma.AttributeConDisCon
	Status        server.Status
	PrevStatus    server.Status
	ResponseCache responseCache
	LastActive time.Time
	Result     *server.SessionResult
	KssProofs map[irma.SchemeManagerIdentifier]*gabi.ProofP
}

type responseCache struct {
	message       []byte
	response      []byte
	status        int
	sessionStatus server.Status
}

type sessionStore interface {
	get(token string) *session
	clientGet(token string) *session
	add(session *session)
	update(session *session)
	deleteExpired()
	stop()
}

type memorySessionStore struct {
	sync.RWMutex
	conf *server.Configuration

	requestor map[string]*session
	client    map[string]*session
}

type redisSessionStore struct {
	client *redis.Client
	conf *server.Configuration
}

const (
	maxSessionLifetime  = 5 * time.Minute // After this a session is cancelled
	tokenLookupPrefix   = "token:"
	sessionLookupPrefix = "session:"
)

var (
	minProtocolVersion = irma.NewVersion(2, 4)
	maxProtocolVersion = irma.NewVersion(2, 7)
)

func (s *memorySessionStore) get(t string) *session {
	s.RLock()
	defer s.RUnlock()
	return s.requestor[t]
}

func (s *memorySessionStore) clientGet(t string) *session {
	s.RLock()
	defer s.RUnlock()
	return s.client[t]
}

func (s *memorySessionStore) add(session *session) {
	s.Lock()
	defer s.Unlock()
	s.requestor[session.Token] = session
	s.client[session.ClientToken] = session
}

func (s *memorySessionStore) update(session *session) {
	session.onUpdate()
}

func (s *memorySessionStore) stop() {
	s.Lock()
	defer s.Unlock()
	for _, session := range s.requestor {
		if session.sse != nil {
			session.sse.CloseChannel("session/" + session.Token)
			session.sse.CloseChannel("session/" + session.ClientToken)
		}
	}
}

func (s *memorySessionStore) deleteExpired() {
	// First check which sessions have expired
	// We don't need a write lock for this yet, so postpone that for actual deleting
	s.RLock()
	expired := make([]string, 0, len(s.requestor))
	for Token, session := range s.requestor {
		session.Lock()

		timeout := maxSessionLifetime
		if session.Status == server.StatusInitialized && session.Rrequest.Base().ClientTimeout != 0 {
			timeout = time.Duration(session.Rrequest.Base().ClientTimeout) * time.Second
		}

		if session.LastActive.Add(timeout).Before(time.Now()) {
			if !session.Status.Finished() {
				s.conf.Logger.WithFields(logrus.Fields{"session": session.Token}).Infof("Session expired")
				session.markAlive()
				session.setStatus(server.StatusTimeout)
			} else {
				s.conf.Logger.WithFields(logrus.Fields{"session": session.Token}).Infof("Deleting session")
				expired = append(expired, Token)
			}
		}
		session.Unlock()
	}
	s.RUnlock()

	// Using a write lock, delete the expired sessions
	s.Lock()
	for _, Token := range expired {
		session := s.requestor[Token]
		if session.sse != nil {
			session.sse.CloseChannel("session/" + session.Token)
			session.sse.CloseChannel("session/" + session.ClientToken)
		}
		delete(s.client, session.ClientToken)
		delete(s.requestor, Token)
	}
	s.Unlock()
}

// MarshalJSON marshals a session to be used in the Redis in-memory datastore.
func (s *session) MarshalJSON() ([]byte, error) {
	return json.Marshal(*s)
}

// UnmarshalJSON unmarshals the sessionData of a session.
func (s *session) UnmarshalJSON(data []byte) error {
	var temp struct {
		Rrequest *json.RawMessage `json:",omitempty"`
		sessionData
	}

	if err := json.Unmarshal(data, &temp); err != nil {
		return err
	}

	s.sessionData = temp.sessionData

	if temp.Rrequest == nil {
		s.Rrequest = nil
		// TODO: return custom error
		fmt.Printf("temp.Rrequest == nil: %d \n", temp.Rrequest)
		return nil
	}

	// unmarshal Rrequest
	ipR := &irma.IdentityProviderRequest{}
	spR := &irma.ServiceProviderRequest{}
	sigR := &irma.SignatureRequestorRequest{}

	if err := json.Unmarshal(*temp.Rrequest, ipR); err == nil && s.Action == "issuing" {
		s.Rrequest = ipR
	} else if err = json.Unmarshal(*temp.Rrequest, spR); err == nil && s.Action == "disclosing" {
		s.Rrequest = spR
	} else if err = json.Unmarshal(*temp.Rrequest, sigR); err == nil && s.Action == "signing" {
		s.Rrequest = sigR
	} else {
		fmt.Printf("unable to unmarshal rrequest: %s \n", err)
		return err
	}
	s.request = s.Rrequest.SessionRequest()

	return nil
}

func (s *redisSessionStore) get(t string) *session {
	//TODO: input validation string?
	val, err := s.client.Get(context.TODO(),tokenLookupPrefix+t).Result()
	if err != nil {
		fmt.Printf("unable to get corresponding clientToken for token %s from redis: %s \n", t, err)
	}

	return s.clientGet(val)
}

func (s *redisSessionStore) clientGet(t string) *session {
	fmt.Println("############ redisSessionStore wants to GET")

	val, err := s.client.Get(context.TODO(),sessionLookupPrefix+t).Result()
	if err != nil {
		fmt.Printf("unable to get session data for clientToken %s from redis: %s \n", t, err)
	}

	fmt.Println("ClientToken redis GET jsonObject:", val)
	var session session
	session.conf = s.conf
	session.sessions = s
	if err := session.UnmarshalJSON([]byte(val)); err != nil {
		// return with error?
		fmt.Printf("unable to unmarshal data into the new example struct due to: %s \n", err)
	}

	return &session
}

func (s *redisSessionStore) add(session *session) {
	fmt.Println("############ redisSessionStore wants to ADD")

	sessionJSON, err := session.MarshalJSON()
	if err != nil {
		fmt.Printf("unable to marshal data to json due to: %s \n", err)
	}

	err1 := s.client.Set(context.TODO(), tokenLookupPrefix+session.sessionData.Token, session.sessionData.ClientToken, maxSessionLifetime).Err()
	err2 := s.client.Set(context.TODO(), sessionLookupPrefix+session.sessionData.ClientToken, sessionJSON, maxSessionLifetime).Err()
	fmt.Println("errors:", err, err1, err2)
	fmt.Println("session.Token, session.ClientToken")
	fmt.Println(session.sessionData.Token, session.sessionData.ClientToken)
}

func (s *redisSessionStore) update(session *session) {
	fmt.Println("############ redisSessionStore wants to UPDATE")
	s.add(session)
	//TODO: remove?
	session.onUpdate()
}

func (s *redisSessionStore) stop() {
	fmt.Println("redisSessionStore wants to stop")
}

func (s *redisSessionStore) deleteExpired() {
	fmt.Println("redisSessionStore wants to deleteExpired")
	//TODO: use redis expiration instead? explicit delete needed?
}

var one *big.Int = big.NewInt(1)

func (s *Server) newSession(action irma.Action, request irma.RequestorRequest) *session {
	token := common.NewSessionToken()
	clientToken := common.NewSessionToken()

	base := request.SessionRequest().Base()
	if s.conf.AugmentClientReturnURL && base.AugmentReturnURL && base.ClientReturnURL != "" {
		if strings.Contains(base.ClientReturnURL, "?") {
			base.ClientReturnURL += "&Token=" + token
		} else {
			base.ClientReturnURL += "?Token=" + token
		}
	}

	sd := sessionData{
		Action:      action,
		Rrequest:    request,
		LastActive:  time.Now(),
		Token:       token,
		ClientToken: clientToken,
		Status:      server.StatusInitialized,
		PrevStatus:  server.StatusInitialized,
		Result: &server.SessionResult{
			LegacySession: request.SessionRequest().Base().Legacy(),
			Token:         token,
			Type:          action,
			Status:        server.StatusInitialized,
		},
	}
	ses := &session{
		sessionData: sd,
		sessions:    s.sessions,
		sse:         s.serverSentEvents,
		conf:        s.conf,
		request:     request.SessionRequest(),
	}

	s.conf.Logger.WithFields(logrus.Fields{"session": ses.Token}).Debug("New session started")
	nonce, _ := gabi.GenerateNonce()
	base.Nonce = nonce
	base.Context = one
	s.sessions.add(ses)

	return ses
}
<commit_msg>Cleanup adding and retrieving Redis data a bit with nicer error messages.<commit_after>package irmaserver

import (
	//TODO: use redigo instead of redis-go v8?
	"context"
	"encoding/json"
	"fmt"
	"strings"
	"sync"
	"time"

	"github.com/alexandrevicenzi/go-sse"
	"github.com/go-redis/redis/v8"
	"github.com/privacybydesign/gabi"
	"github.com/privacybydesign/gabi/big"
	irma "github.com/privacybydesign/irmago"
	"github.com/privacybydesign/irmago/internal/common"
	"github.com/privacybydesign/irmago/server"

	"github.com/sirupsen/logrus"
)

type session struct {
	//TODO: check if we can get rid of this Mutex for Redis
	sync.Mutex `json:-`
	//TODO: note somewhere that state with redis will not support sse for the moment
	sse      *sse.Server
	locked   bool
	sessions sessionStore
	conf     *server.Configuration
	request  irma.SessionRequest

	sessionData
}

type sessionData struct {
	Action             irma.Action
	Token              string
	ClientToken        string
	Version            *irma.ProtocolVersion `json:",omitempty"`
	Rrequest           irma.RequestorRequest
	LegacyCompatible   bool // if the request is convertible to pre-condiscon format
	ImplicitDisclosure irma.AttributeConDisCon
	Status        server.Status
	PrevStatus    server.Status
	ResponseCache responseCache
	LastActive time.Time
	Result     *server.SessionResult
	KssProofs map[irma.SchemeManagerIdentifier]*gabi.ProofP
}

type responseCache struct {
	message       []byte
	response      []byte
	status        int
	sessionStatus server.Status
}

type sessionStore interface {
	get(token string) *session
	clientGet(token string) *session
	add(session *session)
	update(session *session)
	deleteExpired()
	stop()
}

type memorySessionStore struct {
	sync.RWMutex
	conf *server.Configuration

	requestor map[string]*session
	client    map[string]*session
}

type redisSessionStore struct {
	client *redis.Client
	conf *server.Configuration
}

const (
	maxSessionLifetime  = 5 * time.Minute // After this a session is cancelled
	tokenLookupPrefix   = "token:"
	sessionLookupPrefix = "session:"
)

var (
	minProtocolVersion = irma.NewVersion(2, 4)
	maxProtocolVersion = irma.NewVersion(2, 7)
)

func (s *memorySessionStore) get(t string) *session {
	s.RLock()
	defer s.RUnlock()
	return s.requestor[t]
}

func (s *memorySessionStore) clientGet(t string) *session {
	s.RLock()
	defer s.RUnlock()
	return s.client[t]
}

func (s *memorySessionStore) add(session *session) {
	s.Lock()
	defer s.Unlock()
	s.requestor[session.Token] = session
	s.client[session.ClientToken] = session
}

func (s *memorySessionStore) update(session *session) {
	session.onUpdate()
}

func (s *memorySessionStore) stop() {
	s.Lock()
	defer s.Unlock()
	for _, session := range s.requestor {
		if session.sse != nil {
			session.sse.CloseChannel("session/" + session.Token)
			session.sse.CloseChannel("session/" + session.ClientToken)
		}
	}
}

func (s *memorySessionStore) deleteExpired() {
	// First check which sessions have expired
	// We don't need a write lock for this yet, so postpone that for actual deleting
	s.RLock()
	expired := make([]string, 0, len(s.requestor))
	for Token, session := range s.requestor {
		session.Lock()

		timeout := maxSessionLifetime
		if session.Status == server.StatusInitialized && session.Rrequest.Base().ClientTimeout != 0 {
			timeout = time.Duration(session.Rrequest.Base().ClientTimeout) * time.Second
		}

		if session.LastActive.Add(timeout).Before(time.Now()) {
			if !session.Status.Finished() {
				s.conf.Logger.WithFields(logrus.Fields{"session": session.Token}).Infof("Session expired")
				session.markAlive()
				session.setStatus(server.StatusTimeout)
			} else {
				s.conf.Logger.WithFields(logrus.Fields{"session": session.Token}).Infof("Deleting session")
				expired = append(expired, Token)
			}
		}
		session.Unlock()
	}
	s.RUnlock()

	// Using a write lock, delete the expired sessions
	s.Lock()
	for _, Token := range expired {
		session := s.requestor[Token]
		if session.sse != nil {
			session.sse.CloseChannel("session/" + session.Token)
			session.sse.CloseChannel("session/" + session.ClientToken)
		}
		delete(s.client, session.ClientToken)
		delete(s.requestor, Token)
	}
	s.Unlock()
}

// MarshalJSON marshals a session to be used in the Redis in-memory datastore.
func (s *session) MarshalJSON() ([]byte, error) {
	return json.Marshal(*s)
}

// UnmarshalJSON unmarshals the sessionData of a session.
func (s *session) UnmarshalJSON(data []byte) error {
	var temp struct {
		Rrequest *json.RawMessage `json:",omitempty"`
		sessionData
	}

	if err := json.Unmarshal(data, &temp); err != nil {
		return err
	}

	s.sessionData = temp.sessionData

	if temp.Rrequest == nil {
		s.Rrequest = nil
		// TODO: return custom error
		fmt.Printf("temp.Rrequest == nil: %d \n", temp.Rrequest)
		return nil
	}

	// unmarshal Rrequest
	ipR := &irma.IdentityProviderRequest{}
	spR := &irma.ServiceProviderRequest{}
	sigR := &irma.SignatureRequestorRequest{}

	if err := json.Unmarshal(*temp.Rrequest, ipR); err == nil && s.Action == "issuing" {
		s.Rrequest = ipR
	} else if err = json.Unmarshal(*temp.Rrequest, spR); err == nil && s.Action == "disclosing" {
		s.Rrequest = spR
	} else if err = json.Unmarshal(*temp.Rrequest, sigR); err == nil && s.Action == "signing" {
		s.Rrequest = sigR
	} else {
		fmt.Printf("unable to unmarshal rrequest: %s \n", err)
		return err
	}
	s.request = s.Rrequest.SessionRequest()

	return nil
}

func (s *redisSessionStore) get(t string) *session {
	//TODO: input validation string?
	val, err := s.client.Get(context.TODO(),tokenLookupPrefix+t).Result()
	if err != nil {
		fmt.Printf("unable to get corresponding clientToken for token %s from redis: %s \n", t, err)
	}

	return s.clientGet(val)
}

func (s *redisSessionStore) clientGet(t string) *session {
	val, err := s.client.Get(context.TODO(),sessionLookupPrefix+t).Result()
	if err != nil {
		fmt.Printf("unable to get session data for clientToken %s from redis: %s \n", t, err)
	}

	var session session
	session.conf = s.conf
	session.sessions = s
	if err := session.UnmarshalJSON([]byte(val)); err != nil {
		//TODO: return with error? general question how to deal with Redis errors
		fmt.Printf("unable to unmarshal data into the new example struct due to: %s \n", err)
	}

	return &session
}

func (s *redisSessionStore) add(session *session) {
	sessionJSON, err := session.MarshalJSON()
	if err != nil {
		fmt.Printf("unable to marshal data to json due to: %s \n", err)
	}

	err = s.client.Set(context.TODO(), tokenLookupPrefix+session.sessionData.Token, session.sessionData.ClientToken, maxSessionLifetime).Err()
	if err != nil {
		fmt.Printf("unable to save token lookup in Redis datastore: %s \n", err)
	}
	err = s.client.Set(context.TODO(), sessionLookupPrefix+session.sessionData.ClientToken, sessionJSON, maxSessionLifetime).Err()
	if err != nil {
		fmt.Printf("unable to save session data as JSON in Redis datastore: %s \n", err)
	}
}

func (s *redisSessionStore) update(session *session) {
	fmt.Println("############ redisSessionStore wants to UPDATE")
	s.add(session)
	//TODO: remove?
	session.onUpdate()
}

func (s *redisSessionStore) stop() {
	fmt.Println("redisSessionStore wants to stop")
}

func (s *redisSessionStore) deleteExpired() {
	fmt.Println("redisSessionStore wants to deleteExpired")
	//TODO: use redis expiration instead? explicit delete needed?
}

var one *big.Int = big.NewInt(1)

func (s *Server) newSession(action irma.Action, request irma.RequestorRequest) *session {
	token := common.NewSessionToken()
	clientToken := common.NewSessionToken()

	base := request.SessionRequest().Base()
	if s.conf.AugmentClientReturnURL && base.AugmentReturnURL && base.ClientReturnURL != "" {
		if strings.Contains(base.ClientReturnURL, "?") {
			base.ClientReturnURL += "&Token=" + token
		} else {
			base.ClientReturnURL += "?Token=" + token
		}
	}

	sd := sessionData{
		Action:      action,
		Rrequest:    request,
		LastActive:  time.Now(),
		Token:       token,
		ClientToken: clientToken,
		Status:      server.StatusInitialized,
		PrevStatus:  server.StatusInitialized,
		Result: &server.SessionResult{
			LegacySession: request.SessionRequest().Base().Legacy(),
			Token:         token,
			Type:          action,
			Status:        server.StatusInitialized,
		},
	}
	ses := &session{
		sessionData: sd,
		sessions:    s.sessions,
		sse:         s.serverSentEvents,
		conf:        s.conf,
		request:     request.SessionRequest(),
	}

	s.conf.Logger.WithFields(logrus.Fields{"session": ses.Token}).Debug("New session started")
	nonce, _ := gabi.GenerateNonce()
	base.Nonce = nonce
	base.Context = one
	s.sessions.add(ses)

	return ses
}
<|endoftext|>


<commit_before>package ethchain

import (
	"fmt"
	"github.com/ethereum/eth-go/ethutil"
	"math/big"
	"strconv"
	"time"
)

type BlockInfo struct {
	Number uint64
	Hash   []byte
	Parent []byte
}

func (bi *BlockInfo) RlpDecode(data []byte) {
	decoder := ethutil.NewValueFromBytes(data)

	bi.Number = decoder.Get(0).Uint()
	bi.Hash = decoder.Get(1).Bytes()
	bi.Parent = decoder.Get(2).Bytes()
}

func (bi *BlockInfo) RlpEncode() []byte {
	return ethutil.Encode([]interface{}{bi.Number, bi.Hash, bi.Parent})
}

type Block struct {
	// Hash to the previous block
	PrevHash []byte
	// Uncles of this block
	Uncles   []*Block
	UncleSha []byte
	// The coin base address
	Coinbase []byte
	// Block Trie state
	//state *ethutil.Trie
	state *State
	// Difficulty for the current block
	Difficulty *big.Int
	// Creation time
	Time int64
	// The block number
	Number *big.Int
	// Minimum Gas Price
	MinGasPrice *big.Int
	// Gas limit
	GasLimit *big.Int
	// Gas used
	GasUsed *big.Int
	// Extra data
	Extra string
	// Block Nonce for verification
	Nonce []byte
	// List of transactions and/or contracts
	transactions []*Transaction
	TxSha        []byte

	contractStates map[string]*ethutil.Trie
}

// New block takes a raw encoded string
// XXX DEPRICATED
func NewBlockFromData(raw []byte) *Block {
	return NewBlockFromBytes(raw)
}

func NewBlockFromBytes(raw []byte) *Block {
	block := &Block{}
	block.RlpDecode(raw)

	return block
}

// New block takes a raw encoded string
func NewBlockFromRlpValue(rlpValue *ethutil.Value) *Block {
	block := &Block{}
	block.RlpValueDecode(rlpValue)

	return block
}

func CreateBlock(root interface{},
	prevHash []byte,
	base []byte,
	Difficulty *big.Int,
	Nonce []byte,
	extra string,
	txes []*Transaction) *Block {

	block := &Block{
		// Slice of transactions to include in this block
		transactions:   txes,
		PrevHash:       prevHash,
		Coinbase:       base,
		Difficulty:     Difficulty,
		Nonce:          Nonce,
		Time:           time.Now().Unix(),
		Extra:          extra,
		UncleSha:       EmptyShaList,
		GasUsed:        new(big.Int),
		MinGasPrice:    new(big.Int),
		GasLimit:       new(big.Int),
		contractStates: make(map[string]*ethutil.Trie),
	}
	block.SetTransactions(txes)
	block.SetUncles([]*Block{})

	block.state = NewState(ethutil.NewTrie(ethutil.Config.Db, root))

	for _, tx := range txes {
		block.MakeContract(tx)
	}

	return block
}

// Returns a hash of the block
func (block *Block) Hash() []byte {
	return ethutil.Sha3Bin(block.Value().Encode())
}

func (block *Block) HashNoNonce() []byte {
	return ethutil.Sha3Bin(ethutil.Encode([]interface{}{block.PrevHash, block.UncleSha, block.Coinbase, block.state.trie.Root, block.TxSha, block.Difficulty, block.Time, block.Extra}))
}

func (block *Block) State() *State {
	return block.state
}

func (block *Block) Transactions() []*Transaction {
	return block.transactions
}

func (block *Block) PayFee(addr []byte, fee *big.Int) bool {
	contract := block.state.GetStateObject(addr)
	// If we can't pay the fee return
	if contract == nil || contract.Amount.Cmp(fee) < 0 /* amount < fee */ {
		fmt.Println("Contract has insufficient funds", contract.Amount, fee)

		return false
	}

	base := new(big.Int)
	contract.Amount = base.Sub(contract.Amount, fee)
	block.state.trie.Update(string(addr), string(contract.RlpEncode()))

	data := block.state.trie.Get(string(block.Coinbase))

	// Get the ether (Coinbase) and add the fee (gief fee to miner)
	account := NewStateObjectFromBytes(block.Coinbase, []byte(data))

	base = new(big.Int)
	account.Amount = base.Add(account.Amount, fee)

	//block.state.trie.Update(string(block.Coinbase), string(ether.RlpEncode()))
	block.state.UpdateStateObject(account)

	return true
}

func (block *Block) BlockInfo() BlockInfo {
	bi := BlockInfo{}
	data, _ := ethutil.Config.Db.Get(append(block.Hash(), []byte("Info")...))
	bi.RlpDecode(data)

	return bi
}

// Sync the block's state and contract respectively
func (block *Block) Sync() {
	block.state.Sync()
}

func (block *Block) Undo() {
	// Sync the block state itself
	block.state.Reset()
}

func (block *Block) MakeContract(tx *Transaction) {
	contract := MakeContract(tx, block.state)
	if contract != nil {
		block.state.states[string(tx.Hash()[12:])] = contract.state
	}
}

/////// Block Encoding
func (block *Block) rlpTxs() interface{} {
	// Marshal the transactions of this block
	encTx := make([]interface{}, len(block.transactions))
	for i, tx := range block.transactions {
		// Cast it to a string (safe)
		encTx[i] = tx.RlpData()
	}

	return encTx
}

func (block *Block) rlpUncles() interface{} {
	// Marshal the transactions of this block
	uncles := make([]interface{}, len(block.Uncles))
	for i, uncle := range block.Uncles {
		// Cast it to a string (safe)
		uncles[i] = uncle.header()
	}

	return uncles
}

func (block *Block) SetUncles(uncles []*Block) {
	block.Uncles = uncles

	// Sha of the concatenated uncles
	block.UncleSha = ethutil.Sha3Bin(ethutil.Encode(block.rlpUncles()))
}

func (block *Block) SetTransactions(txs []*Transaction) {
	block.transactions = txs

	trie := ethutil.NewTrie(ethutil.Config.Db, "")
	for i, tx := range txs {
		trie.Update(strconv.Itoa(i), string(tx.RlpEncode()))
	}

	switch trie.Root.(type) {
	case string:
		block.TxSha = []byte(trie.Root.(string))
	case []byte:
		block.TxSha = trie.Root.([]byte)
	default:
		panic(fmt.Sprintf("invalid root type %T", trie.Root))
	}

}

func (block *Block) Value() *ethutil.Value {
	return ethutil.NewValue([]interface{}{block.header(), block.rlpTxs(), block.rlpUncles()})
}

func (block *Block) RlpEncode() []byte {
	// Encode a slice interface which contains the header and the list of
	// transactions.
	return block.Value().Encode()
}

func (block *Block) RlpDecode(data []byte) {
	rlpValue := ethutil.NewValueFromBytes(data)
	block.RlpValueDecode(rlpValue)
}

func (block *Block) RlpValueDecode(decoder *ethutil.Value) {
	header := decoder.Get(0)

	block.PrevHash = header.Get(0).Bytes()
	block.UncleSha = header.Get(1).Bytes()
	block.Coinbase = header.Get(2).Bytes()
	block.state = NewState(ethutil.NewTrie(ethutil.Config.Db, header.Get(3).Val))
	block.TxSha = header.Get(4).Bytes()
	block.Difficulty = header.Get(5).BigInt()
	block.Number = header.Get(6).BigInt()
	block.MinGasPrice = header.Get(7).BigInt()
	block.GasLimit = header.Get(8).BigInt()
	block.GasUsed = header.Get(9).BigInt()
	block.Time = int64(header.Get(10).BigInt().Uint64())
	block.Extra = header.Get(11).Str()
	block.Nonce = header.Get(12).Bytes()
	block.contractStates = make(map[string]*ethutil.Trie)

	// Tx list might be empty if this is an uncle. Uncles only have their
	// header set.
	if decoder.Get(1).IsNil() == false { // Yes explicitness
		txes := decoder.Get(1)
		block.transactions = make([]*Transaction, txes.Len())
		for i := 0; i < txes.Len(); i++ {
			tx := NewTransactionFromValue(txes.Get(i))

			block.transactions[i] = tx
		}

	}

	if decoder.Get(2).IsNil() == false { // Yes explicitness
		uncles := decoder.Get(2)
		block.Uncles = make([]*Block, uncles.Len())
		for i := 0; i < uncles.Len(); i++ {
			block.Uncles[i] = NewUncleBlockFromValue(uncles.Get(i))
		}
	}

}

func NewUncleBlockFromValue(header *ethutil.Value) *Block {
	block := &Block{}

	block.PrevHash = header.Get(0).Bytes()
	block.UncleSha = header.Get(1).Bytes()
	block.Coinbase = header.Get(2).Bytes()
	block.state = NewState(ethutil.NewTrie(ethutil.Config.Db, header.Get(3).Val))
	block.TxSha = header.Get(4).Bytes()
	block.Difficulty = header.Get(5).BigInt()
	block.Number = header.Get(6).BigInt()
	block.MinGasPrice = header.Get(7).BigInt()
	block.GasLimit = header.Get(8).BigInt()
	block.GasUsed = header.Get(9).BigInt()
	block.Time = int64(header.Get(10).BigInt().Uint64())
	block.Extra = header.Get(11).Str()
	block.Nonce = header.Get(12).Bytes()

	return block
}

func (block *Block) GetRoot() interface{} {
	return block.state.trie.Root
}

func (block *Block) header() []interface{} {
	return []interface{}{
		// Sha of the previous block
		block.PrevHash,
		// Sha of uncles
		block.UncleSha,
		// Coinbase address
		block.Coinbase,
		// root state
		block.state.trie.Root,
		// Sha of tx
		block.TxSha,
		// Current block Difficulty
		block.Difficulty,
		// The block number
		block.Number,
		// Block minimum gas price
		block.MinGasPrice,
		// Block upper gas bound
		block.GasLimit,
		// Block gas used
		block.GasUsed,
		// Time the block was found?
		block.Time,
		// Extra data
		block.Extra,
		// Block's Nonce for validation
		block.Nonce,
	}
}

func (block *Block) String() string {
	return fmt.Sprintf(`
	BLOCK(%x):
	PrevHash:   %x
	UncleSha:   %x
	Coinbase:   %x
	Root:       %x
	TxSha:      %x
	Difficulty: %v
	Number:     %v
	MinGas:     %v
	MaxLimit:   %v
	GasUsed:    %v
	Time:       %v
	Extra:      %v
	Nonce:      %x
`,
		block.Hash(),
		block.PrevHash,
		block.UncleSha,
		block.Coinbase,
		block.state.trie.Root,
		block.TxSha,
		block.Difficulty,
		block.Number,
		block.MinGasPrice,
		block.GasLimit,
		block.GasUsed,
		block.Time,
		block.Extra,
		block.Nonce)
}
<commit_msg>Removed old contract creation code<commit_after>package ethchain

import (
	"fmt"
	"github.com/ethereum/eth-go/ethutil"
	"math/big"
	"strconv"
	"time"
)

type BlockInfo struct {
	Number uint64
	Hash   []byte
	Parent []byte
}

func (bi *BlockInfo) RlpDecode(data []byte) {
	decoder := ethutil.NewValueFromBytes(data)

	bi.Number = decoder.Get(0).Uint()
	bi.Hash = decoder.Get(1).Bytes()
	bi.Parent = decoder.Get(2).Bytes()
}

func (bi *BlockInfo) RlpEncode() []byte {
	return ethutil.Encode([]interface{}{bi.Number, bi.Hash, bi.Parent})
}

type Block struct {
	// Hash to the previous block
	PrevHash []byte
	// Uncles of this block
	Uncles   []*Block
	UncleSha []byte
	// The coin base address
	Coinbase []byte
	// Block Trie state
	//state *ethutil.Trie
	state *State
	// Difficulty for the current block
	Difficulty *big.Int
	// Creation time
	Time int64
	// The block number
	Number *big.Int
	// Minimum Gas Price
	MinGasPrice *big.Int
	// Gas limit
	GasLimit *big.Int
	// Gas used
	GasUsed *big.Int
	// Extra data
	Extra string
	// Block Nonce for verification
	Nonce []byte
	// List of transactions and/or contracts
	transactions []*Transaction
	TxSha        []byte
}

// New block takes a raw encoded string
// XXX DEPRICATED
func NewBlockFromData(raw []byte) *Block {
	return NewBlockFromBytes(raw)
}

func NewBlockFromBytes(raw []byte) *Block {
	block := &Block{}
	block.RlpDecode(raw)

	return block
}

// New block takes a raw encoded string
func NewBlockFromRlpValue(rlpValue *ethutil.Value) *Block {
	block := &Block{}
	block.RlpValueDecode(rlpValue)

	return block
}

func CreateBlock(root interface{},
	prevHash []byte,
	base []byte,
	Difficulty *big.Int,
	Nonce []byte,
	extra string,
	txes []*Transaction) *Block {

	block := &Block{
		// Slice of transactions to include in this block
		transactions: txes,
		PrevHash:     prevHash,
		Coinbase:     base,
		Difficulty:   Difficulty,
		Nonce:        Nonce,
		Time:         time.Now().Unix(),
		Extra:        extra,
		UncleSha:     EmptyShaList,
		GasUsed:      new(big.Int),
		MinGasPrice:  new(big.Int),
		GasLimit:     new(big.Int),
	}
	block.SetTransactions(txes)
	block.SetUncles([]*Block{})

	block.state = NewState(ethutil.NewTrie(ethutil.Config.Db, root))

	return block
}

// Returns a hash of the block
func (block *Block) Hash() []byte {
	return ethutil.Sha3Bin(block.Value().Encode())
}

func (block *Block) HashNoNonce() []byte {
	return ethutil.Sha3Bin(ethutil.Encode([]interface{}{block.PrevHash, block.UncleSha, block.Coinbase, block.state.trie.Root, block.TxSha, block.Difficulty, block.Time, block.Extra}))
}

func (block *Block) State() *State {
	return block.state
}

func (block *Block) Transactions() []*Transaction {
	return block.transactions
}

func (block *Block) PayFee(addr []byte, fee *big.Int) bool {
	contract := block.state.GetStateObject(addr)
	// If we can't pay the fee return
	if contract == nil || contract.Amount.Cmp(fee) < 0 /* amount < fee */ {
		fmt.Println("Contract has insufficient funds", contract.Amount, fee)

		return false
	}

	base := new(big.Int)
	contract.Amount = base.Sub(contract.Amount, fee)
	block.state.trie.Update(string(addr), string(contract.RlpEncode()))

	data := block.state.trie.Get(string(block.Coinbase))

	// Get the ether (Coinbase) and add the fee (gief fee to miner)
	account := NewStateObjectFromBytes(block.Coinbase, []byte(data))

	base = new(big.Int)
	account.Amount = base.Add(account.Amount, fee)

	//block.state.trie.Update(string(block.Coinbase), string(ether.RlpEncode()))
	block.state.UpdateStateObject(account)

	return true
}

func (block *Block) BlockInfo() BlockInfo {
	bi := BlockInfo{}
	data, _ := ethutil.Config.Db.Get(append(block.Hash(), []byte("Info")...))
	bi.RlpDecode(data)

	return bi
}

// Sync the block's state and contract respectively
func (block *Block) Sync() {
	block.state.Sync()
}

func (block *Block) Undo() {
	// Sync the block state itself
	block.state.Reset()
}

/////// Block Encoding
func (block *Block) rlpTxs() interface{} {
	// Marshal the transactions of this block
	encTx := make([]interface{}, len(block.transactions))
	for i, tx := range block.transactions {
		// Cast it to a string (safe)
		encTx[i] = tx.RlpData()
	}

	return encTx
}

func (block *Block) rlpUncles() interface{} {
	// Marshal the transactions of this block
	uncles := make([]interface{}, len(block.Uncles))
	for i, uncle := range block.Uncles {
		// Cast it to a string (safe)
		uncles[i] = uncle.header()
	}

	return uncles
}

func (block *Block) SetUncles(uncles []*Block) {
	block.Uncles = uncles

	// Sha of the concatenated uncles
	block.UncleSha = ethutil.Sha3Bin(ethutil.Encode(block.rlpUncles()))
}

func (block *Block) SetTransactions(txs []*Transaction) {
	block.transactions = txs

	trie := ethutil.NewTrie(ethutil.Config.Db, "")
	for i, tx := range txs {
		trie.Update(strconv.Itoa(i), string(tx.RlpEncode()))
	}

	switch trie.Root.(type) {
	case string:
		block.TxSha = []byte(trie.Root.(string))
	case []byte:
		block.TxSha = trie.Root.([]byte)
	default:
		panic(fmt.Sprintf("invalid root type %T", trie.Root))
	}

}

func (block *Block) Value() *ethutil.Value {
	return ethutil.NewValue([]interface{}{block.header(), block.rlpTxs(), block.rlpUncles()})
}

func (block *Block) RlpEncode() []byte {
	// Encode a slice interface which contains the header and the list of
	// transactions.
	return block.Value().Encode()
}

func (block *Block) RlpDecode(data []byte) {
	rlpValue := ethutil.NewValueFromBytes(data)
	block.RlpValueDecode(rlpValue)
}

func (block *Block) RlpValueDecode(decoder *ethutil.Value) {
	header := decoder.Get(0)

	block.PrevHash = header.Get(0).Bytes()
	block.UncleSha = header.Get(1).Bytes()
	block.Coinbase = header.Get(2).Bytes()
	block.state = NewState(ethutil.NewTrie(ethutil.Config.Db, header.Get(3).Val))
	block.TxSha = header.Get(4).Bytes()
	block.Difficulty = header.Get(5).BigInt()
	block.Number = header.Get(6).BigInt()
	block.MinGasPrice = header.Get(7).BigInt()
	block.GasLimit = header.Get(8).BigInt()
	block.GasUsed = header.Get(9).BigInt()
	block.Time = int64(header.Get(10).BigInt().Uint64())
	block.Extra = header.Get(11).Str()
	block.Nonce = header.Get(12).Bytes()

	// Tx list might be empty if this is an uncle. Uncles only have their
	// header set.
	if decoder.Get(1).IsNil() == false { // Yes explicitness
		txes := decoder.Get(1)
		block.transactions = make([]*Transaction, txes.Len())
		for i := 0; i < txes.Len(); i++ {
			tx := NewTransactionFromValue(txes.Get(i))

			block.transactions[i] = tx
		}

	}

	if decoder.Get(2).IsNil() == false { // Yes explicitness
		uncles := decoder.Get(2)
		block.Uncles = make([]*Block, uncles.Len())
		for i := 0; i < uncles.Len(); i++ {
			block.Uncles[i] = NewUncleBlockFromValue(uncles.Get(i))
		}
	}

}

func NewUncleBlockFromValue(header *ethutil.Value) *Block {
	block := &Block{}

	block.PrevHash = header.Get(0).Bytes()
	block.UncleSha = header.Get(1).Bytes()
	block.Coinbase = header.Get(2).Bytes()
	block.state = NewState(ethutil.NewTrie(ethutil.Config.Db, header.Get(3).Val))
	block.TxSha = header.Get(4).Bytes()
	block.Difficulty = header.Get(5).BigInt()
	block.Number = header.Get(6).BigInt()
	block.MinGasPrice = header.Get(7).BigInt()
	block.GasLimit = header.Get(8).BigInt()
	block.GasUsed = header.Get(9).BigInt()
	block.Time = int64(header.Get(10).BigInt().Uint64())
	block.Extra = header.Get(11).Str()
	block.Nonce = header.Get(12).Bytes()

	return block
}

func (block *Block) GetRoot() interface{} {
	return block.state.trie.Root
}

func (block *Block) header() []interface{} {
	return []interface{}{
		// Sha of the previous block
		block.PrevHash,
		// Sha of uncles
		block.UncleSha,
		// Coinbase address
		block.Coinbase,
		// root state
		block.state.trie.Root,
		// Sha of tx
		block.TxSha,
		// Current block Difficulty
		block.Difficulty,
		// The block number
		block.Number,
		// Block minimum gas price
		block.MinGasPrice,
		// Block upper gas bound
		block.GasLimit,
		// Block gas used
		block.GasUsed,
		// Time the block was found?
		block.Time,
		// Extra data
		block.Extra,
		// Block's Nonce for validation
		block.Nonce,
	}
}

func (block *Block) String() string {
	return fmt.Sprintf(`
	BLOCK(%x):
	PrevHash:   %x
	UncleSha:   %x
	Coinbase:   %x
	Root:       %x
	TxSha:      %x
	Difficulty: %v
	Number:     %v
	MinGas:     %v
	MaxLimit:   %v
	GasUsed:    %v
	Time:       %v
	Extra:      %v
	Nonce:      %x
`,
		block.Hash(),
		block.PrevHash,
		block.UncleSha,
		block.Coinbase,
		block.state.trie.Root,
		block.TxSha,
		block.Difficulty,
		block.Number,
		block.MinGasPrice,
		block.GasLimit,
		block.GasUsed,
		block.Time,
		block.Extra,
		block.Nonce)
}
<|endoftext|>


<commit_before>package ethutil

import (
	"fmt"
	"math/big"
	"os"
	"os/user"
	"path"
	"path/filepath"
	"runtime"
	"time"

	"github.com/kardianos/osext"
)

func DefaultAssetPath() string {
	var assetPath string
	// If the current working directory is the go-ethereum dir
	// assume a debug build and use the source directory as
	// asset directory.
	pwd, _ := os.Getwd()
	if pwd == path.Join(os.Getenv("GOPATH"), "src", "github.com", "ethereum", "go-ethereum", "cmd", "mist") {
		assetPath = path.Join(pwd, "assets")
	} else {
		switch runtime.GOOS {
		case "darwin":
			// Get Binary Directory
			exedir, _ := osext.ExecutableFolder()
			assetPath = filepath.Join(exedir, "../Resources")
		case "linux":
			assetPath = "/usr/share/mist"
		case "windows":
			assetPath = "./assets"
		default:
			assetPath = "."
		}
	}
	return assetPath
}

func DefaultDataDir() string {
	usr, _ := user.Current()
	if runtime.GOOS == "darwin" {
		return path.Join(usr.HomeDir, "Library/Ethereum")
	} else if runtime.GOOS == "windows" {
		return path.Join(usr.HomeDir, "AppData/Roaming/Ethereum")
	} else {
		return path.Join(usr.HomeDir, ".ethereum")
	}
}
func IsWindows() bool {
	return runtime.GOOS == "windows"
}

func WindonizePath(path string) string {
	if string(path[0]) == "/" && IsWindows() {
		path = path[1:]
	}
	return path
}

// The different number of units
var (
	Douglas  = BigPow(10, 42)
	Einstein = BigPow(10, 21)
	Ether    = BigPow(10, 18)
	Finney   = BigPow(10, 15)
	Szabo    = BigPow(10, 12)
	Shannon  = BigPow(10, 9)
	Babbage  = BigPow(10, 6)
	Ada      = BigPow(10, 3)
	Wei      = big.NewInt(1)
)

//
// Currency to string
// Returns a string representing a human readable format
func CurrencyToString(num *big.Int) string {
	var (
		fin   *big.Int = num
		denom string   = "Wei"
	)

	switch {
	case num.Cmp(Douglas) >= 0:
		fin = new(big.Int).Div(num, Douglas)
		denom = "Douglas"
	case num.Cmp(Einstein) >= 0:
		fin = new(big.Int).Div(num, Einstein)
		denom = "Einstein"
	case num.Cmp(Ether) >= 0:
		fin = new(big.Int).Div(num, Ether)
		denom = "Ether"
	case num.Cmp(Finney) >= 0:
		fin = new(big.Int).Div(num, Finney)
		denom = "Finney"
	case num.Cmp(Szabo) >= 0:
		fin = new(big.Int).Div(num, Szabo)
		denom = "Szabo"
	case num.Cmp(Shannon) >= 0:
		fin = new(big.Int).Div(num, Shannon)
		denom = "Shannon"
	case num.Cmp(Babbage) >= 0:
		fin = new(big.Int).Div(num, Babbage)
		denom = "Babbage"
	case num.Cmp(Ada) >= 0:
		fin = new(big.Int).Div(num, Ada)
		denom = "Ada"
	}

	// TODO add comment clarifying expected behavior
	if len(fin.String()) > 5 {
		return fmt.Sprintf("%sE%d %s", fin.String()[0:5], len(fin.String())-5, denom)
	}

	return fmt.Sprintf("%v %s", fin, denom)
}

// Common big integers often used
var (
	Big1     = big.NewInt(1)
	Big2     = big.NewInt(2)
	Big3     = big.NewInt(3)
	Big0     = big.NewInt(0)
	BigTrue  = Big1
	BigFalse = Big0
	Big32    = big.NewInt(32)
	Big256   = big.NewInt(0xff)
	Big257   = big.NewInt(257)
)

func Bench(pre string, cb func()) {
	start := time.Now()
	cb()
	fmt.Println(pre, ": took:", time.Since(start))
}
<commit_msg>Check source directroy for assets as last resort<commit_after>package ethutil

import (
	"fmt"
	"math/big"
	"os"
	"os/user"
	"path"
	"path/filepath"
	"runtime"
	"time"

	"github.com/kardianos/osext"
)

func DefaultAssetPath() string {
	var assetPath string
	pwd, _ := os.Getwd()
	srcdir := path.Join(os.Getenv("GOPATH"), "src", "github.com", "ethereum", "go-ethereum", "cmd", "mist")

	// If the current working directory is the go-ethereum dir
	// assume a debug build and use the source directory as
	// asset directory.
	if pwd == srcdir {
		assetPath = path.Join(pwd, "assets")
	} else {
		switch runtime.GOOS {
		case "darwin":
			// Get Binary Directory
			exedir, _ := osext.ExecutableFolder()
			assetPath = filepath.Join(exedir, "../Resources")
		case "linux":
			assetPath = "/usr/share/mist"
		case "windows":
			assetPath = "./assets"
		default:
			assetPath = "."
		}

		// Check if the assetPath exists. If not, try the source directory
		// This happens when binary is run from outside cmd/mist directory
		if _, err := os.Stat(assetPath); os.IsNotExist(err) {
			assetPath = path.Join(srcdir, "assets")
		}
	}
	return assetPath
}

func DefaultDataDir() string {
	usr, _ := user.Current()
	if runtime.GOOS == "darwin" {
		return path.Join(usr.HomeDir, "Library/Ethereum")
	} else if runtime.GOOS == "windows" {
		return path.Join(usr.HomeDir, "AppData/Roaming/Ethereum")
	} else {
		return path.Join(usr.HomeDir, ".ethereum")
	}
}
func IsWindows() bool {
	return runtime.GOOS == "windows"
}

func WindonizePath(path string) string {
	if string(path[0]) == "/" && IsWindows() {
		path = path[1:]
	}
	return path
}

// The different number of units
var (
	Douglas  = BigPow(10, 42)
	Einstein = BigPow(10, 21)
	Ether    = BigPow(10, 18)
	Finney   = BigPow(10, 15)
	Szabo    = BigPow(10, 12)
	Shannon  = BigPow(10, 9)
	Babbage  = BigPow(10, 6)
	Ada      = BigPow(10, 3)
	Wei      = big.NewInt(1)
)

//
// Currency to string
// Returns a string representing a human readable format
func CurrencyToString(num *big.Int) string {
	var (
		fin   *big.Int = num
		denom string   = "Wei"
	)

	switch {
	case num.Cmp(Douglas) >= 0:
		fin = new(big.Int).Div(num, Douglas)
		denom = "Douglas"
	case num.Cmp(Einstein) >= 0:
		fin = new(big.Int).Div(num, Einstein)
		denom = "Einstein"
	case num.Cmp(Ether) >= 0:
		fin = new(big.Int).Div(num, Ether)
		denom = "Ether"
	case num.Cmp(Finney) >= 0:
		fin = new(big.Int).Div(num, Finney)
		denom = "Finney"
	case num.Cmp(Szabo) >= 0:
		fin = new(big.Int).Div(num, Szabo)
		denom = "Szabo"
	case num.Cmp(Shannon) >= 0:
		fin = new(big.Int).Div(num, Shannon)
		denom = "Shannon"
	case num.Cmp(Babbage) >= 0:
		fin = new(big.Int).Div(num, Babbage)
		denom = "Babbage"
	case num.Cmp(Ada) >= 0:
		fin = new(big.Int).Div(num, Ada)
		denom = "Ada"
	}

	// TODO add comment clarifying expected behavior
	if len(fin.String()) > 5 {
		return fmt.Sprintf("%sE%d %s", fin.String()[0:5], len(fin.String())-5, denom)
	}

	return fmt.Sprintf("%v %s", fin, denom)
}

// Common big integers often used
var (
	Big1     = big.NewInt(1)
	Big2     = big.NewInt(2)
	Big3     = big.NewInt(3)
	Big0     = big.NewInt(0)
	BigTrue  = Big1
	BigFalse = Big0
	Big32    = big.NewInt(32)
	Big256   = big.NewInt(0xff)
	Big257   = big.NewInt(257)
)

func Bench(pre string, cb func()) {
	start := time.Now()
	cb()
	fmt.Println(pre, ": took:", time.Since(start))
}
<|endoftext|>


<commit_before>package service_test

import (
	"encoding/json"
	"os"
	"testing"

	"github.com/cloudfoundry-incubator/cf-test-helpers/services/context_setup"

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

type redisTestConfig struct {
	context_setup.IntegrationConfig

	ServiceName string   `json:"service_name"`
	PlanNames   []string `json:"plan_names"`
}

func loadConfig() (testConfig redisTestConfig) {
	path := os.Getenv("CONFIG_PATH")
	configFile, err := os.Open(path)
	if err != nil {
		panic(err)
	}

	decoder := json.NewDecoder(configFile)
	err = decoder.Decode(&testConfig)
	if err != nil {
		panic(err)
	}

	return testConfig
}

var config = loadConfig()

func TestService(t *testing.T) {
	context_setup.TimeoutScale = 1
	context_setup.SetupEnvironment(context_setup.NewContext(config.IntegrationConfig, "redis"))
	RegisterFailHandler(Fail)
	RunSpecs(t, "P-Redis Smoke Tests")
}
<commit_msg>more explicit test context prefix<commit_after>package service_test

import (
	"encoding/json"
	"os"
	"testing"

	"github.com/cloudfoundry-incubator/cf-test-helpers/services/context_setup"

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

type redisTestConfig struct {
	context_setup.IntegrationConfig

	ServiceName string   `json:"service_name"`
	PlanNames   []string `json:"plan_names"`
}

func loadConfig() (testConfig redisTestConfig) {
	path := os.Getenv("CONFIG_PATH")
	configFile, err := os.Open(path)
	if err != nil {
		panic(err)
	}

	decoder := json.NewDecoder(configFile)
	err = decoder.Decode(&testConfig)
	if err != nil {
		panic(err)
	}

	return testConfig
}

var config = loadConfig()

func TestService(t *testing.T) {
	context_setup.TimeoutScale = 1
	context_setup.SetupEnvironment(context_setup.NewContext(config.IntegrationConfig, "p-redis-smoke-tests"))
	RegisterFailHandler(Fail)
	RunSpecs(t, "P-Redis Smoke Tests")
}
<|endoftext|>


<commit_before>package geo

import (
	"fmt"
	"testing"

	"github.com/kevinburke/rct/tracks"
)

func TestDiffFlat(t *testing.T) {
	seg := tracks.TS_MAP[tracks.ELEM_FLAT]
	out := Diff(seg, 0)
	expected := Point{1, 0, 0}
	if *out != expected {
		t.Errorf("expected straight line to be %#v, was %#v", expected, out)
	}
}

func TestDiffCurve(t *testing.T) {
	seg := tracks.TS_MAP[tracks.ELEM_RIGHT_QUARTER_TURN_5_TILES]
	out := Diff(seg, 0)
	expected := Point{3, -3, 0}
	if *out != expected {
		t.Errorf("expected curved 5-diameter track to be %#v, was %#v", expected, out)
	}
}

func TestDiffFlatRotated(t *testing.T) {
	seg := tracks.TS_MAP[tracks.ELEM_FLAT]
	out := Diff(seg, 90)
	expected := Point{0, 1, 0}
	if *out != expected {
		t.Errorf("expected flat track to be %#v, was %#v", expected, out)
	}

	out = Diff(seg, 180)
	expected = Point{-1, 0, 0}
	if *out != expected {
		t.Errorf("expected flat track to be %#v, was %#v", expected, out)
	}

	out = Diff(seg, 270)
	expected = Point{0, -1, 0}
	if *out != expected {
		t.Errorf("expected flat track to be %#v, was %#v", expected, out)
	}
}

var advanceTests = []struct {
	seg       *tracks.Segment
	e         int
	forward   int
	sideways  int
	direction tracks.DirectionDelta
	e_out     int
	f_out     int
	s_out     int
	d_out     tracks.DirectionDelta
}{
	{tracks.TS_MAP[tracks.ELEM_FLAT], 0, 0, 0, tracks.DIR_STRAIGHT, 0, 1, 0, tracks.DIR_STRAIGHT},
	{tracks.TS_MAP[tracks.ELEM_FLAT], 0, 0, 0, tracks.DIR_90_DEG_RIGHT, 0, 0, -1, tracks.DIR_90_DEG_RIGHT},
	{tracks.TS_MAP[tracks.ELEM_FLAT], 0, 0, 0, tracks.DIR_180_DEG, 0, -1, 0, tracks.DIR_180_DEG},
	{tracks.TS_MAP[tracks.ELEM_60_DEG_UP], 0, 0, 0, tracks.DIR_STRAIGHT, 8, 1, 0, tracks.DIR_STRAIGHT},
	{tracks.TS_MAP[tracks.ELEM_LEFT_QUARTER_TURN_5_TILES], 0, 0, 0, tracks.DIR_STRAIGHT, 0, 3, 3, tracks.DIR_90_DEG_LEFT},
	{tracks.TS_MAP[tracks.ELEM_LEFT_QUARTER_TURN_5_TILES], 0, 0, 0, tracks.DIR_90_DEG_LEFT, 0, 3, -3, tracks.DIR_180_DEG},
	{tracks.TS_MAP[tracks.ELEM_RIGHT_QUARTER_TURN_5_TILES], 0, 0, 0, tracks.DIR_STRAIGHT, 0, 3, -3, tracks.DIR_90_DEG_RIGHT},
}

func TestAdvance(t *testing.T) {
	t.Skip("These tests don't work for turns...")
	for _, tt := range advanceTests {
		eout, fout, sout, dout := Advance(tt.seg, tt.e, tt.forward, tt.sideways, tt.direction)
		header := fmt.Sprintf("%s (init %d, %d, %d, %s):", tt.seg, tt.e, tt.forward, tt.sideways, tt.direction)
		if eout != tt.e_out {
			t.Errorf("%s expected %d elevation change, got %d", header, tt.e_out, eout)
		}
		if fout != tt.f_out {
			t.Errorf("%s expected %d forward delta, got %d", header, tt.f_out, fout)
		}
		if sout != tt.s_out {
			t.Errorf("%s expected %d sideways delta, got %d", header, tt.s_out, sout)
		}
		if dout != tt.d_out {
			t.Errorf("%s expected to go %s, got %d", header, tt.d_out, dout)
		}
	}
}

func TestFlat90DegRotated(t *testing.T) {
	v := Vector{Point{3, 7, 0}, tracks.DIR_90_DEG_RIGHT}
	seg := tracks.TS_MAP[tracks.ELEM_FLAT]
	out := AdvanceVector(v, seg)
	expected := Point{3, 8, 0}
	if out.Point != expected {
		t.Errorf("expected upward facing track to advance to %#v, got %#v", expected, out.Point)
	}
}

func TestFlat180DegRotated(t *testing.T) {
	v := Vector{Point{3, 7, 0}, tracks.DIR_180_DEG}
	seg := tracks.TS_MAP[tracks.ELEM_FLAT]
	out := AdvanceVector(v, seg)
	expected := Point{2, 7, 0}
	if out.Point != expected {
		t.Errorf("expected upward facing track to advance to %#v, got %#v", expected, out.Point)
	}
}

func TestFlat270DegRotated(t *testing.T) {
	v := Vector{Point{3, 7, 0}, tracks.DIR_90_DEG_LEFT}
	seg := tracks.TS_MAP[tracks.ELEM_FLAT]
	out := AdvanceVector(v, seg)
	expected := Point{3, 6, 0}
	if out.Point != expected {
		t.Errorf("expected upward facing track to advance to %#v, got %#v", expected, out.Point)
	}
}
<commit_msg>add advance vector tests<commit_after>package geo

import (
	"fmt"
	"testing"

	"github.com/kevinburke/rct/tracks"
)

func TestDiffFlat(t *testing.T) {
	seg := tracks.TS_MAP[tracks.ELEM_FLAT]
	out := Diff(seg, 0)
	expected := Point{1, 0, 0}
	if *out != expected {
		t.Errorf("expected straight line to be %#v, was %#v", expected, out)
	}
}

func TestDiffCurve(t *testing.T) {
	seg := tracks.TS_MAP[tracks.ELEM_RIGHT_QUARTER_TURN_5_TILES]
	out := Diff(seg, 0)
	expected := Point{3, -3, 0}
	if *out != expected {
		t.Errorf("expected curved 5-diameter track to be %#v, was %#v", expected, out)
	}
}

func TestDiffFlatRotated(t *testing.T) {
	seg := tracks.TS_MAP[tracks.ELEM_FLAT]
	out := Diff(seg, 90)
	expected := Point{0, 1, 0}
	if *out != expected {
		t.Errorf("expected flat track to be %#v, was %#v", expected, out)
	}

	out = Diff(seg, 180)
	expected = Point{-1, 0, 0}
	if *out != expected {
		t.Errorf("expected flat track to be %#v, was %#v", expected, out)
	}

	out = Diff(seg, 270)
	expected = Point{0, -1, 0}
	if *out != expected {
		t.Errorf("expected flat track to be %#v, was %#v", expected, out)
	}
}

var advanceTests = []struct {
	seg       *tracks.Segment
	e         int
	forward   int
	sideways  int
	direction tracks.DirectionDelta
	e_out     int
	f_out     int
	s_out     int
	d_out     tracks.DirectionDelta
}{
	{tracks.TS_MAP[tracks.ELEM_FLAT], 0, 0, 0, tracks.DIR_STRAIGHT, 0, 1, 0, tracks.DIR_STRAIGHT},
	{tracks.TS_MAP[tracks.ELEM_FLAT], 0, 0, 0, tracks.DIR_90_DEG_RIGHT, 0, 0, -1, tracks.DIR_90_DEG_RIGHT},
	{tracks.TS_MAP[tracks.ELEM_FLAT], 0, 0, 0, tracks.DIR_180_DEG, 0, -1, 0, tracks.DIR_180_DEG},
	{tracks.TS_MAP[tracks.ELEM_60_DEG_UP], 0, 0, 0, tracks.DIR_STRAIGHT, 8, 1, 0, tracks.DIR_STRAIGHT},
	{tracks.TS_MAP[tracks.ELEM_LEFT_QUARTER_TURN_5_TILES], 0, 0, 0, tracks.DIR_STRAIGHT, 0, 3, 3, tracks.DIR_90_DEG_LEFT},
	{tracks.TS_MAP[tracks.ELEM_LEFT_QUARTER_TURN_5_TILES], 0, 0, 0, tracks.DIR_90_DEG_LEFT, 0, 3, -3, tracks.DIR_180_DEG},
	{tracks.TS_MAP[tracks.ELEM_RIGHT_QUARTER_TURN_5_TILES], 0, 0, 0, tracks.DIR_STRAIGHT, 0, 3, -3, tracks.DIR_90_DEG_RIGHT},
}

func TestAdvance(t *testing.T) {
	t.Skip("These tests don't work for turns...")
	for _, tt := range advanceTests {
		eout, fout, sout, dout := Advance(tt.seg, tt.e, tt.forward, tt.sideways, tt.direction)
		header := fmt.Sprintf("%s (init %d, %d, %d, %s):", tt.seg, tt.e, tt.forward, tt.sideways, tt.direction)
		if eout != tt.e_out {
			t.Errorf("%s expected %d elevation change, got %d", header, tt.e_out, eout)
		}
		if fout != tt.f_out {
			t.Errorf("%s expected %d forward delta, got %d", header, tt.f_out, fout)
		}
		if sout != tt.s_out {
			t.Errorf("%s expected %d sideways delta, got %d", header, tt.s_out, sout)
		}
		if dout != tt.d_out {
			t.Errorf("%s expected to go %s, got %d", header, tt.d_out, dout)
		}
	}
}

var advanceVectorTests = []struct {
	v   Vector
	seg *tracks.Segment
	out Vector
}{
	{Vector{Point{0, 0, 0}, tracks.DIR_STRAIGHT}, tracks.TS_MAP[tracks.ELEM_FLAT], Vector{Point{1, 0, 0}, tracks.DIR_STRAIGHT}},
}

func TestAdvanceVector(t *testing.T) {
	for _, tt := range advanceVectorTests {
		out := AdvanceVector(tt.v, tt.seg)
		helper := fmt.Sprintf("%s (%f, %f, %f):", tt.seg, tt.v.Point[0], tt.v.Point[1], tt.v.Point[2])
		if out.Point != tt.out.Point {
			t.Errorf("%s expected point to be %v, was %v", helper, tt.out.Point, out.Point)
		}
		if out.Dir != tt.out.Dir {
			t.Errorf("%s expected direction to be %v, was %v", helper, tt.out.Dir, out.Dir)
		}
	}
}

func TestFlat90DegRotated(t *testing.T) {
	v := Vector{Point{3, 7, 0}, tracks.DIR_90_DEG_RIGHT}
	seg := tracks.TS_MAP[tracks.ELEM_FLAT]
	out := AdvanceVector(v, seg)
	expected := Point{3, 8, 0}
	if out.Point != expected {
		t.Errorf("expected upward facing track to advance to %#v, got %#v", expected, out.Point)
	}
}

func TestFlat180DegRotated(t *testing.T) {
	v := Vector{Point{3, 7, 0}, tracks.DIR_180_DEG}
	seg := tracks.TS_MAP[tracks.ELEM_FLAT]
	out := AdvanceVector(v, seg)
	expected := Point{2, 7, 0}
	if out.Point != expected {
		t.Errorf("expected upward facing track to advance to %#v, got %#v", expected, out.Point)
	}
}

func TestFlat270DegRotated(t *testing.T) {
	v := Vector{Point{3, 7, 0}, tracks.DIR_90_DEG_LEFT}
	seg := tracks.TS_MAP[tracks.ELEM_FLAT]
	out := AdvanceVector(v, seg)
	expected := Point{3, 6, 0}
	if out.Point != expected {
		t.Errorf("expected upward facing track to advance to %#v, got %#v", expected, out.Point)
	}
}
<|endoftext|>


<commit_before>package main

import (
  "fmt"
  "net/http"
  "os"
  "io/ioutil"
)

func main(){
  var url string
  fmt.Println(">> url: (Make sure to use http://)")
  fmt.Scanf("%s", &url)
  if url == "" {
    fmt.Println("Please type in an url")
  }
  data, err := http.Get(url)
  if err != nil{
    os.Exit(1)
  }else{
    defer data.Body.Close()
    contents, err := ioutil.ReadAll(data.Body)
    if err != nil{
      fmt.Println("%s\n", err)
    }
    fmt.Println("%s\n", string(contents))
  }
}
<commit_msg>Exited the program if user didnt type in an url<commit_after>package main

import (
  "fmt"
  "net/http"
  "os"
  "io/ioutil"
)

func main(){
  var url string
  fmt.Println(">> url: (Make sure to use http://)")
  fmt.Scanf("%s", &url)
  if url == "" {
    fmt.Println("Please type in an url")
    os.Exit(1)
  }
  data, err := http.Get(url)
  if err != nil{
    os.Exit(1)
  }else{
    defer data.Body.Close()
    contents, err := ioutil.ReadAll(data.Body)
    if err != nil{
      fmt.Println("%s\n", err)
    }
    fmt.Println("%s\n", string(contents))
  }
}
<|endoftext|>