Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,6 @@ var (
_icmpHandler adapter.NetworkHandler
)

// Start starts the default engine up.
func Start() {
if err := start(); err != nil {
log.Fatalf("[ENGINE] failed to start: %v", err)
}
}

// Stop shuts the default engine down.
func Stop() {
if err := stop(); err != nil {
log.Fatalf("[ENGINE] failed to stop: %v", err)
}
}

// Insert loads *Key to the default engine.
func Insert(k *Key) {
_engineMu.Lock()
Expand All @@ -70,7 +56,8 @@ func SetICMPHandler(h adapter.NetworkHandler) {
_engineMu.Unlock()
}

func start() error {
// Start starts the default engine up.
func Start() error {
_engineMu.Lock()
defer _engineMu.Unlock()

Expand All @@ -90,7 +77,16 @@ func start() error {
return nil
}

func stop() (err error) {
// StartOrFatal starts the default engine up, and exits the
// process if it fails.
func StartOrFatal() {
if err := Start(); err != nil {
log.Fatalf("[ENGINE] failed to start: %v", err)
}
}

// Stop shuts the default engine down.
func Stop() error {
_engineMu.Lock()
if _defaultDevice != nil {
_defaultDevice.Close()
Expand All @@ -103,6 +99,14 @@ func stop() (err error) {
return nil
}

// StopOrFatal shuts the default engine down, and exits the
// process if it fails.
func StopOrFatal() {
if err := Stop(); err != nil {
log.Fatalf("[ENGINE] failed to stop: %v", err)
}
}

func execCommand(cmd string) error {
parts, err := shlex.Split(cmd)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ func main() {

engine.Insert(key)

engine.Start()
defer engine.Stop()
engine.StartOrFatal()
defer engine.StopOrFatal()

sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
Expand Down