-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f1cac10
commit 63d4e50
Showing
3 changed files
with
194 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,80 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
p "github.com/alessio-perugini/peng" | ||
"log" | ||
"net/url" | ||
"os" | ||
) | ||
|
||
var ( | ||
config = p.Config{ | ||
NumberOfBin: 8, | ||
NumberOfModule: 1024, | ||
InfluxUrl: "http://localhost", | ||
InfluxPort: 9999, | ||
InfluxBucket: "", | ||
InfluxOrganization: "", | ||
InfluxAuthToken: "", | ||
} | ||
|
||
versionFlag bool | ||
version = "0.0.0" | ||
commit = "commithash" | ||
) | ||
|
||
func init() { | ||
//Bitmap | ||
flag.UintVar(&config.InfluxPort, "bin", 128, "number of bin in your bitmap") | ||
flag.UintVar(&config.InfluxPort, "module", 1024, "maximum size of your bitmap") | ||
|
||
//influx | ||
flag.StringVar(&config.InfluxUrl, "influxUrl", "http://localhost", "influx url") | ||
flag.UintVar(&config.InfluxPort, "influxPort", 9999, "influxPort number") | ||
flag.StringVar(&config.InfluxBucket, "bucket", "", "bucket string for telegraf") | ||
flag.StringVar(&config.InfluxOrganization, "org", "", "organization string for telegraf") | ||
flag.StringVar(&config.InfluxAuthToken, "token", "", "auth token for influxdb") | ||
|
||
//other | ||
flag.BoolVar(&versionFlag, "version", false, "output version") | ||
} | ||
|
||
func flagConfig() { | ||
appString := fmt.Sprintf("sys-status version %s %s", version, commit) | ||
|
||
flag.Usage = func() { //help flag | ||
fmt.Fprintf(flag.CommandLine.Output(), "%s\n\nUsage: sys-status [options]\n", appString) | ||
flag.PrintDefaults() | ||
} | ||
|
||
flag.Parse() | ||
|
||
if versionFlag { //version flag | ||
fmt.Fprintf(flag.CommandLine.Output(), "%s\n", appString) | ||
os.Exit(2) | ||
} | ||
|
||
if config.InfluxBucket == "" || config.InfluxOrganization == "" || config.InfluxAuthToken == "" { | ||
log.Fatal("You must provide bucket, organization and influxAuthToken") | ||
} | ||
|
||
if _, err := url.ParseRequestURI(config.InfluxUrl); err != nil { | ||
log.Fatal("Influx url is not valid") | ||
} | ||
|
||
fmt.Printf("%s\n", appString) | ||
} | ||
|
||
func main() { | ||
|
||
flagConfig() | ||
|
||
peng := p.New(&config) | ||
peng.Portbitmap.HashFunc = func(port uint16) (uint16, uint64) { | ||
portModuled := (port / uint16(config.NumberOfBin)) % uint16(config.NumberOfModule) | ||
index, bit := portModuled/uint16(config.NumberOfBits), uint64(portModuled)%uint64(config.NumberOfBits) | ||
return index, bit | ||
} | ||
peng.Start() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package portbitmap | ||
|
||
import ( | ||
"errors" | ||
b "github.com/alessio-perugini/peng/pkg/bitmap" | ||
) | ||
|
||
type PortBitmap struct { | ||
Config *Config | ||
InnerBitmap []b.Bitmap | ||
HashFunc func(port uint16) (uint16, uint64) | ||
} | ||
|
||
type Config struct { | ||
NumberOfBin uint | ||
SizeBitmap uint | ||
NumberOfBits uint | ||
} | ||
|
||
//TODO levare il puntatore a config | ||
func New(cfg *Config) *PortBitmap { | ||
var InnerBitmap = make([]b.Bitmap, cfg.NumberOfBin) | ||
cfg.NumberOfBits = cfg.SizeBitmap / cfg.NumberOfBin | ||
|
||
for i := 0; i < int(cfg.NumberOfBin); i++ { | ||
InnerBitmap[i] = *b.New(uint64(cfg.NumberOfBits)) | ||
} | ||
|
||
var hashFunc = func(port uint16) (uint16, uint64) { | ||
portModuled := port % uint16(cfg.SizeBitmap) | ||
index, bit := portModuled/uint16(cfg.NumberOfBits), uint64(portModuled)%uint64(cfg.NumberOfBits) | ||
return index, bit | ||
} | ||
|
||
return &PortBitmap{ | ||
InnerBitmap: InnerBitmap, | ||
HashFunc: hashFunc, | ||
Config: cfg, | ||
} | ||
} | ||
|
||
func (p *PortBitmap) AddPort(port uint16) error { | ||
indexBin, bitBin := p.HashFunc(port) | ||
if indexBin >= uint16(len(p.InnerBitmap)) { | ||
return errors.New("index to access the bin is invalid") | ||
} | ||
p.InnerBitmap[indexBin].SetBit(bitBin, true) | ||
return nil | ||
} | ||
|
||
func (p *PortBitmap) ClearAll() { | ||
for i := 0; i < len(p.InnerBitmap); i++ { | ||
p.InnerBitmap[i].ResetAllBits() | ||
} | ||
} |