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

package uniqush

import (
	//"goconf.googlecode.com/hg"
	"monnand-goconf.googlecode.com/hg"
	"io"
	"os"
	"strings"
)

func (s *UniqushSystem) loadLogInfo(c *conf.ConfigFile, field string, prefix string) (*Logger, error) {
	var loglevel string
	var logswitch bool
	var err error
	var writer io.Writer

	logswitch, err = c.GetBool(field, "log")
	if err != nil {
		logswitch = true
	}

	writer = s.logfile
	if writer == nil {
		writer = os.Stderr
	}

	loglevel, err = c.GetString(field, "loglevel")
	if err != nil {
		loglevel = "standard"
	}
	var level int

	if logswitch {
		switch strings.ToLower(loglevel) {
		case "standard":
			level = LOGLEVEL_INFO
		case "verbose":
			level = LOGLEVEL_DEBUG
		default:
			level = LOGLEVEL_INFO
		}
	} else {
		level = LOGLEVEL_FATAL
	}

	logger := NewLogger(writer, prefix, level)
	return logger, nil
}

func loadDatabaseConfig(cf *conf.ConfigFile) (*DatabaseConfig, error) {
	var err error
	c := new(DatabaseConfig)
	c.psm = GetPushServiceManager()
	c.Engine, err = cf.GetString("Database", "engine")
	if err != nil || c.Engine == "" {
		c.Engine = "redis"
	}
	c.Name, err = cf.GetString("Database", "name")
	if err != nil || c.Name == "" {
		c.Name = "0"
	}
	c.Port, err = cf.GetInt("Database", "port")
	if err != nil || c.Port <= 0 {
		c.Port = -1
	}
	c.Host, err = cf.GetString("Database", "host")
	if err != nil || c.Host == "" {
		c.Host = "localhost"
	}
	c.Password, err = cf.GetString("Database", "password")
	if err != nil {
		c.Password = ""
	}
	i, e := cf.GetInt("Database", "everysec")
	c.EverySec = int64(i)
	if e != nil || c.EverySec <= 60 {
		c.EverySec = 600
	}
	c.LeastDirty, err = cf.GetInt("Database", "leastdirty")
	if err != nil || c.LeastDirty < 0 {
		c.LeastDirty = 10
	}
	c.CacheSize, err = cf.GetInt("Database", "cachesize")
	if err != nil || c.CacheSize < 0 {
		c.CacheSize = 1024
	}

	return c, nil
}

type UniqushSystem struct {
	Frontend UniqushFrontEnd
	Backend  UniqushBackEndIf
	Stopch   chan bool
	Bridge   chan *Request
	Database DatabaseFrontDeskIf
	psm      *PushServiceManager
	logfile  io.WriteCloser
}

var (
	defaultConfigFilePath string = "/etc/uniqush/uniqush.conf"
)

func (s *UniqushSystem) Finalize() {
	s.logfile.Close()
	s.Database.FlushCache()
	s.psm.Finalize()
}

func LoadUniqushSystem(filename string) (*UniqushSystem, error) {
	if filename == "" {
		filename = defaultConfigFilePath
	}
	c, err := conf.ReadConfigFile(filename)
	if err != nil {
		return nil, err
	}

	ret := new(UniqushSystem)
	ret.Stopch = make(chan bool)
	ret.Bridge = make(chan *Request)
	ret.logfile = os.Stderr
	ew := NewEventWriter(&NullWriter{})

	logfilename, err := c.GetString("default", "logfile")
	if err == nil && logfilename != "" {
		ret.logfile, err = os.OpenFile(logfilename,
			os.O_WRONLY|
				os.O_APPEND|
				os.O_CREATE, 0600)
		if err != nil {
			ret.logfile = os.Stderr
		}
	}

	logger, e10 := ret.loadLogInfo(c, "WebFrontend", "[WebFrontend]")
	if e10 != nil {
		return nil, e10
	}
	addr, e20 := c.GetString("WebFrontend", "addr")
	if e20 != nil || addr == "" {
		addr = "localhost:9898"
	}

	psm := GetPushServiceManager()
	ret.psm = psm

	ret.Frontend = NewWebFrontEnd(ret.Bridge, logger, addr, psm)
	ret.Frontend.SetStopChannel(ret.Stopch)
	ret.Frontend.SetEventWriter(ew)

	logger, e10 = ret.loadLogInfo(c, "Backend", "[Backend]")
	if e10 != nil {
		return nil, e10
	}
	ret.Backend = NewUniqushBackEnd(ret.Bridge, logger)

	var p RequestProcessor

	// Load Database
	dbconf, e0 := loadDatabaseConfig(c)
	if e0 != nil {
		return nil, e0
	}
	//dbf, e30 := NewDatabaseFrontDeskWithoutCache(dbconf)
	dbf, e30 := NewDatabaseFrontDesk(dbconf)

	if e30 != nil {
		return nil, e30
	}
	ret.Database = dbf

	// Load Processors
	logger, e10 = ret.loadLogInfo(c, "AddPushServiceProvider", "[AddPushServiceProvider]")
	if e10 != nil {
		return nil, e10
	}
	p = NewAddPushServiceProviderProcessor(logger, ew, dbf)
	ret.Backend.SetProcessor(ACTION_ADD_PUSH_SERVICE_PROVIDER, p)

	logger, e10 = ret.loadLogInfo(c, "RemovePushServiceProvider", "[RemovePushServiceProvider]")
	if e10 != nil {
		return nil, e10
	}
	p = NewRemovePushServiceProviderProcessor(logger, ew, dbf)
	ret.Backend.SetProcessor(ACTION_REMOVE_PUSH_SERVICE_PROVIDER, p)

	logger, e10 = ret.loadLogInfo(c, "Subscribe", "[Subscribe]")
	if e10 != nil {
		return nil, e10
	}
	p = NewSubscribeProcessor(logger, ew, dbf)
	ret.Backend.SetProcessor(ACTION_SUBSCRIBE, p)

	logger, e10 = ret.loadLogInfo(c, "Unsubscribe", "[Unsubscribe]")
	if e10 != nil {
		return nil, e10
	}
	p = NewUnsubscribeProcessor(logger, ew, dbf)
	ret.Backend.SetProcessor(ACTION_UNSUBSCRIBE, p)

	logger, e10 = ret.loadLogInfo(c, "Push", "[Push]")
	if e10 != nil {
		return nil, e10
	}
	p = NewPushProcessor(logger, ew, dbf, ret.Bridge, psm)
	ret.Backend.SetProcessor(ACTION_PUSH, p)
	return ret, nil
}

func (s *UniqushSystem) Run() {
	defer s.Finalize()
	go s.Frontend.Run()
	go s.Backend.Run()
	<-s.Stopch
}
<commit_msg>changed the import of goconf to monnand-goconf<commit_after>/*
 * Copyright 2011 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package uniqush

import (
	//"goconf.googlecode.com/hg"
    "code.google.com/p/monnand-goconf"
	"io"
	"os"
	"strings"
)

func (s *UniqushSystem) loadLogInfo(c *conf.ConfigFile, field string, prefix string) (*Logger, error) {
	var loglevel string
	var logswitch bool
	var err error
	var writer io.Writer

	logswitch, err = c.GetBool(field, "log")
	if err != nil {
		logswitch = true
	}

	writer = s.logfile
	if writer == nil {
		writer = os.Stderr
	}

	loglevel, err = c.GetString(field, "loglevel")
	if err != nil {
		loglevel = "standard"
	}
	var level int

	if logswitch {
		switch strings.ToLower(loglevel) {
		case "standard":
			level = LOGLEVEL_INFO
		case "verbose":
			level = LOGLEVEL_DEBUG
		default:
			level = LOGLEVEL_INFO
		}
	} else {
		level = LOGLEVEL_FATAL
	}

	logger := NewLogger(writer, prefix, level)
	return logger, nil
}

func loadDatabaseConfig(cf *conf.ConfigFile) (*DatabaseConfig, error) {
	var err error
	c := new(DatabaseConfig)
	c.psm = GetPushServiceManager()
	c.Engine, err = cf.GetString("Database", "engine")
	if err != nil || c.Engine == "" {
		c.Engine = "redis"
	}
	c.Name, err = cf.GetString("Database", "name")
	if err != nil || c.Name == "" {
		c.Name = "0"
	}
	c.Port, err = cf.GetInt("Database", "port")
	if err != nil || c.Port <= 0 {
		c.Port = -1
	}
	c.Host, err = cf.GetString("Database", "host")
	if err != nil || c.Host == "" {
		c.Host = "localhost"
	}
	c.Password, err = cf.GetString("Database", "password")
	if err != nil {
		c.Password = ""
	}
	i, e := cf.GetInt("Database", "everysec")
	c.EverySec = int64(i)
	if e != nil || c.EverySec <= 60 {
		c.EverySec = 600
	}
	c.LeastDirty, err = cf.GetInt("Database", "leastdirty")
	if err != nil || c.LeastDirty < 0 {
		c.LeastDirty = 10
	}
	c.CacheSize, err = cf.GetInt("Database", "cachesize")
	if err != nil || c.CacheSize < 0 {
		c.CacheSize = 1024
	}

	return c, nil
}

type UniqushSystem struct {
	Frontend UniqushFrontEnd
	Backend  UniqushBackEndIf
	Stopch   chan bool
	Bridge   chan *Request
	Database DatabaseFrontDeskIf
	psm      *PushServiceManager
	logfile  io.WriteCloser
}

var (
	defaultConfigFilePath string = "/etc/uniqush/uniqush.conf"
)

func (s *UniqushSystem) Finalize() {
	s.logfile.Close()
	s.Database.FlushCache()
	s.psm.Finalize()
}

func LoadUniqushSystem(filename string) (*UniqushSystem, error) {
	if filename == "" {
		filename = defaultConfigFilePath
	}
	c, err := conf.ReadConfigFile(filename)
	if err != nil {
		return nil, err
	}

	ret := new(UniqushSystem)
	ret.Stopch = make(chan bool)
	ret.Bridge = make(chan *Request)
	ret.logfile = os.Stderr
	ew := NewEventWriter(&NullWriter{})

	logfilename, err := c.GetString("default", "logfile")
	if err == nil && logfilename != "" {
		ret.logfile, err = os.OpenFile(logfilename,
			os.O_WRONLY|
				os.O_APPEND|
				os.O_CREATE, 0600)
		if err != nil {
			ret.logfile = os.Stderr
		}
	}

	logger, e10 := ret.loadLogInfo(c, "WebFrontend", "[WebFrontend]")
	if e10 != nil {
		return nil, e10
	}
	addr, e20 := c.GetString("WebFrontend", "addr")
	if e20 != nil || addr == "" {
		addr = "localhost:9898"
	}

	psm := GetPushServiceManager()
	ret.psm = psm

	ret.Frontend = NewWebFrontEnd(ret.Bridge, logger, addr, psm)
	ret.Frontend.SetStopChannel(ret.Stopch)
	ret.Frontend.SetEventWriter(ew)

	logger, e10 = ret.loadLogInfo(c, "Backend", "[Backend]")
	if e10 != nil {
		return nil, e10
	}
	ret.Backend = NewUniqushBackEnd(ret.Bridge, logger)

	var p RequestProcessor

	// Load Database
	dbconf, e0 := loadDatabaseConfig(c)
	if e0 != nil {
		return nil, e0
	}
	//dbf, e30 := NewDatabaseFrontDeskWithoutCache(dbconf)
	dbf, e30 := NewDatabaseFrontDesk(dbconf)

	if e30 != nil {
		return nil, e30
	}
	ret.Database = dbf

	// Load Processors
	logger, e10 = ret.loadLogInfo(c, "AddPushServiceProvider", "[AddPushServiceProvider]")
	if e10 != nil {
		return nil, e10
	}
	p = NewAddPushServiceProviderProcessor(logger, ew, dbf)
	ret.Backend.SetProcessor(ACTION_ADD_PUSH_SERVICE_PROVIDER, p)

	logger, e10 = ret.loadLogInfo(c, "RemovePushServiceProvider", "[RemovePushServiceProvider]")
	if e10 != nil {
		return nil, e10
	}
	p = NewRemovePushServiceProviderProcessor(logger, ew, dbf)
	ret.Backend.SetProcessor(ACTION_REMOVE_PUSH_SERVICE_PROVIDER, p)

	logger, e10 = ret.loadLogInfo(c, "Subscribe", "[Subscribe]")
	if e10 != nil {
		return nil, e10
	}
	p = NewSubscribeProcessor(logger, ew, dbf)
	ret.Backend.SetProcessor(ACTION_SUBSCRIBE, p)

	logger, e10 = ret.loadLogInfo(c, "Unsubscribe", "[Unsubscribe]")
	if e10 != nil {
		return nil, e10
	}
	p = NewUnsubscribeProcessor(logger, ew, dbf)
	ret.Backend.SetProcessor(ACTION_UNSUBSCRIBE, p)

	logger, e10 = ret.loadLogInfo(c, "Push", "[Push]")
	if e10 != nil {
		return nil, e10
	}
	p = NewPushProcessor(logger, ew, dbf, ret.Bridge, psm)
	ret.Backend.SetProcessor(ACTION_PUSH, p)
	return ret, nil
}

func (s *UniqushSystem) Run() {
	defer s.Finalize()
	go s.Frontend.Run()
	go s.Backend.Run()
	<-s.Stopch
}

{'fraction_non_alphanumeric': 0.13807380738073807, 'fraction_numerical': 0.01638163816381638, 'mean_word_length': 3.873245614035088, 'pattern_counts': {'":': 4, '<': 15, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4515470', 'n_tokens_mistral': 4367, 'n_tokens_neox': 4147, 'n_words': 1050}