From 5710f99b671643fb551f913dfc12df291cd2e391 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Tue, 28 Feb 2012 14:16:03 +0200 Subject: [PATCH] renamed inquirer into balancer --- .../inquirer.go => balancer/balancer.go} | 0 .../balancer_test.go} | 0 cmd/{inquirer => balancer}/http_responder.go | 0 .../jsonrpc_responder.go | 0 .../raterlist_test.go} | 0 cmd/{inquirer => balancer}/registration.go | 0 cmd/inquirer/balancer.go | 97 ------------------- 7 files changed, 97 deletions(-) rename cmd/{inquirer/inquirer.go => balancer/balancer.go} (100%) rename cmd/{inquirer/inquirer_test.go => balancer/balancer_test.go} (100%) rename cmd/{inquirer => balancer}/http_responder.go (100%) rename cmd/{inquirer => balancer}/jsonrpc_responder.go (100%) rename cmd/{inquirer/balancer_test.go => balancer/raterlist_test.go} (100%) rename cmd/{inquirer => balancer}/registration.go (100%) delete mode 100644 cmd/inquirer/balancer.go diff --git a/cmd/inquirer/inquirer.go b/cmd/balancer/balancer.go similarity index 100% rename from cmd/inquirer/inquirer.go rename to cmd/balancer/balancer.go diff --git a/cmd/inquirer/inquirer_test.go b/cmd/balancer/balancer_test.go similarity index 100% rename from cmd/inquirer/inquirer_test.go rename to cmd/balancer/balancer_test.go diff --git a/cmd/inquirer/http_responder.go b/cmd/balancer/http_responder.go similarity index 100% rename from cmd/inquirer/http_responder.go rename to cmd/balancer/http_responder.go diff --git a/cmd/inquirer/jsonrpc_responder.go b/cmd/balancer/jsonrpc_responder.go similarity index 100% rename from cmd/inquirer/jsonrpc_responder.go rename to cmd/balancer/jsonrpc_responder.go diff --git a/cmd/inquirer/balancer_test.go b/cmd/balancer/raterlist_test.go similarity index 100% rename from cmd/inquirer/balancer_test.go rename to cmd/balancer/raterlist_test.go diff --git a/cmd/inquirer/registration.go b/cmd/balancer/registration.go similarity index 100% rename from cmd/inquirer/registration.go rename to cmd/balancer/registration.go diff --git a/cmd/inquirer/balancer.go b/cmd/inquirer/balancer.go deleted file mode 100644 index 26a46db4a..000000000 --- a/cmd/inquirer/balancer.go +++ /dev/null @@ -1,97 +0,0 @@ -/* -Rating system designed to be used in VoIP Carriers World -Copyright (C) 2012 Radu Ioan Fericean - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see -*/ -package main - -import ( - "net/rpc" - "sync" -) - -type RaterList struct { - clientAddresses []string - clientConnections []*rpc.Client - balancerIndex int - mu sync.RWMutex -} - -/* -Constructor for RateList holding one slice for addreses and one slice for connections. -*/ -func NewRaterList() *RaterList { - r := &RaterList{balancerIndex: 0} // leaving both slices to nil - return r -} - -/* -Adds a client to the two internal slices. -*/ -func (rl *RaterList) AddClient(address string, client *rpc.Client) { - rl.clientAddresses = append(rl.clientAddresses, address) - rl.clientConnections = append(rl.clientConnections, client) - return -} - -/* -Removes a client from the slices locking the readers and reseting the balancer index. -*/ -func (rl *RaterList) RemoveClient(address string) { - index := -1 - for i, v := range rl.clientAddresses { - if v == address { - index = i - break - } - } - if index == -1 { - return - } - rl.mu.RLock() - defer rl.mu.RUnlock() - rl.clientAddresses = append(rl.clientAddresses[:index], rl.clientAddresses[index+1:]...) - rl.clientConnections = append(rl.clientConnections[:index], rl.clientConnections[index+1:]...) - rl.balancerIndex = 0 -} - -/* -Returns a client for the specifed address. -*/ -func (rl *RaterList) GetClient(address string) (*rpc.Client, bool) { - for i, v := range rl.clientAddresses { - if v == address { - return rl.clientConnections[i], true - } - } - return nil, false -} - -/* -Returns the next available connection at each call looping at the end of connections. -*/ -func (rl *RaterList) Balance() (result *rpc.Client) { - rl.mu.Lock() - defer rl.mu.Unlock() - if rl.balancerIndex >= len(rl.clientAddresses) { - rl.balancerIndex = 0 - } - if len(rl.clientAddresses) > 0 { - result = rl.clientConnections[rl.balancerIndex] - rl.balancerIndex++ - } - - return -}