From f953a6ce015b40d9d56dfacff0bed8422f5dab9d Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Wed, 18 Mar 2020 14:04:43 -0400 Subject: [PATCH] Working out how the current 'accepter' concept might be generalized for an Xlink implementation. (#54) --- router/router.go | 2 +- xlink_transport/accepter.go | 23 +++++++++++++++++++++++ xlink_transport/factory.go | 17 ++++++++++------- xlink_transport/xlink.go | 20 ++++++++++++++++++++ 4 files changed, 54 insertions(+), 8 deletions(-) create mode 100644 xlink_transport/accepter.go diff --git a/router/router.go b/router/router.go index e81fe4bba..0cc0d936a 100644 --- a/router/router.go +++ b/router/router.go @@ -1,5 +1,5 @@ /* - Copyright 2019 NetFoundry, Inc. + (c) Copyright NetFoundry, Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/xlink_transport/accepter.go b/xlink_transport/accepter.go new file mode 100644 index 000000000..6daf0c5c2 --- /dev/null +++ b/xlink_transport/accepter.go @@ -0,0 +1,23 @@ +/* + Copyright 2019 NetFoundry, Inc. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package xlink_transport + +import "github.com/netfoundry/ziti-foundation/channel2" + +type Accepter interface { + Accept(ch channel2.Channel) error +} \ No newline at end of file diff --git a/xlink_transport/factory.go b/xlink_transport/factory.go index e15f8311f..8581c9b4c 100644 --- a/xlink_transport/factory.go +++ b/xlink_transport/factory.go @@ -19,21 +19,24 @@ package xlink_transport import ( "fmt" "github.com/netfoundry/ziti-fabric/xlink" + "github.com/netfoundry/ziti-foundation/channel2" "github.com/netfoundry/ziti-foundation/identity/identity" ) -func newFactory() xlink.Factory { - return &factory{} +func newFactory(accepter Accepter, options *channel2.Options) xlink.Factory { + return &factory{accepter: accepter, options: options} } -func (_ *factory) Create(id *identity.TokenId, configData map[interface{}]interface{}) (xlink.Xlink, error) { +func (self *factory) Create(id *identity.TokenId, configData map[interface{}]interface{}) (xlink.Xlink, error) { c, err := loadConfig(configData) if err != nil { return nil, fmt.Errorf("error loading configuration (%w)", err) } - return &impl{id: id, config: c}, nil + return &impl{id: id, config: c, accepter: self.accepter, options: self.options}, nil } -type factory struct{ - id *identity.TokenId -} \ No newline at end of file +type factory struct { + id *identity.TokenId + accepter Accepter + options *channel2.Options +} diff --git a/xlink_transport/xlink.go b/xlink_transport/xlink.go index e09819b07..e86388a5f 100644 --- a/xlink_transport/xlink.go +++ b/xlink_transport/xlink.go @@ -20,6 +20,7 @@ import ( "fmt" "github.com/netfoundry/ziti-foundation/channel2" "github.com/netfoundry/ziti-foundation/identity/identity" + "github.com/sirupsen/logrus" ) func (self *impl) Listen() error { @@ -27,6 +28,7 @@ func (self *impl) Listen() error { if err := self.listener.Listen(); err != nil { return fmt.Errorf("error listening (%w)", err) } + go self.runAccepter() return nil } @@ -38,8 +40,26 @@ func (_ *impl) GetAdvertisement() string { return "" } +func (self *impl) runAccepter() { + defer logrus.Warn("exited") + logrus.Info("started") + + for { + ch, err := channel2.NewChannel("link", self.listener, self.options) + if err == nil { + if err := self.accepter.Accept(ch); err != nil { + logrus.Errorf("error invoking accepter (%w)", err) + } + } else { + logrus.Errorf("error accepting (%w)", err) + } + } +} + type impl struct { id *identity.TokenId config *config listener channel2.UnderlayListener + accepter Accepter + options *channel2.Options }