protomake

package module
v0.0.0-...-cb034df Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 9, 2018 License: BSD-2-Clause Imports: 2 Imported by: 0

README

protomake

Protocol Maker - Framework to develop realtime protocol.

GoDoc License

example

Simple Ping/Pong Server

Define receive handler
  • receive handler receives message and emits event
  • any cases listed below is OK
    • receive some messages and emit one event
    • receive one message and emit some events
    • receive some messages and emit no events
    • receive no messages and emit some events
type rx struct{}

func (x *rx) Detect(msg interface{}) bool {
	_, ok := msg.(PingMessage)
	return ok
}

func (x *rx) Handle(ctx context.Context, msgChan <-chan interface{}) <-chan interface{} {
	evtChan := make(chan interface{}, len(msgChan))

	go func() {
		for msg := range msgChan {
			select {
			case <-ctx.Done():
			default:
			}

			m := msg.(PingMessage)
			evtChan <- PingGotEvent{
				RxTime: time.Now(),
				TxTime: m.TxTime,
			}
		}
	}()

	return evtChan
}
Define Intercept Handler
  • intercept handler receives event and emits event
  • any cases listed below is OK
    • receive some events and emit one event
    • receive one event and emit some events
    • receive some events and emit no events
    • receive no events and emit some events
type ic struct{}

func (x *ic) Detect(evt interface{}) bool {
	_, ok := evt.(PingGotEvent)
	return ok
}

func (x *ic) Handle(ctx context.Context, inEvtChan <-chan interface{}) <-chan interface{} {
	outEvtChan := make(chan interface{}, len(inEvtChan))

	go func() {
		for evt := range inEvtChan {
			select {
			case <-ctx.Done():
			default:
			}

			fmt.Printf("INTERCEPT: %#v\n", evt)
		}
	}()

	return outEvtChan
}
Define Transmit Handler
  • transmit handler receives event and emits message
  • any cases listed below is OK
    • receive some events and emit one message
    • receive one event and emit some message
    • receive some events and emit no message
    • receive no events and emit some messages
type tx struct{}

func (x *tx) Detect(evt interface{}) bool {
	_, ok := evt.(PingGotEvent)
	return ok
}

func (x *tx) Handle(ctx context.Context, evtChan <-chan interface{}) <-chan interface{} {
	msgChan := make(chan interface{}, len(evtChan))

	go func() {
		for evt := range evtChan {
			select {
			case <-ctx.Done():
			default:
			}

			e := evt.(PingGotEvent)
			msgChan <- PongMessage{
				TxTime: e.TxTime,
				RxTime: e.RxTime,
			}
		}
	}()

	return msgChan
}
Start serve
  • bind inbound / outbound channels to dispatcher
  • register handlers to dispatcher
  • then, start serving

func main() {
	recvChan := make(chan interface{}, 1024)
	sendChan := make(chan interface{}, 1024)

	var r rx
	rd := protomake.NewRxDispatcher(recvChan)
	rd.Register(&r)

	var t tx
	td := protomake.NewTxDispatcher(sendChan)
	td.Register(&t)

	var i ic
	id := protomake.NewInterceptDispatcher()
	id.Register(&i)

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	go func() {
		protomake.Serve(ctx, rd, id, td)
	}()

	go func() {
		ticker := time.NewTicker(time.Second)
		defer ticker.Stop()
		for {
			msg := PingMessage{
				TxTime: <-ticker.C,
			}

			fmt.Printf("RECV: %#v\n", msg)
			recvChan <- msg
		}
	}()

	for msg := range sendChan {
		fmt.Printf("SEND: %#v\n\n", msg)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Serve

Serve starts to serve.

Types

type InterceptDispatcher

type InterceptDispatcher struct {
	// contains filtered or unexported fields
}

InterceptDispatcher represents event dispatcher.

func NewInterceptDispatcher

func NewInterceptDispatcher() *InterceptDispatcher

NewInterceptDispatcher creates a new InterceptDispatcher instance.

func (*InterceptDispatcher) Register

func (d *InterceptDispatcher) Register(h InterceptHandler)

Register registers handlers.

type InterceptHandler

type InterceptHandler interface {
	// Detect returns true when this handler needs to handle the event.
	Detect(evt interface{}) bool
	// Handle handles event from inbound channel (inEvtChan) and emits event to outbound channel (outevtChan).
	Handle(ctx context.Context, inEvtChan <-chan interface{}) (outEvtChan <-chan interface{})
}

InterceptHandler represents entercept handler.

type RxDispatcher

type RxDispatcher struct {
	// contains filtered or unexported fields
}

RxDispatcher represents message dispatcher at receiver side.

func NewRxDispatcher

func NewRxDispatcher(msgChan <-chan interface{}) *RxDispatcher

NewRxDispatcher creates a new RxDispatcher instance.

func (*RxDispatcher) Register

func (d *RxDispatcher) Register(h RxHandler)

Register registers handlers.

type RxHandler

type RxHandler interface {
	// Detect returns true when this handler needs to handle the message
	Detect(msg interface{}) bool
	// Handle handles message from inbound channel (msgChan) and emits event to outbound channel (evtChan).
	Handle(ctx context.Context, msgChan <-chan interface{}) (evtChan <-chan interface{})
}

RxHandler represents message handler at receiver side.

type TxDispatcher

type TxDispatcher struct {
	// contains filtered or unexported fields
}

TxDispatcher represents event dispatcher at transmitter side.

func NewTxDispatcher

func NewTxDispatcher(msgChan chan<- interface{}) *TxDispatcher

NewTxDispatcher creates a new TxDispatcher instance.

func (*TxDispatcher) Register

func (d *TxDispatcher) Register(h TxHandler)

Register registers TxHandler to TxDispatcher.

type TxHandler

type TxHandler interface {
	// Detect returns true when this handler needs to handle the event.
	Detect(evt interface{}) bool
	// Handle handles event from inbound channel (evtChan) and emits message to outband channel (msgChan).
	Handle(ctx context.Context, evtChan <-chan interface{}) (msgChan <-chan interface{})
}

TxHandler represents event handler at transmitter side.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL