Documentation ¶
Overview ¶
Package hbi - Hosting Based Interface - Go1
`HBI` is a `meta protocol` for application systems (read `service` software components), possibly implemented in different programming languages and/or base runtimes, to establish communication channels between os processes (may or may not across computing nodes), as to communicate with `peer-scripting-code` posted to eachother's `hosting environment`.
By providing a `hosting environment` which exposes necessary artifacts (various `functions` in essense, see Mechanism) (https://github.com/complyue/hbi#mechanism) to accommodate the `landing` of the `peer-scripting-code` from the other end, a service process defines both its API and the effect network protocol to access the API, at granted efficience.
Such network protocols are called `API defined protocol`s.
Index ¶
- func DialTCP(addr string, he *HostingEnv) (po *PostingEnd, ho *HostingEnd, err error)
- func DialUnix(addr string, he *HostingEnv) (po *PostingEnd, ho *HostingEnd, err error)
- func Repr(val interface{}) string
- func ServeTCP(addr string, heFactory func() *HostingEnv, cb func(*net.TCPListener) error) (err error)
- func ServeUnix(addr string, heFactory func() *HostingEnv, cb func(*net.UnixListener)) (err error)
- func TakeConn(conn net.Conn, he *proto.HostingEnv) (po *proto.PostingEnd, ho *proto.HostingEnd, err error)
- func TakeSocket(fd int, he *proto.HostingEnv) (po *proto.PostingEnd, ho *proto.HostingEnd, err error)
- type HoCo
- type HostingEnd
- type HostingEnv
- type LitDecimalType
- type LitDictType
- type LitIntType
- type LitListType
- type PoCo
- type PostingEnd
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DialTCP ¶
func DialTCP(addr string, he *HostingEnv) (po *PostingEnd, ho *HostingEnd, err error)
DialTCP connects to specified remote address (host:port), react with specified hosting environment.
The returned posting endpoint is used to create posting conversations to send code & data to remote site for active communication.
The returned hosting endpoint is used to obtain the current hosting conversation triggered by a posting conversation from remote site for passive communication.
func DialUnix ¶
func DialUnix(addr string, he *HostingEnv) (po *PostingEnd, ho *HostingEnd, err error)
DialUnix connects to specified file path (domain socket), react with specified hosting environment.
The returned posting endpoint is used to create posting conversations to send code & data to remote site for active communication.
The returned hosting endpoint is used to obtain the current hosting conversation triggered by a posting conversation from remote site for passive communication.
func Repr ¶
func Repr(val interface{}) string
Repr converts a value object to its textual representation, that can be used to reconstruct the object by Anko (https://github.com/mattn/anko), or an HBI HostingEnv implemented in other programming languages / runtimes.
The syntax is very much JSON like, with `[]interface{}` maps to JSON array, and `map[interface{}]interface{}` maps to JSON object. But note it's not JSON compatible when a non-string key is present.
Despite those few special types, the representation of a value is majorly obtained via `Sprintf("%#v", v)`, which can be customized by overriding `Format(fmt.State, rune)` method of its type, like the example shows. For desired result:
fmt.Printf("(%6s) %s\n", "Repr", hbi.Repr(msg)) fmt.Printf("(%6s) %#v\n", "Repr", msg) fmt.Printf("(%6s) %+v\n", "Long", msg) fmt.Printf("(%6s) %v\n", "Short", msg) fmt.Printf("(%6s) %s\n", "String", msg) // Output: // ( Repr) Msg("Compl","Hello, HBI world!",1557998919) // ( Repr) Msg("Compl","Hello, HBI world!",1557998919) // ( Long) [May 16 17:28:39+08] @Compl: Hello, HBI world! // ( Short) @Compl: Hello, HBI world! // (String) Msg<@Compl
Implement the `Format(fmt.State, rune)` method like this:
func (msg *Msg) Format(s fmt.State, verb rune) { switch verb { case 's': // string form io.WriteString(s, "Msg<@") io.WriteString(s, msg.From) case 'v': if s.Flag('#') { // repr form io.WriteString(s, "Msg(") io.WriteString(s, fmt.Sprintf("%#v", msg.From)) io.WriteString(s, ",") io.WriteString(s, fmt.Sprintf("%#v", msg.Content)) io.WriteString(s, ",") io.WriteString(s, fmt.Sprintf("%d", msg.Time.Unix())) io.WriteString(s, ")") } else { // value form if s.Flag('+') { io.WriteString(s, "[") io.WriteString(s, msg.Time.Format("Jan 02 15:04:05Z07")) io.WriteString(s, "] ") } io.WriteString(s, "@") io.WriteString(s, msg.From) io.WriteString(s, ": ") io.WriteString(s, msg.Content) } } }
See: https://docs.python.org/3/library/functions.html#repr and https://docs.python.org/3/reference/datamodel.html#object.__repr__ for a similar construct in Python.
Expand the `Example` section below to see full source.
Example ¶
package main import ( "fmt" "io" "time" ) type Msg struct { From string Content string Time time.Time } func NewMsg(from, content string, unixTimestamp int64) *Msg { return &Msg{ From: from, Content: content, Time: time.Unix(unixTimestamp, 0), } } func (msg *Msg) Format(s fmt.State, verb rune) { switch verb { case 's': // string form io.WriteString(s, "Msg<@") io.WriteString(s, msg.From) case 'v': if s.Flag('#') { // repr form io.WriteString(s, "Msg(") io.WriteString(s, fmt.Sprintf("%#v", msg.From)) io.WriteString(s, ",") io.WriteString(s, fmt.Sprintf("%#v", msg.Content)) io.WriteString(s, ",") io.WriteString(s, fmt.Sprintf("%d", msg.Time.Unix())) io.WriteString(s, ")") } else { // value form if s.Flag('+') { io.WriteString(s, "[") io.WriteString(s, msg.Time.Format("Jan 02 15:04:05Z07")) io.WriteString(s, "] ") } io.WriteString(s, "@") io.WriteString(s, msg.From) io.WriteString(s, ": ") io.WriteString(s, msg.Content) } } } func main() { saidZone, _ := time.LoadLocation("Asia/Chongqing") saidTime, _ := time.ParseInLocation( "2006-01-02 15:04:05", "2019-05-16 17:28:39", saidZone, ) msg := NewMsg("Compl", "Hello, HBI world!", saidTime.Unix()) fmt.Printf("(%6s) %s\n", "Repr", Repr(msg)) fmt.Printf("(%6s) %#v\n", "Repr", msg) fmt.Printf("(%6s) %+v\n", "Long", msg) fmt.Printf("(%6s) %v\n", "Short", msg) fmt.Printf("(%6s) %s\n", "String", msg) }
Output: ( Repr) Msg("Compl","Hello, HBI world!",1557998919) ( Repr) Msg("Compl","Hello, HBI world!",1557998919) ( Long) [May 16 17:28:39+08] @Compl: Hello, HBI world! ( Short) @Compl: Hello, HBI world! (String) Msg<@Compl
func ServeTCP ¶
func ServeTCP(addr string, heFactory func() *HostingEnv, cb func(*net.TCPListener) error) (err error)
ServeTCP listens on the specified local address (host:port), serves each incoming connection with the hosting environment created from the `heFactory` function.
`cb` will be called with the created `*net.TCPListener`, it's handful to specify port as 0, and receive the actual port from the cb.
This func won't return until the listener is closed.
func ServeUnix ¶
func ServeUnix(addr string, heFactory func() *HostingEnv, cb func(*net.UnixListener)) (err error)
ServeUnix listens on the specified file path (domain socket), serves each incoming connection with the hosting environment created from the `heFactory` function.
`cb` will be called with the created `*net.UnixListener`.
This func won't return until the listener is closed.
func TakeConn ¶
func TakeConn(conn net.Conn, he *proto.HostingEnv) ( po *proto.PostingEnd, ho *proto.HostingEnd, err error)
TakeConn takes a pre-connected socket (Unix or TCP), react with specified hosting environment.
The returned posting endpoint is used to create posting conversations to send code & data to remote site for active communication.
The returned hosting endpoint is used to obtain the current hosting conversation triggered by a posting conversation from remote site for passive communication.
func TakeSocket ¶
func TakeSocket(fd int, he *proto.HostingEnv) ( po *proto.PostingEnd, ho *proto.HostingEnd, err error)
TakeSocket takes a pre-connected socket (Unix or TCP), react with specified hosting environment.
The returned posting endpoint is used to create posting conversations to send code & data to remote site for active communication.
The returned hosting endpoint is used to obtain the current hosting conversation triggered by a posting conversation from remote site for passive communication.
Types ¶
type HoCo ¶
HoCo is the passive, hosting conversation.
A HoCo is triggered by a PoCo from peer's posting endpoint, it is automatically available to application, obtained by calling HostingEnd.Co()
type HostingEnd ¶
type HostingEnd = proto.HostingEnd
HostingEnd is the application programming interface of a hosting endpoint.
type HostingEnv ¶
type HostingEnv = proto.HostingEnv
HostingEnv is the container of HBI artifacts, including:
- functions
- object constructors (special functions taking n args, returning 1 object)
- reactor methods
- value objects
These artifacts need to be explicitly exposed to a hosting environment, to accomodate landing of `peer-scripting-code` from the other end.
func NewHostingEnv ¶
func NewHostingEnv() *HostingEnv
NewHostingEnv creates a new, empty hosting environment.
type LitDecimalType ¶
type LitDecimalType = float64
LitDecimalType is the type of a literal decimal value from peer script
this is decided by the underlying HBI interpreter for Go1, which is Anko
type LitDictType ¶
type LitDictType = map[interface{}]interface{}
LitDictType is the type of a literal dict (as called by Python, or object as called by JavaScript) value from peer script
this is decided by the underlying HBI interpreter for Go1, which is Anko
type LitIntType ¶
type LitIntType = int64
LitIntType is the type of a literal int value from peer script
this is decided by the underlying HBI interpreter for Go1, which is Anko
type LitListType ¶
type LitListType = []interface{}
LitListType is the type of a literal list (as called by Python, or array as called by JavaScript) value from peer script
this is decided by the underlying HBI interpreter for Go1, which is Anko
type PoCo ¶
PoCo is the active, posting conversation.
A PoCo is created from application by calling PostingEnd.NewCo()
type PostingEnd ¶
type PostingEnd = proto.PostingEnd
PostingEnd is the application programming interface of a posting endpoint.
Directories ¶
Path | Synopsis |
---|---|
cmd
|
|
Package mp enables multiprocessing with HBI
|
Package mp enables multiprocessing with HBI |
pkg
|
|
_details
Package details contains implementation details of HBI wire protocol
|
Package details contains implementation details of HBI wire protocol |
proto
Package proto defines the building blocks for API defined protocols with HBI
|
Package proto defines the building blocks for API defined protocols with HBI |