@@ -325,6 +325,7 @@ func (w *WriteBroadcastDistributionTable) produceBvlciBDT(entries []readWriteMod
325325 }
326326 return
327327}
328+
328329func (w * WriteBroadcastDistributionTable ) Encode (bvlpdu Arg ) error {
329330 switch bvlpdu := bvlpdu .(type ) {
330331 case BVLPDU :
@@ -419,6 +420,7 @@ func (w *ReadBroadcastDistributionTable) String() string {
419420
420421type ReadBroadcastDistributionTableAck struct {
421422 * _BVLPDU
423+
422424 bvlciBDT []* Address
423425}
424426
@@ -514,27 +516,108 @@ func (w *ReadBroadcastDistributionTableAck) String() string {
514516 return fmt .Sprintf ("ReadBroadcastDistributionTableAck{%v, bvlciBDT: %v}" , w ._BVLPDU , w .bvlciBDT )
515517}
516518
517- // TODO: finish
518519type ForwardedNPDU struct {
519520 * _BVLPDU
521+
522+ bvlciAddress * Address
520523}
521524
522525var _ BVLPDU = (* ForwardedNPDU )(nil )
523526
524- func NewForwardedNPDU () ( BVLPDU , error ) {
527+ func NewForwardedNPDU (pdu PDU , opts ... func ( * ForwardedNPDU )) ( * ForwardedNPDU , error ) {
525528 b := & ForwardedNPDU {}
526- b ._BVLPDU = NewBVLPDU (nil ).(* _BVLPDU )
529+ for _ , opt := range opts {
530+ opt (b )
531+ }
532+ switch npdu := pdu .(type ) {
533+ case readWriteModel.NPDUExactly :
534+ b ._BVLPDU = NewBVLPDU (readWriteModel .NewBVLCForwardedNPDU (b .produceInnerNPDU (npdu ))).(* _BVLPDU )
535+ case nil :
536+ b ._BVLPDU = NewBVLPDU (nil ).(* _BVLPDU )
537+ default :
538+ // TODO: re-encode seems expensive... check if there is a better option (e.g. only do it on the message bridge)
539+ data := pdu .GetPduData ()
540+ parse , err := readWriteModel .NPDUParse (context .Background (), data , uint16 (len (data )))
541+ if err != nil {
542+ return nil , errors .Wrap (err , "error re-encoding" )
543+ }
544+ b ._BVLPDU = NewBVLPDU (readWriteModel .NewBVLCForwardedNPDU (b .produceInnerNPDU (parse ))).(* _BVLPDU )
545+ }
527546 return b , nil
528547}
529548
530- func (b * ForwardedNPDU ) Encode (pdu Arg ) error {
531- // TODO: finish
532- return nil
549+ func WithForwardedNPDUAddress (addr * Address ) func (* ForwardedNPDU ) {
550+ return func (b * ForwardedNPDU ) {
551+ b .bvlciAddress = addr
552+ }
533553}
534554
535- func (b * ForwardedNPDU ) Decode (pdu Arg ) error {
536- // TODO: finish
537- return nil
555+ func (w * ForwardedNPDU ) GetBvlciAddress () * Address {
556+ return w .bvlciAddress
557+ }
558+
559+ func (w * ForwardedNPDU ) produceInnerNPDU (inNpdu readWriteModel.NPDU ) (ip []uint8 , port uint16 , npdu readWriteModel.NPDU , bvlcPayloadLength uint16 ) {
560+ ip = w .bvlciAddress .AddrAddress [:4 ]
561+ port = uint16 (47808 )
562+ if w .bvlciAddress .AddrPort != nil {
563+ port = * w .bvlciAddress .AddrPort
564+ }
565+ npdu = inNpdu
566+ return
567+ }
568+
569+ func (w * ForwardedNPDU ) Encode (bvlpdu Arg ) error {
570+ switch bvlpdu := bvlpdu .(type ) {
571+ case BVLPDU :
572+ if err := bvlpdu .Update (w ); err != nil {
573+ return errors .Wrap (err , "error updating BVLPDU" )
574+ }
575+
576+ // encode the addrress
577+ bvlpdu .PutData (w .bvlciAddress .AddrAddress ... )
578+
579+ // encode the rest of the data
580+ bvlpdu .PutData (w .GetPduData ()... )
581+
582+ bvlpdu .setBVLC (w .bvlc )
583+ return nil
584+ default :
585+ return errors .Errorf ("invalid BVLPDU type %T" , bvlpdu )
586+ }
587+ }
588+
589+ func (w * ForwardedNPDU ) Decode (bvlpdu Arg ) error {
590+ switch bvlpdu := bvlpdu .(type ) {
591+ case BVLPDU :
592+ if err := w .Update (bvlpdu ); err != nil {
593+ return errors .Wrap (err , "error updating BVLPDU" )
594+ }
595+ switch pduUserData := bvlpdu .GetPDUUserData ().(type ) {
596+ case readWriteModel.BVLCForwardedNPDUExactly :
597+ switch bvlc := pduUserData .(type ) {
598+ case readWriteModel.BVLCForwardedNPDU :
599+ addr := bvlc .GetIp ()
600+ port := bvlc .GetPort ()
601+ var portArray = make ([]byte , 2 )
602+ binary .BigEndian .PutUint16 (portArray , port )
603+ var err error
604+ address , err := NewAddress (zerolog .Nop (), append (addr , portArray ... ))
605+ if err != nil {
606+ return errors .Wrap (err , "error creating address" )
607+ }
608+ w .bvlciAddress = address
609+
610+ w .setBVLC (bvlc )
611+ }
612+ }
613+ return nil
614+ default :
615+ return errors .Errorf ("invalid BVLPDU type %T" , bvlpdu )
616+ }
617+ }
618+
619+ func (w * ForwardedNPDU ) String () string {
620+ return fmt .Sprintf ("ForwardedNPDU{%v, bvlciAddress: %v}" , w ._BVLPDU , w .bvlciAddress )
538621}
539622
540623// TODO: finish
@@ -844,7 +927,7 @@ func init() {
844927 return v
845928 },
846929 0x04 : func () interface { Decode (Arg ) error } {
847- v , _ := NewForwardedNPDU ()
930+ v , _ := NewForwardedNPDU (nil )
848931 return v
849932 },
850933 0x05 : func () interface { Decode (Arg ) error } {
0 commit comments