Skip to content

Commit

Permalink
Added / corrected comments for api, app & bees packages
Browse files Browse the repository at this point in the history
  • Loading branch information
muesli committed Jan 29, 2017
1 parent a742208 commit 59af201
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 43 deletions.
3 changes: 2 additions & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* Christian Muehlhaeuser <[email protected]>
*/

// beehive's RESTful api for introspection and configuration
// Package api is Beehive's RESTful api for introspection and configuration
package api

import (
Expand Down Expand Up @@ -65,6 +65,7 @@ func imageFromPathParam(req *restful.Request, resp *restful.Response) {
actual)
}

// Run sets up the restful API container and an HTTP server go-routine
func Run() {
// to see what happens in the package, uncomment the following
//restful.TraceLogger(log.New(os.Stdout, "[restful] ", log.LstdFlags|log.Lshortfile))
Expand Down
6 changes: 5 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
* Christian Muehlhaeuser <[email protected]>
*/

// beehive's application container. Handles command-line arguments parsing.
// Package app is Beehive's application container. Handles command-line arguments parsing.
package app

import (
"flag"
)

// A CliFlag can be added by Beehive modules to map a command-line parameter
// to a local variable
type CliFlag struct {
V interface{}
Name string
Expand All @@ -36,12 +38,14 @@ var (
appflags []CliFlag
)

// AddFlags adds CliFlags to appflags
func AddFlags(flags []CliFlag) {
for _, flag := range flags {
appflags = append(appflags, flag)
}
}

// Run sets up all the cli-param mappings
func Run() {
for _, f := range appflags {
switch f.Value.(type) {
Expand Down
8 changes: 6 additions & 2 deletions bees/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* Christian Muehlhaeuser <[email protected]>
*/

// Package bees is Beehive's central module system
package bees

import (
Expand All @@ -27,7 +28,7 @@ import (
"text/template"
)

// An Action
// Action describes an action.
type Action struct {
ID string
Bee string
Expand All @@ -39,10 +40,12 @@ var (
actions []Action
)

// GetActions returns all configured actions.
func GetActions() []Action {
return actions
}

// GetAction returns one action with a specific ID.
func GetAction(id string) *Action {
for _, a := range actions {
if a.ID == id {
Expand All @@ -53,11 +56,12 @@ func GetAction(id string) *Action {
return nil
}

// SetActions sets the currently configured actions.
func SetActions(as []Action) {
actions = as
}

// Execute an action and map its ins & outs.
// execAction executes an action and map its ins & outs.
func execAction(action Action, opts map[string]interface{}) bool {
a := Action{
Bee: action.Bee,
Expand Down
31 changes: 18 additions & 13 deletions bees/bees.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* Christian Muehlhaeuser <[email protected]>
*/

// beehive's central module system.
// Package bees is Beehive's central module system
package bees

import (
Expand All @@ -29,7 +29,7 @@ import (
uuid "github.com/nu7hatch/gouuid"
)

// Interface which all bees need to implement
// BeeInterface is an interface all bees implement.
type BeeInterface interface {
// Name of the bee
Name() string
Expand Down Expand Up @@ -72,15 +72,15 @@ type BeeInterface interface {
Action(action Action) []Placeholder
}

// An instance of a bee
// BeeConfig contains all settings for a single Bee.
type BeeConfig struct {
Name string
Class string
Description string
Options BeeOptions
}

// Base-struct to be embedded by bee implementations
// Bee is the base-struct to be embedded by bee implementations.
type Bee struct {
config BeeConfig

Expand All @@ -97,14 +97,14 @@ var (
factories map[string]*BeeFactoryInterface = make(map[string]*BeeFactoryInterface)
)

// Bees need to call this method to register themselves
// RegisterBee gets called by Bees to register themselves.
func RegisterBee(bee BeeInterface) {
log.Println("Worker bee ready:", bee.Name(), "-", bee.Description())

bees[bee.Name()] = &bee
}

// Returns bee with this name
// GetBee returns a bee with a specific name.
func GetBee(identifier string) *BeeInterface {
bee, ok := bees[identifier]
if ok {
Expand All @@ -114,7 +114,7 @@ func GetBee(identifier string) *BeeInterface {
return nil
}

// Returns all known bees
// GetBees returns all known bees.
func GetBees() []*BeeInterface {
r := []*BeeInterface{}
for _, bee := range bees {
Expand All @@ -124,7 +124,7 @@ func GetBees() []*BeeInterface {
return r
}

// Starts a bee and recovers from panics
// startBee starts a bee and recovers from panics.
func startBee(bee *BeeInterface, fatals int) {
if fatals >= 3 {
log.Println("Terminating evil bee", (*bee).Name(), "after", fatals, "failed tries!")
Expand All @@ -142,6 +142,7 @@ func startBee(bee *BeeInterface, fatals int) {
(*bee).Run(eventsIn)
}

// NewBeeInstance sets up a new Bee with supplied config.
func NewBeeInstance(bee BeeConfig) *BeeInterface {
factory := GetFactory(bee.Class)
if factory == nil {
Expand All @@ -153,13 +154,14 @@ func NewBeeInstance(bee BeeConfig) *BeeInterface {
return &mod
}

// DeleteBee removes a Bee instance.
func DeleteBee(bee *BeeInterface) {
(*bee).Stop()

delete(bees, (*bee).Name())
}

// Starts all registered bees
// StartBee starts a bee.
func StartBee(bee BeeConfig) *BeeInterface {
b := NewBeeInstance(bee)

Expand All @@ -171,7 +173,7 @@ func StartBee(bee BeeConfig) *BeeInterface {
return b
}

// Starts all registered bees
// StartBees starts all registered bees.
func StartBees(beeList []BeeConfig) {
eventsIn = make(chan Event)
go handleEvents()
Expand All @@ -181,7 +183,7 @@ func StartBees(beeList []BeeConfig) {
}
}

// Stops all bees gracefully
// StopBees stops all bees gracefully.
func StopBees() {
for _, bee := range bees {
log.Println("Stopping bee:", (*bee).Name())
Expand All @@ -192,6 +194,7 @@ func StopBees() {
bees = make(map[string]*BeeInterface)
}

// RestartBee restarts a Bee.
func RestartBee(bee *BeeInterface) {
(*bee).Stop()

Expand All @@ -203,13 +206,13 @@ func RestartBee(bee *BeeInterface) {
}(bee)
}

// Stops all running bees and restarts a new set of bees
// RestartBees stops all running bees and restarts a new set of bees.
func RestartBees(bees []BeeConfig) {
StopBees()
StartBees(bees)
}

// Returns a new bee and sets up sig-channel & waitGroup
// NewBee returns a new bee and sets up sig-channel & waitGroup.
func NewBee(name, factoryName, description string, options []BeeOption) Bee {
c := BeeConfig{
Name: name,
Expand All @@ -227,6 +230,7 @@ func NewBee(name, factoryName, description string, options []BeeOption) Bee {
return b
}

// BeeConfigs returns configs for all Bees.
func BeeConfigs() []BeeConfig {
bs := []BeeConfig{}
for _, b := range bees {
Expand All @@ -236,6 +240,7 @@ func BeeConfigs() []BeeConfig {
return bs
}

// UUID generates a new unique ID.
func UUID() string {
u, _ := uuid.NewV4()
return u.String()
Expand Down
11 changes: 7 additions & 4 deletions bees/chains.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@
* Christian Muehlhaeuser <[email protected]>
*/

// Package bees is Beehive's central module system
package bees

import "log"

// An element in a Chain
// ChainElement is an element in a Chain
type ChainElement struct {
Action Action
Filter Filter
}

// A user defined Chain
// Chain is a user defined chain
type Chain struct {
Name string
Description string
Expand All @@ -42,10 +43,12 @@ var (
chains []Chain
)

// GetChains returns all chains
func GetChains() []Chain {
return chains
}

// GetChain returns a chain with a specific id
func GetChain(id string) *Chain {
for _, c := range chains {
if c.Name == id {
Expand All @@ -56,7 +59,7 @@ func GetChain(id string) *Chain {
return nil
}

// Setter for chains
// SetChains sets the currently configured chains
func SetChains(cs []Chain) {
newcs := []Chain{}
// migrate old chain style
Expand All @@ -80,7 +83,7 @@ func SetChains(cs []Chain) {
chains = newcs
}

// Execute chains for an event we received.
// execChains executes chains for an event we received
func execChains(event *Event) {
for _, c := range chains {
if c.Event.Name != event.Name || c.Event.Bee != event.Bee {
Expand Down
9 changes: 5 additions & 4 deletions bees/descriptors.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@
* Christian Muehlhaeuser <[email protected]>
*/

// Package bees is Beehive's central module system
package bees

// Modules provide events, which are described in an EventDescriptor.
// EventDescriptor describes an Event provided by a Bee.
type EventDescriptor struct {
Namespace string
Name string
Description string
Options []PlaceholderDescriptor
}

// Modules offer actions, which are described in an ActionDescriptor.
// ActionDescriptor describes an Action provided by a Bee.
type ActionDescriptor struct {
Namespace string
Name string
Expand All @@ -52,7 +53,7 @@ type BeeOptionDescriptor struct {
Mandatory bool
}

// Returns the ActionDescriptor matching an action.
// GetActionDescriptor returns the ActionDescriptor matching an action.
func GetActionDescriptor(action *Action) ActionDescriptor {
bee := GetBee(action.Bee)
if bee == nil {
Expand All @@ -68,7 +69,7 @@ func GetActionDescriptor(action *Action) ActionDescriptor {
return ActionDescriptor{}
}

// Returns the EventDescriptor matching an event.
// GetEventDescriptor returns the EventDescriptor matching an event.
func GetEventDescriptor(event *Event) EventDescriptor {
bee := GetBee(event.Bee)
if bee == nil {
Expand Down
6 changes: 3 additions & 3 deletions bees/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
* Christian Muehlhaeuser <[email protected]>
*/

// beehive's central module system.
// Package bees is Beehive's central module system
package bees

import "log"

// An Event
// An Event describes an event including its parameters.
type Event struct {
Bee string
Name string
Expand All @@ -34,7 +34,7 @@ var (
eventsIn = make(chan Event)
)

// Handles incoming events and executes matching Chains.
// handleEvents handles incoming events and executes matching Chains.
func handleEvents() {
for {
event, ok := <-eventsIn
Expand Down
Loading

0 comments on commit 59af201

Please sign in to comment.