Skip to content

Commit 3e10b7e

Browse files
committed
comments
1 parent 39d5c63 commit 3e10b7e

File tree

8 files changed

+56
-6
lines changed

8 files changed

+56
-6
lines changed

pkg/neti/basicTcpService.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ func (b *basicTcpService) accept(bid []byte, conn HostConn) {
140140
}
141141
}
142142

143+
// InitBaseTcpService creates a new basic tcp service
143144
func InitBaseTcpService(listenAddr string, logger *log.Logger) NetService {
144145
net := NewTcpNet(logger)
145146
net.RegisterMessage(MessageWrap{})

pkg/neti/basicUdpService.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ func (b *basicUdpService) deliver(msg MessageWrap, conn *ServiceHostConn, err er
117117
return errors.New(fmt.Sprintf("Listener with Id %v is not registered, entries: %v", conn.ServiceId, b.listeners))
118118
}
119119

120+
// InitBaseUdpService creates a new basicUdpService
120121
func InitBaseUdpService(listenAddr string, buffsize int) NetService {
121122
net := NewUdpNet(buffsize)
122123
net.RegisterMessage(MessageWrap{})

pkg/neti/configuration.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,31 @@ import (
77
"github.com/spf13/viper"
88
)
99

10+
// Configuration is the configuration for the network
1011
type Configuration struct {
11-
iface string
12-
ip string
13-
port int
12+
iface string
13+
ip string
14+
port int
1415

15-
buffSize int
16+
buffSize int
1617

17-
logger *logrus.Logger
18+
logger *logrus.Logger
1819
}
1920

21+
// String returns a string representation of the configuration
2022
func (c Configuration) String() string {
2123
return fmt.Sprintf("{iface: %v, ip: %v, port: %v, buffsize: %v}", c.iface, c.ip, c.port, c.buffSize)
2224
}
2325

26+
// SetPFlags sets the pflags for the configuration
2427
func SetPFlags() {
2528
pflag.String("net.ip", "", "IP address to bind")
2629
pflag.String("net.iface", "lo0", "Network interface to bind")
2730
pflag.Int("net.port", 10000, "Port to bind")
2831
pflag.String("net.loglvl", "info", "Log Level for network")
2932
}
3033

34+
// LoadConfiguration loads the configuration from viper
3135
func LoadConfiguration(viper *viper.Viper) Configuration {
3236
c := Configuration{}
3337
c.ip = viper.GetString("net.ip")
@@ -54,14 +58,17 @@ func LoadConfiguration(viper *viper.Viper) Configuration {
5458
return c
5559
}
5660

61+
// Address returns the addresses for the configuration
5762
func (c Configuration) Address() string {
5863
return fmt.Sprintf("%s:%d", c.ip, c.port)
5964
}
6065

66+
// BuffSize returns the buffer size for the configuration (used for UDP connections)
6167
func (c Configuration) BuffSize() int {
6268
return c.buffSize
6369
}
6470

71+
// WithPort returns a new configuration with the port changed
6572
func (c Configuration) WithPort(port int) string {
6673
return fmt.Sprintf("%v:%v", c.ip, port)
6774
}

pkg/neti/messages.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,40 @@ import (
55
"fmt"
66
)
77

8+
// MessageWrap is a wrapper for messages that are sent over the network.
89
type MessageWrap struct {
910
Id string
1011
Msg Message
1112
code uint16
1213
buff *bytes.Buffer
1314
}
1415

16+
// String returns a string representation of the message.
1517
func (m MessageWrap) String() string {
1618
return fmt.Sprintf("%v{cid: %v mcode: %v}", m.Name(), m.Id, m.code)
1719
}
1820

21+
// Name returns the name of the message.
1922
func (m MessageWrap) Name() string {
2023
return "MessageWrap"
2124
}
2225

26+
// Code returns the code of the message.
2327
func (m MessageWrap) Code() uint16 {
2428
return 0
2529
}
2630

31+
// Buff returns the buffer of the message.
2732
func (m MessageWrap) Buff() *bytes.Buffer {
2833
return m.buff
2934
}
3035

36+
// MessageCode is a code for a message.
3137
func (m MessageWrap) MessageCode() uint16 {
3238
return m.code
3339
}
3440

41+
// Serialize serializes the message.
3542
func (m MessageWrap) Serialize(buff *bytes.Buffer) error {
3643
_ = EncodeStringToBuffer(m.Id, buff)
3744
_ = EncodeNumberToBuffer(m.Msg.Code(), buff)
@@ -43,6 +50,7 @@ func (m MessageWrap) Serialize(buff *bytes.Buffer) error {
4350
return nil
4451
}
4552

53+
// Deserialize deserializes the message.
4654
func (m MessageWrap) Deserialize(buff *bytes.Buffer) (Message, error) {
4755
m.Id, _ = DecodeStringFromBuffer(buff)
4856
_ = DecodeNumberFromBuffer(&m.code, buff)

pkg/neti/network.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
type MessageDeserializer func(*bytes.Buffer) (Message, error)
1313

14+
// Message is the interface that wraps the basic Message methods.
1415
type Message interface {
1516
String() string
1617
Name() string
@@ -19,6 +20,8 @@ type Message interface {
1920
Deserialize(buff *bytes.Buffer) (Message, error)
2021
}
2122

23+
// HostConn is the interface that wraps the basic HostConn methods.
24+
// This represents a connection to a host.
2225
type HostConn interface {
2326
fmt.Stringer
2427
Addr() net.Addr
@@ -45,6 +48,11 @@ type SentMessage struct {
4548
Err error
4649
}
4750

51+
// Net is the interface that wraps the basic Net methods.
52+
// This represents a network. It can be a TCP, UDP, etc.
53+
// It can be used to send and receive messages.
54+
// It can be used to listen for new connections.
55+
// It can be used to connect to other hosts.
4856
type Net interface {
4957
RegisterMessage(message Message)
5058
Listen(addr string) (<-chan HostConn, error)
@@ -76,7 +84,8 @@ func readFully(reader io.Reader, toRead int) ([]byte, error) {
7684
return b, err
7785
}
7886

79-
//taken from https://gist.github.com/schwarzeni/f25031a3123f895ff3785970921e962c
87+
// GetInterfaceIpv4Addr returns the IPv4 address of the interface with the given name.
88+
// taken from https://gist.github.com/schwarzeni/f25031a3123f895ff3785970921e962c
8089
func GetInterfaceIpv4Addr(interfaceName string) (addr string, err error) {
8190
var (
8291
ief *net.Interface

pkg/neti/service.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ const (
1313
TCP TransportType = 2
1414
)
1515

16+
// NetClient is an interface for a network client for a NetService.
17+
// Register the messages that the client can receive and send.
18+
// It can be used to send and receive messages.
19+
// It can be used to connect to other hosts.
1620
type NetClient interface {
1721
RegisterMessage(message Message) //Register Message in the NetClient (Known how to deserialize)
1822
RecvFrom(conn *ServiceHostConn) (Message, error) //Receive Message from ServiceHostConn
@@ -24,25 +28,32 @@ type NetClient interface {
2428
Id() string //NodeID of NetClient
2529
}
2630

31+
// NetService is an interface for a network service for a NetClient.
32+
// Register in the NetService the client id to get a NetClient.
33+
// NetService multiplexes the connections to the NetClient.
2734
type NetService interface {
2835
RegisterListener(id string) NetClient
2936
GetConfiguration() Configuration
3037
}
3138

39+
// ServiceHostConn is a HostConn that multiplexes the connections to the NetClient.
3240
type ServiceHostConn struct {
3341
Conn HostConn
3442
ServiceId string
3543
Msg Message
3644
}
3745

46+
// String returns the string representation of the ServiceHostConn.
3847
func (s *ServiceHostConn) String() string {
3948
return fmt.Sprintf("%v %v", s.ServiceId, s.Conn.String())
4049
}
4150

51+
// Addr returns the address of the ServiceHostConn.
4252
func (s *ServiceHostConn) Addr() net.Addr {
4353
return s.Conn.Addr()
4454
}
4555

56+
// Send sends the bytes to the Host on the other end of the ServiceHostConn.
4657
func (s *ServiceHostConn) Send(b []byte) error {
4758
buff := new(bytes.Buffer)
4859
if err := EncodeStringToBuffer(s.ServiceId, buff); err != nil {
@@ -54,6 +65,7 @@ func (s *ServiceHostConn) Send(b []byte) error {
5465
return s.Conn.Send(buff.Bytes())
5566
}
5667

68+
// Receive receives the bytes from the Host on the other end of the ServiceHostConn.
5769
func (s *ServiceHostConn) Receive() ([]byte, error) {
5870
b, err := s.Conn.Receive()
5971
buff := bytes.NewBuffer(b)
@@ -66,6 +78,7 @@ func (s *ServiceHostConn) Receive() ([]byte, error) {
6678
return DecodeBytesFromBuffer(buff)
6779
}
6880

81+
// Close closes the ServiceHostConn.
6982
func (s *ServiceHostConn) Close() error {
7083
return s.Conn.Close()
7184
}

pkg/neti/simService.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ type simService struct {
100100
listenners map[string]*simClient
101101
}
102102

103+
// NewSimUDPService creates a new SimUDPService
104+
// This is a service that can be used to simulate a network.
103105
func NewSimUDPService() NetService {
104106
return &simService{
105107
protos: make(map[string]uint64),

pkg/neti/utils.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
)
99

10+
// EncodeString encodes a string to a byte array
1011
func EncodeString(s string) ([]byte, error) {
1112
b := new(bytes.Buffer)
1213
err := EncodeStringToBuffer(s, b)
@@ -17,16 +18,19 @@ func EncodeString(s string) ([]byte, error) {
1718
return b.Bytes(), nil
1819
}
1920

21+
// DecodeString decodes a string from a byte array
2022
func DecodeString(buf []byte) (string, error) {
2123
b := bytes.NewBuffer(buf)
2224
return DecodeStringFromBuffer(b)
2325
}
2426

27+
// EncodeStringToBuffer encodes a string to a buffer
2528
func EncodeStringToBuffer(s string, buffer *bytes.Buffer) error {
2629
sb := []byte(s)
2730
return EncodeBytesToBuffer(sb, buffer)
2831
}
2932

33+
// DecodeStringFromBuffer decodes a string from a buffer
3034
func DecodeStringFromBuffer(buffer *bytes.Buffer) (string, error) {
3135
sb, err := DecodeBytesFromBuffer(buffer)
3236
if err != nil {
@@ -36,6 +40,7 @@ func DecodeStringFromBuffer(buffer *bytes.Buffer) (string, error) {
3640
return string(sb), nil
3741
}
3842

43+
// EncodeBytesToBuffer encodes a byte array to a buffer
3944
func EncodeBytesToBuffer(b []byte, buffer *bytes.Buffer) error {
4045
err := binary.Write(buffer, binary.BigEndian, uint16(len(b)))
4146
if err != nil {
@@ -55,6 +60,7 @@ func EncodeBytesToBuffer(b []byte, buffer *bytes.Buffer) error {
5560
return nil
5661
}
5762

63+
// DecodeBytesFromBuffer decodes a byte array from a buffer
5864
func DecodeBytesFromBuffer(buffer *bytes.Buffer) ([]byte, error) {
5965
var bLen uint16
6066
err := binary.Read(buffer, binary.BigEndian, &bLen)
@@ -78,13 +84,16 @@ func DecodeBytesFromBuffer(buffer *bytes.Buffer) ([]byte, error) {
7884
return b, nil
7985
}
8086

87+
// EncodeNumberToBuffer encodes a number to a buffer
8188
func EncodeNumberToBuffer(n interface{}, buffer *bytes.Buffer) error {
8289
if err := binary.Write(buffer, binary.BigEndian, n); err != nil {
8390
panic(err)
8491
}
8592
return nil
8693
}
8794

95+
// DecodeNumberFromBuffer decodes a number from a buffer
96+
// Note that nPointer must be a pointer to the type of the number
8897
func DecodeNumberFromBuffer(nPointer interface{}, buffer *bytes.Buffer) error {
8998
if err := binary.Read(buffer, binary.BigEndian, nPointer); err != nil {
9099
panic(err)

0 commit comments

Comments
 (0)