<commit_before>package smtpapi

import (
	"encoding/json"
	"io/ioutil"
	"reflect"
	"testing"
)

func exampleJson() map[string]interface{} {
	data, _ := ioutil.ReadFile("smtpapi_test_strings.json")
	var f interface{}
	json.Unmarshal(data, &f)
	json := f.(map[string]interface{})
	return json
}

func TestSMTPAPIVersion(t *testing.T) {
	t.Parallel()
	if Version != "0.4.2" {
		t.Error("SMTPAPI version does not match")
	}
}

func TestNewSMTPIAPIHeader(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	if header == nil {
		t.Error("NewSMTPAPIHeader() should never return nil")
	}
}

func TestAddTo(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	header.AddTo("addTo@mailinator.com")
	result, _ := header.JSONString()
	if result != exampleJson()["add_to"] {
		t.Errorf("Result did not match")
	}
}

func TestAddTos(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	tos := []string{"addTo@mailinator.com"}
	header.AddTos(tos)
	result, _ := header.JSONString()
	if result != exampleJson()["add_to"] {
		t.Errorf("Result did not match")
	}
}

func TestSetTos(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	header.SetTos([]string{"setTos@mailinator.com"})
	result, _ := header.JSONString()
	if result != exampleJson()["set_tos"] {
		t.Errorf("Result did not match")
	}
}

func TestAddSubstitution(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	header.AddSubstitution("sub", "val")
	result, _ := header.JSONString()
	if result != exampleJson()["add_substitution"] {
		t.Errorf("Result did not match")
	}
}

func TestAddSubstitutions(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	header.AddSubstitutions("sub", []string{"val"})
	result, _ := header.JSONString()
	if result != exampleJson()["add_substitution"] {
		t.Errorf("Result did not match")
	}
}

func TestSetSubstitutions(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	sub := make(map[string][]string)
	sub["sub"] = []string{"val"}
	header.SetSubstitutions(sub)
	result, _ := header.JSONString()
	if result != exampleJson()["set_substitutions"] {
		t.Errorf("Result did not match")
	}
}

func TestAddSection(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	header.AddSection("set_section_key", "set_section_value")
	header.AddSection("set_section_key_2", "set_section_value_2")
	result, _ := header.JSONString()
	if result != exampleJson()["add_section"] {
		t.Errorf("Result did not match")
	}
}

func TestSetSections(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	sections := make(map[string]string)
	sections["set_section_key"] = "set_section_value"
	header.SetSections(sections)
	result, _ := header.JSONString()
	if result != exampleJson()["set_sections"] {
		t.Errorf("Result did not match")
	}
}

func TestAddCategory(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	header.AddCategory("addCategory")
	header.AddCategory("addCategory2")
	result, _ := header.JSONString()
	if result != exampleJson()["add_category"] {
		t.Errorf("Result did not match")
	}
}

func TestAddCategoryUnicode(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	header.AddCategory("カテゴリUnicode")
	header.AddCategory("カテゴリ2Unicode")
	header.AddCategory("鼖")
	result, _ := header.JSONString()
	if result != exampleJson()["add_category_unicode"] {
		t.Errorf("Result did not match")
	}
}

func TestAddCategories(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	categories := []string{"addCategory", "addCategory2"}
	header.AddCategories(categories)
	result, _ := header.JSONString()
	if result != exampleJson()["add_category"] {
		t.Errorf("Result did not match")
	}
}

func TestSetCategories(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	header.SetCategories([]string{"setCategories"})
	result, _ := header.JSONString()
	if result != exampleJson()["set_categories"] {
		t.Errorf("Result did not match")
	}
}

func TestAddUniqueArg(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	header.AddUniqueArg("add_unique_argument_key", "add_unique_argument_value")
	header.AddUniqueArg("add_unique_argument_key_2", "add_unique_argument_value_2")
	result, _ := header.JSONString()
	if result != exampleJson()["add_unique_arg"] {
		t.Errorf("Result did not match")
	}
}

func TestSetUniqueArgs(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	args := make(map[string]string)
	args["set_unique_argument_key"] = "set_unique_argument_value"
	header.SetUniqueArgs(args)
	result, _ := header.JSONString()
	if result != exampleJson()["set_unique_args"] {
		t.Errorf("Result did not match")
	}
}

func TestAddFilter(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	header.AddFilter("footer", "text/html", "boo")
	if len(header.Filters) != 1 {
		t.Error("AddFilter failed")
	}
}

func TestSetFilter(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	filter := &Filter{
		Settings: make(map[string]interface{}),
	}
	filter.Settings["enable"] = 1
	filter.Settings["text/plain"] = "You can haz footers!"
	header.SetFilter("footer", filter)
	result, _ := header.JSONString()
	if result != exampleJson()["set_filters"] {
		t.Errorf("Result did not match")
	}
}

func TestSetSendAt(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	header.SetSendAt(1428611024)
	result, _ := header.JSONString()
	if result != exampleJson()["set_send_at"] {
		t.Errorf("Result did not match")
	}
}

func TestAddSendEachAt(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	header.AddSendEachAt(1428611024)
	header.AddSendEachAt(1428611025)
	result, _ := header.JSONString()
	if result != exampleJson()["add_send_each_at"] {
		t.Errorf("Result did not match")
	}
}

func TestSetSendEachAt(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	sendEachAt := []int64{1428611024, 1428611025}
	header.SetSendEachAt(sendEachAt)
	result, _ := header.JSONString()
	if result != exampleJson()["set_send_each_at"] {
		t.Errorf("Result did not match")
	}
}

func TestSetASMGroupID(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	header.SetASMGroupID(1)
	result, _ := header.JSONString()
	if result != exampleJson()["set_asm_group_id"] {
		t.Errorf("Result did not match")
	}
}

func TestSetIpPool(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	header.SetIpPool("testPool")
	result, _ := header.JSONString()
	if result != exampleJson()["set_ip_pool"] {
		t.Errorf("Result did not match")
	}
}

func TestSAddASMGroupToDisplay(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	header.AddASMGroupToDisplay(671332)
	result, _ := header.JSONString()
	if result != exampleJson()["add_asm_group"] {
		t.Errorf("Result did not match")
	}
}

func TestSAddASMGroupsToDisplay(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	header.AddASMGroupsToDisplay([]int{45, 23})
	result, _ := header.JSONString()
	if result != exampleJson()["add_asm_groups"] {
		t.Errorf("Result did not match")
	}
}

func TestJSONString(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	result, _ := header.JSONString()
	if result != exampleJson()["json_string"] {
		t.Errorf("Result did not match")
	}
}

func TestJSONStringWithAdds(t *testing.T) {
	t.Parallel()
	validHeader, _ := json.Marshal([]byte(`{"to":["test@email.com"],"sub":{"subKey":["subValue"]},"section":{"testSection":"sectionValue"},"category":["testCategory"],"unique_args":{"testUnique":"uniqueValue"},"filters":{"testFilter":{"settings":{"filter":"filterValue"}}}}`))
	header := NewSMTPAPIHeader()
	header.AddTo("test@email.com")
	header.AddSubstitution("subKey", "subValue")
	header.AddSection("testSection", "sectionValue")
	header.AddCategory("testCategory")
	header.AddUniqueArg("testUnique", "uniqueValue")
	header.AddFilter("testFilter", "filter", "filterValue")
	if h, e := header.JSONString(); e != nil {
		t.Errorf("Error! %s", e)
	} else {
		testHeader, _ := json.Marshal([]byte(h))
		if reflect.DeepEqual(testHeader, validHeader) {
			t.Logf("Success")
		} else {
			t.Errorf("Invalid header")
		}
	}
}

func TestJSONStringWithSets(t *testing.T) {
	t.Parallel()
	validHeader, _ := json.Marshal([]byte(`{"to":["test@email.com"],"sub":{"subKey":["subValue"]},"section":{"testSection":"sectionValue"},"category":["testCategory"],"unique_args":{"testUnique":"uniqueValue"},"filters":{"testFilter":{"settings":{"filter":"filterValue"}}},"asm_group_id":1,"ip_pool":"testPool"}`))
	header := NewSMTPAPIHeader()
	header.SetTos([]string{"test@email.com"})
	sub := make(map[string][]string)
	sub["subKey"] = []string{"subValue"}
	header.SetSubstitutions(sub)
	sections := make(map[string]string)
	sections["testSection"] = "sectionValue"
	header.SetSections(sections)
	header.SetCategories([]string{"testCategory"})
	unique := make(map[string]string)
	unique["testUnique"] = "uniqueValue"
	header.SetUniqueArgs(unique)
	header.AddFilter("testFilter", "filter", "filterValue")
	header.SetASMGroupID(1)
	header.SetIpPool("testPool")
	if h, e := header.JSONString(); e != nil {
		t.Errorf("Error! %s", e)
	} else {
		testHeader, _ := json.Marshal([]byte(h))
		if reflect.DeepEqual(testHeader, validHeader) {
			t.Logf("Success")
		} else {
			t.Errorf("Invalid header")
		}
	}
}

func TestMarshalUnmarshall(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	header.SetTos([]string{"test@email.com"})
	sub := make(map[string][]string)
	sub["subKey"] = []string{"subValue"}
	header.SetSubstitutions(sub)
	sections := make(map[string]string)
	sections["testSection"] = "sectionValue"
	header.SetSections(sections)
	header.SetCategories([]string{"testCategory"})
	unique := make(map[string]string)
	unique["testUnique"] = "uniqueValue"
	header.SetUniqueArgs(unique)
	header.AddFilter("testFilter", "filter", "filterValue")
	header.SetASMGroupID(1)
	header.SetIpPool("testPool")
	header.SetASMGroupsToDisplay([]int{32, 12})

	newHeader := NewSMTPAPIHeader()
	b, err := header.JSONString()
	if err != nil {
		t.Errorf("Error in JSONString %v", err)
	}
	newHeader.Load([]byte(b))
	if !reflect.DeepEqual(header, newHeader) {
		t.Errorf("Expected %v, but got %v", header, newHeader)
	}
}
<commit_msg>create test for required repo files<commit_after>package smtpapi

import (
	"encoding/json"
	"io/ioutil"
	"os"
	"reflect"
	"testing"
)

func exampleJson() map[string]interface{} {
	data, _ := ioutil.ReadFile("smtpapi_test_strings.json")
	var f interface{}
	json.Unmarshal(data, &f)
	json := f.(map[string]interface{})
	return json
}

func TestSMTPAPIVersion(t *testing.T) {
	t.Parallel()
	if Version != "0.4.2" {
		t.Error("SMTPAPI version does not match")
	}
}

func TestNewSMTPIAPIHeader(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	if header == nil {
		t.Error("NewSMTPAPIHeader() should never return nil")
	}
}

func TestAddTo(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	header.AddTo("addTo@mailinator.com")
	result, _ := header.JSONString()
	if result != exampleJson()["add_to"] {
		t.Errorf("Result did not match")
	}
}

func TestAddTos(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	tos := []string{"addTo@mailinator.com"}
	header.AddTos(tos)
	result, _ := header.JSONString()
	if result != exampleJson()["add_to"] {
		t.Errorf("Result did not match")
	}
}

func TestSetTos(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	header.SetTos([]string{"setTos@mailinator.com"})
	result, _ := header.JSONString()
	if result != exampleJson()["set_tos"] {
		t.Errorf("Result did not match")
	}
}

func TestAddSubstitution(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	header.AddSubstitution("sub", "val")
	result, _ := header.JSONString()
	if result != exampleJson()["add_substitution"] {
		t.Errorf("Result did not match")
	}
}

func TestAddSubstitutions(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	header.AddSubstitutions("sub", []string{"val"})
	result, _ := header.JSONString()
	if result != exampleJson()["add_substitution"] {
		t.Errorf("Result did not match")
	}
}

func TestSetSubstitutions(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	sub := make(map[string][]string)
	sub["sub"] = []string{"val"}
	header.SetSubstitutions(sub)
	result, _ := header.JSONString()
	if result != exampleJson()["set_substitutions"] {
		t.Errorf("Result did not match")
	}
}

func TestAddSection(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	header.AddSection("set_section_key", "set_section_value")
	header.AddSection("set_section_key_2", "set_section_value_2")
	result, _ := header.JSONString()
	if result != exampleJson()["add_section"] {
		t.Errorf("Result did not match")
	}
}

func TestSetSections(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	sections := make(map[string]string)
	sections["set_section_key"] = "set_section_value"
	header.SetSections(sections)
	result, _ := header.JSONString()
	if result != exampleJson()["set_sections"] {
		t.Errorf("Result did not match")
	}
}

func TestAddCategory(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	header.AddCategory("addCategory")
	header.AddCategory("addCategory2")
	result, _ := header.JSONString()
	if result != exampleJson()["add_category"] {
		t.Errorf("Result did not match")
	}
}

func TestAddCategoryUnicode(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	header.AddCategory("カテゴリUnicode")
	header.AddCategory("カテゴリ2Unicode")
	header.AddCategory("鼖")
	result, _ := header.JSONString()
	if result != exampleJson()["add_category_unicode"] {
		t.Errorf("Result did not match")
	}
}

func TestAddCategories(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	categories := []string{"addCategory", "addCategory2"}
	header.AddCategories(categories)
	result, _ := header.JSONString()
	if result != exampleJson()["add_category"] {
		t.Errorf("Result did not match")
	}
}

func TestSetCategories(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	header.SetCategories([]string{"setCategories"})
	result, _ := header.JSONString()
	if result != exampleJson()["set_categories"] {
		t.Errorf("Result did not match")
	}
}

func TestAddUniqueArg(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	header.AddUniqueArg("add_unique_argument_key", "add_unique_argument_value")
	header.AddUniqueArg("add_unique_argument_key_2", "add_unique_argument_value_2")
	result, _ := header.JSONString()
	if result != exampleJson()["add_unique_arg"] {
		t.Errorf("Result did not match")
	}
}

func TestSetUniqueArgs(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	args := make(map[string]string)
	args["set_unique_argument_key"] = "set_unique_argument_value"
	header.SetUniqueArgs(args)
	result, _ := header.JSONString()
	if result != exampleJson()["set_unique_args"] {
		t.Errorf("Result did not match")
	}
}

func TestAddFilter(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	header.AddFilter("footer", "text/html", "boo")
	if len(header.Filters) != 1 {
		t.Error("AddFilter failed")
	}
}

func TestSetFilter(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	filter := &Filter{
		Settings: make(map[string]interface{}),
	}
	filter.Settings["enable"] = 1
	filter.Settings["text/plain"] = "You can haz footers!"
	header.SetFilter("footer", filter)
	result, _ := header.JSONString()
	if result != exampleJson()["set_filters"] {
		t.Errorf("Result did not match")
	}
}

func TestSetSendAt(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	header.SetSendAt(1428611024)
	result, _ := header.JSONString()
	if result != exampleJson()["set_send_at"] {
		t.Errorf("Result did not match")
	}
}

func TestAddSendEachAt(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	header.AddSendEachAt(1428611024)
	header.AddSendEachAt(1428611025)
	result, _ := header.JSONString()
	if result != exampleJson()["add_send_each_at"] {
		t.Errorf("Result did not match")
	}
}

func TestSetSendEachAt(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	sendEachAt := []int64{1428611024, 1428611025}
	header.SetSendEachAt(sendEachAt)
	result, _ := header.JSONString()
	if result != exampleJson()["set_send_each_at"] {
		t.Errorf("Result did not match")
	}
}

func TestSetASMGroupID(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	header.SetASMGroupID(1)
	result, _ := header.JSONString()
	if result != exampleJson()["set_asm_group_id"] {
		t.Errorf("Result did not match")
	}
}

func TestSetIpPool(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	header.SetIpPool("testPool")
	result, _ := header.JSONString()
	if result != exampleJson()["set_ip_pool"] {
		t.Errorf("Result did not match")
	}
}

func TestSAddASMGroupToDisplay(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	header.AddASMGroupToDisplay(671332)
	result, _ := header.JSONString()
	if result != exampleJson()["add_asm_group"] {
		t.Errorf("Result did not match")
	}
}

func TestSAddASMGroupsToDisplay(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	header.AddASMGroupsToDisplay([]int{45, 23})
	result, _ := header.JSONString()
	if result != exampleJson()["add_asm_groups"] {
		t.Errorf("Result did not match")
	}
}

func TestJSONString(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	result, _ := header.JSONString()
	if result != exampleJson()["json_string"] {
		t.Errorf("Result did not match")
	}
}

func TestJSONStringWithAdds(t *testing.T) {
	t.Parallel()
	validHeader, _ := json.Marshal([]byte(`{"to":["test@email.com"],"sub":{"subKey":["subValue"]},"section":{"testSection":"sectionValue"},"category":["testCategory"],"unique_args":{"testUnique":"uniqueValue"},"filters":{"testFilter":{"settings":{"filter":"filterValue"}}}}`))
	header := NewSMTPAPIHeader()
	header.AddTo("test@email.com")
	header.AddSubstitution("subKey", "subValue")
	header.AddSection("testSection", "sectionValue")
	header.AddCategory("testCategory")
	header.AddUniqueArg("testUnique", "uniqueValue")
	header.AddFilter("testFilter", "filter", "filterValue")
	if h, e := header.JSONString(); e != nil {
		t.Errorf("Error! %s", e)
	} else {
		testHeader, _ := json.Marshal([]byte(h))
		if reflect.DeepEqual(testHeader, validHeader) {
			t.Logf("Success")
		} else {
			t.Errorf("Invalid header")
		}
	}
}

func TestJSONStringWithSets(t *testing.T) {
	t.Parallel()
	validHeader, _ := json.Marshal([]byte(`{"to":["test@email.com"],"sub":{"subKey":["subValue"]},"section":{"testSection":"sectionValue"},"category":["testCategory"],"unique_args":{"testUnique":"uniqueValue"},"filters":{"testFilter":{"settings":{"filter":"filterValue"}}},"asm_group_id":1,"ip_pool":"testPool"}`))
	header := NewSMTPAPIHeader()
	header.SetTos([]string{"test@email.com"})
	sub := make(map[string][]string)
	sub["subKey"] = []string{"subValue"}
	header.SetSubstitutions(sub)
	sections := make(map[string]string)
	sections["testSection"] = "sectionValue"
	header.SetSections(sections)
	header.SetCategories([]string{"testCategory"})
	unique := make(map[string]string)
	unique["testUnique"] = "uniqueValue"
	header.SetUniqueArgs(unique)
	header.AddFilter("testFilter", "filter", "filterValue")
	header.SetASMGroupID(1)
	header.SetIpPool("testPool")
	if h, e := header.JSONString(); e != nil {
		t.Errorf("Error! %s", e)
	} else {
		testHeader, _ := json.Marshal([]byte(h))
		if reflect.DeepEqual(testHeader, validHeader) {
			t.Logf("Success")
		} else {
			t.Errorf("Invalid header")
		}
	}
}

func TestMarshalUnmarshall(t *testing.T) {
	t.Parallel()
	header := NewSMTPAPIHeader()
	header.SetTos([]string{"test@email.com"})
	sub := make(map[string][]string)
	sub["subKey"] = []string{"subValue"}
	header.SetSubstitutions(sub)
	sections := make(map[string]string)
	sections["testSection"] = "sectionValue"
	header.SetSections(sections)
	header.SetCategories([]string{"testCategory"})
	unique := make(map[string]string)
	unique["testUnique"] = "uniqueValue"
	header.SetUniqueArgs(unique)
	header.AddFilter("testFilter", "filter", "filterValue")
	header.SetASMGroupID(1)
	header.SetIpPool("testPool")
	header.SetASMGroupsToDisplay([]int{32, 12})

	newHeader := NewSMTPAPIHeader()
	b, err := header.JSONString()
	if err != nil {
		t.Errorf("Error in JSONString %v", err)
	}
	newHeader.Load([]byte(b))
	if !reflect.DeepEqual(header, newHeader) {
		t.Errorf("Expected %v, but got %v", header, newHeader)
	}
}

func TestRepoFiles(t *testing.T) {
	files := []string{"docker/Docker", "docker/docker-compose.yml", ".env_sample",
		".gitignore", ".travis.yml", ".codeclimate.yml", "CHANGELOG.md", "CODE_OF_CONDUCT.md",
		"CONTRIBUTING.md", ".github/ISSUE_TEMPLATE", "LICENSE.md", ".github/PULL_REQUEST_TEMPLATE",
		"README.md", "TROUBLESHOOTING.md", "USAGE.md", "USE_CASES.md"}

	for _, file := range files {
		if _, err := os.Stat(file); os.IsNotExist(err) {
			t.Errorf("Repo file does not exist: %v", file)
		}
	}
}
<|endoftext|>


<commit_before>package sqlconstants

import "errors"
import "strings"

const CURRENT_VENDOR = SQLITE3
const SQLITE3_FILE_URL = "throttle.db"
const SQLITE3_CONNECTION_STRING = "file:" + SQLITE3_FILE_URL
const SQLITE3_CHECK_IF_TABLE_EXISTS = "SELECT NAME FROM SQLITE_MASTER WHERE TYPE=? AND NAME=?;"
const SQLITE3_CREATE_BUCKET_SCHEMA = "CREATE TABLE BUCKETS(NAME TEXT NOT NULL UNIQUE PRIMARY KEY, VOLUME INTEGER DEFAULT 0, CAPACITY INTEGER, TIMEFRAME INTEGER, CREATED_TIMESTAMP TIMESTAMP DEFAULT CURRENT_TIMESTAMP, UPDATED_TIMESTAMP INTEGER DEFAULT (strftime('%s','now')))"
const SQLITE3_INSERT_INTO_BUCKET = "INSERT INTO BUCKETS (NAME, VOLUME, CAPACITY, TIMEFRAME) VALUES (?, ?, ?, ?)"
const SQLITE3_UPDATE_BUCKET = "UPDATE BUCKETS SET (VOLUME,CAPACITY,TIMEFRAME, UPDATED_TIMESTAMP) VALUES (?, ?, ?, ?)"

//COME BACK TO LOOK HERE IF ANYTHING GOES WRONG IN THE TIME DELAY in SQLITE3_REFRESH_BUCKET
const SQLITE3_REFRESH_BUCKET = "UPDATE BUCKETS SET VOLUME = CASE WHEN CAST(ROUND(VOLUME - ((CAPACITY*1.0)/TIMEFRAME)*(STRFTIME('%s','now') - UPDATED_TIMESTAMP)) AS INT) > 0 THEN CAST(ROUND(VOLUME - ((CAPACITY*1.0)/TIMEFRAME)*(STRFTIME('%s','now') - UPDATED_TIMESTAMP)) AS INT) ELSE 0 END, UPDATED_TIMESTAMP = STRFTIME('%s','now') WHERE NAME = ?;"
const SQLITE3_LOAD_BUCKET = "SELECT VOLUME, CAPACITY, TIMEFRAME FROM BUCKETS WHERE NAME = ?"
const SQLITE3_DELETE_BUCKET = "DELETE BUCKETS WHERE NAME = ?"
const SQLITE3_SELECT_ALL_BUCKETS = "SELECT * FROM BUCKETS where NAME = ?"
const SQLITE3_BUCKET_NAME = "BUCKETS"
const SQLITE3_GET_SCHEMA = "SELECT SQL FROM SQLITE_MASTER WHERE TYPE=? AND NAME=?"
const SQLITE3_FILL_BUCKET = "UPDATE BUCKETS SET VOLUME = VOLUME + ? WHERE NAME = ?"
const SQLITE3_DROP_BUCKETS = "DROP TABLE BUCKETS"

const (
	SQLITE3_TYPE_NULL      = ""
	SQLITE3_TYPE_INTEGER   = "INTEGER"
	SQLITE3_TYPE_TEXT      = "TEXT"
	SQLITE3_TYPE_REAL      = "REAL"
	SQLITE3_TYPE_BLOB      = "BLOG"
	SQLITE3_TYPE_BOOL      = "BOOL"
	SQLITE3_TYPE_TIMESTAMP = "TIMESTAMP"
	//add postgres, plus other types here
)

const (
	SQLITE3 = iota
	POSTGRESQL
	MYSQL
	MONGODB
)

func CreateStatementFunctionsToReplace() ([]string, error) {
	if CURRENT_VENDOR == SQLITE3 {
		return []string{"(strftime('%s','now'))"}, nil
	}

	return nil, errors.New("Error: Current Vendor is not support in CreateStatementFunctionsToReplace")
}

func CurrentVendor() string {
	if CURRENT_VENDOR == SQLITE3 {
		return "sqlite3"
	} else if CURRENT_VENDOR == POSTGRESQL {
		return "postgres"
	} else if CURRENT_VENDOR == MYSQL {
		return "mysql"
	} else if CURRENT_VENDOR == MONGODB {
		return "mongodb"
	}

	return ""
}

func IsSQLConstraint(constraint string) (bool, error) {
	c := strings.ToUpper(constraint)
	if CURRENT_VENDOR == SQLITE3 {
		switch c {
		case "PRIMARY":
			return true, nil
		case "DEFAULT":
			return true, nil
		case "NOT":
			return true, nil
		case "UNIQUE":
			return true, nil
		case "CHECK":
			return true, nil
		case "REFERENCES":
			return true, nil
		case "COLLATE":
			return true, nil
		default:
			return false, nil
		}

	}

	return false, errors.New("Go type no recongnized for current db vendor => " + CurrentVendor())

}

func GoType(sqlType string) (string, error) {
	if CURRENT_VENDOR == SQLITE3 {
		switch sqlType {
		case SQLITE3_TYPE_NULL:
			return "nil", nil
		case SQLITE3_TYPE_INTEGER:
			return "int", nil
		case SQLITE3_TYPE_REAL:
			return "float64", nil
		case SQLITE3_TYPE_BOOL:
			return "bool", nil
		case SQLITE3_TYPE_TEXT:
			return "string", nil
		case SQLITE3_TYPE_TIMESTAMP:
			return "time.Time", nil
		default:
			return "", nil

		}
	}

	return "", errors.New("Go type no recongnized for current db vendor => " + CurrentVendor())

}

func SQLType(goType string) (string, error) {
	if CURRENT_VENDOR == SQLITE3 {
		switch goType {
		case "nil":
			return SQLITE3_TYPE_NULL, nil
		case "int":
			return SQLITE3_TYPE_INTEGER, nil
		case "float64":
			return SQLITE3_TYPE_REAL, nil
		case "bool":
			return SQLITE3_TYPE_BOOL, nil
		case "string":
			return SQLITE3_TYPE_TEXT, nil
		case "time.Time":
			return SQLITE3_TYPE_TIMESTAMP, nil

		}
	}

	return "", errors.New("Go type no recongnized for current db vendor => " + CurrentVendor())

}
<commit_msg>Updated sql in SQLITE3_REFRESH_BUCKET so that expression that we set volume to is updated once and not twice<commit_after>package sqlconstants

import "errors"
import "strings"

const CURRENT_VENDOR = SQLITE3
const SQLITE3_FILE_URL = "throttle.db"
const SQLITE3_CONNECTION_STRING = "file:" + SQLITE3_FILE_URL
const SQLITE3_CHECK_IF_TABLE_EXISTS = "SELECT NAME FROM SQLITE_MASTER WHERE TYPE=? AND NAME=?;"
const SQLITE3_CREATE_BUCKET_SCHEMA = "CREATE TABLE BUCKETS(NAME TEXT NOT NULL UNIQUE PRIMARY KEY, VOLUME INTEGER DEFAULT 0, CAPACITY INTEGER, TIMEFRAME INTEGER, CREATED_TIMESTAMP TIMESTAMP DEFAULT CURRENT_TIMESTAMP, UPDATED_TIMESTAMP INTEGER DEFAULT (strftime('%s','now')))"
const SQLITE3_INSERT_INTO_BUCKET = "INSERT INTO BUCKETS (NAME, VOLUME, CAPACITY, TIMEFRAME) VALUES (?, ?, ?, ?)"
const SQLITE3_UPDATE_BUCKET = "UPDATE BUCKETS SET (VOLUME,CAPACITY,TIMEFRAME, UPDATED_TIMESTAMP) VALUES (?, ?, ?, ?)"

const SQLITE3_REFRESH_BUCKET = "UPDATE BUCKETS SET VOLUME = (SELECT CASE WHEN expr > 0 THEN expr ELSE 0 END FROM (SELECT CAST(ROUND(VOLUME - ((CAPACITY*1.0)/TIMEFRAME)*(STRFTIME('%s','now') - UPDATED_TIMESTAMP)) AS INT) AS expr FROM BUCKETS WHERE NAME = ?) b), UPDATED_TIMESTAMP = STRFTIME('%s', 'now') WHERE NAME = ?;"
const SQLITE3_LOAD_BUCKET = "SELECT VOLUME, CAPACITY, TIMEFRAME FROM BUCKETS WHERE NAME = ?"
const SQLITE3_DELETE_BUCKET = "DELETE BUCKETS WHERE NAME = ?"
const SQLITE3_SELECT_ALL_BUCKETS = "SELECT * FROM BUCKETS where NAME = ?"
const SQLITE3_BUCKET_NAME = "BUCKETS"
const SQLITE3_GET_SCHEMA = "SELECT SQL FROM SQLITE_MASTER WHERE TYPE=? AND NAME=?"
const SQLITE3_FILL_BUCKET = "UPDATE BUCKETS SET VOLUME = VOLUME + ? WHERE NAME = ?"
const SQLITE3_DROP_BUCKETS = "DROP TABLE BUCKETS"

const (
	SQLITE3_TYPE_NULL      = ""
	SQLITE3_TYPE_INTEGER   = "INTEGER"
	SQLITE3_TYPE_TEXT      = "TEXT"
	SQLITE3_TYPE_REAL      = "REAL"
	SQLITE3_TYPE_BLOB      = "BLOG"
	SQLITE3_TYPE_BOOL      = "BOOL"
	SQLITE3_TYPE_TIMESTAMP = "TIMESTAMP"
	//add postgres, plus other types here
)

const (
	SQLITE3 = iota
	POSTGRESQL
	MYSQL
	MONGODB
)

func CreateStatementFunctionsToReplace() ([]string, error) {
	if CURRENT_VENDOR == SQLITE3 {
		return []string{"(strftime('%s','now'))"}, nil
	}

	return nil, errors.New("Error: Current Vendor is not support in CreateStatementFunctionsToReplace")
}

func CurrentVendor() string {
	if CURRENT_VENDOR == SQLITE3 {
		return "sqlite3"
	} else if CURRENT_VENDOR == POSTGRESQL {
		return "postgres"
	} else if CURRENT_VENDOR == MYSQL {
		return "mysql"
	} else if CURRENT_VENDOR == MONGODB {
		return "mongodb"
	}

	return ""
}

func IsSQLConstraint(constraint string) (bool, error) {
	c := strings.ToUpper(constraint)
	if CURRENT_VENDOR == SQLITE3 {
		switch c {
		case "PRIMARY":
			return true, nil
		case "DEFAULT":
			return true, nil
		case "NOT":
			return true, nil
		case "UNIQUE":
			return true, nil
		case "CHECK":
			return true, nil
		case "REFERENCES":
			return true, nil
		case "COLLATE":
			return true, nil
		default:
			return false, nil
		}

	}

	return false, errors.New("Go type no recongnized for current db vendor => " + CurrentVendor())

}

func GoType(sqlType string) (string, error) {
	if CURRENT_VENDOR == SQLITE3 {
		switch sqlType {
		case SQLITE3_TYPE_NULL:
			return "nil", nil
		case SQLITE3_TYPE_INTEGER:
			return "int", nil
		case SQLITE3_TYPE_REAL:
			return "float64", nil
		case SQLITE3_TYPE_BOOL:
			return "bool", nil
		case SQLITE3_TYPE_TEXT:
			return "string", nil
		case SQLITE3_TYPE_TIMESTAMP:
			return "time.Time", nil
		default:
			return "", nil

		}
	}

	return "", errors.New("Go type no recongnized for current db vendor => " + CurrentVendor())

}

func SQLType(goType string) (string, error) {
	if CURRENT_VENDOR == SQLITE3 {
		switch goType {
		case "nil":
			return SQLITE3_TYPE_NULL, nil
		case "int":
			return SQLITE3_TYPE_INTEGER, nil
		case "float64":
			return SQLITE3_TYPE_REAL, nil
		case "bool":
			return SQLITE3_TYPE_BOOL, nil
		case "string":
			return SQLITE3_TYPE_TEXT, nil
		case "time.Time":
			return SQLITE3_TYPE_TIMESTAMP, nil

		}
	}

	return "", errors.New("Go type no recongnized for current db vendor => " + CurrentVendor())

}
<|endoftext|>


<commit_before>// Copyright 2015 Dorival de Moraes Pedroso. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package goga

import (
	"github.com/cpmech/gosl/chk"
	"github.com/cpmech/gosl/io"
	"github.com/cpmech/gosl/utl"
)

// Evolver realises the evolutionary process
type Evolver struct {
	C       *ConfParams // configuration parameters
	Islands []*Island   // islands
	Best    *Individual // best individual among all in all islands
}

// NewEvolverPop creates a new evolver based on given populations
func NewEvolver(nova, noor int, C *ConfParams) (o *Evolver) {
	o = new(Evolver)
	o.C = C
	o.Islands = make([]*Island, o.C.Nisl)
	for i := 0; i < o.C.Nisl; i++ {
		o.Islands[i] = NewIsland(i, nova, noor, o.C)
	}
	o.Best = o.Islands[0].Pop[0]
	return
}

// Run runs the evolution process
func (o *Evolver) Run() {

	// check
	nislands := len(o.Islands)
	if nislands < 1 {
		return
	}
	if o.C.Ninds < nislands {
		chk.Panic("number of individuals must be greater than the number of islands")
	}

	// first output
	t := 0
	for _, isl := range o.Islands {
		isl.WritePopToReport(t, 0)
	}
	if o.C.Verbose {
		o.print_legend()
		io.Pf("\nrunning ...\n")
	}
	if o.C.PostProc != nil {
		for _, isl := range o.Islands {
			o.C.PostProc(isl.Id, 0, isl.Pop)
		}
	}

	// for migration
	iworst := o.C.Ninds - 1
	receiveFrom := utl.IntsAlloc(nislands, nislands-1)

	// time loop
	t = 1
	tmig := o.C.Dtmig
	nomig := false
	if tmig > o.C.Tf {
		tmig = o.C.Tf
		nomig = true
	}
	done := make(chan int, nislands)
	for t < o.C.Tf {

		// evolve up to migration time
		if o.C.Pll {
			for i := 0; i < nislands; i++ {
				go func(isl *Island) {
					for time := t; time < tmig; time++ {
						report := o.calc_report(time)
						isl.Run(time, report, (o.C.Verbose && isl.Id == 0))
						if o.C.Verbose && isl.Id == 0 {
							o.print_time(time, report)
						}
					}
					done <- 1
				}(o.Islands[i])
			}
			for i := 0; i < nislands; i++ {
				<-done
			}
		} else {
			for _, isl := range o.Islands {
				for time := t; time < tmig; time++ {
					report := o.calc_report(time)
					isl.Run(time, report, (o.C.Verbose && isl.Id == 0))
					if o.C.Verbose && isl.Id == 0 {
						o.print_time(time, report)
					}
				}
			}
		}

		// update time
		t = tmig
		tmig += o.C.Dtmig
		if tmig > o.C.Tf {
			tmig = o.C.Tf
			nomig = true
		}

		// skip migration
		if nomig {
			continue
		}

		// reset receiveFrom matrix
		for i := 0; i < nislands; i++ {
			for j := 0; j < nislands-1; j++ {
				receiveFrom[i][j] = -1
			}
		}

		// compute destinations
		for i := 0; i < nislands; i++ {
			Aworst := o.Islands[i].Pop[iworst]
			k := 0
			for j := 0; j < nislands; j++ {
				if i != j {
					Bbest := o.Islands[j].Pop[0]
					send, _ := IndCompareDet(Bbest, Aworst)
					if send {
						receiveFrom[i][k] = j // i gets individual from j
						k++
					}
				}
			}
		}

		// migration
		if o.C.Verbose {
			io.Pfyel(" %d", t)
		}
		for i, from := range receiveFrom {
			k := 0
			for _, j := range from {
				if j >= 0 {
					o.Islands[j].Pop[0].CopyInto(o.Islands[i].Pop[iworst-k])
					k++
				}
			}
			o.Islands[i].CalcDemeritsAndSort(o.Islands[i].Pop)
		}
	}

	// best individual
	o.FindBestFromAll()

	// message
	if o.C.Verbose {
		io.Pf("\n... end\n\n")
	}

	// write reports
	if o.C.DoReport {
		for _, isl := range o.Islands {
			isl.SaveReport(o.C.Verbose)
		}
	}

	// plot evolution
	if o.C.DoPlot {
		for i, isl := range o.Islands {
			PlotOvs(isl, ".eps", "", o.C.PltTi, o.C.PltTf, i == 0, i == o.C.Nisl-1)
		}
		for i, isl := range o.Islands {
			PlotOor(isl, ".eps", "", o.C.PltTi, o.C.PltTf, i == 0, i == o.C.Nisl-1)
		}
	}
	return
}

// ResetAllPop resets/re-generates all populations in all islands
func (o *Evolver) ResetAllPop() {
	for _, isl := range o.Islands {
		isl.Pop = o.C.PopFltGen(o.C.Ninds, isl.Nova, isl.Noor, o.C.Nbases, o.C.Noise, o.C.RangeFlt)
		isl.CalcOvs(isl.Pop, 0)
		isl.CalcDemeritsAndSort(isl.Pop)
	}
}

// FindBestFromAll finds best individual from all islands
//  Output: o.Best will point to the best individual
func (o *Evolver) FindBestFromAll() {
	if len(o.Islands) < 1 {
		return
	}
	o.Best = o.Islands[0].Pop[0]
	for i := 1; i < o.C.Nisl; i++ {
		_, other_is_better := IndCompareDet(o.Best, o.Islands[i].Pop[0])
		if other_is_better {
			o.Best = o.Islands[i].Pop[0]
		}
	}
}

// GetFeasible returns all feasible individuals from all islands
func (o *Evolver) GetFeasible() (feasible []*Individual) {
	for _, isl := range o.Islands {
		for _, ind := range isl.Pop {
			unfeasible := false
			for _, oor := range ind.Oors {
				if oor > 0 {
					unfeasible = true
				}
			}
			if !unfeasible {
				feasible = append(feasible, ind)
			}
		}
	}
	return
}

// GetResults returns all ovas and oors from a subset of individuals
//  Output:
//   ovas -- [len(subset)][nova] objective values
//   oors -- [len(subset)][noor] out-of-range values
func (o *Evolver) GetResults(subset []*Individual) (ovas, oors [][]float64) {
	nova := o.Islands[0].Nova
	noor := o.Islands[0].Noor
	ninds := len(subset)
	ovas = utl.DblsAlloc(ninds, nova)
	oors = utl.DblsAlloc(ninds, noor)
	for i, ind := range subset {
		for j := 0; j < nova; j++ {
			ovas[i][j] = ind.Ovas[j]
		}
		for j := 0; j < noor; j++ {
			oors[i][j] = ind.Oors[j]
		}
	}
	return
}

// GetParetoFront returns all feasible individuals on the Pareto front
// Note: input data can be obtained from GetFeasible and GetResults
func (o *Evolver) GetParetoFront(feasible []*Individual, ovas, oors [][]float64) (ovafront, oorfront []*Individual) {
	chk.IntAssert(len(feasible), len(ovas))
	ovaf := utl.ParetoFront(ovas)
	ovafront = make([]*Individual, len(ovaf))
	for i, id := range ovaf {
		ovafront[i] = feasible[id]
	}
	if len(oors) > 0 {
		chk.IntAssert(len(feasible), len(oors))
		oorf := utl.ParetoFront(oors)
		oorfront = make([]*Individual, len(oorf))
		for i, id := range oorf {
			oorfront[i] = feasible[id]
		}
	}
	return
}

// GetFrontOvas collects 2 ova results from Pareto front
//  Input:
//   r and s -- 2 selected objective functions; e.g. r=0 and s=1 for 2D problems
func (o *Evolver) GetFrontOvas(r, s int, front []*Individual) (x, y []float64) {
	x = make([]float64, len(front))
	y = make([]float64, len(front))
	for i, ind := range front {
		x[i] = ind.Ovas[r]
		y[i] = ind.Ovas[s]
	}
	return
}

//func (o *Evolver) GetCompromise(feasible []*Individual) (xova, yova, xoor, yoor []float64) {
//}

// auxiliary //////////////////////////////////////////////////////////////////////////////////////

func (o Evolver) calc_report(t int) bool {
	return t%o.C.Dtout == 0
}

func (o Evolver) print_legend() {
	io.Pf("\nLEGEND\n")
	io.Pfgrey(" 00 -- generation number (time)\n")
	io.Pfblue(" 00 -- reporting time\n")
	io.Pf(" 00 -- prescribed regeneration time\n")
	io.Pfyel(" 00 -- migration time\n")
	io.Pfmag("  . -- automatic regeneration time to improve diversity\n")
}

func (o Evolver) print_time(time int, report bool) {
	io.Pf(" ")
	if report {
		io.Pfblue("%v", time)
		return
	}
	io.Pfgrey("%v", time)
}
<commit_msg>generation of population now takes the id of island => trying to generate different islands<commit_after>// Copyright 2015 Dorival de Moraes Pedroso. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package goga

import (
	"github.com/cpmech/gosl/chk"
	"github.com/cpmech/gosl/io"
	"github.com/cpmech/gosl/utl"
)

// Evolver realises the evolutionary process
type Evolver struct {
	C       *ConfParams // configuration parameters
	Islands []*Island   // islands
	Best    *Individual // best individual among all in all islands
}

// NewEvolverPop creates a new evolver based on given populations
func NewEvolver(nova, noor int, C *ConfParams) (o *Evolver) {
	o = new(Evolver)
	o.C = C
	o.Islands = make([]*Island, o.C.Nisl)
	for i := 0; i < o.C.Nisl; i++ {
		o.Islands[i] = NewIsland(i, nova, noor, o.C)
	}
	o.Best = o.Islands[0].Pop[0]
	return
}

// Run runs the evolution process
func (o *Evolver) Run() {

	// check
	nislands := len(o.Islands)
	if nislands < 1 {
		return
	}
	if o.C.Ninds < nislands {
		chk.Panic("number of individuals must be greater than the number of islands")
	}

	// first output
	t := 0
	for _, isl := range o.Islands {
		isl.WritePopToReport(t, 0)
	}
	if o.C.Verbose {
		o.print_legend()
		io.Pf("\nrunning ...\n")
	}
	if o.C.PostProc != nil {
		for _, isl := range o.Islands {
			o.C.PostProc(isl.Id, 0, isl.Pop)
		}
	}

	// for migration
	iworst := o.C.Ninds - 1
	receiveFrom := utl.IntsAlloc(nislands, nislands-1)

	// time loop
	t = 1
	tmig := o.C.Dtmig
	nomig := false
	if tmig > o.C.Tf {
		tmig = o.C.Tf
		nomig = true
	}
	done := make(chan int, nislands)
	for t < o.C.Tf {

		// evolve up to migration time
		if o.C.Pll {
			for i := 0; i < nislands; i++ {
				go func(isl *Island) {
					for time := t; time < tmig; time++ {
						report := o.calc_report(time)
						isl.Run(time, report, (o.C.Verbose && isl.Id == 0))
						if o.C.Verbose && isl.Id == 0 {
							o.print_time(time, report)
						}
					}
					done <- 1
				}(o.Islands[i])
			}
			for i := 0; i < nislands; i++ {
				<-done
			}
		} else {
			for _, isl := range o.Islands {
				for time := t; time < tmig; time++ {
					report := o.calc_report(time)
					isl.Run(time, report, (o.C.Verbose && isl.Id == 0))
					if o.C.Verbose && isl.Id == 0 {
						o.print_time(time, report)
					}
				}
			}
		}

		// update time
		t = tmig
		tmig += o.C.Dtmig
		if tmig > o.C.Tf {
			tmig = o.C.Tf
			nomig = true
		}

		// skip migration
		if nomig {
			continue
		}

		// reset receiveFrom matrix
		for i := 0; i < nislands; i++ {
			for j := 0; j < nislands-1; j++ {
				receiveFrom[i][j] = -1
			}
		}

		// compute destinations
		for i := 0; i < nislands; i++ {
			Aworst := o.Islands[i].Pop[iworst]
			k := 0
			for j := 0; j < nislands; j++ {
				if i != j {
					Bbest := o.Islands[j].Pop[0]
					send, _ := IndCompareDet(Bbest, Aworst)
					if send {
						receiveFrom[i][k] = j // i gets individual from j
						k++
					}
				}
			}
		}

		// migration
		if o.C.Verbose {
			io.Pfyel(" %d", t)
		}
		for i, from := range receiveFrom {
			k := 0
			for _, j := range from {
				if j >= 0 {
					o.Islands[j].Pop[0].CopyInto(o.Islands[i].Pop[iworst-k])
					k++
				}
			}
			o.Islands[i].CalcDemeritsAndSort(o.Islands[i].Pop)
		}
	}

	// best individual
	o.FindBestFromAll()

	// message
	if o.C.Verbose {
		io.Pf("\n... end\n\n")
	}

	// write reports
	if o.C.DoReport {
		for _, isl := range o.Islands {
			isl.SaveReport(o.C.Verbose)
		}
	}

	// plot evolution
	if o.C.DoPlot {
		for i, isl := range o.Islands {
			PlotOvs(isl, ".eps", "", o.C.PltTi, o.C.PltTf, i == 0, i == o.C.Nisl-1)
		}
		for i, isl := range o.Islands {
			PlotOor(isl, ".eps", "", o.C.PltTi, o.C.PltTf, i == 0, i == o.C.Nisl-1)
		}
	}
	return
}

// ResetAllPop resets/re-generates all populations in all islands
func (o *Evolver) ResetAllPop() {
	for id, isl := range o.Islands {
		isl.Pop = o.C.PopFltGen(id, o.C.Ninds, isl.Nova, isl.Noor, o.C.Nbases, o.C.Noise, o.C.RangeFlt)
		isl.CalcOvs(isl.Pop, 0)
		isl.CalcDemeritsAndSort(isl.Pop)
	}
}

// FindBestFromAll finds best individual from all islands
//  Output: o.Best will point to the best individual
func (o *Evolver) FindBestFromAll() {
	if len(o.Islands) < 1 {
		return
	}
	o.Best = o.Islands[0].Pop[0]
	for i := 1; i < o.C.Nisl; i++ {
		_, other_is_better := IndCompareDet(o.Best, o.Islands[i].Pop[0])
		if other_is_better {
			o.Best = o.Islands[i].Pop[0]
		}
	}
}

// GetFeasible returns all feasible individuals from all islands
func (o *Evolver) GetFeasible() (feasible []*Individual) {
	for _, isl := range o.Islands {
		for _, ind := range isl.Pop {
			unfeasible := false
			for _, oor := range ind.Oors {
				if oor > 0 {
					unfeasible = true
				}
			}
			if !unfeasible {
				feasible = append(feasible, ind)
			}
		}
	}
	return
}

// GetResults returns all ovas and oors from a subset of individuals
//  Output:
//   ovas -- [len(subset)][nova] objective values
//   oors -- [len(subset)][noor] out-of-range values
func (o *Evolver) GetResults(subset []*Individual) (ovas, oors [][]float64) {
	nova := o.Islands[0].Nova
	noor := o.Islands[0].Noor
	ninds := len(subset)
	ovas = utl.DblsAlloc(ninds, nova)
	oors = utl.DblsAlloc(ninds, noor)
	for i, ind := range subset {
		for j := 0; j < nova; j++ {
			ovas[i][j] = ind.Ovas[j]
		}
		for j := 0; j < noor; j++ {
			oors[i][j] = ind.Oors[j]
		}
	}
	return
}

// GetParetoFront returns all feasible individuals on the Pareto front
// Note: input data can be obtained from GetFeasible and GetResults
func (o *Evolver) GetParetoFront(feasible []*Individual, ovas, oors [][]float64) (ovafront, oorfront []*Individual) {
	chk.IntAssert(len(feasible), len(ovas))
	ovaf := utl.ParetoFront(ovas)
	ovafront = make([]*Individual, len(ovaf))
	for i, id := range ovaf {
		ovafront[i] = feasible[id]
	}
	if len(oors) > 0 {
		chk.IntAssert(len(feasible), len(oors))
		oorf := utl.ParetoFront(oors)
		oorfront = make([]*Individual, len(oorf))
		for i, id := range oorf {
			oorfront[i] = feasible[id]
		}
	}
	return
}

// GetFrontOvas collects 2 ova results from Pareto front
//  Input:
//   r and s -- 2 selected objective functions; e.g. r=0 and s=1 for 2D problems
func (o *Evolver) GetFrontOvas(r, s int, front []*Individual) (x, y []float64) {
	x = make([]float64, len(front))
	y = make([]float64, len(front))
	for i, ind := range front {
		x[i] = ind.Ovas[r]
		y[i] = ind.Ovas[s]
	}
	return
}

//func (o *Evolver) GetCompromise(feasible []*Individual) (xova, yova, xoor, yoor []float64) {
//}

// auxiliary //////////////////////////////////////////////////////////////////////////////////////

func (o Evolver) calc_report(t int) bool {
	return t%o.C.Dtout == 0
}

func (o Evolver) print_legend() {
	io.Pf("\nLEGEND\n")
	io.Pfgrey(" 00 -- generation number (time)\n")
	io.Pfblue(" 00 -- reporting time\n")
	io.Pf(" 00 -- prescribed regeneration time\n")
	io.Pfyel(" 00 -- migration time\n")
	io.Pfmag("  . -- automatic regeneration time to improve diversity\n")
}

func (o Evolver) print_time(time int, report bool) {
	io.Pf(" ")
	if report {
		io.Pfblue("%v", time)
		return
	}
	io.Pfgrey("%v", time)
}
<|endoftext|>


<commit_before>// Copyright 2013, Cong Ding. All rights reserved.
//
// 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.
//
// author: Cong Ding <dinggnu@gmail.com>
//
package main

import (
	"github.com/ccding/go-logging/logging"
	"time"
)

func main() {
	logger, _ := logging.SimpleLogger("main")
	logger.SetLevel(logging.NOTSET)
	logger.Error("this is a test from error")
	logger.Debug("this is a test from debug")
	logger.Notset("orz", time.Now().UnixNano())
	logger.Destroy()

	logger2, _ := logging.RichLogger("main")
	logger2.SetLevel(logging.DEBUG)
	logger2.Error("this is a test from error")
	logger2.Debug("this is a test from debug")
	logger2.Notset("orz", time.Now().UnixNano())
	logger2.Destroy()
}
<commit_msg>update example<commit_after>// Copyright 2013, Cong Ding. All rights reserved.
//
// 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.
//
// author: Cong Ding <dinggnu@gmail.com>
//
package main

import (
	"github.com/ccding/go-logging/logging"
	"time"
)

func main() {
	logger1, _ := logging.SimpleLogger("main")
	logger1.SetLevel(logging.NOTSET)
	logger1.Error("this is a test from error")
	logger1.Debug("this is a test from debug")
	logger1.Notset("orz", time.Now().UnixNano())
	logger1.Destroy()

	logger2, _ := logging.RichLogger("main")
	logger2.SetLevel(logging.DEBUG)
	logger2.Error("this is a test from error")
	logger2.Debug("this is a test from debug")
	logger2.Notset("orz", time.Now().UnixNano())
	logger2.Destroy()

	logger3, _ := logging.ConfigLogger("example.conf")
	logger3.SetLevel(logging.DEBUG)
	logger3.Error("this is a test from error")
	logger3.Debug("this is a test from debug")
	logger3.Notset("orz", time.Now().UnixNano())
	logger3.Destroy()
}
<|endoftext|>


<commit_before>package server

import (
	"errors"
	"io"
	"sync"

	"golang.org/x/net/context"

	"github.com/coreos/pkg/capnslog"

	"github.com/barakmich/agro"
	"github.com/barakmich/agro/blockset"
	"github.com/barakmich/agro/models"
)

var clog = capnslog.NewPackageLogger("github.com/barakmich/agro", "server")

type file struct {
	mut       sync.RWMutex
	path      agro.Path
	inode     *models.INode
	srv       *server
	offset    int64
	blocks    agro.Blockset
	blkSize   int64
	inodeRef  agro.INodeRef
	writeOpen bool
	flags     int
}

func (s *server) newFile(path agro.Path, inode *models.INode) (agro.File, error) {
	bs, err := blockset.UnmarshalFromProto(inode.GetBlocks(), s.blocks)
	if err != nil {
		return nil, err
	}
	md, err := s.mds.GlobalMetadata()
	if err != nil {
		return nil, err
	}

	clog.Tracef("Open file %s at inode %d:%d with block length %d and size %d", path, inode.Volume, inode.INode, bs.Length(), inode.Filesize)
	f := &file{
		path:    path,
		inode:   inode,
		srv:     s,
		offset:  0,
		blocks:  bs,
		blkSize: int64(md.BlockSize),
	}
	return f, nil
}

func (f *file) Write(b []byte) (n int, err error) {
	n, err = f.WriteAt(b, f.offset)
	f.offset += int64(n)
	return
}

func (f *file) openWrite() error {
	if f.writeOpen {
		return nil
	}
	vid, err := f.srv.mds.GetVolumeID(f.path.Volume)
	if err != nil {
		return err
	}
	newInode, err := f.srv.mds.CommitInodeIndex()
	if err != nil {
		if err == agro.ErrAgain {
			return f.openWrite()
		}
		return err
	}
	f.inodeRef = agro.INodeRef{
		Volume: vid,
		INode:  newInode,
	}
	if f.inode != nil {
		f.inode.Replaces = f.inode.INode
		f.inode.INode = uint64(newInode)
	}
	f.writeOpen = true
	return nil
}

func (f *file) WriteAt(b []byte, off int64) (n int, err error) {
	f.mut.Lock()
	defer f.mut.Unlock()
	clog.Trace("begin write: offset ", off, " size ", len(b))
	toWrite := len(b)
	err = f.openWrite()
	if err != nil {
		return 0, err
	}

	defer func() {
		if off > int64(f.inode.Filesize) {
			f.inode.Filesize = uint64(off)
		}
	}()

	// Write the front matter, which may dangle from a byte offset
	blkIndex := int(off / f.blkSize)

	if f.blocks.Length() < blkIndex {
		// TODO(barakmich) Support truncate in the block abstraction, fill/return 0s
		return n, errors.New("Can't write past the end of a file")
	}

	blkOff := off - int64(int(f.blkSize)*blkIndex)
	if blkOff != 0 {
		frontlen := int(f.blkSize - blkOff)
		if frontlen > toWrite {
			frontlen = toWrite
		}
		var blk []byte
		if f.blocks.Length() == blkIndex {
			blk = make([]byte, f.blkSize)
		} else {
			blk, err = f.blocks.GetBlock(context.TODO(), blkIndex)
			if err != nil {
				return n, err
			}
		}
		wrote := copy(blk[int(blkOff):int(blkOff)+frontlen], b[:frontlen])
		clog.Tracef("head writing block at index %d, inoderef %s", blkIndex, f.inodeRef)
		err = f.blocks.PutBlock(context.TODO(), f.inodeRef, blkIndex, blk)
		if err != nil {
			return n, err
		}
		if wrote != frontlen {
			return n, errors.New("Couldn't write all of the first block at the offset")
		}
		b = b[frontlen:]
		n += wrote
		off += int64(wrote)
	}

	toWrite = len(b)
	if toWrite == 0 {
		// We're done
		return n, nil
	}

	// Bulk Write! We'd rather be here.
	if off%f.blkSize != 0 {
		panic("Offset not equal to a block boundary")
	}

	for toWrite >= int(f.blkSize) {
		blkIndex := int(off / f.blkSize)
		clog.Tracef("bulk writing block at index %d, inoderef %s", blkIndex, f.inodeRef)
		err = f.blocks.PutBlock(context.TODO(), f.inodeRef, blkIndex, b[:f.blkSize])
		if err != nil {
			return n, err
		}
		b = b[f.blkSize:]
		n += int(f.blkSize)
		off += int64(f.blkSize)
		toWrite = len(b)
	}

	if toWrite == 0 {
		// We're done
		return n, nil
	}

	// Trailing matter. This sucks too.
	if off%f.blkSize != 0 {
		panic("Offset not equal to a block boundary after bulk")
	}
	blkIndex = int(off / f.blkSize)
	var blk []byte
	if f.blocks.Length() == blkIndex {
		blk = make([]byte, f.blkSize)
	} else {
		blk, err = f.blocks.GetBlock(context.TODO(), blkIndex)
		if err != nil {
			return n, err
		}
	}
	wrote := copy(blk[:toWrite], b)
	clog.Tracef("tail writing block at index %d, inoderef %s", blkIndex, f.inodeRef)
	err = f.blocks.PutBlock(context.TODO(), f.inodeRef, blkIndex, blk)
	if err != nil {
		return n, err
	}
	if wrote != toWrite {
		return n, errors.New("Couldn't write all of the last block")
	}
	b = b[wrote:]
	n += wrote
	off += int64(wrote)
	return n, nil
}

func (f *file) Read(b []byte) (n int, err error) {
	n, err = f.ReadAt(b, f.offset)
	f.offset += int64(n)
	return
}

func (f *file) ReadAt(b []byte, off int64) (n int, ferr error) {
	f.mut.RLock()
	defer f.mut.RUnlock()
	toRead := len(b)
	clog.Tracef("begin read of size %d", toRead)
	n = 0
	if int64(toRead)+off > int64(f.inode.Filesize) {
		toRead = int(int64(f.inode.Filesize) - off)
		ferr = io.EOF
		clog.Tracef("read is longer than file")
	}
	for toRead > n {
		blkIndex := int(off / f.blkSize)
		if f.blocks.Length() <= blkIndex {
			// TODO(barakmich) Support truncate in the block abstraction, fill/return 0s
			return n, io.EOF
		}
		blkOff := off - int64(int(f.blkSize)*blkIndex)
		clog.Tracef("getting block index %d", blkIndex)
		blk, err := f.blocks.GetBlock(context.TODO(), blkIndex)
		if err != nil {
			return n, err
		}
		thisRead := f.blkSize - blkOff
		if int64(toRead-n) < thisRead {
			thisRead = int64(toRead - n)
		}
		count := copy(b[n:], blk[blkOff:blkOff+thisRead])
		n += count
		off += int64(count)
	}
	if toRead != n {
		panic("Read more than n bytes?")
	}
	return n, ferr
}

func (f *file) Close() error {
	if f == nil {
		return agro.ErrInvalid
	}
	return f.Sync()
}

func (f *file) Sync() error {
	if !f.writeOpen {
		return nil
	}
	blkdata, err := blockset.MarshalToProto(f.blocks)
	if err != nil {
		return err
	}
	f.inode.Blocks = blkdata
	err = f.srv.inodes.WriteINode(nil, f.inodeRef, f.inode)
	if err != nil {
		return err
	}

	err = f.srv.mds.SetFileINode(f.path, f.inodeRef)
	if err != nil {
		return err
	}
	return nil
}
<commit_msg>hidden nil context<commit_after>package server

import (
	"errors"
	"io"
	"sync"

	"golang.org/x/net/context"

	"github.com/coreos/pkg/capnslog"

	"github.com/barakmich/agro"
	"github.com/barakmich/agro/blockset"
	"github.com/barakmich/agro/models"
)

var clog = capnslog.NewPackageLogger("github.com/barakmich/agro", "server")

type file struct {
	mut       sync.RWMutex
	path      agro.Path
	inode     *models.INode
	srv       *server
	offset    int64
	blocks    agro.Blockset
	blkSize   int64
	inodeRef  agro.INodeRef
	writeOpen bool
	flags     int
}

func (s *server) newFile(path agro.Path, inode *models.INode) (agro.File, error) {
	bs, err := blockset.UnmarshalFromProto(inode.GetBlocks(), s.blocks)
	if err != nil {
		return nil, err
	}
	md, err := s.mds.GlobalMetadata()
	if err != nil {
		return nil, err
	}

	clog.Tracef("Open file %s at inode %d:%d with block length %d and size %d", path, inode.Volume, inode.INode, bs.Length(), inode.Filesize)
	f := &file{
		path:    path,
		inode:   inode,
		srv:     s,
		offset:  0,
		blocks:  bs,
		blkSize: int64(md.BlockSize),
	}
	return f, nil
}

func (f *file) Write(b []byte) (n int, err error) {
	n, err = f.WriteAt(b, f.offset)
	f.offset += int64(n)
	return
}

func (f *file) openWrite() error {
	if f.writeOpen {
		return nil
	}
	vid, err := f.srv.mds.GetVolumeID(f.path.Volume)
	if err != nil {
		return err
	}
	newInode, err := f.srv.mds.CommitInodeIndex()
	if err != nil {
		if err == agro.ErrAgain {
			return f.openWrite()
		}
		return err
	}
	f.inodeRef = agro.INodeRef{
		Volume: vid,
		INode:  newInode,
	}
	if f.inode != nil {
		f.inode.Replaces = f.inode.INode
		f.inode.INode = uint64(newInode)
	}
	f.writeOpen = true
	return nil
}

func (f *file) WriteAt(b []byte, off int64) (n int, err error) {
	f.mut.Lock()
	defer f.mut.Unlock()
	clog.Trace("begin write: offset ", off, " size ", len(b))
	toWrite := len(b)
	err = f.openWrite()
	if err != nil {
		return 0, err
	}

	defer func() {
		if off > int64(f.inode.Filesize) {
			f.inode.Filesize = uint64(off)
		}
	}()

	// Write the front matter, which may dangle from a byte offset
	blkIndex := int(off / f.blkSize)

	if f.blocks.Length() < blkIndex {
		// TODO(barakmich) Support truncate in the block abstraction, fill/return 0s
		return n, errors.New("Can't write past the end of a file")
	}

	blkOff := off - int64(int(f.blkSize)*blkIndex)
	if blkOff != 0 {
		frontlen := int(f.blkSize - blkOff)
		if frontlen > toWrite {
			frontlen = toWrite
		}
		var blk []byte
		if f.blocks.Length() == blkIndex {
			blk = make([]byte, f.blkSize)
		} else {
			blk, err = f.blocks.GetBlock(context.TODO(), blkIndex)
			if err != nil {
				return n, err
			}
		}
		wrote := copy(blk[int(blkOff):int(blkOff)+frontlen], b[:frontlen])
		clog.Tracef("head writing block at index %d, inoderef %s", blkIndex, f.inodeRef)
		err = f.blocks.PutBlock(context.TODO(), f.inodeRef, blkIndex, blk)
		if err != nil {
			return n, err
		}
		if wrote != frontlen {
			return n, errors.New("Couldn't write all of the first block at the offset")
		}
		b = b[frontlen:]
		n += wrote
		off += int64(wrote)
	}

	toWrite = len(b)
	if toWrite == 0 {
		// We're done
		return n, nil
	}

	// Bulk Write! We'd rather be here.
	if off%f.blkSize != 0 {
		panic("Offset not equal to a block boundary")
	}

	for toWrite >= int(f.blkSize) {
		blkIndex := int(off / f.blkSize)
		clog.Tracef("bulk writing block at index %d, inoderef %s", blkIndex, f.inodeRef)
		err = f.blocks.PutBlock(context.TODO(), f.inodeRef, blkIndex, b[:f.blkSize])
		if err != nil {
			return n, err
		}
		b = b[f.blkSize:]
		n += int(f.blkSize)
		off += int64(f.blkSize)
		toWrite = len(b)
	}

	if toWrite == 0 {
		// We're done
		return n, nil
	}

	// Trailing matter. This sucks too.
	if off%f.blkSize != 0 {
		panic("Offset not equal to a block boundary after bulk")
	}
	blkIndex = int(off / f.blkSize)
	var blk []byte
	if f.blocks.Length() == blkIndex {
		blk = make([]byte, f.blkSize)
	} else {
		blk, err = f.blocks.GetBlock(context.TODO(), blkIndex)
		if err != nil {
			return n, err
		}
	}
	wrote := copy(blk[:toWrite], b)
	clog.Tracef("tail writing block at index %d, inoderef %s", blkIndex, f.inodeRef)
	err = f.blocks.PutBlock(context.TODO(), f.inodeRef, blkIndex, blk)
	if err != nil {
		return n, err
	}
	if wrote != toWrite {
		return n, errors.New("Couldn't write all of the last block")
	}
	b = b[wrote:]
	n += wrote
	off += int64(wrote)
	return n, nil
}

func (f *file) Read(b []byte) (n int, err error) {
	n, err = f.ReadAt(b, f.offset)
	f.offset += int64(n)
	return
}

func (f *file) ReadAt(b []byte, off int64) (n int, ferr error) {
	f.mut.RLock()
	defer f.mut.RUnlock()
	toRead := len(b)
	clog.Tracef("begin read of size %d", toRead)
	n = 0
	if int64(toRead)+off > int64(f.inode.Filesize) {
		toRead = int(int64(f.inode.Filesize) - off)
		ferr = io.EOF
		clog.Tracef("read is longer than file")
	}
	for toRead > n {
		blkIndex := int(off / f.blkSize)
		if f.blocks.Length() <= blkIndex {
			// TODO(barakmich) Support truncate in the block abstraction, fill/return 0s
			return n, io.EOF
		}
		blkOff := off - int64(int(f.blkSize)*blkIndex)
		clog.Tracef("getting block index %d", blkIndex)
		blk, err := f.blocks.GetBlock(context.TODO(), blkIndex)
		if err != nil {
			return n, err
		}
		thisRead := f.blkSize - blkOff
		if int64(toRead-n) < thisRead {
			thisRead = int64(toRead - n)
		}
		count := copy(b[n:], blk[blkOff:blkOff+thisRead])
		n += count
		off += int64(count)
	}
	if toRead != n {
		panic("Read more than n bytes?")
	}
	return n, ferr
}

func (f *file) Close() error {
	if f == nil {
		return agro.ErrInvalid
	}
	return f.Sync()
}

func (f *file) Sync() error {
	if !f.writeOpen {
		return nil
	}
	blkdata, err := blockset.MarshalToProto(f.blocks)
	if err != nil {
		return err
	}
	f.inode.Blocks = blkdata
	err = f.srv.inodes.WriteINode(context.TODO(), f.inodeRef, f.inode)
	if err != nil {
		return err
	}

	err = f.srv.mds.SetFileINode(f.path, f.inodeRef)
	if err != nil {
		return err
	}
	return nil
}
<|endoftext|>


<commit_before>/*
	This is the server package.
	The purpouse of this package is to map a connection to each player(who is online) so we have a comunication chanel.

*/
package server

import (
	"fmt"
	"github.com/fzzy/sockjs-go/sockjs"
	"log"
	"net"
	"net/http"
	"warcluster/db_manager"
	"warcluster/entities"
)

var listener net.Listener
var HOST string     //Server scope constant that keeps the server host address.
var PORT int        //Server scope constant that keeps the server port number.

var sessions *sockjs.SessionPool = sockjs.NewSessionPool() //This is the SockJs sessions pull (a list of all the currently active client's sessions).

/*This function goes trough all the procedurs needed for the werver to be initialized.
1.Create an empty connections pool
2.Starts the listening foe messages loop.*/
func Start(host string, port int) error {
	log.Print("Server is starting...")
	log.Println("Server is up and running!")
	mux := sockjs.NewServeMux(http.DefaultServeMux)
	conf := sockjs.NewConfig()

	http.HandleFunc("/console", staticHandler)
	http.Handle("/static", http.FileServer(http.Dir("./static")))
	mux.Handle("/universe", handler, conf)

	if err := ListenAndServe(fmt.Sprintf("%v:%v", HOST, PORT), mux); err != nil {
		log.Println(err)
		return err
	}

	return Stop()
}

// ListenAndServe listens on the TCP network address srv.Addr and then
// calls Serve to handle requests on incoming connections.  If
// srv.Addr is blank, ":http" is used.
func ListenAndServe(address string, mux *sockjs.ServeMux) error {
	var err error

	server := &http.Server{Addr: address, Handler: mux}
	addr := server.Addr
	if addr == "" {
		addr = ":http"
	}
	listener, err = net.Listen("tcp", addr)
	if err != nil {
		return err
	}

	return server.Serve(listener)
}

//Die biatch and get the fuck out.
func Stop() error {
	log.Println("Server is shutting down...")
	listener.Close()
	log.Println("Server has stopped.")
	return nil
}

//Returns the HTML page needed to display the debug page (server "chat" window).
func staticHandler(w http.ResponseWriter, r *http.Request) {
	http.ServeFile(w, r, "./static/index.html")
}

/*This function is called from the message handler to parse the first message for every new connection.
It check for existing user in the DB and logs him if the password is correct.
If the user is new he is initiated and a new home planet nad solar system are generated.*/
func login(session sockjs.Session) (*Client, error) {
	nickname, player := authenticate(session)

	client := &Client{
		Session:  session,
		Nickname: nickname,
		Player:   player,
	}

	home_planet_entity, _ := db_manager.GetEntity(client.Player.HomePlanet)
	home_planet := home_planet_entity.(entities.Planet)
	session.Send([]byte(fmt.Sprintf("{\"Command\": \"login_success\", \"Username\": \"%s\", \"Position\": [%d, %d] }",
		client.Nickname, home_planet.GetCoords()[0], home_planet.GetCoords()[1])))
	return client, nil
}

/*On the first rescived message from each connection the server will call the handler.
So it can complete the following actions:
1.Adding a new session to the session pool.
2.Call the login func to validate the connection
3.If the connection is valid enters "while true" state and uses ParseRequest to parse the requests. Shocking right?!?!*/
func handler(session sockjs.Session) {
	sessions.Add(session)
	defer sessions.Remove(session)
	defer func() {
		if panicked := recover(); panicked != nil {
			return
		}
	}()

	if client, err := login(session); err == nil {
		for {
			message := session.Receive()
			if message == nil {
				break
			}

			if request, err := UnmarshalRequest(message, client); err == nil {
				if action, err := ParseRequest(request); err == nil {
					if err := action(request); err != nil {
						log.Println(err)
					}
				} else {
					log.Println("Error in server.main.handler:", err.Error())
				}
			} else {
				log.Println("Error in server.main.handler:", err.Error())
			}
		}
	} else {
		session.End()
	}
}
<commit_msg>Remove server.HOST and server.PORT<commit_after>/*
	This is the server package.
	The purpouse of this package is to map a connection to each player(who is online) so we have a comunication chanel.

*/
package server

import (
	"fmt"
	"github.com/fzzy/sockjs-go/sockjs"
	"log"
	"net"
	"net/http"
	"warcluster/db_manager"
	"warcluster/entities"
)

var listener net.Listener

var sessions *sockjs.SessionPool = sockjs.NewSessionPool() //This is the SockJs sessions pull (a list of all the currently active client's sessions).

/*This function goes trough all the procedurs needed for the werver to be initialized.
1.Create an empty connections pool
2.Starts the listening foe messages loop.*/
func Start(host string, port int) error {
	log.Print("Server is starting...")
	log.Println("Server is up and running!")
	mux := sockjs.NewServeMux(http.DefaultServeMux)
	conf := sockjs.NewConfig()

	http.HandleFunc("/console", staticHandler)
	http.Handle("/static", http.FileServer(http.Dir("./static")))
	mux.Handle("/universe", handler, conf)

	if err := ListenAndServe(fmt.Sprintf("%v:%v", host, port), mux); err != nil {
		log.Println(err)
		return err
	}

	return Stop()
}

// ListenAndServe listens on the TCP network address srv.Addr and then
// calls Serve to handle requests on incoming connections.  If
// srv.Addr is blank, ":http" is used.
func ListenAndServe(address string, mux *sockjs.ServeMux) error {
	var err error

	server := &http.Server{Addr: address, Handler: mux}
	addr := server.Addr
	if addr == "" {
		addr = ":http"
	}
	listener, err = net.Listen("tcp", addr)
	if err != nil {
		return err
	}

	return server.Serve(listener)
}

//Die biatch and get the fuck out.
func Stop() error {
	log.Println("Server is shutting down...")
	listener.Close()
	log.Println("Server has stopped.")
	return nil
}

//Returns the HTML page needed to display the debug page (server "chat" window).
func staticHandler(w http.ResponseWriter, r *http.Request) {
	http.ServeFile(w, r, "./static/index.html")
}

/*This function is called from the message handler to parse the first message for every new connection.
It check for existing user in the DB and logs him if the password is correct.
If the user is new he is initiated and a new home planet nad solar system are generated.*/
func login(session sockjs.Session) (*Client, error) {
	nickname, player := authenticate(session)

	client := &Client{
		Session:  session,
		Nickname: nickname,
		Player:   player,
	}

	home_planet_entity, _ := db_manager.GetEntity(client.Player.HomePlanet)
	home_planet := home_planet_entity.(entities.Planet)
	session.Send([]byte(fmt.Sprintf("{\"Command\": \"login_success\", \"Username\": \"%s\", \"Position\": [%d, %d] }",
		client.Nickname, home_planet.GetCoords()[0], home_planet.GetCoords()[1])))
	return client, nil
}

/*On the first rescived message from each connection the server will call the handler.
So it can complete the following actions:
1.Adding a new session to the session pool.
2.Call the login func to validate the connection
3.If the connection is valid enters "while true" state and uses ParseRequest to parse the requests. Shocking right?!?!*/
func handler(session sockjs.Session) {
	sessions.Add(session)
	defer sessions.Remove(session)
	defer func() {
		if panicked := recover(); panicked != nil {
			return
		}
	}()

	if client, err := login(session); err == nil {
		for {
			message := session.Receive()
			if message == nil {
				break
			}

			if request, err := UnmarshalRequest(message, client); err == nil {
				if action, err := ParseRequest(request); err == nil {
					if err := action(request); err != nil {
						log.Println(err)
					}
				} else {
					log.Println("Error in server.main.handler:", err.Error())
				}
			} else {
				log.Println("Error in server.main.handler:", err.Error())
			}
		}
	} else {
		session.End()
	}
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"fmt"
	"github.com/joonazan/imagick/imagick"
	"github.com/phzfi/RIC/server/cache"
	"github.com/phzfi/RIC/server/logging"
	"github.com/phzfi/RIC/server/ops"
	"github.com/valyala/fasthttp"
	"log"
	"net"
	"strconv"
	"sync/atomic"
	"time"
)

// MyHandler type is used to encompass HandlerFunc interface.
// In the future this type will probably contain pointers to
// services provided by this program (image cache).
type MyHandler struct {

	// Service started
	started time.Time

	// Request count (statistics)
	requests uint64

	operator    cache.Operator
	imageSource ops.ImageSource
}

// ServeHTTP is called whenever there is a new request.
// This is quite similar to JavaEE Servlet interface.
// TODO: Check that ServeHTTP is called inside a goroutine?
func (h *MyHandler) ServeHTTP(ctx *fasthttp.RequestCtx) {

	// In the future we can use requester can detect request spammers!
	// requester := ctx.RemoteAddr()

	// Increase request count
	count := &(h.requests)
	atomic.AddUint64(count, 1)

	if ctx.IsGet() {

		url := ctx.URI()
		operations, err := ParseURI(url, h.imageSource)
		if err != nil {
			ctx.NotFound()
			logging.Debug(err)
			return
		}
		blob, err := h.operator.GetBlob(operations...)
		if err != nil {
			ctx.NotFound()
			logging.Debug(err)
		} else {
			ctx.Write(blob)
			logging.Debug("Blob returned")
		}

	} else if ctx.IsPost() {
		// POST is currently unused so we can use this for testing
		h.RetrieveHello(ctx)
		logging.Debug("Post request received")
	}
}

func getParams(a *fasthttp.Args) (w *uint, h *uint, m string) {
	qw, e := a.GetUint("width")
	if e == nil {
		uqw := uint(qw)
		w = &uqw
	}
	qh, e := a.GetUint("height")
	if e == nil {
		uqh := uint(qh)
		h = &uqh
	}

	m = string(a.Peek("mode"))
	return
}

// Respond to POST message by saying Hello
func (h MyHandler) RetrieveHello(ctx *fasthttp.RequestCtx) {
	_, err := ctx.WriteString("Hello world!")
	if err != nil {
		log.Println(err)
	}
}

// Create a new fasthttp server and configure it.
// This does not run the server however.
func NewServer(port int, maxMemory uint64) (*fasthttp.Server, *MyHandler, net.Listener) {
	logging.Debug("Creating server")
	imageSource := ops.MakeImageSource()

	// Add roots
	// TODO: This must be externalized outside the source code.
	logging.Debug("Adding roots")
	if imageSource.AddRoot("/var/www") != nil {
		log.Fatal("Root not added /var/www")
	}

	if imageSource.AddRoot(".") != nil {
		log.Println("Root not added .")
	}

	// Configure handler
	logging.Debug("Configuring handler")
	handler := &MyHandler{
		requests:    0,
		imageSource: imageSource,
		operator:    cache.MakeOperator(maxMemory),
	}

	// Configure server
	server := &fasthttp.Server{
		Handler: handler.ServeHTTP,
	}

	logging.Debug("Beginning to listen")
	ln, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
	if err != nil {
		log.Fatal("Error creating listener:" + err.Error())
	}
	logging.Debug("Server ready")
	return server, handler, ln
}

func main() {

	// CLI arguments
	mem := flag.Uint64("m", 1424*1024*1024, "Sets the maximum memory to be used for caching images in bytes. Does not account for memory consumption of other things.")
	flag.Parse()

	imagick.Initialize()
	defer imagick.Terminate()

	log.Println("Server starting...")
	logging.Debug("Debug enabled")

	server, handler, ln := NewServer(8005, *mem)
	handler.started = time.Now()
	err := server.Serve(ln)
	end := time.Now()

	// Get number of requests
	requests := strconv.FormatUint((*handler).requests, 10)

	// Calculate the elapsed time
	duration := end.Sub(handler.started)
	log.Println("Server requests: " + requests)
	log.Println("Server uptime: " + duration.String())

	// Log errors
	if err != nil {
		log.Fatal(err)
	}
}
<commit_msg>fixed the hard coded memory value<commit_after>package main

import (
	"flag"
	"fmt"
	"github.com/joonazan/imagick/imagick"
	"github.com/phzfi/RIC/server/cache"
	"github.com/phzfi/RIC/server/logging"
	"github.com/phzfi/RIC/server/ops"
	"github.com/valyala/fasthttp"
	"log"
	"net"
	"strconv"
	"sync/atomic"
	"time"
)

// MyHandler type is used to encompass HandlerFunc interface.
// In the future this type will probably contain pointers to
// services provided by this program (image cache).
type MyHandler struct {

	// Service started
	started time.Time

	// Request count (statistics)
	requests uint64

	operator    cache.Operator
	imageSource ops.ImageSource
}

// ServeHTTP is called whenever there is a new request.
// This is quite similar to JavaEE Servlet interface.
// TODO: Check that ServeHTTP is called inside a goroutine?
func (h *MyHandler) ServeHTTP(ctx *fasthttp.RequestCtx) {

	// In the future we can use requester can detect request spammers!
	// requester := ctx.RemoteAddr()

	// Increase request count
	count := &(h.requests)
	atomic.AddUint64(count, 1)

	if ctx.IsGet() {

		url := ctx.URI()
		operations, err := ParseURI(url, h.imageSource)
		if err != nil {
			ctx.NotFound()
			logging.Debug(err)
			return
		}
		blob, err := h.operator.GetBlob(operations...)
		if err != nil {
			ctx.NotFound()
			logging.Debug(err)
		} else {
			ctx.Write(blob)
			logging.Debug("Blob returned")
		}

	} else if ctx.IsPost() {
		// POST is currently unused so we can use this for testing
		h.RetrieveHello(ctx)
		logging.Debug("Post request received")
	}
}

func getParams(a *fasthttp.Args) (w *uint, h *uint, m string) {
	qw, e := a.GetUint("width")
	if e == nil {
		uqw := uint(qw)
		w = &uqw
	}
	qh, e := a.GetUint("height")
	if e == nil {
		uqh := uint(qh)
		h = &uqh
	}

	m = string(a.Peek("mode"))
	return
}

// Respond to POST message by saying Hello
func (h MyHandler) RetrieveHello(ctx *fasthttp.RequestCtx) {
	_, err := ctx.WriteString("Hello world!")
	if err != nil {
		log.Println(err)
	}
}

// Create a new fasthttp server and configure it.
// This does not run the server however.
func NewServer(port int, maxMemory uint64) (*fasthttp.Server, *MyHandler, net.Listener) {
	logging.Debug("Creating server")
	imageSource := ops.MakeImageSource()

	// Add roots
	// TODO: This must be externalized outside the source code.
	logging.Debug("Adding roots")
	if imageSource.AddRoot("/var/www") != nil {
		log.Fatal("Root not added /var/www")
	}

	if imageSource.AddRoot(".") != nil {
		log.Println("Root not added .")
	}

	// Configure handler
	logging.Debug("Configuring handler")
	handler := &MyHandler{
		requests:    0,
		imageSource: imageSource,
		operator:    cache.MakeOperator(maxMemory),
	}

	// Configure server
	server := &fasthttp.Server{
		Handler: handler.ServeHTTP,
	}

	logging.Debug("Beginning to listen")
	ln, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
	if err != nil {
		log.Fatal("Error creating listener:" + err.Error())
	}
	logging.Debug("Server ready")
	return server, handler, ln
}

func main() {

	// CLI arguments
	mem := flag.Uint64("m", 500*1024*1024, "Sets the maximum memory to be used for caching images in bytes. Does not account for memory consumption of other things.")
	flag.Parse()

	imagick.Initialize()
	defer imagick.Terminate()

	log.Println("Server starting...")
	logging.Debug("Debug enabled")

	server, handler, ln := NewServer(8005, *mem)
	handler.started = time.Now()
	err := server.Serve(ln)
	end := time.Now()

	// Get number of requests
	requests := strconv.FormatUint((*handler).requests, 10)

	// Calculate the elapsed time
	duration := end.Sub(handler.started)
	log.Println("Server requests: " + requests)
	log.Println("Server uptime: " + duration.String())

	// Log errors
	if err != nil {
		log.Fatal(err)
	}
}
<|endoftext|>


<commit_before>package neo4j

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	// "koding/tools/config"
	"log"
	"net/http"
	"strings"
)

var (
	// todo update this constants, here must be only config file related strings after config files updated
	// BASE_URL         = config.Current.Neo4j.Url + config.Current.Neo4j.Port  // "http://localhost:7474"
	BASE_URL         = "http://localhost:7474"
	NODE_PATH        = "/db/data/index/node/koding/"
	UNIQUE_NODE_PATH = "/db/data/index/node/koding?unique"
	INDEX_PATH       = "/db/data/index/node"
)

// Gets URL and string data to be sent and makes POST request
// reads response body and returns as string
func sendRequest(requestType, url, data string) string {

	//convert string into bytestream
	dataByte := strings.NewReader(data)
	req, err := http.NewRequest(requestType, url, dataByte)

	// read response body
	req.Header.Set("Accept", "application/json")
	req.Header.Set("Content-Type", "application/json")

	res, err := http.DefaultClient.Do(req)
	if err != nil {
		log.Fatal(err)
	}

	body, _ := ioutil.ReadAll(res.Body)

	defer res.Body.Close()

	return string(body)

}

// connect source and target with relation property
func CreateRelationship(relation, source, target string) map[string]interface{} {

	relationshipData := fmt.Sprintf(`{"to" : "%s", "type" : "%s" }`, target, relation)
	relRes := sendRequest("POST", fmt.Sprintf("%s", source), relationshipData)

	relNode, err := jsonDecode(relRes)
	if err != nil {
		fmt.Println("Problem with relation response", relRes)
	}

	return relNode
}

// creates a unique node with given id and node name
func CreateUniqueNode(id string, name string) map[string]interface{} {

	url := BASE_URL + UNIQUE_NODE_PATH

	postData := generatePostJsonData(id, name)

	response := sendRequest("POST", url, postData)

	nodeData, err := jsonDecode(response)
	if err != nil {
		fmt.Println("Problem with response", response)
	}

	return nodeData
}

// creates a unique node with given id and node name
func DeleteNode(id string) map[string]interface{} {

	url := BASE_URL + NODE_PATH

	response := sendRequest("DELETE", url, "")

	nodeData, err := jsonDecode(response)
	if err != nil {
		fmt.Println("Problem with response", response)
	}

	return nodeData
}

// creates a unique tree head node to hold all nodes
// it is called once during runtime while initializing
func CreateUniqueIndex(name string) {
	//create unique index
	url := BASE_URL + INDEX_PATH

	bd := sendRequest("POST", url, `{"name":"`+name+`"}`)

	fmt.Println("Created unique index for data", bd)
}

// This is a custom json string generator as http request body to neo4j
func generatePostJsonData(id, name string) string {
	return fmt.Sprintf(`{ "key" : "id", "value" : "%s", "properties" : { "id" : "%s", "name" : "%s" } }`, id, id, name)
}

//here, mapping of decoded json
func jsonDecode(data string) (map[string]interface{}, error) {
	var source map[string]interface{}

	err := json.Unmarshal([]byte(data), &source)
	if err != nil {
		fmt.Println("Marshalling error:", err)
		return nil, err
	}

	return source, nil
}
<commit_msg>[go][koding][database][neo4j] moved node deletion method<commit_after>package neo4j

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	// "koding/tools/config"
	"log"
	"net/http"
	"strings"
)

var (
	// todo update this constants, here must be only config file related strings after config files updated
	// BASE_URL         = config.Current.Neo4j.Url + config.Current.Neo4j.Port  // "http://localhost:7474"
	BASE_URL         = "http://localhost:7474"
	INDEX_NODE_PATH  = "/db/data/index/node/koding"
	UNIQUE_NODE_PATH = "/db/data/index/node/koding?unique"
	INDEX_PATH       = "/db/data/index/node"
	NODE_URL         = "/db/data/node"
)

// Gets URL and string data to be sent and makes POST request
// reads response body and returns as string
func sendRequest(requestType, url, data string) string {

	//convert string into bytestream
	dataByte := strings.NewReader(data)
	req, err := http.NewRequest(requestType, url, dataByte)

	// read response body
	req.Header.Set("Accept", "application/json")
	req.Header.Set("Content-Type", "application/json")

	res, err := http.DefaultClient.Do(req)
	if err != nil {
		log.Fatal(err)
	}

	body, _ := ioutil.ReadAll(res.Body)

	defer res.Body.Close()

	return string(body)

}

// connect source and target with relation property
func CreateRelationship(relation, source, target string) map[string]interface{} {

	relationshipData := fmt.Sprintf(`{"to" : "%s", "type" : "%s" }`, target, relation)
	relRes := sendRequest("POST", fmt.Sprintf("%s", source), relationshipData)

	relNode, err := jsonDecode(relRes)
	if err != nil {
		fmt.Println("Problem with relation response", relRes)
	}

	return relNode
}

// creates a unique node with given id and node name
func CreateUniqueNode(id string, name string) map[string]interface{} {

	url := BASE_URL + UNIQUE_NODE_PATH

	postData := generatePostJsonData(id, name)

	response := sendRequest("POST", url, postData)

	nodeData, err := jsonDecode(response)
	if err != nil {
		fmt.Println("Problem with response", response)
	}

	return nodeData
}

func DeleteNodeRelationships(nodeUrl string) {
	relationshipsURL := nodeUrl + "/relationships/all"

	response := sendRequest("GET", relationshipsURL, "")

	relationships, err := jsonDecode(response)
	if err != nil {
		fmt.Println("Problem with response", response)
	}

	fmt.Println(relationships)

}

func GetNode(id string) map[string]interface{} {

	url := BASE_URL + INDEX_NODE_PATH + "/id/" + id

	response := sendRequest("GET", url, "")

	nodeData, err := jsonDecode(response)
	if err != nil {
		fmt.Println("Problem with response", response)
	}

	return nodeData
}

// creates a unique node with given id and node name
func DeleteNode(id string) bool {

	// url := BASE_URL + INDEX_NODE_PATH + "/id/" + id

	node := GetNode(id)

	nodeURL := node["self"]

	DeleteNodeRelationships(fmt.Sprintf("%s", nodeURL))

	// response := sendRequest("DELETE", url, "")

	// nodeData, err := jsonDecode(response)
	// if err != nil {
	// 	fmt.Println("Problem with response", response)
	// }

	return true
}

// creates a unique tree head node to hold all nodes
// it is called once during runtime while initializing
func CreateUniqueIndex(name string) {
	//create unique index
	url := BASE_URL + INDEX_PATH

	bd := sendRequest("POST", url, `{"name":"`+name+`"}`)

	fmt.Println("Created unique index for data", bd)
}

// This is a custom json string generator as http request body to neo4j
func generatePostJsonData(id, name string) string {
	return fmt.Sprintf(`{ "key" : "id", "value" : "%s", "properties" : { "id" : "%s", "name" : "%s" } }`, id, id, name)
}

//here, mapping of decoded json
func jsonDecode(data string) (map[string]interface{}, error) {
	var source map[string]interface{}

	err := json.Unmarshal([]byte(data), &source)
	if err != nil {
		fmt.Println("Marshalling error:", err)
		return nil, err
	}

	return source, nil
}
<|endoftext|>