go-bgp

a collection of golang BGP tools to monitor, archive and serve
git clone git://git.2f30.org/go-bgp
Log | Files | Refs | README

commit eeee7a46703a4fb58b8f095aacd1f2cb21969cc9
parent 4abe36f136f908551e2f44eba6c6982926722c88
Author: dsp <dsp@2f30.org>
Date:   Thu, 12 Feb 2015 15:28:42 -0700

implemented a queryParam struct to pack functionality and added type awareness in queries

Diffstat:
Marchive/api.go | 1+
Marchive/archive.go | 55++++++++++++++++++++++++++++++++++++++++++++++---------
2 files changed, 47 insertions(+), 9 deletions(-)

diff --git a/archive/api.go b/archive/api.go @@ -20,6 +20,7 @@ var ( errbaddate = errors.New("dates should be in a YYYYMMDDHHMM format and start should be earlier than end") errempty = errors.New("archive empty") errdate = errors.New("no such date in archive") + errbadtype = errors.New(fmt.Sprintf("Supported file types:%v",ftypestrings[1:ftypesize-1])) ) type Resource interface { diff --git a/archive/archive.go b/archive/archive.go @@ -22,13 +22,15 @@ import ( //constants for archentryfile.ftype const ( - mrtfile = iota + unknownfile = iota + mrtfile xmlfile ftypesize ) var ( ftypestrings = [ftypesize]string { + "UNKNOWN", "MRT", "XML", } @@ -43,10 +45,18 @@ type reply struct { //that all write their results to chan reply, and we also need the waitgroup //to know when we should close the channel to end the http transaction type archive interface { - Query(time.Time, time.Time, chan reply, *sync.WaitGroup) + Query(queryParams) visit(string, os.FileInfo, error) error } +type queryParams struct { + from time.Time + to time.Time + rtype int + rch chan reply + wg *sync.WaitGroup +} + type xmlstring struct { timestr string msg string @@ -82,8 +92,8 @@ func (p timeentryslice) Swap(i, j int) { type fsarchive struct { rootpathstr string entryfiles *timeentryslice - tempentryfiles timeentryslice - curyr int + tempentryfiles timeentryslice //once we finish operating on that, we change the entryfiles pointer + curyr int //in case we need to walk dirs to find dates,these keep state curmon int curday int reqchan chan string @@ -91,7 +101,7 @@ type fsarchive struct { Scanwg *sync.WaitGroup // expose it so callers are able to wait for scan to finish scanch chan struct{} timedelta time.Duration - descriminator string + descriminator string //a string to be matched if a file is to be added while visiting the fs hier Conf *fsarconf //present tha archive as a restful resource PutNotAllowed @@ -141,14 +151,34 @@ func (fsc *fsarconf) Get(values url.Values) (int, chan reply) { } func (fsa *fsarchive) Get(values url.Values) (int, chan reply) { - var grwg sync.WaitGroup + var ( + grwg sync.WaitGroup + ftype int + ) retc := make(chan reply) timeAstrs, ok1 := values["start"] timeBstrs, ok2 := values["end"] + tpstr, ok3 := values["type"] if len(timeAstrs) != len(timeBstrs) || !ok1 || !ok2 { retc <- reply{data: nil, err: errbadreq} goto done } + if !ok3 { //no type specified. ensuring it's 0 (unknownfile) + ftype = unknownfile + } else { + for tpi, tps := range ftypestrings { + //XXX: here i only respect the first type argument + if tps == tpstr[0] { + ftype = tpi + break + } + if tpi == ftypesize - 1 { //we didn't find it let the user know + log.Printf("unknown return type %s in request", tpstr[0]) + retc <- reply{data: nil, err: errbadtype} + goto done + } + } + } //This will parse pairs of start=YYYYMMDDHHMMSS&end=YYYYMMDDHHMMSS requests for i := 0; i < len(timeAstrs); i++ { log.Printf("timeAstr:%s timeBstr:%s", timeAstrs[i], timeBstrs[i]) @@ -158,7 +188,8 @@ func (fsa *fsarchive) Get(values url.Values) (int, chan reply) { retc <- reply{data: nil, err: errbaddate} } else { //buf.WriteString(fmt.Sprintf("quering from t0:%s - t1:%s\n", timeA, timeB)) - fsa.Query(timeA, timeB, retc, &grwg) //this will fire a new goroutine + qp := queryParams{from: timeA, to: timeB, rtype: ftype, rch: retc, wg: &grwg} + fsa.Query(qp) //this will fire a new goroutine } } // the last goroutine that will wait for all we invoked and close the chan @@ -171,8 +202,14 @@ done: return 200, retc } -func (fsar *fsarchive) Query(ta, tb time.Time, retc chan reply, wg *sync.WaitGroup) { - log.Printf("querying mrt from %s to %s\n", ta, tb) +//func (fsar *fsarchive) Query(ta, tb time.Time, retc chan reply, wg *sync.WaitGroup) { +func (fsar *fsarchive) Query(qp queryParams) { + ta := qp.from + tb := qp.to + retc := qp.rch + rt := qp.rtype + wg := qp.wg + log.Printf("querying from %s to %s return as:%s \n", ta, tb, ftypestrings[rt]) go func(rc chan<- reply) { wg.Add(1) ef := *fsar.entryfiles