modgo installation - modrpc/info GitHub Wiki
To run ModGo, install the Go compiler first. For a successful installation, the following Go environment variable must be properly defined: GOPATH
. Once the Go compiler is installed, get the ModGo package using the command below.
$ go get github.com/mvcode/modgo
Then create a link to the modgo directory as shown below.
$ ln -s $GOPATH/src/github.com/mvcode/modgo .
$ cd modgo
Inside the modgo
directory, compile two executables: mgo
and modgo
. Consider mgo
as a client and modgo
a server (node).
First, setup files need to be created.
sudo mkdir -p /opt/modgo
cp -r etc/setup/* /opt/modgo
To test the installations, we run a server on erdos and a client on polya.
$ $GOPATH/bin/modgo -name node0
This will run a node with name node0.
This server has four modules installed: pipeline, nodectl, counter, and rpio. (tool/modgo/register.go
adds these modules). To see how counter module is implemented, see mods/user/counter/impl/impl.go
. This node has has following resources (from its four modules):
-
Counter
: integer property -
AddToCounter(int, int)
: function which will add both arguments toCounter
property -
AddCounter(int)
: function which will add the argument toCounter
property -
CounterTooHigh
: an event which will fire when the Counter value becomes higher than preset value (200) -
Open()
: Call this before using Raspberry Pi I/O -
Close()
: Call this when done with using Raspberry Pi I/O -
SetMode(pin int, mode int)
: Set the mode of given pin (mode=0: INPUT, mode=1: OUTPUT) -
Toggle(pin int)
: Toggle the value of given pin -
WritePin(pin int, val int)
: Write value to pin -
ReadPin(pin int) int
: Read pin value
$ mgo
MODGO> call node0 Counter # get value of Counter
MODGO> call node0 Counter 1234 # set Counter to 1234
MODGO> call node0 AddCounter 100 # call AddCounter(100)
MODGO> call node0 Counter # get value of Counter
First, on a Raspberry Pi device, install Go library for Raspberry Pi I/O. Connect LED to pins 17/GND.
$ go get github.com/stianeikeland/go-rpio
$ cd <modgo directory>
$ git pull # update ModGo
$ make
$ sudo chmod s+u `which modgo` # SETSUID
$ modgo -name rpi
From a Linux device,
$ mgo
MODGO> call rpi Open # open RP I/O
MODGO> call rpi SetMode 17 1 # set pin 17 as output (1)
MODGO> call rpi Toggle 17 # toggle pin 17
First, on a Raspberry Pi device, install Go library for Raspberry Pi I/O. Connect a push button in pin 18/0.
$ go get github.com/stianeikeland/go-rpio
$ cd <modgo directory>
$ git pull # update ModGo
$ make
$ sudo chmod s+u `which modgo` # SETSUID
$ modgo -name rpi
You can check mods/iot/rpio/impl/impl.go
to see how WatchPin
function is defined.
From a Linux device,
$ mgo
MODGO> call rpi Open # open RP I/O
MODGO> call rpi SetMode 18 0 # set pin 18 as input
# Subscribe PinEvent of rpi. Whenever rpi generates PinEvent event,
# notification will be sent to this linux device.
MODGO> subscribe rpi PinEvent
# Call WatchPin function, which will launch an infinite loop which
# checks pin 18 to see if there are any value change. Upon value
# changes, it will publish PinEvent event.
MODGO> pctl rpi WatchPin run 18 # this will create a process with 18 as its argument
MODGO> pctl rpi WatchPin start # this will start the process
# now, you can press button to check if PinEvent event is generated
MODGO> pctl rpi WatchPin stop # this will stop the WatchPin process
Then, try pressing the pushbutton to see if events are properly generated.
A ModGo module (.mg) file can be created anywhere in the GOPATH directory.
The ModGo distribution already contains some ModGo modules in the mods/
directory.
For example, suppose you create a simple module with the following contents:
// mods/user/mymod/mymod.mg
package mymod
//modgo:func
fun WhatIsYourName() string {
return "I'm Jason"
}
Go to mods/user/mymod
directory and run
$ mgo compile .
This will generate two files:
mods/user/mymod/stub.go
mods/user/mymod/impl/impl.go
stub.gois used for accessing mymod resource in remote nodes, inside a Go program. impl.go contains all code which is used to serve remote requests.
Later, if you want to create a new node with this module installed, all you need to do is to create a main program with the following contents.
// mynode.go
package main
import (
MG "github.com/mvcode/modgo/lib"
MYMOD "github.com/mvcode/modgo/mods/user/mymod/impl"
)
func main() {
MG.InitNode("")
// Start runtime
MG.StartRuntime()
// Register modules
MYMOD.Register()
// infinite loop
loop := make(chan int)
<-loop
}
Now compile the program using the following command
$ go build
$ ls
$ mynode.go mynode*
This will create a new node executable, mynode. You can run
# machine LINUX0
$ mynode -name node123
Now on a different machine, you can access this service
# machine LINUX2
$ mgo
MODGO> call node123 WhatIsYourName
RETVAL: &["I'm Jason"]
This feature is still under construction.
# create a new, empty node (with 0 modules) at /opt/modgo/nodes/node0
$ mgo new node0
# add counter module to node0
$ mgo add node0 github.com/mvcode/modgo/mods/user/counter/impl
# for now, manually compile it (later MGO will automatically compile it)
$ cd /opt/modgo/nodes/node0
$ go build
# run node0 executable
$ ./node0