forked from trustmaster/goflow
-
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.
Added plugins feature that allows separate binaries as processes
Used the loader which was compiled out, and had to brutilize it a bit Need a conversation about what the intention was of some of the non-working code i it
- Loading branch information
Showing
8 changed files
with
370 additions
and
69 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
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,4 @@ | ||
#!/bin/bash | ||
go install -buildmode=plugin test_plugins/Adder_goplug.go | ||
go install -buildmode=plugin test_plugins/Plug1_goplug.go | ||
go install -buildmode=plugin test_plugins/NGen_goplug.go |
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,58 @@ | ||
package goflow | ||
|
||
import ( | ||
"io/ioutil" | ||
"log" | ||
"path" | ||
"plugin" | ||
"strings" | ||
) | ||
|
||
const plugInSuffix = `_goplug.so` | ||
|
||
//PlugIn something | ||
type PlugIn interface { | ||
Component | ||
Info() map[string]string | ||
} | ||
|
||
// LoadComponents goes through all paths, opens all plugins in those paths | ||
// and loads them into factory | ||
// Plugins are denoted by *_goplug.so, The filename must begin with a capitolized letter | ||
func LoadComponents(paths []string, factory *Factory) ([]string, error) { | ||
var loaded []string | ||
|
||
for _, apath := range paths { | ||
//fmt.Println("Loading plugins at", apath) | ||
files, err := ioutil.ReadDir(apath) | ||
if err != nil { | ||
log.Printf("Path %s not found, error=%s.", apath, err.Error()) | ||
continue | ||
} | ||
for _, file := range files { | ||
if strings.HasSuffix(file.Name(), plugInSuffix) { | ||
plugpath := path.Join(apath, file.Name()) | ||
plug, err := plugin.Open(plugpath) | ||
if err != nil { | ||
log.Printf("Can't open plugin %s, error=%s.", plugpath, err.Error()) | ||
continue | ||
} | ||
//get name from name - _goplug.so and register with contructor | ||
name := strings.TrimSuffix(file.Name(), plugInSuffix) | ||
symbol, err := plug.Lookup(name) | ||
if err != nil { | ||
log.Printf("Can't find symbol %s in plugin %s, error=%s.", name, plugpath, err.Error()) | ||
continue | ||
} | ||
constructor := symbol.(func() (interface{}, error)) | ||
anerr := factory.Register(name, constructor) | ||
if anerr != nil { | ||
log.Printf("Failed to register plugin %s, error=%s.", plugpath, err.Error()) | ||
continue | ||
} | ||
loaded = append(loaded, name) | ||
} | ||
} | ||
} | ||
return loaded, nil | ||
} |
Oops, something went wrong.