<commit_before>package main

import (
	"fmt"
	"os"

	"github.com/venicegeo/pzsvc-exec/pzse"
	"github.com/venicegeo/pzsvc-exec/pzsvc"
	"github.com/venicegeo/pzsvc-exec/worker/config"
	"github.com/venicegeo/pzsvc-exec/worker/log"
	"github.com/venicegeo/pzsvc-exec/worker/workerexec"

	cli "gopkg.in/urfave/cli.v1"
)

var cliApp *cli.App

func init() {
	cliApp = cli.NewApp()
	cliApp.Name = "pzsvc-worker"
	cliApp.Usage = "run a one-off piazza job in its own process"
	cliApp.Action = runCmd

	cliApp.Flags = []cli.Flag{
		cli.StringFlag{Name: "config", Usage: "JSON pzsvc-exec configuration file (required)"},
		cli.StringFlag{Name: "cliExtra", Usage: "supplemental command arguments to run the Piazza job"},
		cli.StringFlag{Name: "piazzaBaseURL", Usage: "base URL for querying Piazza API (required if not PZ_ADDR)"},
		cli.StringFlag{Name: "piazzaAPIKey", Usage: "API key for use for communicating with Piazza (required if not in vcap)"},
		cli.StringFlag{Name: "userID", Usage: "key authentication string (required)"},
		cli.StringFlag{Name: "serviceID", Usage: "piazza service ID (algorithm name) (required)"},
		cli.StringFlag{Name: "jobID", Usage: "job ID for this run, used for logging"},
		cli.StringSliceFlag{Name: "input, i", Usage: "input source specification (as \"filename:URL\")"},
		cli.StringSliceFlag{Name: "output, o", Usage: "output file name (usable multiple times; at least one required)"},
	}
}

func runCmd(ctx *cli.Context) error {
	cfg := config.WorkerConfig{
		Session:         &pzsvc.Session{AppName: "pzsvc-worker", SessionID: "startup", LogRootDir: "pzsvc-exec"},
		CLICommandExtra: ctx.String("cliExtra"),
		PiazzaBaseURL:   ctx.String("piazzaBaseURL"),
		PiazzaAPIKey:    ctx.String("piazzaAPIKey"),
		PiazzaServiceID: ctx.String("serviceID"),
		UserID:          ctx.String("userID"),
		JobID:           ctx.String("jobID"),
		Inputs:          []config.InputSource{},
		Outputs:         ctx.StringSlice("output"),
		PzSEConfig:      pzse.ConfigType{},
	}
	workerlog.Info(cfg, "startup")

	if ctx.String("config") == "" {
		return cli.NewExitError("pzsvc-exec config file is required", 1)
	}
	if err := cfg.ReadPzSEConfig(ctx.String("config")); err != nil {
		return cli.NewExitError(err, 1)
	}

	if cfg.PiazzaServiceID == "" {
		return cli.NewExitError("Service ID is required", 1)
	}

	if cfg.PiazzaBaseURL == "" {
		cfg.PiazzaBaseURL = os.Getenv(cfg.PzSEConfig.PzAddrEnVar)
	}
	if cfg.PiazzaBaseURL == "" {
		return cli.NewExitError("Piazza base URL is required", 1)
	}
	cfg.Session.PzAddr = cfg.PiazzaBaseURL

	if cfg.PiazzaAPIKey == "" {
		cfg.PiazzaAPIKey = os.Getenv((cfg.PzSEConfig.APIKeyEnVar))
	}
	if cfg.PiazzaAPIKey == "" {
		return cli.NewExitError("Piazza API key is required", 1)
	}
	if len(cfg.Outputs) == 0 {
		return cli.NewExitError("1 or more output files are required", 1)
	}

	for _, sourceString := range ctx.StringSlice("input") {
		inFile, err := config.ParseInputSource(sourceString)
		if err != nil {
			return cli.NewExitError(err, 1)
		}
		cfg.Inputs = append(cfg.Inputs, *inFile)
	}

	workerlog.Info(cfg, fmt.Sprintf("config validated: %s", cfg.Serialize()))

	workerlog.Info(cfg, "Starting actual worker execution")
	err := workerexec.WorkerExec(cfg)
	if err != nil {
		workerlog.SimpleErr(cfg, "execution error, quitting with status 1", err)
		return cli.NewExitError(err, 1)
	}

	workerlog.Info(cfg, "worker done, exiting")
	return nil
}

func main() {
	cliApp.Run(os.Args)
}
<commit_msg>Set the API key in the legacy session<commit_after>package main

import (
	"fmt"
	"os"

	"github.com/venicegeo/pzsvc-exec/pzse"
	"github.com/venicegeo/pzsvc-exec/pzsvc"
	"github.com/venicegeo/pzsvc-exec/worker/config"
	"github.com/venicegeo/pzsvc-exec/worker/log"
	"github.com/venicegeo/pzsvc-exec/worker/workerexec"

	cli "gopkg.in/urfave/cli.v1"
)

var cliApp *cli.App

func init() {
	cliApp = cli.NewApp()
	cliApp.Name = "pzsvc-worker"
	cliApp.Usage = "run a one-off piazza job in its own process"
	cliApp.Action = runCmd

	cliApp.Flags = []cli.Flag{
		cli.StringFlag{Name: "config", Usage: "JSON pzsvc-exec configuration file (required)"},
		cli.StringFlag{Name: "cliExtra", Usage: "supplemental command arguments to run the Piazza job"},
		cli.StringFlag{Name: "piazzaBaseURL", Usage: "base URL for querying Piazza API (required if not PZ_ADDR)"},
		cli.StringFlag{Name: "piazzaAPIKey", Usage: "API key for use for communicating with Piazza (required if not in vcap)"},
		cli.StringFlag{Name: "userID", Usage: "key authentication string (required)"},
		cli.StringFlag{Name: "serviceID", Usage: "piazza service ID (algorithm name) (required)"},
		cli.StringFlag{Name: "jobID", Usage: "job ID for this run, used for logging"},
		cli.StringSliceFlag{Name: "input, i", Usage: "input source specification (as \"filename:URL\")"},
		cli.StringSliceFlag{Name: "output, o", Usage: "output file name (usable multiple times; at least one required)"},
	}
}

func runCmd(ctx *cli.Context) error {
	cfg := config.WorkerConfig{
		Session:         &pzsvc.Session{AppName: "pzsvc-worker", SessionID: "startup", LogRootDir: "pzsvc-exec"},
		CLICommandExtra: ctx.String("cliExtra"),
		PiazzaBaseURL:   ctx.String("piazzaBaseURL"),
		PiazzaAPIKey:    ctx.String("piazzaAPIKey"),
		PiazzaServiceID: ctx.String("serviceID"),
		UserID:          ctx.String("userID"),
		JobID:           ctx.String("jobID"),
		Inputs:          []config.InputSource{},
		Outputs:         ctx.StringSlice("output"),
		PzSEConfig:      pzse.ConfigType{},
	}
	workerlog.Info(cfg, "startup")

	if ctx.String("config") == "" {
		return cli.NewExitError("pzsvc-exec config file is required", 1)
	}
	if err := cfg.ReadPzSEConfig(ctx.String("config")); err != nil {
		return cli.NewExitError(err, 1)
	}

	if cfg.PiazzaServiceID == "" {
		return cli.NewExitError("Service ID is required", 1)
	}

	if cfg.PiazzaBaseURL == "" {
		cfg.PiazzaBaseURL = os.Getenv(cfg.PzSEConfig.PzAddrEnVar)
	}
	if cfg.PiazzaBaseURL == "" {
		return cli.NewExitError("Piazza base URL is required", 1)
	}
	cfg.Session.PzAddr = cfg.PiazzaBaseURL

	if cfg.PiazzaAPIKey == "" {
		cfg.PiazzaAPIKey = os.Getenv((cfg.PzSEConfig.APIKeyEnVar))
	}
	if cfg.PiazzaAPIKey == "" {
		return cli.NewExitError("Piazza API key is required", 1)
	}
	cfg.Session.PzAuth = cfg.PiazzaAPIKey

	if len(cfg.Outputs) == 0 {
		return cli.NewExitError("1 or more output files are required", 1)
	}

	for _, sourceString := range ctx.StringSlice("input") {
		inFile, err := config.ParseInputSource(sourceString)
		if err != nil {
			return cli.NewExitError(err, 1)
		}
		cfg.Inputs = append(cfg.Inputs, *inFile)
	}

	workerlog.Info(cfg, fmt.Sprintf("config validated: %s", cfg.Serialize()))

	workerlog.Info(cfg, "Starting actual worker execution")
	err := workerexec.WorkerExec(cfg)
	if err != nil {
		workerlog.SimpleErr(cfg, "execution error, quitting with status 1", err)
		return cli.NewExitError(err, 1)
	}

	workerlog.Info(cfg, "worker done, exiting")
	return nil
}

func main() {
	cliApp.Run(os.Args)
}
<|endoftext|>


<commit_before>// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

// The machine package implements the API interfaces
// used by the machine agent.
package agent

import (
	"launchpad.net/juju-core/state"
	"launchpad.net/juju-core/state/api/params"
	"launchpad.net/juju-core/state/apiserver/common"
)

// API implements the API provided to an agent.
type API struct {
	*common.PasswordChanger

	st   *state.State
	auth common.Authorizer
}

// NewAPI returns an object implementing an agent API
// with the given authorizer representing the currently logged in client.
func NewAPI(st *state.State, auth common.Authorizer) (*API, error) {
	// Agents are defined to be any user that's not a client user.
	if !auth.AuthMachineAgent() && !auth.AuthUnitAgent() {
		return nil, common.ErrPerm
	}
	getCanChange := func() (common.AuthFunc, error) {
		return auth.AuthOwner, nil
	}
	return &API{
		PasswordChanger: common.NewPasswordChanger(st, getCanChange),
		st:              st,
		auth:            auth,
	}, nil
}

func (api *API) GetEntities(args params.Entities) params.AgentGetEntitiesResults {
	results := params.AgentGetEntitiesResults{
		Entities: make([]params.AgentGetEntitiesResult, len(args.Entities)),
	}
	for i, entity := range args.Entities {
		result, err := api.getEntity(entity.Tag)
		result.Error = common.ServerError(err)
		results.Entities[i] = result
	}
	return results
}

func (api *API) getEntity(tag string) (result params.AgentGetEntitiesResult, err error) {
	// Allow only for the owner agent.
	// Note: having a bulk API call for this is utter madness, given that
	// this check means we can only ever return a single object.
	if !api.auth.AuthOwner(tag) {
		err = common.ErrPerm
		return
	}
	entity0, err := api.st.FindEntity(tag)
	if err != nil {
		return
	}
	entity, ok := entity0.(state.Lifer)
	if !ok {
		err = common.NotSupportedError(tag, "life cycles")
		return
	}
	result.Life = params.Life(entity.Life().String())
	if machine, ok := entity.(*state.Machine); ok {
		result.Jobs = stateJobsToAPIParamsJobs(machine.Jobs())
		result.ContainerType = machine.ContainerType()
	}
	return
}

func stateJobsToAPIParamsJobs(jobs []state.MachineJob) []params.MachineJob {
	pjobs := make([]params.MachineJob, len(jobs))
	for i, job := range jobs {
		pjobs[i] = params.MachineJob(job.String())
	}
	return pjobs
}
<commit_msg>New StateServingInfo api method<commit_after>// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

// The machine package implements the API interfaces
// used by the machine agent.
package agent

import (
	"launchpad.net/juju-core/state"
	"launchpad.net/juju-core/state/api/params"
	"launchpad.net/juju-core/state/apiserver/common"
)

// API implements the API provided to an agent.
type API struct {
	*common.PasswordChanger

	st   *state.State
	auth common.Authorizer
}

// NewAPI returns an object implementing an agent API
// with the given authorizer representing the currently logged in client.
func NewAPI(st *state.State, auth common.Authorizer) (*API, error) {
	// Agents are defined to be any user that's not a client user.
	if !auth.AuthMachineAgent() && !auth.AuthUnitAgent() {
		return nil, common.ErrPerm
	}
	getCanChange := func() (common.AuthFunc, error) {
		return auth.AuthOwner, nil
	}
	return &API{
		PasswordChanger: common.NewPasswordChanger(st, getCanChange),
		st:              st,
		auth:            auth,
	}, nil
}

func (api *API) GetEntities(args params.Entities) params.AgentGetEntitiesResults {
	results := params.AgentGetEntitiesResults{
		Entities: make([]params.AgentGetEntitiesResult, len(args.Entities)),
	}
	for i, entity := range args.Entities {
		result, err := api.getEntity(entity.Tag)
		result.Error = common.ServerError(err)
		results.Entities[i] = result
	}
	return results
}

func (api *API) getEntity(tag string) (result params.AgentGetEntitiesResult, err error) {
	// Allow only for the owner agent.
	// Note: having a bulk API call for this is utter madness, given that
	// this check means we can only ever return a single object.
	if !api.auth.AuthOwner(tag) {
		err = common.ErrPerm
		return
	}
	entity0, err := api.st.FindEntity(tag)
	if err != nil {
		return
	}
	entity, ok := entity0.(state.Lifer)
	if !ok {
		err = common.NotSupportedError(tag, "life cycles")
		return
	}
	result.Life = params.Life(entity.Life().String())
	if machine, ok := entity.(*state.Machine); ok {
		result.Jobs = stateJobsToAPIParamsJobs(machine.Jobs())
		result.ContainerType = machine.ContainerType()
	}
	return
}

func (api *API) StateServingInfo() (result params.StateServingInfo, err error) {
	if !api.auth.AuthEnvironManager() {
		err = common.ErrPerm
		return
	}
	return api.st.StateServingInfo()
}

func stateJobsToAPIParamsJobs(jobs []state.MachineJob) []params.MachineJob {
	pjobs := make([]params.MachineJob, len(jobs))
	for i, job := range jobs {
		pjobs[i] = params.MachineJob(job.String())
	}
	return pjobs
}
<|endoftext|>


<commit_before><commit_msg>detach disk first when it's attached to another instance<commit_after><|endoftext|>


<commit_before>package middleware

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

	respond "gopkg.in/matryer/respond.v1"
)

// Authorizer represents authorization domain service interface
type Authorizer interface {
	Authorize(context.Context, string) error
}

// WithJWTAuthMethod represents http authentication middleware
// It uses authorizer domain service to check for valid session
func WithJWTAuthMethod(j Authorizer) Adapter {
	return func(h http.Handler) http.Handler {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			ah := r.Header.Get("Authorization")
			if ah == "" {
				respond.With(w, r, http.StatusBadRequest, fmt.Errorf("no authorization header found"))
				return
			}

			s := strings.Split(ah, " ")
			if len(s) < 2 || s[1] == "" {
				respond.With(w, r, http.StatusBadRequest, fmt.Errorf("no bearer token found"))
				return
			}

			if err := j.Authorize(context.Background(), s[1]); err != nil {
				respond.With(w, r, http.StatusUnauthorized, fmt.Errorf("unauthorized: %v", err))
				return
			}

			h.ServeHTTP(w, r)
		})
	}
}
<commit_msg>Fixed wrong import<commit_after>package middleware

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

	"github.com/tonto/kit/http/respond"
)

// Authorizer represents authorization domain service interface
type Authorizer interface {
	Authorize(context.Context, string) error
}

// WithJWTAuthMethod represents http authentication middleware
// It uses authorizer domain service to check for valid session
func WithJWTAuthMethod(j Authorizer) Adapter {
	return func(h http.Handler) http.Handler {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			ah := r.Header.Get("Authorization")
			if ah == "" {
				respond.With(w, r, http.StatusBadRequest, fmt.Errorf("no authorization header found"))
				return
			}

			s := strings.Split(ah, " ")
			if len(s) < 2 || s[1] == "" {
				respond.With(w, r, http.StatusBadRequest, fmt.Errorf("no bearer token found"))
				return
			}

			if err := j.Authorize(context.Background(), s[1]); err != nil {
				respond.With(w, r, http.StatusUnauthorized, fmt.Errorf("unauthorized: %v", err))
				return
			}

			h.ServeHTTP(w, r)
		})
	}
}
<|endoftext|>


<commit_before>package gorill

// newline returns a string with exactly one terminating newline character.
// More simple than strings.TrimRight.  When input string has multiple newline
// characters, it will strip off all but first one, reusing the same underlying
// string bytes.  When string does not end in a newline character, it returns
// the original string with a newline character appended.
func newline(s string) string {
	l := len(s)
	if l == 0 {
		return "\n"
	}

	// While this is O(length s), it stops as soon as it finds the first non
	// newline character in the string starting from the right hand side of the
	// input string.  Generally this only scans one or two characters and
	// returns.

	for i := l - 1; i >= 0; i-- {
		if s[i] != '\n' {
			if i+1 < l && s[i+1] == '\n' {
				return s[:i+2]
			}
			return s[:i+1] + "\n"
		}
	}

	return s[:1] // all newline characters, so just return the first one
}
<commit_msg>comment updated<commit_after>package gorill

// newline returns a string with exactly one terminating newline character.
// More simple than strings.TrimRight.  When input string ends with multiple
// newline characters, it will strip off all but first one, reusing the same
// underlying string bytes.  When string does not end in a newline character, it
// returns the original string with a newline character appended.  Newline
// characters before any non-newline characters are ignored.
func newline(s string) string {
	l := len(s)
	if l == 0 {
		return "\n"
	}

	// While this is O(length s), it stops as soon as it finds the first non
	// newline character in the string starting from the right hand side of the
	// input string.  Generally this only scans one or two characters and
	// returns.

	for i := l - 1; i >= 0; i-- {
		if s[i] != '\n' {
			if i+1 < l && s[i+1] == '\n' {
				return s[:i+2]
			}
			return s[:i+1] + "\n"
		}
	}

	return s[:1] // all newline characters, so just return the first one
}
<|endoftext|>


<commit_before><commit_msg>skip not uploaded assets<commit_after><|endoftext|>


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

package main

import (
	"errors"
	"fmt"
	"time"

	"github.com/coreos/rkt/common"
	"github.com/coreos/rkt/pkg/lock"
	"github.com/coreos/rkt/store"
	"github.com/hashicorp/errwrap"
	"github.com/spf13/cobra"
)

const (
	defaultImageGracePeriod = 24 * time.Hour
)

var (
	cmdImageGC = &cobra.Command{
		Use:   "gc",
		Short: "Garbage collect local store",
		Long: `This is intended to be run periodically from a timer or cron job.

The default grace period is 24h. Use --grace-period=0s to effectively disable
the grace-period.`,
		Run: runWrapper(runGCImage),
	}
	flagImageGracePeriod time.Duration
)

func init() {
	cmdImage.AddCommand(cmdImageGC)
	cmdImageGC.Flags().DurationVar(&flagImageGracePeriod, "grace-period", defaultImageGracePeriod, "duration to wait since an image was last used before removing it")
}

func runGCImage(cmd *cobra.Command, args []string) (exit int) {
	s, err := store.NewStore(getDataDir())
	if err != nil {
		stderr.PrintE("cannot open store", err)
		return 1
	}

	if err := gcTreeStore(s); err != nil {
		stderr.PrintE("failed to remove unreferenced treestores", err)
		return 1
	}

	if err := gcStore(s, flagImageGracePeriod); err != nil {
		stderr.Error(err)
		return 1
	}

	return 0
}

// gcTreeStore removes all treeStoreIDs not referenced by any non garbage
// collected pod from the store.
func gcTreeStore(s *store.Store) error {
	// Take an exclusive lock to block other pods being created.
	// This is needed to avoid races between the below steps (getting the
	// list of referenced treeStoreIDs, getting the list of treeStoreIDs
	// from the store, removal of unreferenced treeStoreIDs) and new
	// pods/treeStores being created/referenced
	keyLock, err := lock.ExclusiveKeyLock(lockDir(), common.PrepareLock)
	if err != nil {
		return errwrap.Wrap(errors.New("cannot get exclusive prepare lock"), err)
	}
	defer keyLock.Close()
	referencedTreeStoreIDs, err := getReferencedTreeStoreIDs()
	if err != nil {
		return errwrap.Wrap(errors.New("cannot get referenced treestoreIDs"), err)
	}
	treeStoreIDs, err := s.GetTreeStoreIDs()
	if err != nil {
		return errwrap.Wrap(errors.New("cannot get treestoreIDs from the store"), err)
	}
	for _, treeStoreID := range treeStoreIDs {
		if _, ok := referencedTreeStoreIDs[treeStoreID]; !ok {
			if err := s.RemoveTreeStore(treeStoreID); err != nil {
				stderr.PrintE(fmt.Sprintf("error removing treestore %q", treeStoreID), err)
			} else {
				stderr.Printf("removed treestore %q", treeStoreID)
			}
		}
	}
	return nil
}

func getReferencedTreeStoreIDs() (map[string]struct{}, error) {
	treeStoreIDs := map[string]struct{}{}
	var walkErr error
	// Consider pods in preparing, prepared, run, exitedgarbage state
	if err := walkPods(includeMostDirs, func(p *pod) {
		stage1TreeStoreID, err := p.getStage1TreeStoreID()
		if err != nil {
			walkErr = errwrap.Wrap(fmt.Errorf("cannot get stage1 treestoreID for pod %s", p.uuid), err)
			return
		}
		appsTreeStoreIDs, err := p.getAppsTreeStoreIDs()
		if err != nil {
			walkErr = errwrap.Wrap(fmt.Errorf("cannot get apps treestoreIDs for pod %s", p.uuid), err)
			return
		}
		allTreeStoreIDs := append(appsTreeStoreIDs, stage1TreeStoreID)

		for _, treeStoreID := range allTreeStoreIDs {
			treeStoreIDs[treeStoreID] = struct{}{}
		}
	}); err != nil {
		return nil, errwrap.Wrap(errors.New("failed to get pod handles"), err)
	}
	if walkErr != nil {
		return nil, walkErr
	}
	return treeStoreIDs, nil
}

func gcStore(s *store.Store, gracePeriod time.Duration) error {
	var imagesToRemove []string
	aciinfos, err := s.GetAllACIInfos([]string{"lastused"}, true)
	if err != nil {
		return errwrap.Wrap(errors.New("failed to get aciinfos"), err)
	}
	for _, ai := range aciinfos {
		if time.Now().Sub(ai.LastUsed) <= gracePeriod {
			break
		}
		imagesToRemove = append(imagesToRemove, ai.BlobKey)
	}

	if err := rmImages(s, imagesToRemove); err != nil {
		return err
	}

	return nil
}
<commit_msg>rkt/image_gc: don't fail hard when unable to get some treestore IDs<commit_after>// Copyright 2015 The rkt Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
	"errors"
	"fmt"
	"time"

	"github.com/coreos/rkt/common"
	"github.com/coreos/rkt/pkg/lock"
	"github.com/coreos/rkt/store"
	"github.com/hashicorp/errwrap"
	"github.com/spf13/cobra"
)

const (
	defaultImageGracePeriod = 24 * time.Hour
)

var (
	cmdImageGC = &cobra.Command{
		Use:   "gc",
		Short: "Garbage collect local store",
		Long: `This is intended to be run periodically from a timer or cron job.

The default grace period is 24h. Use --grace-period=0s to effectively disable
the grace-period.`,
		Run: runWrapper(runGCImage),
	}
	flagImageGracePeriod time.Duration
)

func init() {
	cmdImage.AddCommand(cmdImageGC)
	cmdImageGC.Flags().DurationVar(&flagImageGracePeriod, "grace-period", defaultImageGracePeriod, "duration to wait since an image was last used before removing it")
}

func runGCImage(cmd *cobra.Command, args []string) (exit int) {
	s, err := store.NewStore(getDataDir())
	if err != nil {
		stderr.PrintE("cannot open store", err)
		return 1
	}

	if err := gcTreeStore(s); err != nil {
		stderr.PrintE("failed to remove unreferenced treestores", err)
		return 1
	}

	if err := gcStore(s, flagImageGracePeriod); err != nil {
		stderr.Error(err)
		return 1
	}

	return 0
}

// gcTreeStore removes all treeStoreIDs not referenced by any non garbage
// collected pod from the store.
func gcTreeStore(s *store.Store) error {
	// Take an exclusive lock to block other pods being created.
	// This is needed to avoid races between the below steps (getting the
	// list of referenced treeStoreIDs, getting the list of treeStoreIDs
	// from the store, removal of unreferenced treeStoreIDs) and new
	// pods/treeStores being created/referenced
	keyLock, err := lock.ExclusiveKeyLock(lockDir(), common.PrepareLock)
	if err != nil {
		return errwrap.Wrap(errors.New("cannot get exclusive prepare lock"), err)
	}
	defer keyLock.Close()
	referencedTreeStoreIDs, err := getReferencedTreeStoreIDs()
	if err != nil {
		return errwrap.Wrap(errors.New("cannot get referenced treestoreIDs"), err)
	}
	treeStoreIDs, err := s.GetTreeStoreIDs()
	if err != nil {
		return errwrap.Wrap(errors.New("cannot get treestoreIDs from the store"), err)
	}
	for _, treeStoreID := range treeStoreIDs {
		if _, ok := referencedTreeStoreIDs[treeStoreID]; !ok {
			if err := s.RemoveTreeStore(treeStoreID); err != nil {
				stderr.PrintE(fmt.Sprintf("error removing treestore %q", treeStoreID), err)
			} else {
				stderr.Printf("removed treestore %q", treeStoreID)
			}
		}
	}
	return nil
}

func getReferencedTreeStoreIDs() (map[string]struct{}, error) {
	treeStoreIDs := map[string]struct{}{}
	// Consider pods in preparing, prepared, run, exitedgarbage state
	if err := walkPods(includeMostDirs, func(p *pod) {
		stage1TreeStoreID, err := p.getStage1TreeStoreID()
		if err != nil {
			stderr.PrintE(fmt.Sprintf("cannot get stage1 treestoreID for pod %s", p.uuid), err)
			return
		}
		appsTreeStoreIDs, err := p.getAppsTreeStoreIDs()
		if err != nil {
			stderr.PrintE(fmt.Sprintf("cannot get apps treestoreID for pod %s", p.uuid), err)
			return
		}
		allTreeStoreIDs := append(appsTreeStoreIDs, stage1TreeStoreID)

		for _, treeStoreID := range allTreeStoreIDs {
			treeStoreIDs[treeStoreID] = struct{}{}
		}
	}); err != nil {
		return nil, errwrap.Wrap(errors.New("failed to get pod handles"), err)
	}
	return treeStoreIDs, nil
}

func gcStore(s *store.Store, gracePeriod time.Duration) error {
	var imagesToRemove []string
	aciinfos, err := s.GetAllACIInfos([]string{"lastused"}, true)
	if err != nil {
		return errwrap.Wrap(errors.New("failed to get aciinfos"), err)
	}
	for _, ai := range aciinfos {
		if time.Now().Sub(ai.LastUsed) <= gracePeriod {
			break
		}
		imagesToRemove = append(imagesToRemove, ai.BlobKey)
	}

	if err := rmImages(s, imagesToRemove); err != nil {
		return err
	}

	return nil
}
<|endoftext|>


<commit_before>package commandline

import "strings"

func Parse(s string) *Command {
	ss := strings.Split(s, " ")
	if ss[0] == "" {
		return nil
	}
	return &Command{
		Name: ss[0],
		Args: ss[1:],
	}
}

type Command struct {
	Name string
	Args []string
}

type scanner struct {
	src  []byte
	size int
	off  int
}

func newScanner(src []byte) *scanner {
	return &scanner{
		src:  src,
		size: len(src),
	}
}

func (s *scanner) scan() (byte, bool) {
	if s.off >= s.size {
		return 0, true
	}

	ret := s.src[s.off]

	s.off++
	return ret, false
}
	return nil
}

func isIdent(r byte) bool {
	return 'A' <= r && r <= 'Z' || 'a' <= r && r <= 'z'
}

type tokenType int

type token struct {
	tt tokenType
}

const (
	ident tokenType = iota
	str
)
<commit_msg>Add scanner.lex<commit_after>package commandline

import "strings"

func Parse(s string) *Command {
	ss := strings.Split(s, " ")
	if ss[0] == "" {
		return nil
	}
	return &Command{
		Name: ss[0],
		Args: ss[1:],
	}
}

type Command struct {
	Name string
	Args []string
}

type scanner struct {
	src  []byte
	size int
	off  int
}

func newScanner(src []byte) *scanner {
	return &scanner{
		src:  src,
		size: len(src),
	}
}

func (s *scanner) scan() (byte, bool) {
	if s.off >= s.size {
		return 0, true
	}

	ret := s.src[s.off]

	s.off++
	return ret, false
}

func (s *scanner) lex() *token {
	ch, eof := s.scan()
	if eof {
		return nil
	}
	switch {
	case isIdent(ch):
		ch, eof := s.scan()
		if eof {
			return nil
		}
		for isIdent(ch) {
			ch, eof = s.scan()
			if eof {
				break
			}
		}
		return &token{tt: ident}
	case ch == '"':
	}
	return nil
}

func isIdent(r byte) bool {
	return 'A' <= r && r <= 'Z' || 'a' <= r && r <= 'z'
}

type tokenType int

type token struct {
	tt tokenType
}

const (
	ident tokenType = iota
	str
)
<|endoftext|>


<commit_before>// Copyright (c) 2013 by Michael Dvorkin. All Rights Reserved.
// Use of this source code is governed by a MIT-style license that can
// be found in the LICENSE file.

package donna

import(`bytes`)

var tree [1024]Position
var node int

type Flags struct {
        enpassant     int       // En-passant square caused by previous move.
        irreversible  bool      // Is this position reversible?
}

type Position struct {
        game      *Game
        flags     Flags         // Flags set by last move leading to this position.
        pieces    [64]Piece     // Array of 64 squares with pieces on them.
        board     [3]Bitmask    // [0] white pieces only, [1] black pieces, and [2] all pieces.
        outposts  [14]Bitmask   // Bitmasks of each piece on the board, ex. white pawns, black king, etc.
        count     [16]int       // counts of each piece on the board, ex. white pawns: 6, etc.
        color     int           // Side to make next move.
        stage     int           // Game stage (256 in the initial position).
        hash      uint64        // Polyglot hash value.
        castles   uint8         // Castle rights mask.
}

func NewPosition(game *Game, pieces [64]Piece, color int, flags Flags) *Position {
        tree[node] = Position{ game: game, pieces: pieces, color: color }
        p := &tree[node]

        p.castles = castleKingside[White] | castleQueenside[White] |
                    castleKingside[Black] | castleQueenside[Black]

        if p.pieces[E1] != King(White) || p.pieces[H1] != Rook(White) {
                p.castles &= ^castleKingside[White]
        }
        if p.pieces[E1] != King(White) || p.pieces[A1] != Rook(White) {
                p.castles &= ^castleQueenside[White]
        }

        if p.pieces[E8] != King(Black) || p.pieces[H8] != Rook(Black) {
                p.castles &= ^castleKingside[Black]
        }
        if p.pieces[E8] != King(Black) || p.pieces[A8] != Rook(Black) {
                p.castles &= ^castleQueenside[Black]
        }

        return p.setupPieces().computeStage()
}

func (p *Position) setupPieces() *Position {
        for square, piece := range p.pieces {
                if piece != 0 {
                        p.outposts[piece].set(square)
                        p.board[piece.color()].set(square)
                        p.count[piece]++
                }
        }
        p.board[2] = p.board[White] | p.board[Black]
        return p
}

func (p *Position) updatePieces(updates [64]Piece, squares []int) *Position {
        for _, square := range squares {
		if newer, older := updates[square], p.pieces[square]; newer != older {
			if older != 0 {
				p.count[older]--
			}
			p.outposts[older].clear(square)
			p.board[newer.color()^1].clear(square)
			p.pieces[square] = newer
	                if newer != 0 {
	                        p.outposts[newer].set(square)
	                        p.board[newer.color()].set(square)
				p.count[newer]++
	                } else {
	                        p.outposts[newer].clear(square)
	                        p.board[newer.color()].clear(square)
	                }
		}
        }
        return p
}

func (p *Position) computeStage() *Position {
        p.hash  = p.polyglot()
        p.stage = 2 * (p.count[Pawn(White)]   + p.count[Pawn(Black)])   +
                  6 * (p.count[Knight(White)] + p.count[Knight(Black)]) +
                 12 * (p.count[Bishop(White)] + p.count[Bishop(Black)]) +
                 16 * (p.count[Rook(White)]   + p.count[Rook(Black)])   +
                 44 * (p.count[Queen(White)]  + p.count[Queen(Black)])
        return p
}

func (p *Position) MakeMove(move Move) *Position {
        color := move.color()
        flags := Flags{}

        from, to, piece, capture := move.split()
        flags.irreversible = capture != 0

        squares := []int{ from, to }
        delta := p.pieces
        delta[from], delta[to] = 0, piece

        // Castle rights for current node are based on the castle rights from
        // the previous node.
        castles := tree[node].castles & castleRights[from] & castleRights[to]

	node++
	tree[node] = tree[node - 1] // Faster that tree[node] = *p ?!

        if piece.isKing() {
                castles &= ^castleKingside[color]
                castles &= ^castleQueenside[color]
                if move.isCastle() {
                        flags.irreversible = true
                        squares = []int{}
                        tree[node].pieces[from], tree[node].pieces[to] = 0, piece
                        switch to {
                        case G1:
                                tree[node].outposts[piece] ^= Bit(E1) | Bit(G1)
                                tree[node].outposts[Rook(White)] ^= Bit(H1) | Bit(F1)
                                tree[node].pieces[H1], tree[node].pieces[F1] = 0, Rook(White)
                                tree[node].board[White] ^= gapKing[White] | Bit(E1) | Bit(H1)
                        case C1:
                                tree[node].outposts[piece] ^= Bit(E1) | Bit(C1)
                                tree[node].outposts[Rook(White)] ^= Bit(A1) | Bit(D1)
                                tree[node].pieces[A1], tree[node].pieces[D1] = 0, Rook(White)
                                tree[node].board[White] ^= Bit(C1) | Bit(D1) | Bit(E1) | Bit(A1)
                        case G8:
                                tree[node].outposts[piece] ^= Bit(E8) | Bit(G8)
                                tree[node].outposts[Rook(Black)] ^= Bit(H8) | Bit(F8)
                                tree[node].pieces[H8], tree[node].pieces[F8] = 0, Rook(Black)
                                tree[node].board[Black] ^= gapKing[Black] | Bit(E8) | Bit(H8)
                        case C8:
                                tree[node].outposts[piece] ^= Bit(E8) | Bit(C8)
                                tree[node].outposts[Rook(Black)] ^= Bit(A8) | Bit(D8)
                                tree[node].pieces[A8], tree[node].pieces[D8] = 0, Rook(Black)
                                tree[node].board[Black] ^= Bit(C8) | Bit(D8) | Bit(E8) | Bit(A8)
                        }
                }
        } else if piece.isPawn() {
                flags.irreversible = true
                if move.isEnpassant() {
                        flags.enpassant = from + eight[color]           // Save the en-passant square.
                } else if to == p.flags.enpassant {
                        delta[to - eight[color]] = 0                    // Take out the en-passant pawn...
                        squares = append(squares, to - eight[color])    // and decrement opponent's pawn count.
                } else if promo := move.promo(); promo != 0 {
                        delta[to] = promo                               // Place the promoted piece.
                }
        }

        if len(squares) > 0 {
	        tree[node].updatePieces(delta, squares)
        }

	tree[node].color = color^1
	tree[node].flags = flags
	tree[node].castles = castles
	tree[node].board[2] = tree[node].board[White] | tree[node].board[Black]

	if tree[node].isInCheck(color) {
		node--
		return nil
	}
	return tree[node].computeStage()

}

func (p *Position) TakeBack(move Move) *Position {
        node--
        return &tree[node]
}

func (p *Position) isInCheck(color int) bool {
        return p.isAttacked(p.outposts[King(color)].first(), color^1)
}

func (p *Position) isRepetition() bool {
        if p.flags.irreversible {
                return false
        }

        for reps, prevNode := 1, node - 1; prevNode >= 0; prevNode-- {
                if tree[prevNode].flags.irreversible {
                        return false
                }
                if tree[prevNode].color == p.color && tree[prevNode].hash == p.hash {
                        reps++
                        if reps == 3 {
                                return true
                        }
                }
        }

        return false
}

func (p *Position) saveBest(ply int, move Move) {
        p.game.bestLine[ply][ply] = move
        p.game.bestLength[ply] = ply + 1
        for i := ply + 1; i < p.game.bestLength[ply + 1]; i++ {
                p.game.bestLine[ply][i] = p.game.bestLine[ply + 1][i]
                p.game.bestLength[ply]++
        }
}

func (p *Position) canCastle(color int) (kingside, queenside bool) {
        attacks := p.attacks(color^1)
        kingside = p.castles & castleKingside[color] != 0 &&
                   (gapKing[color] & p.board[2] == 0) &&
                   (castleKing[color] & attacks == 0)

        queenside = p.castles & castleQueenside[color] != 0 &&
                    (gapQueen[color] & p.board[2] == 0) &&
                    (castleQueen[color] & attacks == 0)
        return
}

// Compute position's polyglot hash.
func (p *Position) polyglot() (key uint64) {
        board := p.board[2]
        for board != 0 {
                square := board.pop() // Inline polyhash() is at lest 10% faster.
                key ^= polyglotRandom[64 * p.pieces[square].polyglot() + square]
        }

	key ^= hashCastle[p.castles]

	if p.flags.enpassant != 0 {
                key ^= hashEnpassant[Col(p.flags.enpassant)]
	}
	if p.color == White {
                key ^= polyglotRandomWhite
	}

	return
}

func (p *Position) polyhash (square int) uint64 {
       return polyglotRandom[64 * p.pieces[square].polyglot() + square]
}

func (p *Position) String() string {
	buffer := bytes.NewBufferString("  a b c d e f g h")
        if !p.isInCheck(p.color) {
                buffer.WriteString("\n")
        } else {
                buffer.WriteString("  Check to " + C(p.color) + "\n")
        }
	for row := 7;  row >= 0;  row-- {
		buffer.WriteByte('1' + byte(row))
		for col := 0;  col <= 7; col++ {
			square := Square(row, col)
			buffer.WriteByte(' ')
			if piece := p.pieces[square]; piece != 0 {
				buffer.WriteString(piece.String())
			} else {
				buffer.WriteString("\u22C5")
			}
		}
		buffer.WriteByte('\n')
	}
	return buffer.String()
}
<commit_msg>On its way to incremental hash computation<commit_after>// Copyright (c) 2013 by Michael Dvorkin. All Rights Reserved.
// Use of this source code is governed by a MIT-style license that can
// be found in the LICENSE file.

package donna

import(`bytes`)

var tree [1024]Position
var node int

type Flags struct {
        enpassant     int       // En-passant square caused by previous move.
        irreversible  bool      // Is this position reversible?
}

type Position struct {
        game      *Game
        flags     Flags         // Flags set by last move leading to this position.
        pieces    [64]Piece     // Array of 64 squares with pieces on them.
        board     [3]Bitmask    // [0] white pieces only, [1] black pieces, and [2] all pieces.
        outposts  [14]Bitmask   // Bitmasks of each piece on the board, ex. white pawns, black king, etc.
        count     [16]int       // counts of each piece on the board, ex. white pawns: 6, etc.
        color     int           // Side to make next move.
        stage     int           // Game stage (256 in the initial position).
        hash      uint64        // Polyglot hash value.
        castles   uint8         // Castle rights mask.
}

func NewPosition(game *Game, pieces [64]Piece, color int, flags Flags) *Position {
        tree[node] = Position{ game: game, pieces: pieces, color: color }
        p := &tree[node]

        p.castles = castleKingside[White] | castleQueenside[White] |
                    castleKingside[Black] | castleQueenside[Black]

        if p.pieces[E1] != King(White) || p.pieces[H1] != Rook(White) {
                p.castles &= ^castleKingside[White]
        }
        if p.pieces[E1] != King(White) || p.pieces[A1] != Rook(White) {
                p.castles &= ^castleQueenside[White]
        }

        if p.pieces[E8] != King(Black) || p.pieces[H8] != Rook(Black) {
                p.castles &= ^castleKingside[Black]
        }
        if p.pieces[E8] != King(Black) || p.pieces[A8] != Rook(Black) {
                p.castles &= ^castleQueenside[Black]
        }

        return p.setupPieces().computeStage()
}

func (p *Position) setupPieces() *Position {
        for square, piece := range p.pieces {
                if piece != 0 {
                        p.outposts[piece].set(square)
                        p.board[piece.color()].set(square)
                        p.count[piece]++
                }
        }
        p.board[2] = p.board[White] | p.board[Black]
        return p
}

func (p *Position) updatePieces(updates [64]Piece, squares []int) *Position {
        for _, square := range squares {
		if newer, older := updates[square], p.pieces[square]; newer != older {
			if older != 0 {
				p.count[older]--
			}
			p.outposts[older].clear(square)
			p.board[newer.color()^1].clear(square)
			p.pieces[square] = newer
	                if newer != 0 {
	                        p.outposts[newer].set(square)
	                        p.board[newer.color()].set(square)
				p.count[newer]++
	                } else {
	                        p.outposts[newer].clear(square)
	                        p.board[newer.color()].clear(square)
	                }
		}
        }
        return p
}

func (p *Position) computeStage() *Position {
        p.hash  = p.polyglot()
        p.stage = 2 * (p.count[Pawn(White)]   + p.count[Pawn(Black)])   +
                  6 * (p.count[Knight(White)] + p.count[Knight(Black)]) +
                 12 * (p.count[Bishop(White)] + p.count[Bishop(Black)]) +
                 16 * (p.count[Rook(White)]   + p.count[Rook(Black)])   +
                 44 * (p.count[Queen(White)]  + p.count[Queen(Black)])
        return p
}

func (p *Position) movePiece(piece Piece, from, to int) *Position {
        mask := Bit(from) | Bit(to)
        p.outposts[piece] ^= mask
        p.board[piece.color()] ^= mask // p.board[2] ^= mask
        p.pieces[from], p.pieces[to] = 0, piece
        return p
}

func (p *Position) MakeMove(move Move) *Position {
        color := move.color()
        flags := Flags{}

        from, to, piece, capture := move.split()
        flags.irreversible = capture != 0

        squares := []int{ from, to }
        delta := p.pieces
        delta[from], delta[to] = 0, piece
        //
        // Copy over the contents of previous tree node to the current one.
        //
        node++
        tree[node] = tree[node - 1] // Faster that tree[node] = *p ?!
        //
        // Castle rights for current node are based on the castle rights from
        // the previous node.
        //
        tree[node].castles &= castleRights[from] & castleRights[to]

        if piece.isKing() {
                if move.isCastle() {
                        flags.irreversible = true
                        squares = []int{}
                        switch to {
                        case G1:
                                tree[node].movePiece(King(White), E1, G1).movePiece(Rook(White), H1, F1)
                        case C1:
                                tree[node].movePiece(King(White), E1, C1).movePiece(Rook(White), A1, D1)
                        case G8:
                                tree[node].movePiece(King(Black), E8, G8).movePiece(Rook(Black), H8, F8)
                        case C8:
                                tree[node].movePiece(King(Black), E8, C8).movePiece(Rook(Black), A8, D8)
                        }
                }
        } else if piece.isPawn() {
                flags.irreversible = true
                if move.isEnpassant() {
                        flags.enpassant = from + eight[color]           // Save the en-passant square.
                } else if to == p.flags.enpassant {
                        delta[to - eight[color]] = 0                    // Take out the en-passant pawn...
                        squares = append(squares, to - eight[color])    // and decrement opponent's pawn count.
                } else if promo := move.promo(); promo != 0 {
                        delta[to] = promo                               // Place the promoted piece.
                }
        }

        if len(squares) > 0 {
	        tree[node].updatePieces(delta, squares)
        }

	tree[node].color = color^1
	tree[node].flags = flags
	tree[node].board[2] = tree[node].board[White] | tree[node].board[Black]

	if tree[node].isInCheck(color) {
		node--
		return nil
	}
	return tree[node].computeStage()

}

func (p *Position) TakeBack(move Move) *Position {
        node--
        return &tree[node]
}

func (p *Position) isInCheck(color int) bool {
        return p.isAttacked(p.outposts[King(color)].first(), color^1)
}

func (p *Position) isRepetition() bool {
        if p.flags.irreversible {
                return false
        }

        for reps, prevNode := 1, node - 1; prevNode >= 0; prevNode-- {
                if tree[prevNode].flags.irreversible {
                        return false
                }
                if tree[prevNode].color == p.color && tree[prevNode].hash == p.hash {
                        reps++
                        if reps == 3 {
                                return true
                        }
                }
        }

        return false
}

func (p *Position) saveBest(ply int, move Move) {
        p.game.bestLine[ply][ply] = move
        p.game.bestLength[ply] = ply + 1
        for i := ply + 1; i < p.game.bestLength[ply + 1]; i++ {
                p.game.bestLine[ply][i] = p.game.bestLine[ply + 1][i]
                p.game.bestLength[ply]++
        }
}

func (p *Position) canCastle(color int) (kingside, queenside bool) {
        attacks := p.attacks(color^1)
        kingside = p.castles & castleKingside[color] != 0 &&
                   (gapKing[color] & p.board[2] == 0) &&
                   (castleKing[color] & attacks == 0)

        queenside = p.castles & castleQueenside[color] != 0 &&
                    (gapQueen[color] & p.board[2] == 0) &&
                    (castleQueen[color] & attacks == 0)
        return
}

// Compute position's polyglot hash.
func (p *Position) polyglot() (key uint64) {
        board := p.board[2]
        for board != 0 {
                square := board.pop() // Inline polyhash() is at lest 10% faster.
                key ^= polyglotRandom[64 * p.pieces[square].polyglot() + square]
        }

	key ^= hashCastle[p.castles]

	if p.flags.enpassant != 0 {
                key ^= hashEnpassant[Col(p.flags.enpassant)]
	}
	if p.color == White {
                key ^= polyglotRandomWhite
	}

	return
}

func (p *Position) polyhash (square int) uint64 {
       return polyglotRandom[64 * p.pieces[square].polyglot() + square]
}

func (p *Position) String() string {
	buffer := bytes.NewBufferString("  a b c d e f g h")
        if !p.isInCheck(p.color) {
                buffer.WriteString("\n")
        } else {
                buffer.WriteString("  Check to " + C(p.color) + "\n")
        }
	for row := 7;  row >= 0;  row-- {
		buffer.WriteByte('1' + byte(row))
		for col := 0;  col <= 7; col++ {
			square := Square(row, col)
			buffer.WriteByte(' ')
			if piece := p.pieces[square]; piece != 0 {
				buffer.WriteString(piece.String())
			} else {
				buffer.WriteString("\u22C5")
			}
		}
		buffer.WriteByte('\n')
	}
	return buffer.String()
}
<|endoftext|>


<commit_before>// Copyright (c) Ilia Kravets, 2015. All rights reserved. PROVIDED "AS IS"
// WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. See LICENSE file for details.

package pcap2log

import (
	"fmt"
	"github.com/kr/pretty"
	"io"
	"log"
)

var _ = pretty.Print

type simulator struct {
	w io.Writer
}

func NewSimulator(w io.Writer) simulator {
	return simulator{
		w: w,
	}
}

func (s *simulator) addMessage(qom *QOMessage, typeChar byte) {
	s.outMessageNorm(qom, typeChar)
}

func (s *simulator) Printf(format string, vs ...interface{}) {
	if _, err := fmt.Fprintf(s.w, format, vs...); err != nil {
		log.Fatal("output error", err)
	}
}

func (s *simulator) Printfln(format string, vs ...interface{}) {
	s.Printf(format, vs...)
	if _, err := fmt.Fprintln(s.w); err != nil {
		log.Fatal("output error", err)
	}
}

func (s *simulator) outMessageNorm(m *QOMessage, typeChar byte) {
	out := func(name, f string, vs ...interface{}) {
		s.Printf("NORM %s %c ", name, typeChar)
		s.Printfln(f, vs...)
	}
	ord, bid, ask := &m.side1, &m.side1, &m.side2
	if bid.side == MarketSideSell {
		bid, ask = ask, bid
	}
	switch m.typ {
	case MessageTypeUnknown: // ignore
	case MessageTypeQuoteAdd:
		out("QBID", "%08x %08x %08x %08x", m.optionId, bid.refNumDelta, bid.size, bid.price)
		out("QASK", "%08x %08x %08x %08x", m.optionId, ask.refNumDelta, ask.size, ask.price)
	case MessageTypeQuoteReplace:
		out("QBID", "%08x %08x %08x %08x", bid.refNumDelta, bid.origRefNumDelta, bid.size, bid.price)
		out("QASK", "%08x %08x %08x %08x", ask.refNumDelta, ask.origRefNumDelta, ask.size, ask.price)
	case MessageTypeQuoteDelete:
		out("QBID", "%08x", bid.origRefNumDelta)
		out("QASK", "%08x", ask.origRefNumDelta)
	case MessageTypeOrderAdd:
		out("ORDER", "%c %08x %08x %08x %08x", ord.side, m.optionId, ord.refNumDelta, ord.size, ord.price)
	case MessageTypeOrderExecute, MessageTypeOrderExecuteWPrice, MessageTypeOrderCancel:
		out("ORDER", "%08x %08x", ord.origRefNumDelta, ord.size)
	case MessageTypeOrderUpdate:
		out("ORDER", "%08x %08x %08x", ord.origRefNumDelta, ord.size, ord.price)
	case MessageTypeOrderReplace:
		out("ORDER", "%08x %08x %08x %08x", ord.refNumDelta, ord.origRefNumDelta, ord.size, ord.price)
	case MessageTypeOrderDelete:
		out("ORDER", "%08x", ord.origRefNumDelta)
	case MessageTypeBlockOrderDelete:
		for _, r := range m.bssdRefs {
			out("ORDER", "%08x", r)
		}
	default:
		log.Fatalf("Unexpected message type %d in %+v\n", m.typ, m)
	}
}
<commit_msg>pcap2log: refactor unknown message handling<commit_after>// Copyright (c) Ilia Kravets, 2015. All rights reserved. PROVIDED "AS IS"
// WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. See LICENSE file for details.

package pcap2log

import (
	"fmt"
	"github.com/kr/pretty"
	"io"
	"log"
)

var _ = pretty.Print

type simulator struct {
	w io.Writer
}

func NewSimulator(w io.Writer) simulator {
	return simulator{
		w: w,
	}
}

func (s *simulator) addMessage(qom *QOMessage, typeChar byte) {
	if qom.typ == MessageTypeUnknown {
		return
	}
	s.outMessageNorm(qom, typeChar)
}

func (s *simulator) Printf(format string, vs ...interface{}) {
	if _, err := fmt.Fprintf(s.w, format, vs...); err != nil {
		log.Fatal("output error", err)
	}
}

func (s *simulator) Printfln(format string, vs ...interface{}) {
	s.Printf(format, vs...)
	if _, err := fmt.Fprintln(s.w); err != nil {
		log.Fatal("output error", err)
	}
}

func (s *simulator) outMessageNorm(m *QOMessage, typeChar byte) {
	out := func(name, f string, vs ...interface{}) {
		s.Printf("NORM %s %c ", name, typeChar)
		s.Printfln(f, vs...)
	}
	ord, bid, ask := &m.side1, &m.side1, &m.side2
	if bid.side == MarketSideSell {
		bid, ask = ask, bid
	}
	switch m.typ {
	case MessageTypeQuoteAdd:
		out("QBID", "%08x %08x %08x %08x", m.optionId, bid.refNumDelta, bid.size, bid.price)
		out("QASK", "%08x %08x %08x %08x", m.optionId, ask.refNumDelta, ask.size, ask.price)
	case MessageTypeQuoteReplace:
		out("QBID", "%08x %08x %08x %08x", bid.refNumDelta, bid.origRefNumDelta, bid.size, bid.price)
		out("QASK", "%08x %08x %08x %08x", ask.refNumDelta, ask.origRefNumDelta, ask.size, ask.price)
	case MessageTypeQuoteDelete:
		out("QBID", "%08x", bid.origRefNumDelta)
		out("QASK", "%08x", ask.origRefNumDelta)
	case MessageTypeOrderAdd:
		out("ORDER", "%c %08x %08x %08x %08x", ord.side, m.optionId, ord.refNumDelta, ord.size, ord.price)
	case MessageTypeOrderExecute, MessageTypeOrderExecuteWPrice, MessageTypeOrderCancel:
		out("ORDER", "%08x %08x", ord.origRefNumDelta, ord.size)
	case MessageTypeOrderUpdate:
		out("ORDER", "%08x %08x %08x", ord.origRefNumDelta, ord.size, ord.price)
	case MessageTypeOrderReplace:
		out("ORDER", "%08x %08x %08x %08x", ord.refNumDelta, ord.origRefNumDelta, ord.size, ord.price)
	case MessageTypeOrderDelete:
		out("ORDER", "%08x", ord.origRefNumDelta)
	case MessageTypeBlockOrderDelete:
		for _, r := range m.bssdRefs {
			out("ORDER", "%08x", r)
		}
	default:
		log.Fatalf("Unexpected message type %d in %+v\n", m.typ, m)
	}
}
<|endoftext|>


<commit_before>package misc

import (
	. "jvmgo/any"
	"jvmgo/jvm/rtda"
	rtc "jvmgo/jvm/rtda/class"
	"jvmgo/util"
	"sync/atomic"
	"unsafe"
)

func init() {
	_unsafe(compareAndSwapInt, "compareAndSwapInt", "(Ljava/lang/Object;JII)Z")
	_unsafe(compareAndSwapLong, "compareAndSwapLong", "(Ljava/lang/Object;JJJ)Z")
	_unsafe(compareAndSwapObject, "compareAndSwapObject", "(Ljava/lang/Object;JLjava/lang/Object;Ljava/lang/Object;)Z")
	_unsafe(putObject, "putObject", "(Ljava/lang/Object;JLjava/lang/Object;)V")
	_unsafe(getObject, "getObject", "(Ljava/lang/Object;J)Ljava/lang/Object;")
	_unsafe(putObjectVolatile, "putObjectVolatile", "(Ljava/lang/Object;JLjava/lang/Object;)V")
	_unsafe(getObjectVolatile, "getObjectVolatile", "(Ljava/lang/Object;J)Ljava/lang/Object;")
	_unsafe(putOrderedObject, "putOrderedObject", "(Ljava/lang/Object;JLjava/lang/Object;)V")
	_unsafe(getOrderedObject, "getOrderedObject", "(Ljava/lang/Object;J)Ljava/lang/Object;")
}

// public final native boolean compareAndSwapInt(Object o, long offset, int expected, int x);
// (Ljava/lang/Object;JII)Z
func compareAndSwapInt(frame *rtda.Frame) {
	vars := frame.LocalVars()
	fields := vars.GetRef(1).Fields()
	offset := vars.GetLong(2)
	expected := vars.GetInt(4)
	newVal := vars.GetInt(5)

	if anys, ok := fields.([]Any); ok {
		// object
		swapped := util.CasInt32(anys[offset], expected, newVal)
		frame.OperandStack().PushBoolean(swapped)
	} else if ints, ok := fields.([]int32); ok {
		// int[]
		swapped := atomic.CompareAndSwapInt32(&ints[offset], expected, newVal)
		frame.OperandStack().PushBoolean(swapped)
	} else {
		// todo
		panic("todo: compareAndSwapInt!")
	}
}

// public final native boolean compareAndSwapLong(Object o, long offset, long expected, long x);
// (Ljava/lang/Object;JJJ)Z
func compareAndSwapLong(frame *rtda.Frame) {
	vars := frame.LocalVars()
	fields := vars.GetRef(1).Fields()
	offset := vars.GetLong(2)
	expected := vars.GetLong(4)
	newVal := vars.GetLong(6)

	if anys, ok := fields.([]Any); ok {
		// object
		swapped := util.CasInt64(anys[offset], expected, newVal)
		frame.OperandStack().PushBoolean(swapped)
	} else if ints, ok := fields.([]int64); ok {
		// long[]
		swapped := atomic.CompareAndSwapInt64(&ints[offset], expected, newVal)
		frame.OperandStack().PushBoolean(swapped)
	} else {
		// todo
		panic("todo: compareAndSwapLong!")
	}
}

// public final native boolean compareAndSwapObject(Object o, long offset, Object expected, Object x)
// (Ljava/lang/Object;JLjava/lang/Object;Ljava/lang/Object;)Z
func compareAndSwapObject(frame *rtda.Frame) {
	vars := frame.LocalVars()
	obj := vars.GetRef(1)
	fields := obj.Fields()
	offset := vars.GetLong(2)
	expected := vars.GetRef(4)
	newVal := vars.GetRef(5)

	// todo
	if anys, ok := fields.([]Any); ok {
		// object
		swapped := _casObj(obj, anys, offset, expected, newVal)
		frame.OperandStack().PushBoolean(swapped)
	} else if objs, ok := fields.([]*rtc.Obj); ok {
		// ref[]
		ps := *((*[]unsafe.Pointer)(unsafe.Pointer(&objs))) // cast to []unsafe.Pointer

		addr := &ps[offset]
		old := unsafe.Pointer(expected)
		_new := unsafe.Pointer(newVal)
		swapped := atomic.CompareAndSwapPointer(addr, old, _new)

		frame.OperandStack().PushBoolean(swapped)
	} else {
		// todo
		panic("todo: compareAndSwapObject!")
	}
}
func _casObj(obj *rtc.Obj, fields []Any, offset int64, old, _new *rtc.Obj) bool {
	// todo
	obj.LockState()
	defer obj.UnlockState()

	var current *rtc.Obj
	if f := fields[offset]; f != nil {
	 	current = f.(*rtc.Obj)
	} else {
		current = nil
	}

	if current == old {
		fields[offset] = _new
		return true
	} else {
		return false
	}
}

// public native void putObject(Object o, long offset, Object x);
// (Ljava/lang/Object;JLjava/lang/Object;)V
func putObject(frame *rtda.Frame) {
	vars := frame.LocalVars()
	obj := vars.GetRef(1)
	offset := vars.GetLong(2)
	x := vars.GetRef(4)

	fields := obj.Fields()
	switch fields.(type) {
	case []Any: // object
		fields.([]Any)[offset] = x
	case []*rtc.Obj: // array of ref
		fields.([]*rtc.Obj)[offset] = x
	}
}

// public native Object getObject(Object o, long offset);
// (Ljava/lang/Object;J)Ljava/lang/Object;
func getObject(frame *rtda.Frame) {
	vars := frame.LocalVars()
	obj := vars.GetRef(1)
	offset := vars.GetLong(2)

	var x *rtc.Obj
	fields := obj.Fields()
	switch fields.(type) {
	case []Any: // object
		x = fields.([]Any)[offset].(*rtc.Obj)
	case []*rtc.Obj: // array of ref
		x = fields.([]*rtc.Obj)[offset]
	}

	stack := frame.OperandStack()
	stack.PushRef(x)
}

// public native void putObjectVolatile(Object o, long offset, Object x);
// (Ljava/lang/Object;JLjava/lang/Object;)V
func putObjectVolatile(frame *rtda.Frame) {
	putObject(frame) // todo
}

// public native Object getObjectVolatile(Object o, long offset);
//(Ljava/lang/Object;J)Ljava/lang/Object;
func getObjectVolatile(frame *rtda.Frame) {
	getObject(frame) // todo
}

// public native void putOrderedObject(Object o, long offset, Object x);
// (Ljava/lang/Object;JLjava/lang/Object;)V
func putOrderedObject(frame *rtda.Frame) {
	putObjectVolatile(frame) // todo
}

// public native Object getOrderedObject(Object o, long offset);
//(Ljava/lang/Object;J)Ljava/lang/Object;
func getOrderedObject(frame *rtda.Frame) {
	getObjectVolatile(frame) // todo
}
<commit_msg>extract func<commit_after>package misc

import (
	. "jvmgo/any"
	"jvmgo/jvm/rtda"
	rtc "jvmgo/jvm/rtda/class"
	"jvmgo/util"
	"sync/atomic"
	"unsafe"
)

func init() {
	_unsafe(compareAndSwapInt, "compareAndSwapInt", "(Ljava/lang/Object;JII)Z")
	_unsafe(compareAndSwapLong, "compareAndSwapLong", "(Ljava/lang/Object;JJJ)Z")
	_unsafe(compareAndSwapObject, "compareAndSwapObject", "(Ljava/lang/Object;JLjava/lang/Object;Ljava/lang/Object;)Z")
	_unsafe(putObject, "putObject", "(Ljava/lang/Object;JLjava/lang/Object;)V")
	_unsafe(getObject, "getObject", "(Ljava/lang/Object;J)Ljava/lang/Object;")
	_unsafe(putObjectVolatile, "putObjectVolatile", "(Ljava/lang/Object;JLjava/lang/Object;)V")
	_unsafe(getObjectVolatile, "getObjectVolatile", "(Ljava/lang/Object;J)Ljava/lang/Object;")
	_unsafe(putOrderedObject, "putOrderedObject", "(Ljava/lang/Object;JLjava/lang/Object;)V")
	_unsafe(getOrderedObject, "getOrderedObject", "(Ljava/lang/Object;J)Ljava/lang/Object;")
}

// public final native boolean compareAndSwapInt(Object o, long offset, int expected, int x);
// (Ljava/lang/Object;JII)Z
func compareAndSwapInt(frame *rtda.Frame) {
	vars := frame.LocalVars()
	fields := vars.GetRef(1).Fields()
	offset := vars.GetLong(2)
	expected := vars.GetInt(4)
	newVal := vars.GetInt(5)

	if anys, ok := fields.([]Any); ok {
		// object
		swapped := util.CasInt32(anys[offset], expected, newVal)
		frame.OperandStack().PushBoolean(swapped)
	} else if ints, ok := fields.([]int32); ok {
		// int[]
		swapped := atomic.CompareAndSwapInt32(&ints[offset], expected, newVal)
		frame.OperandStack().PushBoolean(swapped)
	} else {
		// todo
		panic("todo: compareAndSwapInt!")
	}
}

// public final native boolean compareAndSwapLong(Object o, long offset, long expected, long x);
// (Ljava/lang/Object;JJJ)Z
func compareAndSwapLong(frame *rtda.Frame) {
	vars := frame.LocalVars()
	fields := vars.GetRef(1).Fields()
	offset := vars.GetLong(2)
	expected := vars.GetLong(4)
	newVal := vars.GetLong(6)

	if anys, ok := fields.([]Any); ok {
		// object
		swapped := util.CasInt64(anys[offset], expected, newVal)
		frame.OperandStack().PushBoolean(swapped)
	} else if ints, ok := fields.([]int64); ok {
		// long[]
		swapped := atomic.CompareAndSwapInt64(&ints[offset], expected, newVal)
		frame.OperandStack().PushBoolean(swapped)
	} else {
		// todo
		panic("todo: compareAndSwapLong!")
	}
}

// public final native boolean compareAndSwapObject(Object o, long offset, Object expected, Object x)
// (Ljava/lang/Object;JLjava/lang/Object;Ljava/lang/Object;)Z
func compareAndSwapObject(frame *rtda.Frame) {
	vars := frame.LocalVars()
	obj := vars.GetRef(1)
	fields := obj.Fields()
	offset := vars.GetLong(2)
	expected := vars.GetRef(4)
	newVal := vars.GetRef(5)

	// todo
	if anys, ok := fields.([]Any); ok {
		// object
		swapped := _casObj(obj, anys, offset, expected, newVal)
		frame.OperandStack().PushBoolean(swapped)
	} else if objs, ok := fields.([]*rtc.Obj); ok {
		// ref[]
		swapped := _casArr(objs, offset, expected, newVal)
		frame.OperandStack().PushBoolean(swapped)
	} else {
		// todo
		panic("todo: compareAndSwapObject!")
	}
}
func _casObj(obj *rtc.Obj, fields []Any, offset int64, expected, newVal *rtc.Obj) bool {
	// todo
	obj.LockState()
	defer obj.UnlockState()

	var current *rtc.Obj
	if f := fields[offset]; f != nil {
	 	current = f.(*rtc.Obj)
	} else {
		current = nil
	}

	if current == expected {
		fields[offset] = newVal
		return true
	} else {
		return false
	}
}
func _casArr(objs []*rtc.Obj, offset int64, expected, newVal *rtc.Obj) bool {
	// cast to []unsafe.Pointer
	ps := *((*[]unsafe.Pointer)(unsafe.Pointer(&objs)))

	addr := &ps[offset]
	old := unsafe.Pointer(expected)
	_new := unsafe.Pointer(newVal)
	return atomic.CompareAndSwapPointer(addr, old, _new)
}

// public native void putObject(Object o, long offset, Object x);
// (Ljava/lang/Object;JLjava/lang/Object;)V
func putObject(frame *rtda.Frame) {
	vars := frame.LocalVars()
	obj := vars.GetRef(1)
	offset := vars.GetLong(2)
	x := vars.GetRef(4)

	fields := obj.Fields()
	switch fields.(type) {
	case []Any: // object
		fields.([]Any)[offset] = x
	case []*rtc.Obj: // array of ref
		fields.([]*rtc.Obj)[offset] = x
	}
}

// public native Object getObject(Object o, long offset);
// (Ljava/lang/Object;J)Ljava/lang/Object;
func getObject(frame *rtda.Frame) {
	vars := frame.LocalVars()
	obj := vars.GetRef(1)
	offset := vars.GetLong(2)

	var x *rtc.Obj
	fields := obj.Fields()
	switch fields.(type) {
	case []Any: // object
		x = fields.([]Any)[offset].(*rtc.Obj)
	case []*rtc.Obj: // array of ref
		x = fields.([]*rtc.Obj)[offset]
	}

	stack := frame.OperandStack()
	stack.PushRef(x)
}

// public native void putObjectVolatile(Object o, long offset, Object x);
// (Ljava/lang/Object;JLjava/lang/Object;)V
func putObjectVolatile(frame *rtda.Frame) {
	putObject(frame) // todo
}

// public native Object getObjectVolatile(Object o, long offset);
//(Ljava/lang/Object;J)Ljava/lang/Object;
func getObjectVolatile(frame *rtda.Frame) {
	getObject(frame) // todo
}

// public native void putOrderedObject(Object o, long offset, Object x);
// (Ljava/lang/Object;JLjava/lang/Object;)V
func putOrderedObject(frame *rtda.Frame) {
	putObjectVolatile(frame) // todo
}

// public native Object getOrderedObject(Object o, long offset);
//(Ljava/lang/Object;J)Ljava/lang/Object;
func getOrderedObject(frame *rtda.Frame) {
	getObjectVolatile(frame) // todo
}
<|endoftext|>


<commit_before>// Copyright 2017 Mathew Robinson <mrobinson@praelatus.io>. All rights reserved.
// Use of this source code is governed by the AGPLv3 license that can be found in
// the LICENSE file.

// Package middleware contains the HTTP middleware used in the api as
// well as utility functions for interacting with them
package middleware

import (
	"net/http"

	"github.com/praelatus/praelatus/repo"
)

// Cache is the global SessionCache
var Cache repo.Cache

// ContentHeaders will set the content-type header for the API to application/json
func ContentHeaders(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.URL.Path[len("/api"):] == "/api" {
			w.Header().Set("Content-Type", "application/json")
		}

		next.ServeHTTP(w, r)
	})
}

// LoadMw will wrap the given http.Handler in the DefaultMiddleware
func LoadMw(handler http.Handler) http.Handler {
	h := handler

	for _, m := range DefaultMiddleware {
		h = m(h)
	}

	return h
}

// DefaultMiddleware is the default middleware stack for Praelatus
var DefaultMiddleware = []func(http.Handler) http.Handler{
	ContentHeaders,
	Logger,
}
<commit_msg>fix ContentHeaders in middleware<commit_after>// Copyright 2017 Mathew Robinson <mrobinson@praelatus.io>. All rights reserved.
// Use of this source code is governed by the AGPLv3 license that can be found in
// the LICENSE file.

// Package middleware contains the HTTP middleware used in the api as
// well as utility functions for interacting with them
package middleware

import (
	"net/http"
	"strings"

	"github.com/praelatus/praelatus/api/utils"
	"github.com/praelatus/praelatus/repo"
)

// Cache is the global SessionCache
var Cache repo.Cache

// ContentHeaders will set the content-type header for the API to application/json
func ContentHeaders(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.URL.Path[:len("/api")] == "/api" {
			if !strings.HasPrefix(r.Header.Get("Content-Type"), "application/json") {
				utils.APIErr(w, http.StatusBadRequest,
					"incorrect content-type")
				return
			}
			w.Header().Set("Content-Type", "application/json")
		}

		next.ServeHTTP(w, r)
	})
}

// LoadMw will wrap the given http.Handler in the DefaultMiddleware
func LoadMw(handler http.Handler) http.Handler {
	h := handler

	for _, m := range DefaultMiddleware {
		h = m(h)
	}

	return h
}

// DefaultMiddleware is the default middleware stack for Praelatus
var DefaultMiddleware = []func(http.Handler) http.Handler{
	ContentHeaders,
	Logger,
}
<|endoftext|>


<commit_before>/*
																	NOTES:
	Key distribution problem:
		How do we distribute the key in a safe way? Can, as of now, be picked up with
		a packet sniffer since it is sent in plaintext.

	Features to add:
	#1: Let server send messages. In that case, let server use commands like !quit or !makeroom.
				TO DISCUSS 1: Is server message a wide broadcast or for a specified room? E.g lobby.
	#2: We need to save all data to JSON objects instead of saving it to the ram.
			To do this we need to use channels to be able to exchange and edit data
			from and to a go routine.
				TO DISCUSS 1:
						In case of using JSON objects for saving data, do we delete all files
						after server is closed or do we keep them? How does that look from
						a security perspective?
						Do we hash or encrypt data in the json files? If so, what technique
						do we use to hash/encrypt this data?
	#3: Nickname for each client
				TO DISCUSS 1:
					Should clients only be allowed to set nick on startup to remove confusion
					or should clients always be able to change nickname with a command? e.g !nick [nickname].
	#4: Let users choose what IP and port to connect to upon startup.
	#5: Set different privileges for different users? Admin, moderator, user etc.
	#6: Display amount of clients in a room on !room command. E.g - Lobby (3 clients).
	#7: Add a docker version of this server for easy setup?
	#8: Some other way to make the setup and use of this chat easier for everyone?

	Bigger implementations for the future:
	#1: Implement a file sender?
				TO DISCUSS 1:
					Is it a implementation that is useful for our chat?
				TO DISCUSS 2:
					How do we make the filesender as easy to use as possible?
	#2: Public key encryption implementation? E.g Diffie Hellman
				TO DISCUSS 1:
					How do we distribute the key pair? Keep in mind, every message has to
					be encrypted with the reciepients public key. Therefore we always have
					to keep track of everyones public key.
				TO DISCUSS 2:
					Where do we create the key pair? On the server or on the client?
				TO DISCUSS 3:
					How is a message sent to everyone? On the other side, how is a message
					sent to only e.g client in a specified chatroom.
*/

package main

import (
	"bufio"
	"crypto/aes"
	"crypto/cipher"
	"crypto/rand"
	"encoding/base64"
	"fmt"
	"io"
	"net"
	"strings"
)

// client list
var rooms []Room
var clientRoom map[Client]Room
var key []byte

// Room is a struct keeping track of room details
type Room struct {
	name        string
	discription string
	password    string
	maxClients  int
	welcomeMsg  string
}

// Client represents a connected client
type Client struct {
	connection net.Conn
	nick       string
}

// send is a function for sending a plaintext message
func (c *Client) send(message []byte) {
	c.connection.Write(message)
}

// sendEncrypted is a function for sending an encrypted message
func (c *Client) sendEncrypted(message string) {
	cryptMsg := encrypt(key, message)
	c.connection.Write([]byte(cryptMsg + "\n"))
}

// setAndSendKey is a function for sending the
// encryption key to a new connection
func (c *Client) setAndSendKey(key []byte) {
	c.send([]byte(string(key) + "\n"))
}

// listener is a function which runs in the background as a
// go routine which listens for incoming messages.
// As of now, for display purposes, the message gets decrypted and displayed
// in plaintext in the server window. Then it is checked for command words with
// the checkForCmd function. If that returns false the message gets sent
// to all clients in the same room as the sender. At the end, variable "message"
// is still decrypted, therefore it is sent using only the send function.
func (c Client) listener() {
	for {
		message, _ := bufio.NewReader(c.connection).ReadString('\n')
		if len(message) > 0 {
			msg := message[0 : len(message)-1]
			fmt.Print(c.connection.RemoteAddr().String() + ": " + decrypt(key, msg))
			if !checkForCmd(c, decrypt(key, msg)) {
				for key, value := range clientRoom {
					if key != c && value == clientRoom[c] {
						key.send([]byte(message + "\n"))
					}
				}
			}
		}
	}
}

// Starts a goroutine that listens for incoming messages.
func (c *Client) startThread() {
	go c.listener()
}

func main() {
	// Upon startup we create a key which is used to encrypt and decrypt all messages
	// with. This is distributed to all new connections upon connection. This key
	// is 32 bytes long(256 bits) which is still concidered safe and not broken.
	key = createKey()

	// Then we set up default rooms. Every new conncetion will be set to the
	// lobby room.
	makeRoom("Lobby", "Welcome to Lobby")
	makeRoom("TestRoom", "Welcome to TestRoom")

	// Then we set up the socket to be a TCP connection and to listen to a
	// specified port. This port can be set to all port from 1024 and up.
	port := ":8081"
	ln, _ := net.Listen("tcp", port)
	fmt.Println("Server is listening on " + port)

	// Here we set up a map which keeps track of which room all clients are connected to.
	clientRoom = make(map[Client]Room)

	// This loop listens for new connections. It accepts the new connection, creates
	// a new Client struct, sends the encryption/decryption key and starts a listener
	// thread which listens for incoming messages. Then it prints out in the
	// server window that a new client has connected and sets the clients room
	// to lobby, which is the default landing room.
	for {
		conn, _ := ln.Accept()
		newClient := Client{connection: conn}
		newClient.setAndSendKey(key)
		newClient.startThread()
		fmt.Println(conn.RemoteAddr().String() + " connected.")
		switchRoom(newClient, rooms[0])
	}
}

// Function to check for commands in a message sent by a client
func checkForCmd(client Client, msg string) bool {
	if len(msg) > 1 {
		words := strings.Split(msg[0:len(msg)-1], " ")
		switch words[0] {
		// The room command is used to both list all rooms and to change rooms.
		// !room sends a list over all rooms to the client
		// !room [roomname] is a command used to change room to the selected room
		case "!room":
			if len(words) > 1 {
				if words[1] == clientRoom[client].name {
					message := "You are already in this room!\nType !room to get a list of other available chatrooms"
					client.sendEncrypted(message)
				} else {
					for _, element := range rooms {
						if element.name == words[1] {
							client.sendEncrypted("Switching room: " + element.name + "\n")
							switchRoom(client, element)
						}
					}
				}
			} else {
				roomMsg := ""
				for _, element := range rooms {
					roomMsg += " - " + element.name + "\n"
				}
				client.sendEncrypted(roomMsg)
			}
			return true
		// The !newroom command lets clients create new rooms
		case "!newroom":
			if len(words) > 1 {
				name := words[1]
				welcomeMsg := words[2]
				for _, element := range rooms {
					if element.name == name {
						client.sendEncrypted("A room with this name already exists!\n")
					} else {
						makeRoom(name, welcomeMsg)
						for _, element := range rooms {
							if element.name == words[1] {
								client.sendEncrypted("Switching room: " + element.name + "\n")
								switchRoom(client, element)
							}
						}
					}
				}

			}
		}
	}
	return false
}

func switchRoom(client Client, room Room) {
	clientRoom[client] = room
	client.sendEncrypted(room.welcomeMsg + "\n")
}

func makeRoom(name string, welcomeMsg string) {
	newRoom := Room{name: name, welcomeMsg: welcomeMsg}
	rooms = append(rooms, newRoom)
}

// create key
func createKey() []byte {
	randKey := make([]byte, 32)
	_, err := rand.Read(randKey)
	if err != nil {
		panic(err)
	}
	return randKey
}

// encrypt message
func encrypt(key []byte, text string) string {
	plaintext := []byte(text)

	block, err := aes.NewCipher(key)
	if err != nil {
		panic(err)
	}

	ciphertext := make([]byte, aes.BlockSize+len(plaintext))
	iv := ciphertext[:aes.BlockSize]
	if _, err := io.ReadFull(rand.Reader, iv); err != nil {
		panic(err)
	}

	stream := cipher.NewCFBEncrypter(block, iv)
	stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)

	return base64.URLEncoding.EncodeToString(ciphertext)
}

// decrypt message
func decrypt(key []byte, cryptoText string) string {
	ciphertext, _ := base64.URLEncoding.DecodeString(cryptoText)

	block, err := aes.NewCipher(key)
	if err != nil {
		panic(err)
	}
	if len(ciphertext) < aes.BlockSize {
		panic("Ciphertext too short")
	}

	iv := ciphertext[:aes.BlockSize]
	ciphertext = ciphertext[aes.BlockSize:]

	stream := cipher.NewCFBDecrypter(block, iv)

	stream.XORKeyStream(ciphertext, ciphertext)

	return fmt.Sprintf("%s", ciphertext)
}
<commit_msg>Removed the long comment section in the top of the server .go file<commit_after>package main

import (
	"bufio"
	"crypto/aes"
	"crypto/cipher"
	"crypto/rand"
	"encoding/base64"
	"fmt"
	"io"
	"net"
	"strings"
)

// client list
var rooms []Room
var clientRoom map[Client]Room
var key []byte

// Room is a struct keeping track of room details
type Room struct {
	name        string
	discription string
	password    string
	maxClients  int
	welcomeMsg  string
}

// Client represents a connected client
type Client struct {
	connection net.Conn
	nick       string
}

// send is a function for sending a plaintext message
func (c *Client) send(message []byte) {
	c.connection.Write(message)
}

// sendEncrypted is a function for sending an encrypted message
func (c *Client) sendEncrypted(message string) {
	cryptMsg := encrypt(key, message)
	c.connection.Write([]byte(cryptMsg + "\n"))
}

// setAndSendKey is a function for sending the
// encryption key to a new connection
func (c *Client) setAndSendKey(key []byte) {
	c.send([]byte(string(key) + "\n"))
}

// listener is a function which runs in the background as a
// go routine which listens for incoming messages.
// As of now, for display purposes, the message gets decrypted and displayed
// in plaintext in the server window. Then it is checked for command words with
// the checkForCmd function. If that returns false the message gets sent
// to all clients in the same room as the sender. At the end, variable "message"
// is still decrypted, therefore it is sent using only the send function.
func (c Client) listener() {
	for {
		message, _ := bufio.NewReader(c.connection).ReadString('\n')
		if len(message) > 0 {
			msg := message[0 : len(message)-1]
			fmt.Print(c.connection.RemoteAddr().String() + ": " + decrypt(key, msg))
			if !checkForCmd(c, decrypt(key, msg)) {
				for key, value := range clientRoom {
					if key != c && value == clientRoom[c] {
						key.send([]byte(message + "\n"))
					}
				}
			}
		}
	}
}

// Starts a goroutine that listens for incoming messages.
func (c *Client) startThread() {
	go c.listener()
}

func main() {
	// Upon startup we create a key which is used to encrypt and decrypt all messages
	// with. This is distributed to all new connections upon connection. This key
	// is 32 bytes long(256 bits) which is still concidered safe and not broken.
	key = createKey()

	// Then we set up default rooms. Every new conncetion will be set to the
	// lobby room.
	makeRoom("Lobby", "Welcome to Lobby")
	makeRoom("TestRoom", "Welcome to TestRoom")

	// Then we set up the socket to be a TCP connection and to listen to a
	// specified port. This port can be set to all port from 1024 and up.
	port := ":8081"
	ln, _ := net.Listen("tcp", port)
	fmt.Println("Server is listening on " + port)

	// Here we set up a map which keeps track of which room all clients are connected to.
	clientRoom = make(map[Client]Room)

	// This loop listens for new connections. It accepts the new connection, creates
	// a new Client struct, sends the encryption/decryption key and starts a listener
	// thread which listens for incoming messages. Then it prints out in the
	// server window that a new client has connected and sets the clients room
	// to lobby, which is the default landing room.
	for {
		conn, _ := ln.Accept()
		newClient := Client{connection: conn}
		newClient.setAndSendKey(key)
		newClient.startThread()
		fmt.Println(conn.RemoteAddr().String() + " connected.")
		switchRoom(newClient, rooms[0])
	}
}

// Function to check for commands in a message sent by a client
func checkForCmd(client Client, msg string) bool {
	if len(msg) > 1 {
		words := strings.Split(msg[0:len(msg)-1], " ")
		switch words[0] {
		// The room command is used to both list all rooms and to change rooms.
		// !room sends a list over all rooms to the client
		// !room [roomname] is a command used to change room to the selected room
		case "!room":
			if len(words) > 1 {
				if words[1] == clientRoom[client].name {
					message := "You are already in this room!\nType !room to get a list of other available chatrooms"
					client.sendEncrypted(message)
				} else {
					for _, element := range rooms {
						if element.name == words[1] {
							client.sendEncrypted("Switching room: " + element.name + "\n")
							switchRoom(client, element)
						}
					}
				}
			} else {
				roomMsg := ""
				for _, element := range rooms {
					roomMsg += " - " + element.name + "\n"
				}
				client.sendEncrypted(roomMsg)
			}
			return true
		// The !newroom command lets clients create new rooms
		case "!newroom":
			if len(words) > 1 {
				name := words[1]
				welcomeMsg := words[2]
				for _, element := range rooms {
					if element.name == name {
						client.sendEncrypted("A room with this name already exists!\n")
					} else {
						makeRoom(name, welcomeMsg)
						for _, element := range rooms {
							if element.name == words[1] {
								client.sendEncrypted("Switching room: " + element.name + "\n")
								switchRoom(client, element)
							}
						}
					}
				}

			}
		}
	}
	return false
}

func switchRoom(client Client, room Room) {
	clientRoom[client] = room
	client.sendEncrypted(room.welcomeMsg + "\n")
}

func makeRoom(name string, welcomeMsg string) {
	newRoom := Room{name: name, welcomeMsg: welcomeMsg}
	rooms = append(rooms, newRoom)
}

// create key
func createKey() []byte {
	randKey := make([]byte, 32)
	_, err := rand.Read(randKey)
	if err != nil {
		panic(err)
	}
	return randKey
}

// encrypt message
func encrypt(key []byte, text string) string {
	plaintext := []byte(text)

	block, err := aes.NewCipher(key)
	if err != nil {
		panic(err)
	}

	ciphertext := make([]byte, aes.BlockSize+len(plaintext))
	iv := ciphertext[:aes.BlockSize]
	if _, err := io.ReadFull(rand.Reader, iv); err != nil {
		panic(err)
	}

	stream := cipher.NewCFBEncrypter(block, iv)
	stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)

	return base64.URLEncoding.EncodeToString(ciphertext)
}

// decrypt message
func decrypt(key []byte, cryptoText string) string {
	ciphertext, _ := base64.URLEncoding.DecodeString(cryptoText)

	block, err := aes.NewCipher(key)
	if err != nil {
		panic(err)
	}
	if len(ciphertext) < aes.BlockSize {
		panic("Ciphertext too short")
	}

	iv := ciphertext[:aes.BlockSize]
	ciphertext = ciphertext[aes.BlockSize:]

	stream := cipher.NewCFBDecrypter(block, iv)

	stream.XORKeyStream(ciphertext, ciphertext)

	return fmt.Sprintf("%s", ciphertext)
}
<|endoftext|>


<commit_before>package ircstats

type User struct {
	Username   string
	Url        string
	Avatar     string
	LineCount  int64
	WordCount  int64
	DaysActive int64
	CharCount  int64
	WordsLine  int64
	LineLength int64
	LinesDay   int64
	WordsDay   int64
	Vocabulary int64
	DaysTotal  int64
	MaxHours   int64
	Words      map[string]int64	// A Map of words and usage
	HoursAndDaysStats
	Seen
}

func (u *User) CalculateTotals() {
	u.Vocabulary = int64(len(u.Words))
	u.DaysTotal = int64(len(u.Days))

	if u.LineCount > 0 {
		u.WordsLine = int64(u.WordCount / u.LineCount)
	} else {
		u.WordsLine = 0
	}

	// @todo finish
}

// Add Word to Words map, or if it already exists incremenet its usage count
func (u *User) AddWord(word string) {
	if u.HasWord(word) == true {
		u.Words[word]++
	} else {
		u.Words[word] = 1;
	}
}

// Check to see if User contains word
func (u User) HasWord(word string) bool {
	if _, ok := u.Words[word]; ok {
		return true
	}
	return false
}

// User Struct constructor
func NewUser(nick string, timestamp int64) *User {
	u := User{Username: nick}
	u.Initiate()
	u.Words = make(map[string]int64)
	u.UpdateSeen(timestamp)
	return &u
}
<commit_msg>Sorting out dead variables?<commit_after>package ircstats

type User struct {
	Username   string
	Url        string
	Avatar     string
	LineCount  int64
	WordCount  int64            // *
	DaysActive int64            // *
	CharCount  int64            // *
				    //WordsLine  int64
	LineLength int64
	LinesDay   int64
	WordsDay   int64
	Vocabulary int64
				    //DaysTotal  int64
				    //MaxHours   int64
	Words      map[string]int64 // A Map of words and usage
	HoursAndDaysStats
	Seen
}

// Add Word to Words map, or if it already exists incremenet its usage count
func (u *User) AddWord(word string) {
	if u.HasWord(word) == true {
		u.Words[word]++
	} else {
		u.Words[word] = 1;
	}
}

// Check to see if User contains word
func (u User) HasWord(word string) bool {
	if _, ok := u.Words[word]; ok {
		return true
	}
	return false
}

// User Struct constructor
func NewUser(nick string, timestamp int64) *User {
	u := User{Username: nick}
	u.Initiate()
	u.Words = make(map[string]int64)
	u.UpdateSeen(timestamp)
	return &u
}
<|endoftext|>


<commit_before>package main

import (
	"encoding/json"
	"flag"
	"github.com/mingzhi/gomath/random"
	. "github.com/mingzhi/popsimu/cmd"
	"github.com/mingzhi/popsimu/pop"
	"github.com/mingzhi/seqcor/calculator"
	"io"
	"math/rand"
	"os"
	"runtime"
	"time"
)

var (
	maxl   int
	ncpu   int
	input  string
	output string
)

func init() {
	const (
		defaultMaxl = 200
	)
	defaultNCPU := runtime.NumCPU()

	flag.IntVar(&maxl, "maxl", defaultMaxl, "maxl")
	flag.IntVar(&ncpu, "ncpu", defaultNCPU, "ncpu")
	flag.Parse()
	input = flag.Arg(0)
	output = flag.Arg(1)

	runtime.GOMAXPROCS(ncpu)
}

func main() {
	configChan := read(input)
	results := run(configChan)
	write(output, results)
}

type popConfig struct {
	p *pop.Pop
	c pop.Config
}

func run(configChan chan pop.Config) []Result {
	simResChan := batchSimu(configChan)
	calcChan := calc(simResChan, maxl)
	results := collect(calcChan)
	return results
}

func batchSimu(configChan chan pop.Config) (resChan chan popConfig) {
	numWorker := runtime.GOMAXPROCS(0)
	resChan = make(chan popConfig, numWorker)
	done := make(chan bool)
	simulator := func() {
		defer send(done)
		for c := range configChan {
			p := simu(c)
			pc := popConfig{p: p, c: c}
			resChan <- pc
		}
	}

	for i := 0; i < numWorker; i++ {
		go simulator()
	}

	go func() {
		defer close(resChan)
		wait(done, numWorker)
	}()

	return resChan
}

type calculators struct {
	ks *calculator.Ks
	ct *calculator.AutoCovFFT
}

func (c *calculators) Increment(xs []float64) {
	for i := 0; i < len(xs); i++ {
		c.ks.Increment(xs[i])
	}

	c.ct.Increment(xs)
}

func (c *calculators) Append(c2 *calculators) {
	c.ks.Append(c2.ks)
	c.ct.Append(c2.ct)
}

func newCalculators(maxl int, circular bool) *calculators {
	c := calculators{}
	c.ks = calculator.NewKs()
	c.ct = calculator.NewAutoCovFFT(maxl, circular)
	return &c
}

type calcConfig struct {
	cfg pop.Config
	c   *calculators
}

func calc(simResChan chan popConfig, maxl int) chan calcConfig {
	numWorker := runtime.GOMAXPROCS(0)
	circular := true
	done := make(chan bool)

	calcChan := make(chan calcConfig, numWorker)
	worker := func() {
		defer send(done)
		for res := range simResChan {
			cc := calcConfig{}
			cc.cfg = res.c

			sequences := [][]byte{}
			for _, g := range res.p.Genomes {
				sequences = append(sequences, g.Sequence)
			}
			ks := calculator.CalcKs(sequences)
			ct := calculator.CalcCtFFT(sequences, maxl, circular)

			cc.c = &calculators{}
			cc.c.ks = ks
			cc.c.ct = ct

			calcChan <- cc
		}
	}

	for i := 0; i < numWorker; i++ {
		go worker()
	}

	go func() {
		defer close(calcChan)
		wait(done, numWorker)
	}()

	return calcChan
}

func collect(calcChan chan calcConfig) []Result {
	m := make(map[pop.Config]*calculators)
	for cc := range calcChan {
		c, found := m[cc.cfg]
		if !found {
			c = cc.c
		}
		c.Append(cc.c)
		m[cc.cfg] = c
	}

	var results []Result
	for cfg, c := range m {
		res := Result{}
		res.Config = cfg
		res.C = createCovResult(c)
		results = append(results, res)
	}

	return results
}

func newPop(c pop.Config, src rand.Source) *pop.Pop {
	r := rand.New(src)
	popGenerator := pop.NewRandomPopGenerator(r)
	p := c.NewPop(popGenerator)
	return p
}

func generateEvents(moranEvent *pop.Event, otherEvents []*pop.Event, numGen int) chan *pop.Event {
	c := make(chan *pop.Event)

	go func() {
		defer close(c)
		totalRate := 0.0
		for _, e := range otherEvents {
			totalRate += e.Rate
		}

		seed := time.Now().UnixNano()
		src := rand.NewSource(seed)
		poissonSampler := random.NewPoisson(totalRate, src)

		r := rand.New(src)

		for i := 0; i < numGen; i++ {
			c <- moranEvent
			num := poissonSampler.Int()
			for j := 0; j < num; j++ {
				c <- pop.Emit(otherEvents, r)
			}
		}
	}()

	return c
}

func simu(c pop.Config) *pop.Pop {
	seed := time.Now().UnixNano()
	src := rand.NewSource(seed)

	p := newPop(c, src)
	r := rand.New(src)

	moranEvent := &pop.Event{
		Ops: pop.NewMoranSampler(r),
		Pop: p,
	}

	mutationEvent := &pop.Event{
		Ops:  pop.NewSimpleMutator(r),
		Pop:  p,
		Rate: c.Mutation.Rate * float64(c.Length),
	}

	lambda := 1.0 / float64(c.Transfer.In.Fragment)
	fragGenerator := pop.NewExpFrag(lambda, src)
	transferEvent := &pop.Event{
		Ops:  pop.NewSimpleTransfer(fragGenerator, r),
		Pop:  p,
		Rate: c.Transfer.In.Rate * float64(c.Length),
	}

	otherEvents := []*pop.Event{mutationEvent, transferEvent}
	eventChan := generateEvents(moranEvent, otherEvents, c.NumGen)

	pop.Evolve(eventChan)

	return p
}

func send(done chan bool) {
	done <- true
}

func wait(done chan bool, numWorker int) {
	for i := 0; i < numWorker; i++ {
		<-done
	}
}

func read(filename string) (configChan chan pop.Config) {
	f, err := os.Open(filename)
	if err != nil {
		panic(err)
	}
	defer f.Close()

	configs := readConfigs(f)
	configChan = make(chan pop.Config)

	makeConfigChan := func() {
		defer close(configChan)
		for _, c := range configs {
			configChan <- c
		}
	}

	go makeConfigChan()

	return
}

func readConfigs(r io.Reader) (configs []pop.Config) {
	d := json.NewDecoder(r)
	err := d.Decode(&configs)
	if err != nil {
		panic(err)
	}
	return
}

func createCovResult(c *calculators) CovResult {
	var cr CovResult
	cr.Ks = c.ks.Mean.GetResult()
	for i := 0; i < c.ct.N; i++ {
		cr.Ct = append(cr.Ct, c.ct.GetResult(i))
	}
	return cr
}

func write(filename string, results []Result) {
	w, err := os.Create(filename)
	if err != nil {
		panic(err)
	}
	defer w.Close()

	encoder := json.NewEncoder(w)
	if err := encoder.Encode(results); err != nil {
		panic(err)
	}
}
<commit_msg>use fftw<commit_after>package main

import (
	"encoding/json"
	"flag"
	"github.com/mingzhi/gomath/random"
	. "github.com/mingzhi/popsimu/cmd"
	"github.com/mingzhi/popsimu/pop"
	"github.com/mingzhi/seqcor/calculator"
	"io"
	"math/rand"
	"os"
	"runtime"
	"time"
)

var (
	ncpu   int
	input  string
	output string
)

func init() {
	const (
		defaultMaxl = 200
	)
	defaultNCPU := runtime.NumCPU()

	flag.IntVar(&ncpu, "ncpu", defaultNCPU, "ncpu")
	flag.Parse()
	input = flag.Arg(0)
	output = flag.Arg(1)

	runtime.GOMAXPROCS(ncpu)
}

func main() {
	configChan := read(input)
	results := run(configChan)
	write(output, results)
}

type popConfig struct {
	p *pop.Pop
	c pop.Config
}

func run(configChan chan pop.Config) []Result {
	simResChan := batchSimu(configChan)
	calcChan := calc(simResChan)
	results := collect(calcChan)
	return results
}

func batchSimu(configChan chan pop.Config) (resChan chan popConfig) {
	numWorker := runtime.GOMAXPROCS(0)
	resChan = make(chan popConfig, numWorker)
	done := make(chan bool)
	simulator := func() {
		defer send(done)
		for c := range configChan {
			p := simu(c)
			pc := popConfig{p: p, c: c}
			resChan <- pc
		}
	}

	for i := 0; i < numWorker; i++ {
		go simulator()
	}

	go func() {
		defer close(resChan)
		wait(done, numWorker)
	}()

	return resChan
}

type calculators struct {
	ks *calculator.Ks
	ct *calculator.AutoCovFFTW
}

func (c *calculators) Increment(xs []float64) {
	for i := 0; i < len(xs); i++ {
		c.ks.Increment(xs[i])
	}

	c.ct.Increment(xs)
}

func (c *calculators) Append(c2 *calculators) {
	c.ks.Append(c2.ks)
	c.ct.Append(c2.ct)
}

func newCalculators(n int, circular bool) *calculators {
	c := calculators{}
	c.ks = calculator.NewKs()
	c.ct = calculator.NewAutoCovFFTW(n, circular)
	return &c
}

type calcConfig struct {
	cfg pop.Config
	c   *calculators
}

func calc(simResChan chan popConfig) chan calcConfig {
	numWorker := runtime.GOMAXPROCS(0)
	circular := true
	done := make(chan bool)

	calcChan := make(chan calcConfig, numWorker)
	worker := func() {
		defer send(done)
		for res := range simResChan {
			cc := calcConfig{}
			cc.cfg = res.c

			sequences := [][]byte{}
			for _, g := range res.p.Genomes {
				sequences = append(sequences, g.Sequence)
			}
			ks := calculator.CalcKs(sequences)
			ct := calculator.CalcCtFFTW(sequences, circular)

			cc.c = &calculators{}
			cc.c.ks = ks
			cc.c.ct = ct

			calcChan <- cc
		}
	}

	for i := 0; i < numWorker; i++ {
		go worker()
	}

	go func() {
		defer close(calcChan)
		wait(done, numWorker)
	}()

	return calcChan
}

func collect(calcChan chan calcConfig) []Result {
	m := make(map[pop.Config]*calculators)
	for cc := range calcChan {
		c, found := m[cc.cfg]
		if !found {
			c = cc.c
		}
		c.Append(cc.c)
		m[cc.cfg] = c
	}

	var results []Result
	for cfg, c := range m {
		res := Result{}
		res.Config = cfg
		res.C = createCovResult(c)
		results = append(results, res)
	}

	return results
}

func newPop(c pop.Config, src rand.Source) *pop.Pop {
	r := rand.New(src)
	popGenerator := pop.NewRandomPopGenerator(r)
	p := c.NewPop(popGenerator)
	return p
}

func generateEvents(moranEvent *pop.Event, otherEvents []*pop.Event, numGen int) chan *pop.Event {
	c := make(chan *pop.Event)

	go func() {
		defer close(c)
		totalRate := 0.0
		for _, e := range otherEvents {
			totalRate += e.Rate
		}

		seed := time.Now().UnixNano()
		src := rand.NewSource(seed)
		poissonSampler := random.NewPoisson(totalRate, src)

		r := rand.New(src)

		for i := 0; i < numGen; i++ {
			c <- moranEvent
			num := poissonSampler.Int()
			for j := 0; j < num; j++ {
				c <- pop.Emit(otherEvents, r)
			}
		}
	}()

	return c
}

func simu(c pop.Config) *pop.Pop {
	seed := time.Now().UnixNano()
	src := rand.NewSource(seed)

	p := newPop(c, src)
	r := rand.New(src)

	moranEvent := &pop.Event{
		Ops: pop.NewMoranSampler(r),
		Pop: p,
	}

	mutationEvent := &pop.Event{
		Ops:  pop.NewSimpleMutator(r),
		Pop:  p,
		Rate: c.Mutation.Rate * float64(c.Length),
	}

	lambda := 1.0 / float64(c.Transfer.In.Fragment)
	fragGenerator := pop.NewExpFrag(lambda, src)
	transferEvent := &pop.Event{
		Ops:  pop.NewSimpleTransfer(fragGenerator, r),
		Pop:  p,
		Rate: c.Transfer.In.Rate * float64(c.Length),
	}

	otherEvents := []*pop.Event{mutationEvent, transferEvent}
	eventChan := generateEvents(moranEvent, otherEvents, c.NumGen)

	pop.Evolve(eventChan)

	return p
}

func send(done chan bool) {
	done <- true
}

func wait(done chan bool, numWorker int) {
	for i := 0; i < numWorker; i++ {
		<-done
	}
}

func read(filename string) (configChan chan pop.Config) {
	f, err := os.Open(filename)
	if err != nil {
		panic(err)
	}
	defer f.Close()

	configs := readConfigs(f)
	configChan = make(chan pop.Config)

	makeConfigChan := func() {
		defer close(configChan)
		for _, c := range configs {
			configChan <- c
		}
	}

	go makeConfigChan()

	return
}

func readConfigs(r io.Reader) (configs []pop.Config) {
	d := json.NewDecoder(r)
	err := d.Decode(&configs)
	if err != nil {
		panic(err)
	}
	return
}

func createCovResult(c *calculators) CovResult {
	var cr CovResult
	cr.Ks = c.ks.Mean.GetResult()
	for i := 0; i < c.ct.N; i++ {
		cr.Ct = append(cr.Ct, c.ct.GetResult(i))
	}
	return cr
}

func write(filename string, results []Result) {
	w, err := os.Create(filename)
	if err != nil {
		panic(err)
	}
	defer w.Close()

	encoder := json.NewEncoder(w)
	if err := encoder.Encode(results); err != nil {
		panic(err)
	}
}
<|endoftext|>