Skip to end of metadata
Go to start of metadata

Contents

Requirements

  • goruss is available on bitbucket.org
  • russng v6.3 C implementation

Code

russhello_server.go
// russhello_server.go

package main

/*
//#cgo LDFLAGS: -lruss
#cgo LDFLAGS: /usr/lib/libruss.a
#include <russ/russ.h>
*/
import "C"

import (
	"bitbucket.org/russng/goruss"
	"os"
	"syscall"
)

var conf *goruss.Conf

func svc_root_handler(s *goruss.Session) {
	sconn := s.GetServerConn()
	req := s.GetRequest()
	outfd := sconn.GetFd(1)
	opnum := req.GetOpnum()

	if opnum == goruss.RUSS_OPNUM_EXECUTE {
		syscall.Write(outfd, []byte("hello world\n"))
		sconn.Exit(0)
	}
}

func main() {
	if len(os.Args) == 2 && os.Args[1] == "-h" {
		os.Stderr.Write([]byte("usage: hellosvr [<conf options>]\n"))
		os.Exit(1)
	}
	conf, _ = goruss.LoadConf(os.Args)
	if conf == nil {
		os.Stderr.Write([]byte("error: cannot configure\n"))
		os.Exit(1)
	}

	svr := goruss.Init(conf)
	if svr == nil ||
		svr.SetType(goruss.RUSS_SVR_TYPE_THREAD) != nil ||
		svr.SetAutoswitchuser(0) != nil ||
		svr.SetHelp("Hello world example server (using goruss).\n") != nil {
		os.Stderr.Write([]byte("error: cannot set up server\n"))
		os.Exit(1)
	}
	root := svr.GetRoot()
	if root == nil ||
		root.SetHandler(svc_root_handler) != nil ||
		root.SetVirtual(1) != nil {
		os.Stderr.Write([]byte("error: cannot set up server\n"))
	}

	svr.LoopThread()
	os.Exit(0)
}

Notes:

  • linking with /usr/lib/libruss.a to create mostly static binary
  • running as threaded server RUSS_SVR_TYPE_THREAD and svr.LoopThread() (in future, use RUSS_SVR_TYPE_THREAD only)
  • only one handler for all services under / (by setting root.SetVirtual(1))

Build:

mkdir-p  ~/tmp/russhello_server/src
cd ~/tmp/russhello_server
export GOPATH=$PWD
cd src
vi russhello_server.go
go get -d
go build russhello_server.go

Run

Configuration file:

hello.conf
#russ service=socket
#
# hello.conf

[main]
path=/home/jdm/tmp/russhello_server/russhello_server
addr=/tmp/hello

Start server:

rustart -f /home/jdm/tmp/russhello_server/hello.conf 

Call services:

ruhelp /tmp/hello
ruls /tmp/hello
ruexec /tmp/hello
  • No labels