diff --git a/agents/httpagent.go b/agents/httpagent.go new file mode 100644 index 000000000..92428e3a2 --- /dev/null +++ b/agents/httpagent.go @@ -0,0 +1,47 @@ +/* +Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments +Copyright (C) ITsysCOM GmbH + +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 agents + +import ( + "net/http" + + "github.com/cgrates/cgrates/config" + "github.com/cgrates/rpcclient" +) + +// NewHttpAgent will construct a HttpAgent +func NewHttpAgent(sessionS rpcclient.RpcClientConnection, + timezone, reqPayload, rplyPayload string, + reqProcessors []*config.HttpAgntProcCfg) *HttpAgent { + return &HttpAgent{sessionS: sessionS, timezone: timezone, + reqPayload: reqPayload, rplyPayload: rplyPayload, + reqProcessors: reqProcessors} +} + +type HttpAgent struct { + sessionS rpcclient.RpcClientConnection + timezone, + reqPayload, + rplyPayload string + reqProcessors []*config.HttpAgntProcCfg +} + +// ServeHTTP implements http.Handler interface +func (ha *HttpAgent) ServeHTTP(w http.ResponseWriter, req *http.Request) { +} diff --git a/cmd/cgr-engine/cgr-engine.go b/cmd/cgr-engine/cgr-engine.go index 1d39845c1..a844d09ac 100644 --- a/cmd/cgr-engine/cgr-engine.go +++ b/cmd/cgr-engine/cgr-engine.go @@ -350,6 +350,30 @@ func startKamAgent(internalSMGChan chan rpcclient.RpcClientConnection, exitChan exitChan <- true } +func startHTTPAgent(internalSMGChan chan rpcclient.RpcClientConnection, exitChan chan bool, server *utils.Server) { + utils.Logger.Info("Starting HTTP agent") + var err error + for _, agntCfg := range cfg.HttpAgentCfg() { + var sSConn *rpcclient.RpcClientPool + if len(agntCfg.SessionSConns) != 0 { + sSConn, err = engine.NewRPCPool(rpcclient.POOL_FIRST, cfg.TLSClientKey, + cfg.TLSClientCerificate, cfg.ConnectAttempts, cfg.Reconnects, + cfg.ConnectTimeout, cfg.ReplyTimeout, + agntCfg.SessionSConns, internalSMGChan, cfg.InternalTtl) + if err != nil { + utils.Logger.Crit(fmt.Sprintf("<%s> could not connect to %s, error: %s", + utils.HTTPAgent, utils.SessionS, err.Error())) + exitChan <- true + return + } + } + server.RegisterHttpHandler(agntCfg.Url, + agents.NewHttpAgent(sSConn, agntCfg.Timezone, agntCfg.RequestPayload, + agntCfg.ReplyPayload, agntCfg.RequestProcessors)) + } + exitChan <- true +} + func startCDRS(internalCdrSChan chan rpcclient.RpcClientConnection, cdrDb engine.CdrStorage, dm *engine.DataManager, internalRaterChan, internalPubSubSChan, internalAttributeSChan, internalUserSChan, internalAliaseSChan, diff --git a/utils/consts.go b/utils/consts.go index fc0d4f5b8..937a01bcf 100755 --- a/utils/consts.go +++ b/utils/consts.go @@ -871,6 +871,7 @@ const ( DiameterAgent = "DiameterAgent" FreeSWITCHAgent = "FreeSWITCHAgent" AsteriskAgent = "AsteriskAgent" + HTTPAgent = "HTTPAgent" ) func buildCacheInstRevPrefixes() { diff --git a/utils/server.go b/utils/server.go index b74615d15..95576a5da 100644 --- a/utils/server.go +++ b/utils/server.go @@ -68,6 +68,13 @@ func (s *Server) RegisterHttpFunc(pattern string, handler func(http.ResponseWrit s.Unlock() } +func (s *Server) RegisterHttpHandler(pattern string, handler http.Handler) { + http.Handle(pattern, handler) + s.Lock() + s.httpEnabled = true + s.Unlock() +} + // Registers a new BiJsonRpc name func (s *Server) BiRPCRegisterName(method string, handlerFunc interface{}) { s.RLock()