Skip to content

Commit

Permalink
Remove AddSrvHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
louisroyer committed Feb 2, 2022
1 parent 8f18018 commit d680276
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 58 deletions.
30 changes: 8 additions & 22 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,31 @@ package pfcp_networking

import (
"fmt"
"log"
"net"

"github.com/wmnsk/go-pfcp/ie"
"github.com/wmnsk/go-pfcp/message"
)

func handleHeartbeatRequest(entity *PFCPEntity, senderAddr net.Addr, msg message.Message) error {
res := message.NewHeartbeatResponse(msg.Sequence(), entity.RecoveryTimeStamp)
func handleHeartbeatRequest(entity PFCPEntityInterface, senderAddr net.Addr, msg message.Message) error {
res := message.NewHeartbeatResponse(msg.Sequence(), entity.RecoveryTimeStamp())
return entity.ReplyTo(senderAddr, msg, res)
}

func handleAssociationSetupRequest(srv *PFCPServerEntity, senderAddr net.Addr, msg message.Message) error {
func handleAssociationSetupRequest(entity PFCPEntityInterface, senderAddr net.Addr, msg message.Message) error {
m, ok := msg.(*message.AssociationSetupRequest)
if !ok {
return fmt.Errorf("Issue with Association Setup Request")
}
peer, err := NewPFCPPeer(&srv.PFCPEntity, m.NodeID)
peer, err := NewPFCPPeer(entity, m.NodeID)
if err != nil {
return err
}
association := NewAssociation(peer, srv)
err = srv.CreatePFCPAssociation(&association)
association := NewAssociation(peer, entity)
err = entity.CreatePFCPAssociation(&association)
if err != nil {
return err
}
switch {
case msg == nil:
log.Println("msg is nil")
fallthrough
case srv == nil:
log.Println("srv is nil")
fallthrough
case srv.NodeID == nil:
log.Println("srv.NodeID is nil")
fallthrough
case srv.RecoveryTimeStamp == nil:
log.Println("srv.RecoveryTimeStamp is nil")
}
res := message.NewAssociationSetupResponse(msg.Sequence(), srv.NodeID, ie.NewCause(ie.CauseRequestAccepted), srv.RecoveryTimeStamp)
return srv.ReplyTo(senderAddr, msg, res)
res := message.NewAssociationSetupResponse(msg.Sequence(), entity.NodeID(), ie.NewCause(ie.CauseRequestAccepted), entity.RecoveryTimeStamp())
return entity.ReplyTo(senderAddr, msg, res)
}
11 changes: 6 additions & 5 deletions pfcp_client_entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ func NewPFCPClientEntity(nodeID string) PFCPClientEntity {
associations: make(map[string]*PFCPAssociation),
muAssociations: sync.Mutex{},
}
e.iface = e
return e
}

// Add an association to the association table
func (e *PFCPClientEntity) createPFCPAssociation(association *PFCPAssociation) error {
func (e PFCPClientEntity) CreatePFCPAssociation(association *PFCPAssociation) error {
nid, err := association.NodeID.NodeID()
if err != nil {
return err
Expand All @@ -36,7 +37,7 @@ func (e *PFCPClientEntity) createPFCPAssociation(association *PFCPAssociation) e
}

// Remove an association from the association table
func (e *PFCPClientEntity) RemovePFCPAssociation(association *PFCPAssociation) error {
func (e PFCPClientEntity) RemovePFCPAssociation(association *PFCPAssociation) error {
nid, err := association.NodeID.NodeID()
if err != nil {
return err
Expand All @@ -49,7 +50,7 @@ func (e *PFCPClientEntity) RemovePFCPAssociation(association *PFCPAssociation) e

// Returns an existing PFCP Association
func (e *PFCPClientEntity) GetPFCPAssociation(nodeID *ie.IE) (association *PFCPAssociation, err error) {
nid, err := e.NodeID.NodeID()
nid, err := e.NodeID().NodeID()
if err != nil {
return nil, err
}
Expand All @@ -71,7 +72,7 @@ func (e *PFCPClientEntity) NewPFCPAssociation(peer *PFCPPeer) (association *PFCP
if _, exists := e.associations[nid]; exists {
return nil, fmt.Errorf("Only one association shall be setup between given pair of CP and UP functions.")
}
sar := message.NewAssociationSetupRequest(0, e.NodeID, e.RecoveryTimeStamp)
sar := message.NewAssociationSetupRequest(0, e.NodeID(), e.RecoveryTimeStamp())
resp, err := peer.Send(sar)
if err != nil {
return nil, err
Expand All @@ -87,7 +88,7 @@ func (e *PFCPClientEntity) NewPFCPAssociation(peer *PFCPPeer) (association *PFCP
}
if cause == ie.CauseRequestAccepted {
a := &PFCPAssociation{PFCPPeer: peer, localEntity: e}
e.createPFCPAssociation(a)
e.CreatePFCPAssociation(a)
return a, nil
}
return nil, fmt.Errorf("Associaton setup request rejected")
Expand Down
34 changes: 25 additions & 9 deletions pfcp_entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ import (
)

type PFCPEntityInterface interface {
NodeID() *ie.IE
RecoveryTimeStamp() *ie.IE
CreatePFCPAssociation(association *PFCPAssociation) error
RemovePFCPAssociation(association *PFCPAssociation) error
ReplyTo(ipAddress net.Addr, requestMessage message.Message, responseMessage message.Message) error
}

func (entity *PFCPEntity) ReplyTo(ipAddress net.Addr, requestMessage message.Message, responseMessage message.Message) error {
func (entity PFCPEntity) ReplyTo(ipAddress net.Addr, requestMessage message.Message, responseMessage message.Message) error {
if !pfcputil.IsMessageTypeRequest(requestMessage.MessageType()) {
return fmt.Errorf("requestMessage shall be a Request Message")
}
Expand All @@ -40,14 +44,22 @@ func (entity *PFCPEntity) ReplyTo(ipAddress net.Addr, requestMessage message.Mes
return nil
}

type handler = func(entity *PFCPEntity, senderAddr net.Addr, msg message.Message) error
type handler = func(entity PFCPEntityInterface, senderAddr net.Addr, msg message.Message) error

type PFCPEntity struct {
NodeID *ie.IE
RecoveryTimeStamp *ie.IE
nodeID *ie.IE
recoveryTimeStamp *ie.IE
handlers map[pfcputil.MessageType]handler
conn *net.UDPConn
mu sync.Mutex
iface PFCPEntityInterface
}

func (e PFCPEntity) NodeID() *ie.IE {
return e.nodeID
}
func (e PFCPEntity) RecoveryTimeStamp() *ie.IE {
return e.recoveryTimeStamp
}

func newDefaultPFCPEntityHandlers() map[pfcputil.MessageType]handler {
Expand All @@ -58,17 +70,21 @@ func newDefaultPFCPEntityHandlers() map[pfcputil.MessageType]handler {

func NewPFCPEntity(nodeID string) PFCPEntity {
return PFCPEntity{
NodeID: pfcputil.CreateNodeID(nodeID),
RecoveryTimeStamp: nil,
nodeID: pfcputil.CreateNodeID(nodeID),
recoveryTimeStamp: nil,
handlers: newDefaultPFCPEntityHandlers(),
conn: nil,
mu: sync.Mutex{},
iface: nil,
}
}

func (e *PFCPEntity) Start() error {
e.RecoveryTimeStamp = ie.NewRecoveryTimeStamp(time.Now())
ipAddr, err := e.NodeID.NodeID()
if e.iface == nil {
return fmt.Errorf("PFCPEntity is incorrectly initialized")
}
e.recoveryTimeStamp = ie.NewRecoveryTimeStamp(time.Now())
ipAddr, err := e.NodeID().NodeID()
if err != nil {
return err
}
Expand All @@ -95,7 +111,7 @@ func (e *PFCPEntity) Start() error {
continue
}
if f, exists := e.handlers[msg.MessageType()]; exists {
err := f(e, addr, msg)
err := f(e.iface, addr, msg)
if err != nil {
log.Println(err)
}
Expand Down
8 changes: 4 additions & 4 deletions pfcp_peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type messageChan chan []byte

type PFCPPeer struct {
NodeID *ie.IE
Srv *PFCPEntity
Srv PFCPEntityInterface
conn *net.UDPConn
udpAddr *net.UDPAddr
seq uint32
Expand All @@ -24,7 +24,7 @@ type PFCPPeer struct {
stop bool
}

func NewPFCPPeer(srv *PFCPEntity, nodeID *ie.IE) (peer *PFCPPeer, err error) {
func NewPFCPPeer(srv PFCPEntityInterface, nodeID *ie.IE) (peer *PFCPPeer, err error) {
ipAddr, err := nodeID.NodeID()
if err != nil {
return nil, err
Expand Down Expand Up @@ -187,12 +187,12 @@ func (peer *PFCPPeer) Send(msg message.Message) (m message.Message, err error) {

// Send an Heartbeat request, return true if the PFCP peer is alive.
func (peer *PFCPPeer) IsAlive() (res bool, err error) {
if peer.Srv.RecoveryTimeStamp == nil {
if peer.Srv.RecoveryTimeStamp() == nil {
return false, fmt.Errorf("SMF is not started.")
}
hreq := message.NewHeartbeatRequest(
0,
peer.Srv.RecoveryTimeStamp,
peer.Srv.RecoveryTimeStamp(),
nil)

_, err = peer.Send(hreq)
Expand Down
22 changes: 4 additions & 18 deletions pfcp_server_entity.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package pfcp_networking

import (
"fmt"
"net"
"sync"

"github.com/louisroyer/go-pfcp-networking/pfcputil"
"github.com/wmnsk/go-pfcp/message"
)

Expand All @@ -22,29 +20,17 @@ func NewPFCPServerEntity(nodeID string) PFCPServerEntity {
associations: make(map[string]*PFCPAssociation),
muAssociations: sync.Mutex{},
}
e.iface = e
e.initDefaultHandlers()
return e
}

func (e *PFCPServerEntity) initDefaultHandlers() error {
return e.AddServerHandler(message.MsgTypeAssociationSetupRequest, handleAssociationSetupRequest)
}

func (e *PFCPServerEntity) AddServerHandler(t pfcputil.MessageType, h serverHandler) error {
f := func(entity *PFCPEntity, senderAddr net.Addr, msg message.Message) error {
if e == nil {
return fmt.Errorf("PFCPServerEntity is nil")
}
if e.RecoveryTimeStamp == nil {
return fmt.Errorf("RecoveryTimestamp is nil")
}
return h(e, senderAddr, msg)
}
return e.AddHandler(t, f)
return e.AddHandler(message.MsgTypeAssociationSetupRequest, handleAssociationSetupRequest)
}

// Add an association to the association table
func (e *PFCPServerEntity) CreatePFCPAssociation(association *PFCPAssociation) error {
func (e PFCPServerEntity) CreatePFCPAssociation(association *PFCPAssociation) error {
nid, err := association.NodeID.NodeID()
if err != nil {
return err
Expand All @@ -56,7 +42,7 @@ func (e *PFCPServerEntity) CreatePFCPAssociation(association *PFCPAssociation) e
}

// Remove an association from the association table
func (e *PFCPServerEntity) RemovePFCPAssociation(association *PFCPAssociation) error {
func (e PFCPServerEntity) RemovePFCPAssociation(association *PFCPAssociation) error {
nid, err := association.NodeID.NodeID()
if err != nil {
return err
Expand Down

0 comments on commit d680276

Please sign in to comment.