base

package
v2.4.5 Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2021 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action func(time.Time) []Event

Action is what to do of an event, time of the event is passed in, return following events of this event

type Event

type Event interface {
	// Time when to act
	Time() time.Time
	// Action what to do
	Action() Action
	// HookBefore hook the event with the given action handled before the actual action
	HookBefore(action Action)
	// HookAfter hook the event with the given action handled after the actual action
	HookAfter(action Action)
}

Event is an action to be acted at the exactly time point

func Aggregate

func Aggregate(events ...Event) []Event

Aggregate the events into a slice, utility function

func NewDelayedEvent

func NewDelayedEvent(action Action, delay time.Duration, now time.Time) Event

NewDelayedEvent create an event with delay

func NewFixedEvent

func NewFixedEvent(action Action, time time.Time) Event

NewFixedEvent create an event at the time point

func NewPeriodicEvent

func NewPeriodicEvent(action Action, period time.Duration, t time.Time) Event

NewPeriodicEvent create a periodic event, generate itself each time

func NewRepeatEvent added in v2.3.3

func NewRepeatEvent(action RepeatAction, t time.Time) Event

NewRepeatEvent create an event repeated after the return delay, if such delay is not negative

type EventBuffer

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

EventBuffer is a thread-safe, lock-free buffer used to store simulated packets, implemented like a single link list

func NewEventBuffer

func NewEventBuffer() *EventBuffer

NewEventBuffer creates a new packet buffer

func (*EventBuffer) Insert

func (b *EventBuffer) Insert(events ...Event)

Insert a simulated packet to the buffer, thread-safe

func (*EventBuffer) Reduce

func (b *EventBuffer) Reduce(action func(event Event))

Reduce means clear the buffer and do an action on the event cleared, thread-safe

type EventQueue added in v2.2.0

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

EventQueue is used to sort events according to the time of events

func NewEventQueue added in v2.2.0

func NewEventQueue(bucketSize time.Duration, maxBuckets int) *EventQueue

func (*EventQueue) Dequeue added in v2.2.0

func (q *EventQueue) Dequeue() Event

func (*EventQueue) Enqueue added in v2.2.0

func (q *EventQueue) Enqueue(event Event)

func (*EventQueue) IsEmpty added in v2.2.0

func (q *EventQueue) IsEmpty() bool

func (*EventQueue) Length added in v2.2.0

func (q *EventQueue) Length() int

func (*EventQueue) Peek added in v2.2.0

func (q *EventQueue) Peek() Event

type IPPacket

type IPPacket struct {
	Version            byte   // 4bit
	HeaderSize         byte   // 4 bit
	ServiceType        byte   // 8 bit
	TotalSize          uint16 // 16 bit
	Identifier         uint16 // 16 bit
	Flags              byte   // 3 bit
	FragmentOffset     uint16 // 13 bit
	TTL                byte   // 8 bit
	Protocol           byte   // 8 bit
	HeaderChecksum     uint16 // 16 bit
	SourceAddress      uint32 // 32 bit
	DestinationAddress uint32 // 32 bit
	Options            []byte // vary
	Data               Packet
}

IPPacket is a packet for the ip protocol (ipv4) DO NOT USE NOW, need further support

func (*IPPacket) Size

func (p *IPPacket) Size() int

type Node

type Node interface {
	// Check whether the node can work correctly, usually called by network just before the simulation
	Check()
	// GetNext nodes of the node
	GetNext() []Node
	// SetNext nodes of the node, should not be used during simulation
	SetNext(nodes ...Node)
	// Transfer the given packet to somewhere at sometime the node decides
	// time calculation should use the given time as current time point
	// return the following events caused by the packet transfer
	Transfer(packet Packet, now time.Time) []Event
	// GetTransferCallback get the TransferCallback of the node
	GetTransferCallback() TransferCallback
	// SetTransferCallback set the TransferCallback of the node, should not be used during simulation
	SetTransferCallback(callback TransferCallback)
}

Node indicates a simulated node in the network

type Packet

type Packet interface {
	Size() int
}

Packet in the network

type Queue added in v2.2.0

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

Queue is a ring queue implemented in array/slice O(1) time complexity for enqueue/dequeue/random access O(n) space complexity, where n is the maximum count of elements in the queue at the same time

func NewQueue added in v2.2.0

func NewQueue(length int) *Queue

NewQueue create a queue with the initial capacity hint

func (*Queue) At added in v2.2.0

func (q *Queue) At(index int) interface{}

At return the element of the given index

func (*Queue) Dequeue added in v2.2.0

func (q *Queue) Dequeue() interface{}

Dequeue remove and return element at the head of the queue, panic if empty

func (*Queue) Do added in v2.2.0

func (q *Queue) Do(action func(interface{}))

Do iterate the queue with the given action

func (*Queue) Enqueue added in v2.2.0

func (q *Queue) Enqueue(data interface{})

Enqueue insert the given element to the end of queue

func (*Queue) IsEmpty added in v2.2.0

func (q *Queue) IsEmpty() bool

IsEmpty is used to determine whether the queue is empty

func (*Queue) Length added in v2.2.0

func (q *Queue) Length() int

Length of the queue

type RawPacket

type RawPacket []byte

func (RawPacket) Size

func (p RawPacket) Size() int

type RepeatAction added in v2.3.3

type RepeatAction func(time.Time) ([]Event, time.Duration)

RepeatAction is same to Action, but repeated after the return delay. if return delay is negative, no longer repeat

type SimulatePacket added in v2.2.1

type SimulatePacket struct {
	Data   Packet
	Source Node
	Target Node
}

func (*SimulatePacket) Size added in v2.2.1

func (p *SimulatePacket) Size() int

type TransferCallback

type TransferCallback func(packet Packet, source, target Node, now time.Time)

TransferCallback called when a packet is transferred

type UDPPacket

type UDPPacket struct {
	// Size field has no use
	SourcePort uint16 // 2 byte
	TargetPort uint16 // 2 byte
	Checksum   uint16 // 2 byte
	Data       Packet
}

UDPPacket is a packet for the udp protocol DO NOT USE NOW, need further support

func (*UDPPacket) Size

func (p *UDPPacket) Size() int

Jump to

Keyboard shortcuts

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