<commit_before>package store

import (
	"math/rand"
	"testing"
	"time"
)

func TestBasicStats(t *testing.T) {
	s := New()
	keys := GenKeys(rand.Intn(100), 5)

	var i uint64
	var GetSuccess, GetFail, SetSuccess, SetFail, DeleteSuccess, DeleteFail uint64
	var UpdateSuccess, UpdateFail, TestAndSetSuccess, TestAndSetFail, watcher_number uint64

	for _, k := range keys {
		i++
		_, err := s.Create(k, "bar", time.Now().Add(time.Second*time.Duration(rand.Intn(6))), i, 1)
		if err != nil {
			SetFail++
		} else {
			SetSuccess++
		}
	}

	//fmt.Println("create")

	time.Sleep(time.Second * 3)

	for _, k := range keys {
		_, err := s.Get(k, false, false, i, 1)
		if err != nil {
			GetFail++
		} else {
			GetSuccess++
		}
	}

	//fmt.Println("get")

	for _, k := range keys {
		i++
		_, err := s.Update(k, "foo", time.Now().Add(time.Second*time.Duration(rand.Intn(6))), i, 1)
		if err != nil {
			UpdateFail++
		} else {
			UpdateSuccess++
		}
	}

	//fmt.Println("update")

	time.Sleep(time.Second * 3)

	for _, k := range keys {
		_, err := s.Get(k, false, false, i, 1)
		if err != nil {
			GetFail++
		} else {
			GetSuccess++
		}
	}

	for _, k := range keys {
		i++
		_, err := s.TestAndSet(k, "foo", 0, "bar", Permanent, i, 1)
		if err != nil {
			TestAndSetFail++
		} else {
			TestAndSetSuccess++
		}
	}

	//fmt.Println("get testAndSet")

	for _, k := range keys {
		s.Watch(k, false, 0, i, 1)
		watcher_number++
	}

	//fmt.Println("watch")

	for _, k := range keys {
		_, err := s.Get(k, false, false, i, 1)
		if err != nil {
			GetFail++
		} else {
			GetSuccess++
		}
	}

	for _, k := range keys {
		i++
		_, err := s.Delete(k, false, i, 1)
		if err != nil {
			DeleteFail++
		} else {
			watcher_number--
			DeleteSuccess++
		}
	}

	//fmt.Println("get delete")

	for _, k := range keys {
		_, err := s.Get(k, false, false, i, 1)
		if err != nil {
			GetFail++
		} else {
			GetSuccess++
		}
	}

	if GetSuccess != s.Stats.GetSuccess {
		t.Fatalf("GetSuccess [%d] != Stats.GetSuccess [%d]", GetSuccess, s.Stats.GetSuccess)
	}

	if GetFail != s.Stats.GetFail {
		t.Fatalf("GetFail [%d] != Stats.GetFail [%d]", GetFail, s.Stats.GetFail)
	}

	if SetSuccess != s.Stats.SetSuccess {
		t.Fatalf("SetSuccess [%d] != Stats.SetSuccess [%d]", SetSuccess, s.Stats.SetSuccess)
	}

	if SetFail != s.Stats.SetFail {
		t.Fatalf("SetFail [%d] != Stats.SetFail [%d]", SetFail, s.Stats.SetFail)
	}

	if DeleteSuccess != s.Stats.DeleteSuccess {
		t.Fatalf("DeleteSuccess [%d] != Stats.DeleteSuccess [%d]", DeleteSuccess, s.Stats.DeleteSuccess)
	}

	if DeleteFail != s.Stats.DeleteFail {
		t.Fatalf("DeleteFail [%d] != Stats.DeleteFail [%d]", DeleteFail, s.Stats.DeleteFail)
	}

	if UpdateSuccess != s.Stats.UpdateSuccess {
		t.Fatalf("UpdateSuccess [%d] != Stats.UpdateSuccess [%d]", UpdateSuccess, s.Stats.UpdateSuccess)
	}

	if UpdateFail != s.Stats.UpdateFail {
		t.Fatalf("UpdateFail [%d] != Stats.UpdateFail [%d]", UpdateFail, s.Stats.UpdateFail)
	}

	if TestAndSetSuccess != s.Stats.TestAndSetSuccess {
		t.Fatalf("TestAndSetSuccess [%d] != Stats.TestAndSetSuccess [%d]", TestAndSetSuccess, s.Stats.TestAndSetSuccess)
	}

	if TestAndSetFail != s.Stats.TestAndSetFail {
		t.Fatalf("TestAndSetFail [%d] != Stats.TestAndSetFail [%d]", TestAndSetFail, s.Stats.TestAndSetFail)
	}

	s = New()
	SetSuccess = 0
	SetFail = 0

	for _, k := range keys {
		i++
		_, err := s.Create(k, "bar", time.Now().Add(time.Second*3), i, 1)
		if err != nil {
			SetFail++
		} else {
			SetSuccess++
		}
	}

	time.Sleep(6 * time.Second)

	ExpireCount := SetSuccess

	if ExpireCount != s.Stats.ExpireCount {
		t.Fatalf("ExpireCount [%d] != Stats.ExpireCount [%d]", ExpireCount, s.Stats.ExpireCount)
	}

}
<commit_msg>delete some debug comments in stats_test.go<commit_after>package store

import (
	"math/rand"
	"testing"
	"time"
)

func TestBasicStats(t *testing.T) {
	s := New()
	keys := GenKeys(rand.Intn(100), 5)

	var i uint64
	var GetSuccess, GetFail, SetSuccess, SetFail, DeleteSuccess, DeleteFail uint64
	var UpdateSuccess, UpdateFail, TestAndSetSuccess, TestAndSetFail, watcher_number uint64

	for _, k := range keys {
		i++
		_, err := s.Create(k, "bar", time.Now().Add(time.Second*time.Duration(rand.Intn(6))), i, 1)
		if err != nil {
			SetFail++
		} else {
			SetSuccess++
		}
	}

	time.Sleep(time.Second * 3)

	for _, k := range keys {
		_, err := s.Get(k, false, false, i, 1)
		if err != nil {
			GetFail++
		} else {
			GetSuccess++
		}
	}

	for _, k := range keys {
		i++
		_, err := s.Update(k, "foo", time.Now().Add(time.Second*time.Duration(rand.Intn(6))), i, 1)
		if err != nil {
			UpdateFail++
		} else {
			UpdateSuccess++
		}
	}

	time.Sleep(time.Second * 3)

	for _, k := range keys {
		_, err := s.Get(k, false, false, i, 1)
		if err != nil {
			GetFail++
		} else {
			GetSuccess++
		}
	}

	for _, k := range keys {
		i++
		_, err := s.TestAndSet(k, "foo", 0, "bar", Permanent, i, 1)
		if err != nil {
			TestAndSetFail++
		} else {
			TestAndSetSuccess++
		}
	}

	for _, k := range keys {
		s.Watch(k, false, 0, i, 1)
		watcher_number++
	}

	for _, k := range keys {
		_, err := s.Get(k, false, false, i, 1)
		if err != nil {
			GetFail++
		} else {
			GetSuccess++
		}
	}

	for _, k := range keys {
		i++
		_, err := s.Delete(k, false, i, 1)
		if err != nil {
			DeleteFail++
		} else {
			watcher_number--
			DeleteSuccess++
		}
	}

	for _, k := range keys {
		_, err := s.Get(k, false, false, i, 1)
		if err != nil {
			GetFail++
		} else {
			GetSuccess++
		}
	}

	if GetSuccess != s.Stats.GetSuccess {
		t.Fatalf("GetSuccess [%d] != Stats.GetSuccess [%d]", GetSuccess, s.Stats.GetSuccess)
	}

	if GetFail != s.Stats.GetFail {
		t.Fatalf("GetFail [%d] != Stats.GetFail [%d]", GetFail, s.Stats.GetFail)
	}

	if SetSuccess != s.Stats.SetSuccess {
		t.Fatalf("SetSuccess [%d] != Stats.SetSuccess [%d]", SetSuccess, s.Stats.SetSuccess)
	}

	if SetFail != s.Stats.SetFail {
		t.Fatalf("SetFail [%d] != Stats.SetFail [%d]", SetFail, s.Stats.SetFail)
	}

	if DeleteSuccess != s.Stats.DeleteSuccess {
		t.Fatalf("DeleteSuccess [%d] != Stats.DeleteSuccess [%d]", DeleteSuccess, s.Stats.DeleteSuccess)
	}

	if DeleteFail != s.Stats.DeleteFail {
		t.Fatalf("DeleteFail [%d] != Stats.DeleteFail [%d]", DeleteFail, s.Stats.DeleteFail)
	}

	if UpdateSuccess != s.Stats.UpdateSuccess {
		t.Fatalf("UpdateSuccess [%d] != Stats.UpdateSuccess [%d]", UpdateSuccess, s.Stats.UpdateSuccess)
	}

	if UpdateFail != s.Stats.UpdateFail {
		t.Fatalf("UpdateFail [%d] != Stats.UpdateFail [%d]", UpdateFail, s.Stats.UpdateFail)
	}

	if TestAndSetSuccess != s.Stats.TestAndSetSuccess {
		t.Fatalf("TestAndSetSuccess [%d] != Stats.TestAndSetSuccess [%d]", TestAndSetSuccess, s.Stats.TestAndSetSuccess)
	}

	if TestAndSetFail != s.Stats.TestAndSetFail {
		t.Fatalf("TestAndSetFail [%d] != Stats.TestAndSetFail [%d]", TestAndSetFail, s.Stats.TestAndSetFail)
	}

	s = New()
	SetSuccess = 0
	SetFail = 0

	for _, k := range keys {
		i++
		_, err := s.Create(k, "bar", time.Now().Add(time.Second*3), i, 1)
		if err != nil {
			SetFail++
		} else {
			SetSuccess++
		}
	}

	time.Sleep(6 * time.Second)

	ExpireCount := SetSuccess

	if ExpireCount != s.Stats.ExpireCount {
		t.Fatalf("ExpireCount [%d] != Stats.ExpireCount [%d]", ExpireCount, s.Stats.ExpireCount)
	}

}
<|endoftext|>


<commit_before>/*
 * This file is part of the KubeVirt project
 *
 * 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.
 *
 * Copyright 2019 Red Hat, Inc.
 *
 */
package rest

import (
	"net/http"

	"github.com/emicklei/go-restful"

	"k8s.io/client-go/tools/cache"

	"kubevirt.io/client-go/log"
	cmdclient "kubevirt.io/kubevirt/pkg/virt-handler/cmd-client"
)

type LifecycleHandler struct {
	vmiInformer  cache.SharedIndexInformer
	virtShareDir string
}

func NewLifecycleHandler(vmiInformer cache.SharedIndexInformer, virtShareDir string) *LifecycleHandler {
	return &LifecycleHandler{
		vmiInformer:  vmiInformer,
		virtShareDir: virtShareDir,
	}
}

func (lh *LifecycleHandler) PauseHandler(request *restful.Request, response *restful.Response) {
	vmi, code, err := getVMI(request, lh.vmiInformer)
	if err != nil {
		log.Log.Object(vmi).Reason(err).Error("Failed to retrieve VMI")
		response.WriteError(code, err)
		return
	}

	sockFile := cmdclient.SocketFromUID(lh.virtShareDir, string(vmi.GetUID()), true)
	client, err := cmdclient.NewClient(sockFile)
	if err != nil {
		log.Log.Object(vmi).Reason(err).Error("Failed to connect cmd client")
		response.WriteError(http.StatusInternalServerError, err)
	}

	err = client.PauseVirtualMachine(vmi)
	if err != nil {
		log.Log.Object(vmi).Reason(err).Error("Failed to pause VMI")
		response.WriteError(http.StatusInternalServerError, err)
	}

	response.WriteHeader(http.StatusAccepted)
}

func (lh *LifecycleHandler) UnpauseHandler(request *restful.Request, response *restful.Response) {
	vmi, code, err := getVMI(request, lh.vmiInformer)
	if err != nil {
		log.Log.Object(vmi).Reason(err).Error("Failed to retrieve VMI")
		response.WriteError(code, err)
		return
	}

	sockFile := cmdclient.SocketFromUID(lh.virtShareDir, string(vmi.GetUID()), true)
	client, err := cmdclient.NewClient(sockFile)
	if err != nil {
		log.Log.Object(vmi).Reason(err).Error("Failed to connect cmd client")
		response.WriteError(http.StatusInternalServerError, err)
	}

	err = client.UnpauseVirtualMachine(vmi)
	if err != nil {
		log.Log.Object(vmi).Reason(err).Error("Failed to unpause VMI")
		response.WriteError(http.StatusInternalServerError, err)
	}

	response.WriteHeader(http.StatusAccepted)
}

func (lh *LifecycleHandler) GetGuestInfo(request *restful.Request, response *restful.Response) {
	log.Log.Info("Retreiving guestinfo")
	vmi, code, err := getVMI(request, lh.vmiInformer)
	if err != nil {
		log.Log.Object(vmi).Reason(err).Error("Failed to retrieve VMI")
		response.WriteError(code, err)
		return
	}

	log.Log.Object(vmi).Infof("Retreiving guestinfo from %s", vmi.Name)

	sockFile := cmdclient.SocketFromUID(lh.virtShareDir, string(vmi.GetUID()), true)
	client, err := cmdclient.NewClient(sockFile)
	if err != nil {
		log.Log.Object(vmi).Reason(err).Error("Failed to connect cmd client")
		response.WriteError(http.StatusInternalServerError, err)
	}

	guestInfo, err := client.GetGuestInfo()
	if err != nil {
		log.Log.Object(vmi).Reason(err).Error("Failed to get guest info")
		response.WriteError(http.StatusInternalServerError, err)
	}

	log.Log.Object(vmi).Infof("returning guestinfo :%v", guestInfo)
	response.WriteEntity(guestInfo)
}

func (lh *LifecycleHandler) GetUsers(request *restful.Request, response *restful.Response) {
	vmi, code, err := getVMI(request, lh.vmiInformer)
	if err != nil {
		log.Log.Object(vmi).Reason(err).Error("Failed to retrieve VMI")
		response.WriteError(code, err)
		return
	}

	log.Log.Object(vmi).Infof("Retreiving userlist from %s", vmi.Name)

	sockFile := cmdclient.SocketFromUID(lh.virtShareDir, string(vmi.GetUID()))
	client, err := cmdclient.NewClient(sockFile)
	if err != nil {
		log.Log.Object(vmi).Reason(err).Error("Failed to connect cmd client")
		response.WriteError(http.StatusInternalServerError, err)
	}

	userList, err := client.GetUsers()
	if err != nil {
		log.Log.Object(vmi).Reason(err).Error("Failed to get user list")
		response.WriteError(http.StatusInternalServerError, err)
	}

	response.WriteEntity(userList)
}

func (lh *LifecycleHandler) GetFilesystems(request *restful.Request, response *restful.Response) {
	vmi, code, err := getVMI(request, lh.vmiInformer)
	if err != nil {
		log.Log.Object(vmi).Reason(err).Error("Failed to retrieve VMI")
		response.WriteError(code, err)
		return
	}

	log.Log.Object(vmi).Infof("Retreiving filesystem list from %s", vmi.Name)

	sockFile := cmdclient.SocketFromUID(lh.virtShareDir, string(vmi.GetUID()))
	client, err := cmdclient.NewClient(sockFile)
	if err != nil {
		log.Log.Object(vmi).Reason(err).Error("Failed to connect cmd client")
		response.WriteError(http.StatusInternalServerError, err)
	}

	fsList, err := client.GetFilesystems()
	if err != nil {
		log.Log.Object(vmi).Reason(err).Error("Failed to get guest info")
		response.WriteError(http.StatusInternalServerError, err)
	}

	response.WriteEntity(fsList)
}
<commit_msg>Fix lifecycle monitoring for new cmd socket location<commit_after>/*
 * This file is part of the KubeVirt project
 *
 * 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.
 *
 * Copyright 2019 Red Hat, Inc.
 *
 */
package rest

import (
	"net/http"

	"github.com/emicklei/go-restful"

	"k8s.io/client-go/tools/cache"

	"kubevirt.io/client-go/log"
	cmdclient "kubevirt.io/kubevirt/pkg/virt-handler/cmd-client"
)

type LifecycleHandler struct {
	vmiInformer  cache.SharedIndexInformer
	virtShareDir string
}

func NewLifecycleHandler(vmiInformer cache.SharedIndexInformer, virtShareDir string) *LifecycleHandler {
	return &LifecycleHandler{
		vmiInformer:  vmiInformer,
		virtShareDir: virtShareDir,
	}
}

func (lh *LifecycleHandler) PauseHandler(request *restful.Request, response *restful.Response) {
	vmi, code, err := getVMI(request, lh.vmiInformer)
	if err != nil {
		log.Log.Object(vmi).Reason(err).Error("Failed to retrieve VMI")
		response.WriteError(code, err)
		return
	}

	sockFile := cmdclient.SocketFromUID(lh.virtShareDir, string(vmi.GetUID()), true)
	client, err := cmdclient.NewClient(sockFile)
	if err != nil {
		log.Log.Object(vmi).Reason(err).Error("Failed to connect cmd client")
		response.WriteError(http.StatusInternalServerError, err)
	}

	err = client.PauseVirtualMachine(vmi)
	if err != nil {
		log.Log.Object(vmi).Reason(err).Error("Failed to pause VMI")
		response.WriteError(http.StatusInternalServerError, err)
	}

	response.WriteHeader(http.StatusAccepted)
}

func (lh *LifecycleHandler) UnpauseHandler(request *restful.Request, response *restful.Response) {
	vmi, code, err := getVMI(request, lh.vmiInformer)
	if err != nil {
		log.Log.Object(vmi).Reason(err).Error("Failed to retrieve VMI")
		response.WriteError(code, err)
		return
	}

	sockFile := cmdclient.SocketFromUID(lh.virtShareDir, string(vmi.GetUID()), true)
	client, err := cmdclient.NewClient(sockFile)
	if err != nil {
		log.Log.Object(vmi).Reason(err).Error("Failed to connect cmd client")
		response.WriteError(http.StatusInternalServerError, err)
	}

	err = client.UnpauseVirtualMachine(vmi)
	if err != nil {
		log.Log.Object(vmi).Reason(err).Error("Failed to unpause VMI")
		response.WriteError(http.StatusInternalServerError, err)
	}

	response.WriteHeader(http.StatusAccepted)
}

func (lh *LifecycleHandler) GetGuestInfo(request *restful.Request, response *restful.Response) {
	log.Log.Info("Retreiving guestinfo")
	vmi, code, err := getVMI(request, lh.vmiInformer)
	if err != nil {
		log.Log.Object(vmi).Reason(err).Error("Failed to retrieve VMI")
		response.WriteError(code, err)
		return
	}

	log.Log.Object(vmi).Infof("Retreiving guestinfo from %s", vmi.Name)

	sockFile := cmdclient.SocketFromUID(lh.virtShareDir, string(vmi.GetUID()), true)
	client, err := cmdclient.NewClient(sockFile)
	if err != nil {
		log.Log.Object(vmi).Reason(err).Error("Failed to connect cmd client")
		response.WriteError(http.StatusInternalServerError, err)
	}

	guestInfo, err := client.GetGuestInfo()
	if err != nil {
		log.Log.Object(vmi).Reason(err).Error("Failed to get guest info")
		response.WriteError(http.StatusInternalServerError, err)
	}

	log.Log.Object(vmi).Infof("returning guestinfo :%v", guestInfo)
	response.WriteEntity(guestInfo)
}

func (lh *LifecycleHandler) GetUsers(request *restful.Request, response *restful.Response) {
	vmi, code, err := getVMI(request, lh.vmiInformer)
	if err != nil {
		log.Log.Object(vmi).Reason(err).Error("Failed to retrieve VMI")
		response.WriteError(code, err)
		return
	}

	log.Log.Object(vmi).Infof("Retreiving userlist from %s", vmi.Name)

	sockFile := cmdclient.SocketFromUID(lh.virtShareDir, string(vmi.GetUID()), true)
	client, err := cmdclient.NewClient(sockFile)
	if err != nil {
		log.Log.Object(vmi).Reason(err).Error("Failed to connect cmd client")
		response.WriteError(http.StatusInternalServerError, err)
	}

	userList, err := client.GetUsers()
	if err != nil {
		log.Log.Object(vmi).Reason(err).Error("Failed to get user list")
		response.WriteError(http.StatusInternalServerError, err)
	}

	response.WriteEntity(userList)
}

func (lh *LifecycleHandler) GetFilesystems(request *restful.Request, response *restful.Response) {
	vmi, code, err := getVMI(request, lh.vmiInformer)
	if err != nil {
		log.Log.Object(vmi).Reason(err).Error("Failed to retrieve VMI")
		response.WriteError(code, err)
		return
	}

	log.Log.Object(vmi).Infof("Retreiving filesystem list from %s", vmi.Name)

	sockFile := cmdclient.SocketFromUID(lh.virtShareDir, string(vmi.GetUID()), true)
	client, err := cmdclient.NewClient(sockFile)
	if err != nil {
		log.Log.Object(vmi).Reason(err).Error("Failed to connect cmd client")
		response.WriteError(http.StatusInternalServerError, err)
	}

	fsList, err := client.GetFilesystems()
	if err != nil {
		log.Log.Object(vmi).Reason(err).Error("Failed to get guest info")
		response.WriteError(http.StatusInternalServerError, err)
	}

	response.WriteEntity(fsList)
}
<|endoftext|>


<commit_before>// Copyright 2013 ChaiShushan <chaishushan{AT}gmail.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package gettext

import (
	"io/ioutil"
	"strings"
	"testing"
)

var testZipData = func() []byte {
	if data, err := ioutil.ReadFile("./examples/local.zip"); err == nil {
		return data
	}
	return nil
}()

func TestGettext(t *testing.T) {
	Textdomain("hello")

	// local file system
	BindTextdomain("hello", "./examples/local", nil)
	testGettext(t, true)
	BindTextdomain("hello", "", nil)
	testGettext(t, false)

	// local zip file system
	BindTextdomain("hello", "./examples/local.zip", nil)
	testGettext(t, true)
	BindTextdomain("hello", "", nil)
	testGettext(t, false)

	// embedded zip file system
	BindTextdomain("hello", "local.zip", testZipData)
	testGettext(t, true)
	BindTextdomain("hello", "", nil)
	testGettext(t, false)
}

func TestGetdata(t *testing.T) {
	Textdomain("hello")

	// local file system
	BindTextdomain("hello", "./examples/local", nil)
	testGetdata(t, true)
	BindTextdomain("hello", "", nil)
	testGetdata(t, false)

	// local zip file system
	BindTextdomain("hello", "./examples/local.zip", nil)
	testGetdata(t, true)
	BindTextdomain("hello", "", nil)
	testGetdata(t, false)

	// embedded zip file system
	BindTextdomain("hello", "local.zip", testZipData)
	testGetdata(t, true)
	BindTextdomain("hello", "", nil)
	testGetdata(t, false)
}

func testGettext(t *testing.T, hasTransle bool) {
	for i, v := range testTexts {
		if lang := SetLocale(v.lang); lang != v.lang {
			t.Fatalf("%d: expect = %s, got = %v", i, v.lang, lang)
		}
		if hasTransle {
			if dst := PGettext(v.ctx, v.src); dst != v.dst {
				t.Fatalf("%d: expect = %q, got = %q", i, v.dst, dst)
			}
		} else {
			if dst := PGettext(v.ctx, v.src); dst != v.src {
				t.Fatalf("%d: expect = %s, got = %v", i, v.src, dst)
			}
		}
	}
}

func testGetdata(t *testing.T, hasTransle bool) {
	for i, v := range testResources {
		if lang := SetLocale(v.lang); lang != v.lang {
			t.Fatalf("%d: expect = %s, got = %v", i, v.lang, lang)
		}
		if hasTransle {
			v.data = strings.Replace(v.data, "\r", "", -1)
			data := strings.Replace(string(Getdata(v.path)), "\r", "", -1)
			if data != v.data {
				t.Fatalf("%d: expect = %q, got = %q", i, v.data, data)
			}
		} else {
			if data := string(Getdata(v.path)); data != "" {
				t.Fatalf("%d: expect = %s, got = %v", i, "", data)
			}
		}
	}
}

func BenchmarkGettext(b *testing.B) {
	SetLocale("zh_CN")
	BindTextdomain("hello", "./examples/local", nil)
	Textdomain("hello")

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		PGettext(testTexts[0].ctx, testTexts[0].src)
	}
}
func BenchmarkGettext_Zip(b *testing.B) {
	SetLocale("zh_CN")
	BindTextdomain("hello", "./examples/local.zip", nil)
	Textdomain("hello")

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		PGettext(testTexts[0].ctx, testTexts[0].src)
	}
}

func BenchmarkGetdata(b *testing.B) {
	SetLocale("zh_CN")
	BindTextdomain("hello", "./examples/local", nil)
	Textdomain("hello")

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		Getdata(testResources[0].path)
	}
}
func BenchmarkGetdata_Zip(b *testing.B) {
	SetLocale("zh_CN")
	BindTextdomain("hello", "./examples/local.zip", nil)
	Textdomain("hello")

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		Getdata(testResources[0].path)
	}
}

var testTexts = []struct {
	lang string
	ctx  string
	src  string
	dst  string
}{
	// default
	{"default", "main.init", "Gettext in init.", "Gettext in init."},
	{"default", "main.main", "Hello, world!", "Hello, world!"},
	{"default", "main.func", "Gettext in func.", "Gettext in func."},
	{"default", "github.com/chai2010/gettext-go/examples/hi.SayHi", "pkg hi: Hello, world!", "pkg hi: Hello, world!"},

	// zh_CN
	{"zh_CN", "main.init", "Gettext in init.", "Init函数中的Gettext."},
	{"zh_CN", "main.main", "Hello, world!", "你好, 世界!"},
	{"zh_CN", "main.func", "Gettext in func.", "闭包函数中的Gettext."},
	{"zh_CN", "github.com/chai2010/gettext-go/examples/hi.SayHi", "pkg hi: Hello, world!", "来自\"Hi\"包的问候: 你好, 世界!"},

	// zh_TW
	{"zh_TW", "main.init", "Gettext in init.", "Init函數中的Gettext."},
	{"zh_TW", "main.main", "Hello, world!", "你好, 世界!"},
	{"zh_TW", "main.func", "Gettext in func.", "閉包函數中的Gettext."},
	{"zh_TW", "github.com/chai2010/gettext-go/examples/hi.SayHi", "pkg hi: Hello, world!", "來自\"Hi\"包的問候: 你好, 世界!"},
}

var testResources = []struct {
	lang string
	path string
	data string
}{
	// default
	{
		"default",
		"poems.txt",
		`Drinking Alone Under the Moon
Li Bai

flowers among one jar liquor
alone carouse without mutual intimate

raise cup greet bright moon
facing shadow become three persons

moon since not free to-drink
shadow follow accompany my body

briefly accompany moon with shadow
go happy should avail-oneself-of spring

my song moon walk-to-and-fro irresolute
my dance shadow fragments disorderly

sober time together mix glad
drunk after each divide scatter

eternal connect without consciouness-of-self roam
mutual appointment remote cloud Milky-Way
`,
	},

	// zh_CN
	{
		"zh_CN",
		"poems.txt",
		`yuèxiàdúzhuó
月下独酌
lǐbái
李白

huājiānyīhújiǔ，dúzhuówúxiānɡqīn。
花间一壶酒，独酌无相亲。
jǔbēiyāomínɡyuè，duìyǐnɡchénɡsānrén。
举杯邀明月，对影成三人。
yuèjìbùjiěyǐn，yǐnɡtúsuíwǒshēn。
月既不解饮，影徒随我身。
zànbànyuèjiānɡyǐnɡ，xínɡlèxūjíchūn。
暂伴月将影，行乐须及春。
wǒɡēyuèpáihuái，wǒwǔyǐnɡlínɡluàn。
我歌月徘徊，我舞影零乱。
xǐnɡshítónɡjiāohuān，zuìhòuɡèfēnsàn。
醒时同交欢，醉后各分散。
yǒnɡjiéwúqínɡyóu，xiānɡqīmiǎoyúnhàn。
永结无情游，相期邈云汉。
`,
	},

	// zh_TW
	{
		"zh_TW",
		"poems.txt",
		`yuèxiàdúzhuó
月下獨酌
lǐbái
李白

huājiānyīhújiǔ，dúzhuówúxiānɡqīn。
花間一壺酒，獨酌無相親。
jǔbēiyāomínɡyuè，duìyǐnɡchénɡsānrén。
舉杯邀明月，對影成三人。
yuèjìbùjiěyǐn，yǐnɡtúsuíwǒshēn。
月既不解飲，影徒隨我身。
zànbànyuèjiānɡyǐnɡ，xínɡlèxūjíchūn。
暫伴月將影，行樂須及春。
wǒɡēyuèpáihuái，wǒwǔyǐnɡlínɡluàn。
我歌月徘徊，我舞影零亂。
xǐnɡshítónɡjiāohuān，zuìhòuɡèfēnsàn。
醒時同交歡，醉後各分散。
yǒnɡjiéwúqínɡyóu，xiānɡqīmiǎoyúnhàn。
永結無情遊，相期邈雲漢。
`,
	},
}
<commit_msg>fix go test<commit_after>// Copyright 2013 ChaiShushan <chaishushan{AT}gmail.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package gettext

import (
	"io/ioutil"
	"strings"
	"testing"
)

var testZipData = func() []byte {
	if data, err := ioutil.ReadFile("./examples/local.zip"); err == nil {
		return data
	}
	return nil
}()

func TestGettext(t *testing.T) {
	Textdomain("hello")

	// local file system
	BindTextdomain("hello", "./examples/local", nil)
	testGettext(t, true)
	BindTextdomain("hello", "", nil)
	testGettext(t, false)

	// local zip file system
	BindTextdomain("hello", "./examples/local.zip", nil)
	testGettext(t, true)
	BindTextdomain("hello", "", nil)
	testGettext(t, false)

	// embedded zip file system
	BindTextdomain("hello", "local.zip", testZipData)
	testGettext(t, true)
	BindTextdomain("hello", "", nil)
	testGettext(t, false)
}

func TestGetdata(t *testing.T) {
	Textdomain("hello")

	// local file system
	BindTextdomain("hello", "./examples/local", nil)
	testGetdata(t, true)
	BindTextdomain("hello", "", nil)
	testGetdata(t, false)

	// local zip file system
	BindTextdomain("hello", "./examples/local.zip", nil)
	testGetdata(t, true)
	BindTextdomain("hello", "", nil)
	testGetdata(t, false)

	// embedded zip file system
	BindTextdomain("hello", "local.zip", testZipData)
	testGetdata(t, true)
	BindTextdomain("hello", "", nil)
	testGetdata(t, false)
}

func testGettext(t *testing.T, hasTransle bool) {
	for i, v := range testTexts {
		if lang := SetLocale(v.lang); lang != v.lang {
			t.Fatalf("%d: expect = %s, got = %v", i, v.lang, lang)
		}
		if hasTransle {
			if dst := PGettext(v.ctx, v.src); dst != v.dst {
				t.Fatalf("%d: expect = %q, got = %q", i, v.dst, dst)
			}
		} else {
			if dst := PGettext(v.ctx, v.src); dst != v.src {
				t.Fatalf("%d: expect = %s, got = %v", i, v.src, dst)
			}
		}
	}
}

func testGetdata(t *testing.T, hasTransle bool) {
	for i, v := range testResources {
		if lang := SetLocale(v.lang); lang != v.lang {
			t.Fatalf("%d: expect = %s, got = %v", i, v.lang, lang)
		}
		if hasTransle {
			v.data = strings.Replace(v.data, "\r", "", -1)
			data := strings.Replace(string(Getdata(v.path)), "\r", "", -1)
			if data != v.data {
				t.Fatalf("%d: expect = %q, got = %q", i, v.data, data)
			}
		} else {
			if data := string(Getdata(v.path)); data != "" {
				t.Fatalf("%d: expect = %s, got = %v", i, "", data)
			}
		}
	}
}

func BenchmarkGettext(b *testing.B) {
	SetLocale("zh_CN")
	BindTextdomain("hello", "./examples/local", nil)
	Textdomain("hello")

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		PGettext(testTexts[0].ctx, testTexts[0].src)
	}
}
func BenchmarkGettext_Zip(b *testing.B) {
	SetLocale("zh_CN")
	BindTextdomain("hello", "./examples/local.zip", nil)
	Textdomain("hello")

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		PGettext(testTexts[0].ctx, testTexts[0].src)
	}
}

func BenchmarkGetdata(b *testing.B) {
	SetLocale("zh_CN")
	BindTextdomain("hello", "./examples/local", nil)
	Textdomain("hello")

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		Getdata(testResources[0].path)
	}
}
func BenchmarkGetdata_Zip(b *testing.B) {
	SetLocale("zh_CN")
	BindTextdomain("hello", "./examples/local.zip", nil)
	Textdomain("hello")

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		Getdata(testResources[0].path)
	}
}

var testTexts = []struct {
	lang string
	ctx  string
	src  string
	dst  string
}{
	// default
	{"default", "main.init", "Gettext in init.", "Gettext in init."},
	{"default", "main.main", "Hello, world!", "Hello, world!"},
	{"default", "main.func", "Gettext in func.", "Gettext in func."},
	{"default", "github.com/chai2010/gettext-go/examples/hi.SayHi", "pkg hi: Hello, world!", "pkg hi: Hello, world!"},

	// zh_CN
	{"zh_CN", "main.init", "Gettext in init.", "Init函数中的Gettext.(ctx:main.init)"},
	{"zh_CN", "main.main", "Hello, world!", "你好, 世界!(ctx:main.main)"},
	{"zh_CN", "main.func", "Gettext in func.", "闭包函数中的Gettext.(ctx:main.func)"},
	{"zh_CN", "code.google.com/p/gettext-go/examples/hi.SayHi", "pkg hi: Hello, world!", "来自\"Hi\"包的问候: 你好, 世界!(ctx:code.google.com/p/gettext-go/examples/hi.SayHi)"},

	// zh_TW
	{"zh_TW", "main.init", "Gettext in init.", "Init函數中的Gettext.(ctx:main.init)"},
	{"zh_TW", "main.main", "Hello, world!", "你好, 世界!(ctx:main.main)"},
	{"zh_TW", "main.func", "Gettext in func.", "閉包函數中的Gettext.(ctx:main.func)"},
	{"zh_TW", "code.google.com/p/gettext-go/examples/hi.SayHi", "pkg hi: Hello, world!", "來自\"Hi\"包的問候: 你好, 世界!(ctx:code.google.com/p/gettext-go/examples/hi.SayHi)"},
}

var testResources = []struct {
	lang string
	path string
	data string
}{
	// default
	{
		"default",
		"poems.txt",
		`Drinking Alone Under the Moon
Li Bai

flowers among one jar liquor
alone carouse without mutual intimate

raise cup greet bright moon
facing shadow become three persons

moon since not free to-drink
shadow follow accompany my body

briefly accompany moon with shadow
go happy should avail-oneself-of spring

my song moon walk-to-and-fro irresolute
my dance shadow fragments disorderly

sober time together mix glad
drunk after each divide scatter

eternal connect without consciouness-of-self roam
mutual appointment remote cloud Milky-Way
`,
	},

	// zh_CN
	{
		"zh_CN",
		"poems.txt",
		`yuèxiàdúzhuó
月下独酌
lǐbái
李白

huājiānyīhújiǔ，dúzhuówúxiānɡqīn。
花间一壶酒，独酌无相亲。
jǔbēiyāomínɡyuè，duìyǐnɡchénɡsānrén。
举杯邀明月，对影成三人。
yuèjìbùjiěyǐn，yǐnɡtúsuíwǒshēn。
月既不解饮，影徒随我身。
zànbànyuèjiānɡyǐnɡ，xínɡlèxūjíchūn。
暂伴月将影，行乐须及春。
wǒɡēyuèpáihuái，wǒwǔyǐnɡlínɡluàn。
我歌月徘徊，我舞影零乱。
xǐnɡshítónɡjiāohuān，zuìhòuɡèfēnsàn。
醒时同交欢，醉后各分散。
yǒnɡjiéwúqínɡyóu，xiānɡqīmiǎoyúnhàn。
永结无情游，相期邈云汉。
`,
	},

	// zh_TW
	{
		"zh_TW",
		"poems.txt",
		`yuèxiàdúzhuó
月下獨酌
lǐbái
李白

huājiānyīhújiǔ，dúzhuówúxiānɡqīn。
花間一壺酒，獨酌無相親。
jǔbēiyāomínɡyuè，duìyǐnɡchénɡsānrén。
舉杯邀明月，對影成三人。
yuèjìbùjiěyǐn，yǐnɡtúsuíwǒshēn。
月既不解飲，影徒隨我身。
zànbànyuèjiānɡyǐnɡ，xínɡlèxūjíchūn。
暫伴月將影，行樂須及春。
wǒɡēyuèpáihuái，wǒwǔyǐnɡlínɡluàn。
我歌月徘徊，我舞影零亂。
xǐnɡshítónɡjiāohuān，zuìhòuɡèfēnsàn。
醒時同交歡，醉後各分散。
yǒnɡjiéwúqínɡyóu，xiānɡqīmiǎoyúnhàn。
永結無情遊，相期邈雲漢。
`,
	},
}
<|endoftext|>


<commit_before>// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Contributor: Aaron Meihm ameihm@mozilla.com [:alm]
package pkg

import (
	"bytes"
	"compress/gzip"
	"encoding/base64"
	"encoding/json"
	"fmt"
	ovallib "github.com/ameihm0912/mozoval/go/src/oval"
	"io"
	"io/ioutil"
	"mig/modules"
	"time"
)

var stats Statistics

// Various counters used to populate module statistics at the end of the
// run.
var counters struct {
	startTime time.Time
}

func startCounters() {
	counters.startTime = time.Now()
}

func endCounters() {
	stats.OvalRuntime = time.Now().Sub(counters.startTime)
}

func init() {
	modules.Register("pkg", func() interface{} {
		return new(Runner)
	})
}

type Runner struct {
	Parameters Parameters
	Results    modules.Result
}

func (r Runner) Run(in io.Reader) (resStr string) {
	defer func() {
		if e := recover(); e != nil {
			// return error in json
			r.Results.Errors = append(r.Results.Errors, fmt.Sprintf("%v", e))
			r.Results.Success = false
			endCounters()
			r.Results.Statistics = stats
			err, _ := json.Marshal(r.Results)
			resStr = string(err)
			return
		}
	}()

	startCounters()

	// Read module parameters from stdin
	err := modules.ReadInputParameters(in, &r.Parameters)
	if err != nil {
		panic(err)
	}

	err = r.ValidateParameters()
	if err != nil {
		panic(err)
	}

	ovallib.Init()
	ovallib.SetMaxChecks(r.Parameters.MaxConcurrentEval)

	e := &elements{}

	if len(r.Parameters.PkgMatch.Matches) > 0 {
		oresp := ovallib.PackageQuery(r.Parameters.PkgMatch.Matches)
		for _, x := range oresp {
			npi := &PkgInfo{PkgName: x.Name, PkgVersion: x.Version, PkgType: x.PkgType}
			e.Matches = append(e.Matches, *npi)
		}

		r.Results.Success = true
		if len(e.Matches) > 0 {
			r.Results.FoundAnything = true
		}
		r.Results.Elements = e
		endCounters()
		r.Results.Statistics = stats
		buf, err := json.Marshal(r.Results)
		if err != nil {
			panic(err)
		}
		resStr = string(buf)
		return
	} else if r.Parameters.OvalDef != "" {
		b := bytes.NewBufferString(r.Parameters.OvalDef)
		decoder := base64.NewDecoder(base64.StdEncoding, b)
		gz, err := gzip.NewReader(decoder)
		if err != nil {
			panic(err)
		}
		ovalbuf, err := ioutil.ReadAll(gz)
		if err != nil {
			panic(err)
		}

		od, err := ovallib.ParseBuffer(string(ovalbuf))
		if err != nil {
			panic(err)
		}
		ovalresults, err := ovallib.Execute(od)
		if err != nil {
			panic(err)
		}
		for _, x := range ovalresults {
			if !r.Parameters.IncludeFalse {
				if x.Status == ovallib.RESULT_FALSE {
					continue
				}
			}
			nmor := &MOResult{}
			nmor.Title = x.Title
			nmor.Status = x.StatusString()
			nmor.ID = x.ID
			e.OvalResults = append(e.OvalResults, *nmor)
		}

		r.Results.Success = true
		if len(e.OvalResults) > 0 {
			r.Results.FoundAnything = true
		}
		r.Results.Elements = e
		endCounters()
		r.Results.Statistics = stats
		buf, err := json.Marshal(r.Results)
		if err != nil {
			panic(err)
		}
		resStr = string(buf)
		return
	}

	panic("no function specified")
	return
}

func (r Runner) ValidateParameters() (err error) {
	if r.Parameters.MaxConcurrentEval <= 0 || r.Parameters.MaxConcurrentEval > 10 {
		return fmt.Errorf("concurrent evaluation must be between > 0 and <= 10")
	}
	return
}

func (r Runner) PrintResults(result modules.Result, foundOnly bool) (prints []string, err error) {
	var elem elements

	err = result.GetElements(&elem)
	if err != nil {
		panic(err)
	}

	for _, x := range elem.Matches {
		resStr := fmt.Sprintf("pkgmatch name=%v version=%v type=%v", x.PkgName, x.PkgVersion, x.PkgType)
		prints = append(prints, resStr)
	}

	for _, x := range elem.OvalResults {
		resStr := fmt.Sprintf("ovalresult id=%v title=\"%v\" outcome=%v", x.ID, x.Title, x.Status)
		prints = append(prints, resStr)
	}

	return
}

type elements struct {
	// In package match mode, the packages the agent has found that match
	// the query parameters.
	Matches []PkgInfo `json:"matches"`

	// Results of OVAL definition checks in OVAL mode
	OvalResults []MOResult `json:"ovalresults"`
}

type MOResult struct {
	Title  string `json:"title"`
	ID     string `json:"id"`
	Status string `json:"status"`
}

type PkgInfo struct {
	PkgName    string `json:"name"`
	PkgVersion string `json:"version"`
	PkgType    string `json:"type"`
}

type Statistics struct {
	OvalRuntime time.Duration `json:"ovalruntime"`
}

type Parameters struct {
	// Package match mode, contains a list of strings to use as substring
	// matches
	PkgMatch PkgMatch `json:"pkgmatch"`

	// A compressed, base64 encoded OVAL definition file for processing
	// using OVAL library on agent.
	OvalDef string `json:"ovaldef"`

	// Concurrent checks to run on agent
	MaxConcurrentEval int `json:"maxconneval"`

	// Include false results for checks
	IncludeFalse bool `json:"includefalse"`
}

type PkgMatch struct {
	Matches []string `json:"matches"`
}

func newParameters() *Parameters {
	return &Parameters{}
}
<commit_msg>[minor] update import for new location, remove call to Init<commit_after>// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Contributor: Aaron Meihm ameihm@mozilla.com [:alm]
package pkg

import (
	"bytes"
	"compress/gzip"
	"encoding/base64"
	"encoding/json"
	"fmt"
	ovallib "github.com/mozilla/mozoval/go/src/oval"
	"io"
	"io/ioutil"
	"mig/modules"
	"time"
)

var stats Statistics

// Various counters used to populate module statistics at the end of the
// run.
var counters struct {
	startTime time.Time
}

func startCounters() {
	counters.startTime = time.Now()
}

func endCounters() {
	stats.OvalRuntime = time.Now().Sub(counters.startTime)
}

func init() {
	modules.Register("pkg", func() interface{} {
		return new(Runner)
	})
}

type Runner struct {
	Parameters Parameters
	Results    modules.Result
}

func (r Runner) Run(in io.Reader) (resStr string) {
	defer func() {
		if e := recover(); e != nil {
			// return error in json
			r.Results.Errors = append(r.Results.Errors, fmt.Sprintf("%v", e))
			r.Results.Success = false
			endCounters()
			r.Results.Statistics = stats
			err, _ := json.Marshal(r.Results)
			resStr = string(err)
			return
		}
	}()

	startCounters()

	// Read module parameters from stdin
	err := modules.ReadInputParameters(in, &r.Parameters)
	if err != nil {
		panic(err)
	}

	err = r.ValidateParameters()
	if err != nil {
		panic(err)
	}

	ovallib.SetMaxChecks(r.Parameters.MaxConcurrentEval)

	e := &elements{}

	if len(r.Parameters.PkgMatch.Matches) > 0 {
		oresp := ovallib.PackageQuery(r.Parameters.PkgMatch.Matches)
		for _, x := range oresp {
			npi := &PkgInfo{PkgName: x.Name, PkgVersion: x.Version, PkgType: x.PkgType}
			e.Matches = append(e.Matches, *npi)
		}

		r.Results.Success = true
		if len(e.Matches) > 0 {
			r.Results.FoundAnything = true
		}
		r.Results.Elements = e
		endCounters()
		r.Results.Statistics = stats
		buf, err := json.Marshal(r.Results)
		if err != nil {
			panic(err)
		}
		resStr = string(buf)
		return
	} else if r.Parameters.OvalDef != "" {
		b := bytes.NewBufferString(r.Parameters.OvalDef)
		decoder := base64.NewDecoder(base64.StdEncoding, b)
		gz, err := gzip.NewReader(decoder)
		if err != nil {
			panic(err)
		}
		ovalbuf, err := ioutil.ReadAll(gz)
		if err != nil {
			panic(err)
		}

		od, err := ovallib.ParseBuffer(string(ovalbuf))
		if err != nil {
			panic(err)
		}
		ovalresults, err := ovallib.Execute(od)
		if err != nil {
			panic(err)
		}
		for _, x := range ovalresults {
			if !r.Parameters.IncludeFalse {
				if x.Status == ovallib.RESULT_FALSE {
					continue
				}
			}
			nmor := &MOResult{}
			nmor.Title = x.Title
			nmor.Status = x.StatusString()
			nmor.ID = x.ID
			e.OvalResults = append(e.OvalResults, *nmor)
		}

		r.Results.Success = true
		if len(e.OvalResults) > 0 {
			r.Results.FoundAnything = true
		}
		r.Results.Elements = e
		endCounters()
		r.Results.Statistics = stats
		buf, err := json.Marshal(r.Results)
		if err != nil {
			panic(err)
		}
		resStr = string(buf)
		return
	}

	panic("no function specified")
	return
}

func (r Runner) ValidateParameters() (err error) {
	if r.Parameters.MaxConcurrentEval <= 0 || r.Parameters.MaxConcurrentEval > 10 {
		return fmt.Errorf("concurrent evaluation must be between > 0 and <= 10")
	}
	return
}

func (r Runner) PrintResults(result modules.Result, foundOnly bool) (prints []string, err error) {
	var elem elements

	err = result.GetElements(&elem)
	if err != nil {
		panic(err)
	}

	for _, x := range elem.Matches {
		resStr := fmt.Sprintf("pkgmatch name=%v version=%v type=%v", x.PkgName, x.PkgVersion, x.PkgType)
		prints = append(prints, resStr)
	}

	for _, x := range elem.OvalResults {
		resStr := fmt.Sprintf("ovalresult id=%v title=\"%v\" outcome=%v", x.ID, x.Title, x.Status)
		prints = append(prints, resStr)
	}

	return
}

type elements struct {
	// In package match mode, the packages the agent has found that match
	// the query parameters.
	Matches []PkgInfo `json:"matches"`

	// Results of OVAL definition checks in OVAL mode
	OvalResults []MOResult `json:"ovalresults"`
}

type MOResult struct {
	Title  string `json:"title"`
	ID     string `json:"id"`
	Status string `json:"status"`
}

type PkgInfo struct {
	PkgName    string `json:"name"`
	PkgVersion string `json:"version"`
	PkgType    string `json:"type"`
}

type Statistics struct {
	OvalRuntime time.Duration `json:"ovalruntime"`
}

type Parameters struct {
	// Package match mode, contains a list of strings to use as substring
	// matches
	PkgMatch PkgMatch `json:"pkgmatch"`

	// A compressed, base64 encoded OVAL definition file for processing
	// using OVAL library on agent.
	OvalDef string `json:"ovaldef"`

	// Concurrent checks to run on agent
	MaxConcurrentEval int `json:"maxconneval"`

	// Include false results for checks
	IncludeFalse bool `json:"includefalse"`
}

type PkgMatch struct {
	Matches []string `json:"matches"`
}

func newParameters() *Parameters {
	return &Parameters{}
}
<|endoftext|>


<commit_before>package glfw

import (
	"bytes"
	"github.com/ajhager/rog"
	"github.com/banthar/gl"
	"github.com/jteeuwen/glfw"
	"image"
	"image/draw"
	_ "image/png"
)

func init() {
	rog.SetBackend(new(glfwBackend))
}

var (
    vs = []float32{0, 0, 0, 0, 0, 0, 0, 0}
    cs = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
    ts = []float32{0, 0, 0, 0, 0, 0, 0, 0}
)

type glfwBackend struct {
	open  bool
	mouse *rog.MouseData
	key   int
	font  image.Image
    s, t float32
	width, height, zoom int
    verts []float32
}

func (w *glfwBackend) Open(width, height, zoom int) {
	if err := glfw.Init(); err != nil {
		panic(err)
	}

	w.zoom = zoom
    w.width = width
    w.height = height

	glfw.OpenWindowHint(glfw.WindowNoResize, gl.TRUE)
	err := glfw.OpenWindow(width*16*zoom, height*16*zoom, 8, 8, 8, 8, 0, 0, glfw.Windowed)
	if err != nil {
		panic(err)
	}

	glfw.SetWindowCloseCallback(func() int { w.Close(); return 0 })
	glfw.SetKeyCallback(func(key, state int) { w.setKey(key, state) })
	glfw.Enable(glfw.KeyRepeat)

	w.mouse = new(rog.MouseData)
	glfw.SetMousePosCallback(func(x, y int) { w.mouseMove(x, y) })
	glfw.SetMouseButtonCallback(func(but, state int) { w.mousePress(but, state) })

	font, _, err := image.Decode(bytes.NewBuffer(rog.FontData()))
	if err != nil {
		panic(err)
	}
	w.font = font
    w.s = 16 / float32(font.Bounds().Max.X)
    w.t = 16 / float32(font.Bounds().Max.Y)

	fc := float32(16 * zoom)

	for y := 0; y < height; y++ {
	    for x := 0; x < width; x++ {
	        cx := float32(x) * fc
	        cy := float32(y) * fc
	        w.verts = append(w.verts, cx, cy, cx, cy + fc, cx + fc, cy + fc, cx + fc, cy)
		}
	}

	glInit(width*16*zoom, height*16*zoom, w.font)

	w.open = true
}

func (w *glfwBackend) IsOpen() bool {
	return w.open && glfw.WindowParam(glfw.Opened) == 1
}

func (w *glfwBackend) Close() {
	w.open = false
	glfw.CloseWindow()
	glfw.Terminate()
}

func (w *glfwBackend) Name(title string) {
	glfw.SetWindowTitle(title)
}

func (w *glfwBackend) Render(console *rog.Console) {
	if w.IsOpen() {
		w.mouse.Left.Released = false
		w.mouse.Right.Released = false
		w.mouse.Middle.Released = false
		w.key = -1

		gl.Clear(gl.COLOR_BUFFER_BIT)

		for y := 0; y < console.Height(); y++ {
			for x := 0; x < console.Width(); x++ {
				fg, bg, ch := console.Get(x, y)
				w.letter(x, y, 0, bg)

				if ch != 0 && ch != 32 {
					w.letter(x, y, ch, fg)
				}
			}
		}

		glfw.SwapBuffers()
	}
}

func (w *glfwBackend) Mouse() *rog.MouseData {
	return w.mouse
}

func (w *glfwBackend) mouseMove(x, y int) {
	w.mouse.Pos.X = x
	w.mouse.Pos.Y = y
	w.mouse.Cell.X = x / (16 * w.zoom)
	w.mouse.Cell.Y = y / (16 * w.zoom)
}

func (w *glfwBackend) mousePress(button, state int) {

	switch state {
	case glfw.KeyPress:
		switch button {
		case glfw.MouseLeft:
			w.mouse.Left.Pressed = true
		case glfw.MouseRight:
			w.mouse.Right.Pressed = true
		case glfw.MouseMiddle:
			w.mouse.Middle.Pressed = true
		}
	case glfw.KeyRelease:
		switch button {
		case glfw.MouseLeft:
			w.mouse.Left.Pressed = false
			w.mouse.Left.Released = true
		case glfw.MouseRight:
			w.mouse.Right.Pressed = false
			w.mouse.Right.Released = true
		case glfw.MouseMiddle:
			w.mouse.Middle.Pressed = false
			w.mouse.Middle.Released = true
		}
	}
}

func (w *glfwBackend) Key() int {
	return w.key
}

func (w *glfwBackend) setKey(key, state int) {
	if state == glfw.KeyPress {
		rogKey, exists := glfwToRogKey[key]
		if exists {
			w.key = rogKey
		}

		if key < 256 {
			w.key = key
		}
	}
}

func glInit(width, height int, font image.Image) {
	gl.Init()
	gl.Enable(gl.TEXTURE_2D)
	gl.PixelStorei(gl.UNPACK_ALIGNMENT, 1)
	gl.Viewport(0, 0, width, height)
	gl.MatrixMode(gl.PROJECTION)
	gl.LoadIdentity()
	gl.Ortho(0, float64(width), float64(height), 0, -1, 1)
	gl.MatrixMode(gl.MODELVIEW)
	gl.LoadIdentity()
	gl.Enable(gl.BLEND)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
	textures := make([]gl.Texture, 1)
	gl.GenTextures(textures)
	textures[0].Bind(gl.TEXTURE_2D)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)

	b := font.Bounds()
	m := image.NewRGBA(image.Rect(0, 0, b.Dx(), b.Dy()))
	draw.Draw(m, m.Bounds(), font, b.Min, draw.Src)

	gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, m.Bounds().Max.X, m.Bounds().Max.Y, 0, gl.RGBA, gl.UNSIGNED_BYTE, m.Pix)
	gl.EnableClientState(gl.VERTEX_ARRAY)
	gl.EnableClientState(gl.COLOR_ARRAY)
	gl.EnableClientState(gl.TEXTURE_COORD_ARRAY)
	gl.VertexPointer(2, 0, vs)
	gl.ColorPointer(3, 0, cs)
	gl.TexCoordPointer(2, 0, ts)
}

// Draw a letter at a certain coordinate
func (w *glfwBackend) letter(lx, ly int, c rune, cl rog.RGB) {
    start := 8 * (ly*w.width+lx)

    vs[0] = w.verts[start]
    vs[1] = w.verts[start+1]
    vs[2] = w.verts[start+2]
    vs[3] = w.verts[start+3]
    vs[4] = w.verts[start+4]
    vs[5] = w.verts[start+5]
    vs[6] = w.verts[start+6]
    vs[7] = w.verts[start+7]

    cs[0] = cl.R
    cs[1] = cl.G
    cs[2] = cl.B
    cs[3] = cl.R
    cs[4] = cl.G
    cs[5] = cl.B
    cs[6] = cl.R
    cs[7] = cl.G
    cs[8] = cl.B
    cs[9] = cl.R
    cs[10] = cl.G
    cs[11] = cl.B

	u := float32(c % 256) * w.s
	v := float32(c / 256) * w.t
    ts[0] = u
    ts[1] = v
    ts[2] = u
    ts[3] = v+w.t
    ts[4] = u+w.s
    ts[5] = v+w.t
    ts[6] = u+w.s
    ts[7] = v

	gl.DrawArrays(gl.POLYGON, 0, 4)
}

var glfwToRogKey = map[int]int{
	glfw.KeyBackspace:  rog.Backspace,
	glfw.KeyTab:        rog.Tab,
	glfw.KeyEsc:        rog.Escape,
	glfw.KeySpace:      rog.Space,
	glfw.KeyDel:        rog.Delete,
	glfw.KeyLsuper:     rog.LSuper,
	glfw.KeyRsuper:     rog.RSuper,
	glfw.KeyLshift:     rog.LShift,
	glfw.KeyRshift:     rog.RShift,
	glfw.KeyLctrl:      rog.LControl,
	glfw.KeyRctrl:      rog.RControl,
	glfw.KeyLalt:       rog.LAlt,
	glfw.KeyRalt:       rog.RAlt,
	glfw.KeyF1:         rog.F1,
	glfw.KeyF2:         rog.F2,
	glfw.KeyF3:         rog.F3,
	glfw.KeyF4:         rog.F4,
	glfw.KeyF5:         rog.F5,
	glfw.KeyF6:         rog.F6,
	glfw.KeyF7:         rog.F7,
	glfw.KeyF8:         rog.F8,
	glfw.KeyF9:         rog.F9,
	glfw.KeyF10:        rog.F10,
	glfw.KeyF11:        rog.F11,
	glfw.KeyF12:        rog.F12,
	glfw.KeyF13:        rog.F13,
	glfw.KeyF14:        rog.F14,
	glfw.KeyF15:        rog.F15,
	glfw.KeyF16:        rog.F16,
	glfw.KeyUp:         rog.Up,
	glfw.KeyDown:       rog.Down,
	glfw.KeyLeft:       rog.Left,
	glfw.KeyRight:      rog.Right,
	glfw.KeyEnter:      rog.Return,
	glfw.KeyInsert:     rog.Insert,
	glfw.KeyHome:       rog.Home,
	glfw.KeyEnd:        rog.End,
	glfw.KeyCapslock:   rog.Capslock,
	glfw.KeyKPDivide:  rog.KPDivide,
	glfw.KeyKPMultiply: rog.KPMultiply,
	glfw.KeyKPSubtract: rog.KPSubtract,
	glfw.KeyKPAdd:      rog.KPAdd,
	glfw.KeyKPDecimal:  rog.KPDecimal,
	glfw.KeyKPEqual:    rog.KPEqual,
	glfw.KeyKPEnter:    rog.KPEnter,
	glfw.KeyKPNumlock:  rog.KPNumlock,
	glfw.KeyKP0:        rog.KP0,
	glfw.KeyKP1:        rog.KP1,
	glfw.KeyKP2:        rog.KP2,
	glfw.KeyKP3:        rog.KP3,
	glfw.KeyKP4:        rog.KP4,
	glfw.KeyKP5:        rog.KP5,
	glfw.KeyKP6:        rog.KP6,
	glfw.KeyKP7:        rog.KP7,
	glfw.KeyKP8:        rog.KP8,
	glfw.KeyKP9:        rog.KP9}
<commit_msg>Decrease load time by 10%<commit_after>package glfw

import (
	"bytes"
	"github.com/ajhager/rog"
	"github.com/banthar/gl"
	"github.com/jteeuwen/glfw"
	"image"
	_ "image/png"
)

func init() {
	rog.SetBackend(new(glfwBackend))
}

var (
    vs = []float32{0, 0, 0, 0, 0, 0, 0, 0}
    cs = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
    ts = []float32{0, 0, 0, 0, 0, 0, 0, 0}
)

type glfwBackend struct {
	open  bool
	mouse *rog.MouseData
	key   int
	font  image.Image
    s, t float32
	width, height, zoom int
    verts []float32
}

func (w *glfwBackend) Open(width, height, zoom int) {
	if err := glfw.Init(); err != nil {
		panic(err)
	}

	w.zoom = zoom
    w.width = width
    w.height = height

	glfw.OpenWindowHint(glfw.WindowNoResize, gl.TRUE)
	err := glfw.OpenWindow(width*16*zoom, height*16*zoom, 8, 8, 8, 8, 0, 0, glfw.Windowed)
	if err != nil {
		panic(err)
	}

	glfw.SetWindowCloseCallback(func() int { w.Close(); return 0 })
	glfw.SetKeyCallback(func(key, state int) { w.setKey(key, state) })
	glfw.Enable(glfw.KeyRepeat)

	w.mouse = new(rog.MouseData)
	glfw.SetMousePosCallback(func(x, y int) { w.mouseMove(x, y) })
	glfw.SetMouseButtonCallback(func(but, state int) { w.mousePress(but, state) })

	font, _, err := image.Decode(bytes.NewBuffer(rog.FontData()))
	if err != nil {
		panic(err)
	}
	w.font = font
    w.s = 16 / float32(font.Bounds().Max.X)
    w.t = 16 / float32(font.Bounds().Max.Y)

	fc := float32(16 * zoom)

	for y := 0; y < height; y++ {
	    for x := 0; x < width; x++ {
	        cx := float32(x) * fc
	        cy := float32(y) * fc
	        w.verts = append(w.verts, cx, cy, cx, cy + fc, cx + fc, cy + fc, cx + fc, cy)
		}
	}

	glInit(width*16*zoom, height*16*zoom, w.font)

	w.open = true
}

func (w *glfwBackend) IsOpen() bool {
	return w.open && glfw.WindowParam(glfw.Opened) == 1
}

func (w *glfwBackend) Close() {
	w.open = false
	glfw.CloseWindow()
	glfw.Terminate()
}

func (w *glfwBackend) Name(title string) {
	glfw.SetWindowTitle(title)
}

func (w *glfwBackend) Render(console *rog.Console) {
	if w.IsOpen() {
		w.mouse.Left.Released = false
		w.mouse.Right.Released = false
		w.mouse.Middle.Released = false
		w.key = -1

		gl.Clear(gl.COLOR_BUFFER_BIT)

		for y := 0; y < console.Height(); y++ {
			for x := 0; x < console.Width(); x++ {
				fg, bg, ch := console.Get(x, y)
				w.letter(x, y, 0, bg)

				if ch != 0 && ch != 32 {
					w.letter(x, y, ch, fg)
				}
			}
		}

		glfw.SwapBuffers()
	}
}

func (w *glfwBackend) Mouse() *rog.MouseData {
	return w.mouse
}

func (w *glfwBackend) mouseMove(x, y int) {
	w.mouse.Pos.X = x
	w.mouse.Pos.Y = y
	w.mouse.Cell.X = x / (16 * w.zoom)
	w.mouse.Cell.Y = y / (16 * w.zoom)
}

func (w *glfwBackend) mousePress(button, state int) {

	switch state {
	case glfw.KeyPress:
		switch button {
		case glfw.MouseLeft:
			w.mouse.Left.Pressed = true
		case glfw.MouseRight:
			w.mouse.Right.Pressed = true
		case glfw.MouseMiddle:
			w.mouse.Middle.Pressed = true
		}
	case glfw.KeyRelease:
		switch button {
		case glfw.MouseLeft:
			w.mouse.Left.Pressed = false
			w.mouse.Left.Released = true
		case glfw.MouseRight:
			w.mouse.Right.Pressed = false
			w.mouse.Right.Released = true
		case glfw.MouseMiddle:
			w.mouse.Middle.Pressed = false
			w.mouse.Middle.Released = true
		}
	}
}

func (w *glfwBackend) Key() int {
	return w.key
}

func (w *glfwBackend) setKey(key, state int) {
	if state == glfw.KeyPress {
		rogKey, exists := glfwToRogKey[key]
		if exists {
			w.key = rogKey
		}

		if key < 256 {
			w.key = key
		}
	}
}

func glInit(width, height int, font image.Image) {
	gl.Init()
	gl.Enable(gl.TEXTURE_2D)
	gl.PixelStorei(gl.UNPACK_ALIGNMENT, 1)
	gl.Viewport(0, 0, width, height)
	gl.MatrixMode(gl.PROJECTION)
	gl.LoadIdentity()
	gl.Ortho(0, float64(width), float64(height), 0, -1, 1)
	gl.MatrixMode(gl.MODELVIEW)
	gl.LoadIdentity()
	gl.Enable(gl.BLEND)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
	textures := make([]gl.Texture, 1)
	gl.GenTextures(textures)
	textures[0].Bind(gl.TEXTURE_2D)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)

    m := font.(*image.NRGBA)

	gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, m.Bounds().Max.X, m.Bounds().Max.Y, 0, gl.RGBA, gl.UNSIGNED_BYTE, m.Pix)
	gl.EnableClientState(gl.VERTEX_ARRAY)
	gl.EnableClientState(gl.COLOR_ARRAY)
	gl.EnableClientState(gl.TEXTURE_COORD_ARRAY)
	gl.VertexPointer(2, 0, vs)
	gl.ColorPointer(3, 0, cs)
	gl.TexCoordPointer(2, 0, ts)
}

// Draw a letter at a certain coordinate
func (w *glfwBackend) letter(lx, ly int, c rune, cl rog.RGB) {
    start := 8 * (ly*w.width+lx)

    vs[0] = w.verts[start]
    vs[1] = w.verts[start+1]
    vs[2] = w.verts[start+2]
    vs[3] = w.verts[start+3]
    vs[4] = w.verts[start+4]
    vs[5] = w.verts[start+5]
    vs[6] = w.verts[start+6]
    vs[7] = w.verts[start+7]

    cs[0] = cl.R
    cs[1] = cl.G
    cs[2] = cl.B
    cs[3] = cl.R
    cs[4] = cl.G
    cs[5] = cl.B
    cs[6] = cl.R
    cs[7] = cl.G
    cs[8] = cl.B
    cs[9] = cl.R
    cs[10] = cl.G
    cs[11] = cl.B

	u := float32(c % 256) * w.s
	v := float32(c / 256) * w.t
    ts[0] = u
    ts[1] = v
    ts[2] = u
    ts[3] = v+w.t
    ts[4] = u+w.s
    ts[5] = v+w.t
    ts[6] = u+w.s
    ts[7] = v

	gl.DrawArrays(gl.POLYGON, 0, 4)
}

var glfwToRogKey = map[int]int{
	glfw.KeyBackspace:  rog.Backspace,
	glfw.KeyTab:        rog.Tab,
	glfw.KeyEsc:        rog.Escape,
	glfw.KeySpace:      rog.Space,
	glfw.KeyDel:        rog.Delete,
	glfw.KeyLsuper:     rog.LSuper,
	glfw.KeyRsuper:     rog.RSuper,
	glfw.KeyLshift:     rog.LShift,
	glfw.KeyRshift:     rog.RShift,
	glfw.KeyLctrl:      rog.LControl,
	glfw.KeyRctrl:      rog.RControl,
	glfw.KeyLalt:       rog.LAlt,
	glfw.KeyRalt:       rog.RAlt,
	glfw.KeyF1:         rog.F1,
	glfw.KeyF2:         rog.F2,
	glfw.KeyF3:         rog.F3,
	glfw.KeyF4:         rog.F4,
	glfw.KeyF5:         rog.F5,
	glfw.KeyF6:         rog.F6,
	glfw.KeyF7:         rog.F7,
	glfw.KeyF8:         rog.F8,
	glfw.KeyF9:         rog.F9,
	glfw.KeyF10:        rog.F10,
	glfw.KeyF11:        rog.F11,
	glfw.KeyF12:        rog.F12,
	glfw.KeyF13:        rog.F13,
	glfw.KeyF14:        rog.F14,
	glfw.KeyF15:        rog.F15,
	glfw.KeyF16:        rog.F16,
	glfw.KeyUp:         rog.Up,
	glfw.KeyDown:       rog.Down,
	glfw.KeyLeft:       rog.Left,
	glfw.KeyRight:      rog.Right,
	glfw.KeyEnter:      rog.Return,
	glfw.KeyInsert:     rog.Insert,
	glfw.KeyHome:       rog.Home,
	glfw.KeyEnd:        rog.End,
	glfw.KeyCapslock:   rog.Capslock,
	glfw.KeyKPDivide:  rog.KPDivide,
	glfw.KeyKPMultiply: rog.KPMultiply,
	glfw.KeyKPSubtract: rog.KPSubtract,
	glfw.KeyKPAdd:      rog.KPAdd,
	glfw.KeyKPDecimal:  rog.KPDecimal,
	glfw.KeyKPEqual:    rog.KPEqual,
	glfw.KeyKPEnter:    rog.KPEnter,
	glfw.KeyKPNumlock:  rog.KPNumlock,
	glfw.KeyKP0:        rog.KP0,
	glfw.KeyKP1:        rog.KP1,
	glfw.KeyKP2:        rog.KP2,
	glfw.KeyKP3:        rog.KP3,
	glfw.KeyKP4:        rog.KP4,
	glfw.KeyKP5:        rog.KP5,
	glfw.KeyKP6:        rog.KP6,
	glfw.KeyKP7:        rog.KP7,
	glfw.KeyKP8:        rog.KP8,
	glfw.KeyKP9:        rog.KP9}
<|endoftext|>


<commit_before>package network

import (
	"common"
	"common/log"
	"encoding/json"
	"net"
)

type NetworkMultiplexer struct {
	in            chan common.NetworkMessage
	out           chan idHandler
	hosts         map[string][]common.NetworkMessageHandler
	createdServer bool
}

type idHandler struct {
	handler common.NetworkMessageHandler
	SwarmId string
}

func NewNetworkMultiplexer() common.NetworkMultiplexer {
	m := new(NetworkMultiplexer)
	m.in = make(chan common.NetworkMessage)
	m.out = make(chan idHandler)
	m.hosts = make(map[string][]common.NetworkMessageHandler)
	m.createdServer = false
	go m.listen()
	return m
}

func (m *NetworkMultiplexer) listen() {
	for {
		select {
		case c := <-m.out:
			m.hosts[c.SwarmId] = append(m.hosts[c.SwarmId], c.handler)
		case o := <-m.in:
			log.Debugln("MULTI: Transaction ", o, "to be sent to", len(m.hosts[o.SwarmId]))
			for _, s := range m.hosts[o.SwarmId] {
				go s.HandleNetworkMessage(o)
			}
		}
	}
}

func (m *NetworkMultiplexer) AddListener(SwarmId string, c common.NetworkMessageHandler) {
	m.out <- idHandler{c, SwarmId}
}

func (m *NetworkMultiplexer) SendNetworkMessage(o common.NetworkMessage) {
	m.in <- o
}

func (m *NetworkMultiplexer) Listen(addr string) {

	ln, err := net.Listen("tcp", addr)
	if err != nil {
		log.Debugln("MULTI: ERROR COULD NOT CREATE SERVER WITH ADDRESS:", addr)
		log.Fatal("MULTI ERROR MESSAGE:", err)
	}

	log.Debugln("MULTI: SUCCESSFULLY CREATED ADDRESS: addr")
	log.Debugln("MULTI: LISTENING FOR CONNECTORS")

	for {
		conn, err := ln.Accept()
		if err != nil {
			// Or it might be conn.LocalAddr().String(), unsure which to use
			log.Debugln("MULTI: ERROR CONNECTION REFUSED FOR ADDRESS:", conn.RemoteAddr().String())
			log.Debugln("MULTI ERROR MESSAGE:", err)
			continue
		}
		log.Debugln("MULTI: CONNECTED TO ADDRESS: ", conn.RemoteAddr().String())
		log.Debugln("MULTI: SENDING MESSAGE TO ADDRESS: ", conn.RemoteAddr().String())

		msg := "TESTING CONNECTION WITH " + addr
		en := json.NewEncoder(conn)
		err = en.Encode(msg)
		if err != nil {
			log.Debugln(err)
			continue
		}

		de := json.NewDecoder(conn)
		err = de.Decode(msg)
		if err != nil {
			log.Debugln(err)
		}

		log.Debugln("MULTI: MESSAGE RECEIVED FROM:", conn.RemoteAddr().String())
		log.Debugln(msg)

		defer conn.Close()
	}

	defer ln.Close()

}

func (m *NetworkMultiplexer) Connect(addr string) {
	conn, err := net.Dial("tcp", addr)

	if err != nil {
		log.Debugln("MULTI: ERROR CANNOT CONNECT TO SPECIFIED ADDRESS")
		log.Fatal("MULTI ERROR MESSAGE:", err)
	}

	log.Debugln("MULTI: CONNECTED TO ADDRESS:", addr)

	var msg string

	de := json.NewDecoder(conn)
	err = de.Decode(msg)
	if err != nil {
		log.Fatal(err)
	}

	log.Debugln("MULTI: MESSAGE RECEIVED FROM:", addr)
	log.Debugln(msg)

	msg = "MESSAGE CONFIRMED AS RECEIVED"

	en := json.NewEncoder(conn)
	err = en.Encode(msg)
	if err != nil {
		log.Fatal(err)
	}

	defer conn.Close()
}
<commit_msg>Delete multiplexer.go<commit_after><|endoftext|>


<commit_before>// Package gohinetradio is to get hichannel radio path and with token to play without flash.
package gohinetradio

import (
	"bytes"
	"encoding/json"
	"errors"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"runtime"
	"sort"
	"strconv"
	"text/tabwriter"
)

var nCPU = runtime.NumCPU()

// Init.
func init() {
	runtime.GOMAXPROCS(nCPU)
}

// Base URL.
const (
	PLAYURL  string = "http://hichannel.hinet.net/radio/play.do?id=%s"
	LISTURL  string = "http://hichannel.hinet.net/radio/channelList.do?radioType=&freqType=&freq=&area=&pN=%d"
	LISTPAGE int    = 1
)

// RadioData is the json of `http://hichannel.hinet.net/radio/play.do?id=232`
type RadioData struct {
	ChannelTitle   string `json:"channel_title"`
	PlayRadio      string `json:"playRadio"`
	ProgramName    string `json:"programName"`
	ChannelCollect bool   `json:"channel_collect"`
}

// GetURL is getting radio channel url with token.
func GetURL(No string) (RadioData, error) {
	var (
		err  error
		r    RadioData
		req  *http.Request
		resp *http.Response
	)

	client := &http.Client{}
	req, _ = http.NewRequest("GET", fmt.Sprintf(PLAYURL, No), nil)
	req.Header.Add("Referer", "http://hichannel.hinet.net/radio/index.do")
	resp, err = client.Do(req)
	if err != nil {
		log.Fatal("No network.")
	}
	defer resp.Body.Close()

	if data, err := ioutil.ReadAll(resp.Body); err == nil {
		json.NewDecoder(bytes.NewReader(data)).Decode(&r)
		if len(r.PlayRadio) == 0 {
			err = errors.New("No channel data.")
		}
	}
	return r, err
}

// RadioListData is the json of `http://hichannel.hinet.net/radio/channelList.do?radioType=&freqType=&freq=&area=&pN=1`
type RadioListData struct {
	PageNo   int              `json:"pageNo"`
	PageSize int              `json:"pageSize"`
	List     []RadioListDatas `json:"list"`
}

//RadioListDatas is RadioListData.List type.
type RadioListDatas struct {
	ChannelImage string `json:"channel_image"`
	ChannelTitle string `json:"channel_title"`
	RadioType    string `json:"radio_type"`
	IsChannel    bool   `json:"isChannel"`
	ProgramName  string `json:"program_name"`
	ChannelID    string `json:"channel_id"`
}

func getRadioPageList(page int) RadioListData {
	var (
		err  error
		r    RadioListData
		resp *http.Response
	)
	if resp, err = http.Get(fmt.Sprintf(LISTURL, page)); err == nil {
		defer resp.Body.Close()
	} else {
		log.Fatal("No network.")
	}

	if data, err := ioutil.ReadAll(resp.Body); err == nil {
		json.NewDecoder(bytes.NewReader(data)).Decode(&r)
	}
	return r
}

// GetRadioList is getting all channel list.
func GetRadioList() []RadioListDatas {
	//queue := make(chan RadioListData, nCPU)
	//defer close(queue)

	//var wg sync.WaitGroup
	//wg.Add(LISTPAGE)
	//for i := 1; i <= LISTPAGE; i++ {
	//	go func(i int) {
	//		runtime.Gosched()
	//		queue <- getRadioPageList(i)
	//	}(i)
	//}
	//var r []RadioListDatas
	//go func() {
	//	for v := range queue {
	//		for _, data := range v.List {
	//			r = append(r, data)
	//		}
	//		wg.Done()
	//	}
	//}()
	//wg.Wait()
	//return r
	return getRadioPageList(LISTPAGE).List
}

type byChannel []RadioListDatas

func (c byChannel) Len() int      { return len(c) }
func (c byChannel) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
func (c byChannel) Less(i, j int) bool {
	a, _ := strconv.Atoi(c[i].ChannelID)
	b, _ := strconv.Atoi(c[j].ChannelID)
	return a < b
}

// GenList is to output table list.
func GenList() {
	w := new(tabwriter.Writer)
	w.Init(os.Stdout, 0, 8, 0, '\t', 0)
	var (
		no        int
		output    string
		radioList []RadioListDatas
	)
	radioList = GetRadioList()
	sort.Sort(byChannel(radioList))
	for _, data := range radioList {
		if data.IsChannel {
			output += fmt.Sprintf("%d. [%v] %s\t", no+1, data.ChannelID, data.ChannelTitle)
			if (no+1)%3 == 0 {
				fmt.Fprintln(w, output)
				output = ""
			}
			no++
		}
	}
	fmt.Fprintln(w, output)
	w.Flush()
}

// PrintChannel is my fav channel XD.
func PrintChannel() {
	fmt.Println("[207] 中廣新聞網")
	fmt.Println("[205] 中廣流行網 i like")
	fmt.Println("[206] 中廣音樂網i radio")
	fmt.Println("[232] 飛碟電台")
	fmt.Println("[222] HitFm聯播網 Taipei 北部")
	fmt.Println("[156] KISS RADIO 大眾廣播電台")
	fmt.Println("[308] KISS RADIO 網路音樂台")
	fmt.Println("[187] NEWS98新聞網")
	fmt.Println("[370] POP Radio 台北流行廣播電台")
}
<commit_msg>Tiny changed tabwriter format.<commit_after>// Package gohinetradio is to get hichannel radio path and with token to play without flash.
package gohinetradio

import (
	"bytes"
	"encoding/json"
	"errors"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"runtime"
	"sort"
	"strconv"
	"text/tabwriter"
)

var nCPU = runtime.NumCPU()

// Init.
func init() {
	runtime.GOMAXPROCS(nCPU)
}

// Base URL.
const (
	PLAYURL  string = "http://hichannel.hinet.net/radio/play.do?id=%s"
	LISTURL  string = "http://hichannel.hinet.net/radio/channelList.do?radioType=&freqType=&freq=&area=&pN=%d"
	LISTPAGE int    = 1
)

// RadioData is the json of `http://hichannel.hinet.net/radio/play.do?id=232`
type RadioData struct {
	ChannelTitle   string `json:"channel_title"`
	PlayRadio      string `json:"playRadio"`
	ProgramName    string `json:"programName"`
	ChannelCollect bool   `json:"channel_collect"`
}

// GetURL is getting radio channel url with token.
func GetURL(No string) (RadioData, error) {
	var (
		err  error
		r    RadioData
		req  *http.Request
		resp *http.Response
	)

	client := &http.Client{}
	req, _ = http.NewRequest("GET", fmt.Sprintf(PLAYURL, No), nil)
	req.Header.Add("Referer", "http://hichannel.hinet.net/radio/index.do")
	resp, err = client.Do(req)
	if err != nil {
		log.Fatal("No network.")
	}
	defer resp.Body.Close()

	if data, err := ioutil.ReadAll(resp.Body); err == nil {
		json.NewDecoder(bytes.NewReader(data)).Decode(&r)
		if len(r.PlayRadio) == 0 {
			err = errors.New("No channel data.")
		}
	}
	return r, err
}

// RadioListData is the json of `http://hichannel.hinet.net/radio/channelList.do?radioType=&freqType=&freq=&area=&pN=1`
type RadioListData struct {
	PageNo   int              `json:"pageNo"`
	PageSize int              `json:"pageSize"`
	List     []RadioListDatas `json:"list"`
}

//RadioListDatas is RadioListData.List type.
type RadioListDatas struct {
	ChannelImage string `json:"channel_image"`
	ChannelTitle string `json:"channel_title"`
	RadioType    string `json:"radio_type"`
	IsChannel    bool   `json:"isChannel"`
	ProgramName  string `json:"program_name"`
	ChannelID    string `json:"channel_id"`
}

func getRadioPageList(page int) RadioListData {
	var (
		err  error
		r    RadioListData
		resp *http.Response
	)
	if resp, err = http.Get(fmt.Sprintf(LISTURL, page)); err == nil {
		defer resp.Body.Close()
	} else {
		log.Fatal("No network.")
	}

	if data, err := ioutil.ReadAll(resp.Body); err == nil {
		json.NewDecoder(bytes.NewReader(data)).Decode(&r)
	}
	return r
}

// GetRadioList is getting all channel list.
func GetRadioList() []RadioListDatas {
	//queue := make(chan RadioListData, nCPU)
	//defer close(queue)

	//var wg sync.WaitGroup
	//wg.Add(LISTPAGE)
	//for i := 1; i <= LISTPAGE; i++ {
	//	go func(i int) {
	//		runtime.Gosched()
	//		queue <- getRadioPageList(i)
	//	}(i)
	//}
	//var r []RadioListDatas
	//go func() {
	//	for v := range queue {
	//		for _, data := range v.List {
	//			r = append(r, data)
	//		}
	//		wg.Done()
	//	}
	//}()
	//wg.Wait()
	//return r
	return getRadioPageList(LISTPAGE).List
}

type byChannel []RadioListDatas

func (c byChannel) Len() int      { return len(c) }
func (c byChannel) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
func (c byChannel) Less(i, j int) bool {
	a, _ := strconv.Atoi(c[i].ChannelID)
	b, _ := strconv.Atoi(c[j].ChannelID)
	return a < b
}

// GenList is to output table list.
func GenList() {
	w := new(tabwriter.Writer)
	w.Init(os.Stdout, 38, 0, 0, ' ', 0)
	var (
		no        int
		output    string
		radioList []RadioListDatas
	)
	radioList = GetRadioList()
	sort.Sort(byChannel(radioList))
	for _, data := range radioList {
		if data.IsChannel {
			output += fmt.Sprintf("%3d. [%v] %s\t", no+1, data.ChannelID, data.ChannelTitle)
			if (no+1)%3 == 0 {
				fmt.Fprintln(w, output)
				output = ""
			}
			no++
		}
	}
	fmt.Fprintln(w, output)
	w.Flush()
}

// PrintChannel is my fav channel XD.
func PrintChannel() {
	fmt.Println("[207] 中廣新聞網")
	fmt.Println("[205] 中廣流行網 i like")
	fmt.Println("[206] 中廣音樂網i radio")
	fmt.Println("[232] 飛碟電台")
	fmt.Println("[222] HitFm聯播網 Taipei 北部")
	fmt.Println("[156] KISS RADIO 大眾廣播電台")
	fmt.Println("[308] KISS RADIO 網路音樂台")
	fmt.Println("[187] NEWS98新聞網")
	fmt.Println("[370] POP Radio 台北流行廣播電台")
}
<|endoftext|>


<commit_before>package main

// Send hello message to node
func (this *Gossip) _sendHello(node string) error {
	// log.Infof("Gossip to %s", node)

	// Create message with the runtime ID
	msg := newGossipMessage(HelloGossipMessageType, []byte(runtime.Id))

	// Send
	err := this._send(node, msg)

	// Update last sent
	if err == nil {
		// We have sent hello
		this.GetNodeState(node).UpdateLastHelloSent()
	} else {
		log.Warnf("Failed gossip to %s: %s", node, err)
	}
	return err
}

// Receive hello
func (this *Gossip) _receiveHello(cmeta *TransportConnectionMeta, msg *GossipMessage) {
	// log.Infof("Gossip from %s", cmeta.GetNode())

	// Hello runtime ID
	remoteRuntimeId := string(msg.Data)

	// Ignore messages from ourselves
	if remoteRuntimeId == runtime.Id {
		runtime.SetNode(cmeta.GetNode())
		log.Debugf("Ignoring gossip hello from ourselves with runtime ID %s", runtime.Id)
		return
	}

	// State
	state := this.GetNodeState(cmeta.GetNode())

	// Runtime
	state.SetRuntimeId(remoteRuntimeId)

	// Handshake complete?
	if state.GetLastHelloReceived() == 0 {
		go this._onHelloHandshakeComplete(cmeta.GetNode())
	}

	// We have received hello
	state.UpdateLastHelloReceived()

	// Should we send something back immediately?
	timeSinceLastHelloSent := unixTsUint32() - state.GetLastHelloSent()
	if timeSinceLastHelloSent > conf.GossipHelloInterval {
		go this._sendHello(cmeta.GetNode())
	}
}
<commit_msg>Set runtime id for local nodes as well<commit_after>package main

// Send hello message to node
func (this *Gossip) _sendHello(node string) error {
	// log.Infof("Gossip to %s", node)

	// Create message with the runtime ID
	msg := newGossipMessage(HelloGossipMessageType, []byte(runtime.Id))

	// Send
	err := this._send(node, msg)

	// Update last sent
	if err == nil {
		// We have sent hello
		this.GetNodeState(node).UpdateLastHelloSent()
	} else {
		log.Warnf("Failed gossip to %s: %s", node, err)
	}
	return err
}

// Receive hello
func (this *Gossip) _receiveHello(cmeta *TransportConnectionMeta, msg *GossipMessage) {
	// log.Infof("Gossip from %s", cmeta.GetNode())

	// Hello runtime ID
	remoteRuntimeId := string(msg.Data)

	// State
	state := this.GetNodeState(cmeta.GetNode())

	// Runtime
	state.SetRuntimeId(remoteRuntimeId)

	// Ignore messages from ourselves
	if remoteRuntimeId == runtime.Id {
		runtime.SetNode(cmeta.GetNode())
		log.Debugf("Ignoring gossip hello from ourselves with runtime ID %s", runtime.Id)
		return
	}

	// Handshake complete?
	if state.GetLastHelloReceived() == 0 {
		go this._onHelloHandshakeComplete(cmeta.GetNode())
	}

	// We have received hello
	state.UpdateLastHelloReceived()

	// Should we send something back immediately?
	timeSinceLastHelloSent := unixTsUint32() - state.GetLastHelloSent()
	if timeSinceLastHelloSent > conf.GossipHelloInterval {
		go this._sendHello(cmeta.GetNode())
	}
}
<|endoftext|>


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

// +build !appengine

package main

import (
	"flag"
	"fmt"
	"go/build"
	"io"
	"log"
	"net"
	"net/http"
	"os"
	"os/exec"
	"path/filepath"
	"runtime"
	"strings"
	"time"

	"code.google.com/p/go.talks/pkg/socket"

	// Imports so that go build/install automatically installs them.
	_ "code.google.com/p/go-tour/pic"
	_ "code.google.com/p/go-tour/tree"
	_ "code.google.com/p/go-tour/wc"
)

const (
	basePkg    = "code.google.com/p/go-tour/"
	socketPath = "/socket"
)

var (
	httpListen  = flag.String("http", "127.0.0.1:3999", "host:port to listen on")
	htmlOutput  = flag.Bool("html", false, "render program output as HTML")
	openBrowser = flag.Bool("openbrowser", true, "open browser automatically")
)

var (
	// a source of numbers, for naming temporary files
	uniq = make(chan int)

	// GOPATH containing the tour packages
	gopath = os.Getenv("GOPATH")

	httpAddr string
)

func isRoot(path string) bool {
	_, err := os.Stat(filepath.Join(path, "tour.article"))
	return err == nil
}

func findRoot() (string, error) {
	ctx := build.Default
	p, err := ctx.Import(basePkg, "", build.FindOnly)
	if err == nil && isRoot(p.Dir) {
		return p.Dir, nil
	}
	tourRoot := filepath.Join(runtime.GOROOT(), "misc", "tour")
	ctx.GOPATH = tourRoot
	p, err = ctx.Import(basePkg, "", build.FindOnly)
	if err == nil && isRoot(tourRoot) {
		gopath = tourRoot
		return tourRoot, nil
	}
	return "", fmt.Errorf("could not find go-tour content; check $GOROOT and $GOPATH")
}

func main() {
	flag.Parse()

	// source of unique numbers
	go func() {
		for i := 0; ; i++ {
			uniq <- i
		}
	}()

	// find and serve the go tour files
	root, err := findRoot()
	if err != nil {
		log.Fatalf("Couldn't find tour files: %v", err)
	}

	log.Println("Serving content from", root)

	host, port, err := net.SplitHostPort(*httpListen)
	if err != nil {
		log.Fatal(err)
	}
	if host == "" {
		host = "localhost"
	}
	if host != "127.0.0.1" && host != "localhost" {
		log.Print(localhostWarning)
	}
	httpAddr = host + ":" + port

	if err := initTour(root); err != nil {
		log.Fatal(err)
	}

	fs := http.FileServer(http.Dir(root))
	http.Handle("/favicon.ico", fs)
	http.Handle("/static/", fs)
	http.Handle("/talks/", fs)

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		if r.URL.Path == "/" {
			if err := renderTour(w); err != nil {
				log.Println(err)
			}
			return
		}
		http.Error(w, "not found", 404)
	})

	http.Handle(socketPath, socket.Handler)

	err = serveScripts(filepath.Join(root, "js"), "socket.js")
	if err != nil {
		log.Fatal(err)
	}

	go func() {
		url := "http://" + httpAddr
		if waitServer(url) && *openBrowser && startBrowser(url) {
			log.Printf("A browser window should open. If not, please visit %s", url)
		} else {
			log.Printf("Please open your web browser and visit %s", url)
		}
	}()
	log.Fatal(http.ListenAndServe(httpAddr, nil))
}

const localhostWarning = `
WARNING!  WARNING!  WARNING!

I appear to be listening on an address that is not localhost.
Anyone with access to this address and port will have access
to this machine as the user running gotour.

If you don't understand this message, hit Control-C to terminate this process.

WARNING!  WARNING!  WARNING!
`

type response struct {
	Output string `json:"output"`
	Errors string `json:"compile_errors"`
}

func init() {
	socket.Environ = environ
}

// environ returns an execution environment containing only GO* variables
// and replacing GOPATH with the value of the global var gopath.
func environ() (env []string) {
	for _, v := range os.Environ() {
		if !strings.HasPrefix(v, "GO") {
			continue
		}
		if strings.HasPrefix(v, "GOPATH=") {
			v = "GOPATH=" + gopath
		}
		env = append(env, v)
	}
	return
}

// waitServer waits some time for the http Server to start
// serving url and returns whether it starts
func waitServer(url string) bool {
	tries := 20
	for tries > 0 {
		resp, err := http.Get(url)
		if err == nil {
			resp.Body.Close()
			return true
		}
		time.Sleep(100 * time.Millisecond)
		tries--
	}
	return false
}

// startBrowser tries to open the URL in a browser, and returns
// whether it succeed.
func startBrowser(url string) bool {
	// try to start the browser
	var args []string
	switch runtime.GOOS {
	case "darwin":
		args = []string{"open"}
	case "windows":
		args = []string{"cmd", "/c", "start"}
	default:
		args = []string{"xdg-open"}
	}
	cmd := exec.Command(args[0], append(args[1:], url)...)
	return cmd.Start() == nil
}

// prepContent for the local tour simply returns the content as-is.
func prepContent(r io.Reader) io.Reader { return r }

// socketAddr returns the WebSocket handler address.
func socketAddr() string { return "ws://" + httpAddr + socketPath }
<commit_msg>go-tour: pass TEMP and TMPDIR environment variables to go tool<commit_after>// Copyright 2011 The Go Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build !appengine

package main

import (
	"flag"
	"fmt"
	"go/build"
	"io"
	"log"
	"net"
	"net/http"
	"os"
	"os/exec"
	"path/filepath"
	"runtime"
	"strings"
	"time"

	"code.google.com/p/go.talks/pkg/socket"

	// Imports so that go build/install automatically installs them.
	_ "code.google.com/p/go-tour/pic"
	_ "code.google.com/p/go-tour/tree"
	_ "code.google.com/p/go-tour/wc"
)

const (
	basePkg    = "code.google.com/p/go-tour/"
	socketPath = "/socket"
)

var (
	httpListen  = flag.String("http", "127.0.0.1:3999", "host:port to listen on")
	htmlOutput  = flag.Bool("html", false, "render program output as HTML")
	openBrowser = flag.Bool("openbrowser", true, "open browser automatically")
)

var (
	// a source of numbers, for naming temporary files
	uniq = make(chan int)

	// GOPATH containing the tour packages
	gopath = os.Getenv("GOPATH")

	httpAddr string
)

func isRoot(path string) bool {
	_, err := os.Stat(filepath.Join(path, "tour.article"))
	return err == nil
}

func findRoot() (string, error) {
	ctx := build.Default
	p, err := ctx.Import(basePkg, "", build.FindOnly)
	if err == nil && isRoot(p.Dir) {
		return p.Dir, nil
	}
	tourRoot := filepath.Join(runtime.GOROOT(), "misc", "tour")
	ctx.GOPATH = tourRoot
	p, err = ctx.Import(basePkg, "", build.FindOnly)
	if err == nil && isRoot(tourRoot) {
		gopath = tourRoot
		return tourRoot, nil
	}
	return "", fmt.Errorf("could not find go-tour content; check $GOROOT and $GOPATH")
}

func main() {
	flag.Parse()

	// source of unique numbers
	go func() {
		for i := 0; ; i++ {
			uniq <- i
		}
	}()

	// find and serve the go tour files
	root, err := findRoot()
	if err != nil {
		log.Fatalf("Couldn't find tour files: %v", err)
	}

	log.Println("Serving content from", root)

	host, port, err := net.SplitHostPort(*httpListen)
	if err != nil {
		log.Fatal(err)
	}
	if host == "" {
		host = "localhost"
	}
	if host != "127.0.0.1" && host != "localhost" {
		log.Print(localhostWarning)
	}
	httpAddr = host + ":" + port

	if err := initTour(root); err != nil {
		log.Fatal(err)
	}

	fs := http.FileServer(http.Dir(root))
	http.Handle("/favicon.ico", fs)
	http.Handle("/static/", fs)
	http.Handle("/talks/", fs)

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		if r.URL.Path == "/" {
			if err := renderTour(w); err != nil {
				log.Println(err)
			}
			return
		}
		http.Error(w, "not found", 404)
	})

	http.Handle(socketPath, socket.Handler)

	err = serveScripts(filepath.Join(root, "js"), "socket.js")
	if err != nil {
		log.Fatal(err)
	}

	go func() {
		url := "http://" + httpAddr
		if waitServer(url) && *openBrowser && startBrowser(url) {
			log.Printf("A browser window should open. If not, please visit %s", url)
		} else {
			log.Printf("Please open your web browser and visit %s", url)
		}
	}()
	log.Fatal(http.ListenAndServe(httpAddr, nil))
}

const localhostWarning = `
WARNING!  WARNING!  WARNING!

I appear to be listening on an address that is not localhost.
Anyone with access to this address and port will have access
to this machine as the user running gotour.

If you don't understand this message, hit Control-C to terminate this process.

WARNING!  WARNING!  WARNING!
`

type response struct {
	Output string `json:"output"`
	Errors string `json:"compile_errors"`
}

func init() {
	socket.Environ = environ
}

// environ returns an execution environment containing only GO* variables
// and replacing GOPATH with the value of the global var gopath.
func environ() (env []string) {
	for _, v := range os.Environ() {
		if strings.HasPrefix(v, "GO") ||
			strings.HasPrefix(v, "TMPDIR=") ||
			strings.HasPrefix(v, "TEMP=") {
			if strings.HasPrefix(v, "GOPATH=") {
				v = "GOPATH=" + gopath
			}
			env = append(env, v)
		}
	}
	return
}

// waitServer waits some time for the http Server to start
// serving url and returns whether it starts
func waitServer(url string) bool {
	tries := 20
	for tries > 0 {
		resp, err := http.Get(url)
		if err == nil {
			resp.Body.Close()
			return true
		}
		time.Sleep(100 * time.Millisecond)
		tries--
	}
	return false
}

// startBrowser tries to open the URL in a browser, and returns
// whether it succeed.
func startBrowser(url string) bool {
	// try to start the browser
	var args []string
	switch runtime.GOOS {
	case "darwin":
		args = []string{"open"}
	case "windows":
		args = []string{"cmd", "/c", "start"}
	default:
		args = []string{"xdg-open"}
	}
	cmd := exec.Command(args[0], append(args[1:], url)...)
	return cmd.Start() == nil
}

// prepContent for the local tour simply returns the content as-is.
func prepContent(r io.Reader) io.Reader { return r }

// socketAddr returns the WebSocket handler address.
func socketAddr() string { return "ws://" + httpAddr + socketPath }
<|endoftext|>


<commit_before>package graph

import (
	"database/sql/driver"
	"encoding/json"
	"fmt"
	"strconv"
	"strings"

	"sourcegraph.com/sourcegraph/srcgraph/repo"
)

type (
	SID        int64
	SymbolPath string
	SymbolKind string
)

// SymbolKey specifies a symbol, either concretely or abstractly. A concrete
// symbol key has a non-empty CommitID and refers to a symbol defined in a
// specific commit. An abstract symbol key has an empty CommitID and is
// considered to refer to symbols from any number of commits (so long as the
// Repo, UnitType, Unit, and Path match).
//
// You can think of CommitID as the time dimension. With an empty CommitID, you
// are referring to a symbol that may or may not exist at various times. With a
// non-empty CommitID, you are referring to a specific definition of a symbol at
// the time specified by the CommitID.
type SymbolKey struct {
	// Repo is the VCS repository that defines this symbol. Its Elasticsearch mapping is defined
	// separately.
	Repo repo.URI `json:",omitempty"`

	// CommitID is the ID of the VCS commit that this symbol was defined in. The
	// CommitID is always a full commit ID (40 hexadecimal characters for git
	// and hg), never a branch or tag name.
	CommitID string `db:"commit_id" json:",omitempty"`

	// UnitType is the type name of the source unit (obtained from unit.Type(u))
	// that this symbol was defined in.
	UnitType string `db:"unit_type" json:",omitempty"`

	// Unit is the name of the source unit (obtained from u.Name()) that this
	// symbol was defined in.
	Unit string `json:",omitempty"`

	// Path is the path to this symbol, relative to the repo. Its Elasticsearch mapping is defined
	// separately (because it is a multi_field, which the struct tag can't currently represent).
	Path SymbolPath
}

func (s SymbolKey) String() string {
	b, err := json.Marshal(s)
	if err != nil {
		panic("SymbolKey.String: " + err.Error())
	}
	return string(b)
}

type Symbol struct {
	// SID is a unique, sequential ID for a symbol. It is regenerated each time
	// the symbol is emitted by the grapher and saved to the database. The SID
	// is used as an optimization (e.g., joins are faster on SID than on
	// SymbolKey).
	SID SID `db:"sid" json:",omitempty" elastic:"type:integer,index:no"`

	// SymbolKey is the natural unique key for a symbol. It is stable
	// (subsequent runs of a grapher will emit the same symbols with the same
	// SymbolKeys).
	SymbolKey

	// SpecificPath is the language-specific "path" to this symbol, using
	// language-specific separators (e.g., "::" and "." instead of "/", which is
	// used in the SymbolKey.Path value).
	SpecificPath string `db:"specific_path"`

	// Kind is the language-independent kind of this symbol.
	Kind SymbolKind `elastic:"type:string,index:analyzed"`

	// SpecificKind is the language-specific kind of this symbol (which is in
	// some cases equal to the Kind).
	SpecificKind string `db:"specific_kind"`

	Name string

	// Callable is true if this symbol may be called or invoked, such as in the
	// case of functions or methods.
	Callable bool `db:"callable"`

	File string `elastic:"type:string,index:no"`

	DefStart int `db:"def_start" elastic:"type:integer,index:no"`
	DefEnd   int `db:"def_end" elastic:"type:integer,index:no"`

	Exported bool `elastic:"type:boolean,index:not_analyzed"`

	// Test is whether this symbol is defined in test code (as opposed to main
	// code). For example, definitions in Go *_test.go files have Test = true.
	Test bool `elastic:"type:boolean,index:not_analyzed" json:",omitempty"`

	TypeExpr string `db:"type_expr" json:",omitempty"`
}

func (s *Symbol) Language() string {
	switch s.UnitType {
	case "GoPackage":
		return "Go"
	case "CommonJSPackage":
		return "JavaScript"
		// TODO(sqs): add Python, etc.
	}
	return "unknown language"
}

// TODO!(sqs): factor this into the individual source unit packages
func (s *Symbol) Signature() string {
	var removeOwnImportPath = func(str string) string {
		if s.UnitType == "GoPackage" {
			return strings.Replace(strings.Replace(str, string(s.Repo)+".", "", -1), string(s.Repo)+"/", "", -1)
		}
		return str
	}

	if s.TypeExpr == "" {
		return ""
	}
	if !s.Callable {
		if (s.Kind == Field || s.Kind == Var || s.Kind == Type) && len(s.TypeExpr) < 50 {
			return " " + removeOwnImportPath(s.TypeExpr)
		}
		return ""
	}

	switch s.UnitType {
	case "GoPackage":
		return removeOwnImportPath(strings.TrimPrefix(s.TypeExpr, "func"))
	case "CommonJSPackage":
		return strings.TrimPrefix(s.TypeExpr, "fn")
	case "python":
		// remove up to first paren
		i := strings.Index(s.TypeExpr, "(")
		if i == -1 {
			return s.TypeExpr
		}
		return s.TypeExpr[:i]
	case "ruby":
		return s.TypeExpr
	}
	return s.TypeExpr
}

func (s *Symbol) sortKey() string { return s.SymbolKey.String() }

// Propagate describes type/value propagation in code. A Propagate entry from A
// (src) to B (dst) indicates that the type/value of A propagates to B. In Tern,
// this is indicated by A having a "fwd" property whose value is an array that
// includes B.
//
//
// ## Motivation & example
//
// For example, consider the following JavaScript code:
//
//   var a = Foo;
//   var b = a;
//
// Foo, a, and b are each their own symbol. We could resolve all of them to the
// symbol of their original type (perhaps Foo), but there are occasions when you
// do want to see only the definition of a or b and examples thereof. Therefore,
// we need to represent them as distinct symbols.
//
// Even though Foo, a, and b are distinct symbols, there are propagation
// relationships between them that are important to represent. The type of Foo
// propagates to both a and b, and the type of a propagates to b. In this case,
// we would have 3 Propagates: Propagate{Src: "Foo", Dst: "a"}, Propagate{Src:
// "Foo", Dst: "b"}, and Propagate{Src: "a", Dst: "b"}. (The propagation
// relationships could be described by just the first and last Propagates, but
// we explicitly include all paths as a denormalization optimization to avoid
// requiring an unbounded number of DB queries to determine which symbols a type
// propagates to or from.)
//
//
// ## Directionality
//
// Propagation is unidirectional, in the general case. In the example above, if
// Foo referred to a JavaScript object and if the code were evaluated, any
// *runtime* type changes (e.g., setting a property) on Foo, a, and b would be
// reflected on all of the others. But this doesn't hold for static analysis;
// it's not always true that if a property "a.x" or "b.x" exists, then "Foo.x"
// exists. The simplest example is when Foo is an external definition. Perhaps
// this example file (which uses Foo as a library) modifies Foo to add a new
// property, but other libraries that use Foo would never see that property
// because they wouldn't be executed in the same context as this example file.
// So, in general, we cannot say that Foo receives all types applied to symbols
// that Foo propagates to.
//
//
// ## Hypothetical Python example
//
// Consider the following 2 Python files:
//
//   """file1.py"""
//   class Foo(object): end
//
//   """file2.py"""
//   from .file1 import Foo
//   Foo2 = Foo
//
// In this example, there would be one Propagate: Propagate{Src: "file1/Foo",
// Dst: "file2/Foo2}.
type Propagate struct {
	// Src is the symbol whose type/value is being propagated to the dst symbol.
	SrcRepo     repo.URI
	SrcPath     SymbolPath
	SrcUnit     string
	SrcUnitType string

	// Dst is the symbol that is receiving a propagated type/value from the src symbol.
	DstRepo     repo.URI
	DstPath     SymbolPath
	DstUnit     string
	DstUnitType string
}

const (
	Const   SymbolKind = "const"
	Field              = "field"
	Func               = "func"
	Module             = "module"
	Package            = "package"
	Type               = "type"
	Var                = "var"
)

var AllSymbolKinds = []SymbolKind{Const, Field, Func, Module, Package, Type, Var}

// Returns true iff k is a known symbol kind.
func (k SymbolKind) Valid() bool {
	for _, kk := range AllSymbolKinds {
		if k == kk {
			return true
		}
	}
	return false
}

func KindName(k string) string {
	if strings.HasSuffix(k, "_module") {
		return "module"
	}
	return strings.Replace(k, "_", " ", -1)
}

type ConstData struct {
	ConstValue string
}

// SQL

func (x SID) Value() (driver.Value, error) {
	return int64(x), nil
}

func (x *SID) Scan(v interface{}) error {
	if data, ok := v.(int64); ok {
		*x = SID(data)
		return nil
	}
	return fmt.Errorf("%T.Scan failed: %v", x, v)
}

func (x SymbolPath) Value() (driver.Value, error) {
	return string(x), nil
}

func (x *SymbolPath) Scan(v interface{}) error {
	if data, ok := v.([]byte); ok {
		*x = SymbolPath(data)
		return nil
	}
	return fmt.Errorf("%T.Scan failed: %v", x, v)
}

func (x SymbolKind) Value() (driver.Value, error) {
	return string(x), nil
}

func (x *SymbolKind) Scan(v interface{}) error {
	if data, ok := v.([]byte); ok {
		*x = SymbolKind(data)
		return nil
	}
	return fmt.Errorf("%T.Scan failed: %v", x, v)
}

func (x *ConstData) Value() (driver.Value, error) {
	if x == nil {
		return nil, nil
	}
	return json.Marshal(x)
}

func (x *ConstData) Scan(v interface{}) error {
	if data, ok := v.([]byte); ok {
		return json.Unmarshal(data, x)
	}
	return fmt.Errorf("%T.Scan failed: %v", x, v)
}

// Debugging

func (x Symbol) String() string {
	s, _ := json.Marshal(x)
	return string(s)
}

// Sorting

type Symbols []*Symbol

func (vs Symbols) Len() int           { return len(vs) }
func (vs Symbols) Swap(i, j int)      { vs[i], vs[j] = vs[j], vs[i] }
func (vs Symbols) Less(i, j int) bool { return vs[i].sortKey() < vs[j].sortKey() }

func (syms Symbols) Keys() (keys []SymbolKey) {
	keys = make([]SymbolKey, len(syms))
	for i, sym := range syms {
		keys[i] = sym.SymbolKey
	}
	return
}

func (syms Symbols) SIDs() (ids []SID) {
	ids = make([]SID, len(syms))
	for i, sym := range syms {
		ids[i] = sym.SID
	}
	return
}

func ParseSIDs(sidstrs []string) (sids []SID) {
	sids = make([]SID, len(sidstrs))
	for i, sidstr := range sidstrs {
		sid, err := strconv.Atoi(sidstr)
		if err == nil {
			sids[i] = SID(sid)
		}
	}
	return
}
<commit_msg>symbol page: show hierarchy for symbol container<commit_after>package graph

import (
	"database/sql/driver"
	"encoding/json"
	"fmt"
	"strconv"
	"strings"

	"sourcegraph.com/sourcegraph/srcgraph/repo"
)

type (
	SID        int64
	SymbolPath string
	SymbolKind string
)

// SymbolKey specifies a symbol, either concretely or abstractly. A concrete
// symbol key has a non-empty CommitID and refers to a symbol defined in a
// specific commit. An abstract symbol key has an empty CommitID and is
// considered to refer to symbols from any number of commits (so long as the
// Repo, UnitType, Unit, and Path match).
//
// You can think of CommitID as the time dimension. With an empty CommitID, you
// are referring to a symbol that may or may not exist at various times. With a
// non-empty CommitID, you are referring to a specific definition of a symbol at
// the time specified by the CommitID.
type SymbolKey struct {
	// Repo is the VCS repository that defines this symbol. Its Elasticsearch mapping is defined
	// separately.
	Repo repo.URI `json:",omitempty"`

	// CommitID is the ID of the VCS commit that this symbol was defined in. The
	// CommitID is always a full commit ID (40 hexadecimal characters for git
	// and hg), never a branch or tag name.
	CommitID string `db:"commit_id" json:",omitempty"`

	// UnitType is the type name of the source unit (obtained from unit.Type(u))
	// that this symbol was defined in.
	UnitType string `db:"unit_type" json:",omitempty"`

	// Unit is the name of the source unit (obtained from u.Name()) that this
	// symbol was defined in.
	Unit string `json:",omitempty"`

	// Path is the path to this symbol, relative to the repo. Its Elasticsearch mapping is defined
	// separately (because it is a multi_field, which the struct tag can't currently represent).
	Path SymbolPath
}

func (s SymbolKey) String() string {
	b, err := json.Marshal(s)
	if err != nil {
		panic("SymbolKey.String: " + err.Error())
	}
	return string(b)
}

type Symbol struct {
	// SID is a unique, sequential ID for a symbol. It is regenerated each time
	// the symbol is emitted by the grapher and saved to the database. The SID
	// is used as an optimization (e.g., joins are faster on SID than on
	// SymbolKey).
	SID SID `db:"sid" json:",omitempty" elastic:"type:integer,index:no"`

	// SymbolKey is the natural unique key for a symbol. It is stable
	// (subsequent runs of a grapher will emit the same symbols with the same
	// SymbolKeys).
	SymbolKey

	// SpecificPath is the language-specific "path" to this symbol, using
	// language-specific separators (e.g., "::" and "." instead of "/", which is
	// used in the SymbolKey.Path value).
	SpecificPath string `db:"specific_path"`

	// Kind is the language-independent kind of this symbol.
	Kind SymbolKind `elastic:"type:string,index:analyzed"`

	// SpecificKind is the language-specific kind of this symbol (which is in
	// some cases equal to the Kind).
	SpecificKind string `db:"specific_kind"`

	Name string

	// Callable is true if this symbol may be called or invoked, such as in the
	// case of functions or methods.
	Callable bool `db:"callable"`

	File string `elastic:"type:string,index:no"`

	DefStart int `db:"def_start" elastic:"type:integer,index:no"`
	DefEnd   int `db:"def_end" elastic:"type:integer,index:no"`

	Exported bool `elastic:"type:boolean,index:not_analyzed"`

	// Test is whether this symbol is defined in test code (as opposed to main
	// code). For example, definitions in Go *_test.go files have Test = true.
	Test bool `elastic:"type:boolean,index:not_analyzed" json:",omitempty"`

	TypeExpr string `db:"type_expr" json:",omitempty"`
}

func (s *Symbol) Language() string {
	switch s.UnitType {
	case "GoPackage":
		return "Go"
	case "CommonJSPackage":
		return "JavaScript"
		// TODO(sqs): add Python, etc.
	}
	return "unknown language"
}

// TODO!(sqs): factor this into the individual source unit packages
func (s *Symbol) Signature() string {
	var removeOwnImportPath = func(str string) string {
		if s.UnitType == "GoPackage" {
			return strings.Replace(strings.Replace(str, string(s.Repo)+".", "", -1), string(s.Repo)+"/", "", -1)
		}
		return str
	}

	if s.TypeExpr == "" {
		return ""
	}
	if !s.Callable {
		if (s.Kind == Field || s.Kind == Var || s.Kind == Type) && len(s.TypeExpr) < 50 {
			return " " + removeOwnImportPath(s.TypeExpr)
		}
		return ""
	}

	switch s.UnitType {
	case "GoPackage":
		return removeOwnImportPath(strings.TrimPrefix(s.TypeExpr, "func"))
	case "CommonJSPackage":
		return strings.TrimPrefix(s.TypeExpr, "fn")
	case "python":
		// remove up to first paren
		i := strings.Index(s.TypeExpr, "(")
		if i == -1 {
			return s.TypeExpr
		}
		return s.TypeExpr[:i]
	case "ruby":
		return s.TypeExpr
	}
	return s.TypeExpr
}

func (s *Symbol) sortKey() string { return s.SymbolKey.String() }

// Propagate describes type/value propagation in code. A Propagate entry from A
// (src) to B (dst) indicates that the type/value of A propagates to B. In Tern,
// this is indicated by A having a "fwd" property whose value is an array that
// includes B.
//
//
// ## Motivation & example
//
// For example, consider the following JavaScript code:
//
//   var a = Foo;
//   var b = a;
//
// Foo, a, and b are each their own symbol. We could resolve all of them to the
// symbol of their original type (perhaps Foo), but there are occasions when you
// do want to see only the definition of a or b and examples thereof. Therefore,
// we need to represent them as distinct symbols.
//
// Even though Foo, a, and b are distinct symbols, there are propagation
// relationships between them that are important to represent. The type of Foo
// propagates to both a and b, and the type of a propagates to b. In this case,
// we would have 3 Propagates: Propagate{Src: "Foo", Dst: "a"}, Propagate{Src:
// "Foo", Dst: "b"}, and Propagate{Src: "a", Dst: "b"}. (The propagation
// relationships could be described by just the first and last Propagates, but
// we explicitly include all paths as a denormalization optimization to avoid
// requiring an unbounded number of DB queries to determine which symbols a type
// propagates to or from.)
//
//
// ## Directionality
//
// Propagation is unidirectional, in the general case. In the example above, if
// Foo referred to a JavaScript object and if the code were evaluated, any
// *runtime* type changes (e.g., setting a property) on Foo, a, and b would be
// reflected on all of the others. But this doesn't hold for static analysis;
// it's not always true that if a property "a.x" or "b.x" exists, then "Foo.x"
// exists. The simplest example is when Foo is an external definition. Perhaps
// this example file (which uses Foo as a library) modifies Foo to add a new
// property, but other libraries that use Foo would never see that property
// because they wouldn't be executed in the same context as this example file.
// So, in general, we cannot say that Foo receives all types applied to symbols
// that Foo propagates to.
//
//
// ## Hypothetical Python example
//
// Consider the following 2 Python files:
//
//   """file1.py"""
//   class Foo(object): end
//
//   """file2.py"""
//   from .file1 import Foo
//   Foo2 = Foo
//
// In this example, there would be one Propagate: Propagate{Src: "file1/Foo",
// Dst: "file2/Foo2}.
type Propagate struct {
	// Src is the symbol whose type/value is being propagated to the dst symbol.
	SrcRepo     repo.URI
	SrcPath     SymbolPath
	SrcUnit     string
	SrcUnitType string

	// Dst is the symbol that is receiving a propagated type/value from the src symbol.
	DstRepo     repo.URI
	DstPath     SymbolPath
	DstUnit     string
	DstUnitType string
}

const (
	Const   SymbolKind = "const"
	Field              = "field"
	Func               = "func"
	Module             = "module"
	Package            = "package"
	Type               = "type"
	Var                = "var"
)

var AllSymbolKinds = []SymbolKind{Const, Field, Func, Module, Package, Type, Var}

func IsContainer(symbolKind SymbolKind) bool {
	switch symbolKind {
	case Module:
		fallthrough
	case Package:
		fallthrough
	case Type:
		return true
	default:
		return false
	}
}

// Returns true iff k is a known symbol kind.
func (k SymbolKind) Valid() bool {
	for _, kk := range AllSymbolKinds {
		if k == kk {
			return true
		}
	}
	return false
}

func KindName(k string) string {
	if strings.HasSuffix(k, "_module") {
		return "module"
	}
	return strings.Replace(k, "_", " ", -1)
}

type ConstData struct {
	ConstValue string
}

// SQL

func (x SID) Value() (driver.Value, error) {
	return int64(x), nil
}

func (x *SID) Scan(v interface{}) error {
	if data, ok := v.(int64); ok {
		*x = SID(data)
		return nil
	}
	return fmt.Errorf("%T.Scan failed: %v", x, v)
}

func (x SymbolPath) Value() (driver.Value, error) {
	return string(x), nil
}

func (x *SymbolPath) Scan(v interface{}) error {
	if data, ok := v.([]byte); ok {
		*x = SymbolPath(data)
		return nil
	}
	return fmt.Errorf("%T.Scan failed: %v", x, v)
}

func (x SymbolKind) Value() (driver.Value, error) {
	return string(x), nil
}

func (x *SymbolKind) Scan(v interface{}) error {
	if data, ok := v.([]byte); ok {
		*x = SymbolKind(data)
		return nil
	}
	return fmt.Errorf("%T.Scan failed: %v", x, v)
}

func (x *ConstData) Value() (driver.Value, error) {
	if x == nil {
		return nil, nil
	}
	return json.Marshal(x)
}

func (x *ConstData) Scan(v interface{}) error {
	if data, ok := v.([]byte); ok {
		return json.Unmarshal(data, x)
	}
	return fmt.Errorf("%T.Scan failed: %v", x, v)
}

// Debugging

func (x Symbol) String() string {
	s, _ := json.Marshal(x)
	return string(s)
}

// Sorting

type Symbols []*Symbol

func (vs Symbols) Len() int           { return len(vs) }
func (vs Symbols) Swap(i, j int)      { vs[i], vs[j] = vs[j], vs[i] }
func (vs Symbols) Less(i, j int) bool { return vs[i].sortKey() < vs[j].sortKey() }

func (syms Symbols) Keys() (keys []SymbolKey) {
	keys = make([]SymbolKey, len(syms))
	for i, sym := range syms {
		keys[i] = sym.SymbolKey
	}
	return
}

func (syms Symbols) SIDs() (ids []SID) {
	ids = make([]SID, len(syms))
	for i, sym := range syms {
		ids[i] = sym.SID
	}
	return
}

func ParseSIDs(sidstrs []string) (sids []SID) {
	sids = make([]SID, len(sidstrs))
	for i, sidstr := range sidstrs {
		sid, err := strconv.Atoi(sidstr)
		if err == nil {
			sids[i] = SID(sid)
		}
	}
	return
}
<|endoftext|>