2018-01-12 18:31:47 +00:00
|
|
|
package connection
|
|
|
|
|
|
|
|
import (
|
2018-01-14 16:45:30 +00:00
|
|
|
"errors"
|
2018-01-12 18:31:47 +00:00
|
|
|
"github.com/s-rah/go-ricochet/utils"
|
|
|
|
"github.com/s-rah/go-ricochet/wire/control"
|
|
|
|
)
|
|
|
|
|
2018-01-17 18:18:46 +00:00
|
|
|
// ControlChannel encapsulates logic for the control channel processing
|
2018-01-14 16:45:30 +00:00
|
|
|
type ControlChannel struct {
|
|
|
|
channelManager *ChannelManager
|
|
|
|
}
|
|
|
|
|
2018-01-17 18:18:46 +00:00
|
|
|
// Init sets up a control channel
|
2018-01-14 16:45:30 +00:00
|
|
|
func (ctrl *ControlChannel) Init(channelManager *ChannelManager) {
|
|
|
|
ctrl.channelManager = channelManager
|
|
|
|
}
|
|
|
|
|
2018-01-17 18:18:46 +00:00
|
|
|
// ProcessChannelResult contains the logic for processing a channelresult message
|
2018-01-14 16:45:30 +00:00
|
|
|
func (ctrl *ControlChannel) ProcessChannelResult(cr *Protocol_Data_Control.ChannelResult) (bool, error) {
|
|
|
|
id := cr.GetChannelIdentifier()
|
|
|
|
|
|
|
|
channel, found := ctrl.channelManager.GetChannel(id)
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
return false, utils.UnexpectedChannelResultError
|
|
|
|
}
|
|
|
|
|
|
|
|
if cr.GetOpened() {
|
|
|
|
//rc.traceLog(fmt.Sprintf("channel of type %v opened on %v", channel.Type, id))
|
|
|
|
channel.Handler.OpenOutboundResult(nil, cr)
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
//rc.traceLog(fmt.Sprintf("channel of type %v rejected on %v", channel.Type, id))
|
|
|
|
channel.Handler.OpenOutboundResult(errors.New(cr.GetCommonError().String()), cr)
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2018-01-17 18:18:46 +00:00
|
|
|
// ProcessKeepAlive contains logic for responding to keep alives
|
2018-01-14 16:45:30 +00:00
|
|
|
func (ctrl *ControlChannel) ProcessKeepAlive(ka *Protocol_Data_Control.KeepAlive) (bool, []byte) {
|
2018-01-13 16:43:49 +00:00
|
|
|
if ka.GetResponseRequested() {
|
|
|
|
messageBuilder := new(utils.MessageBuilder)
|
|
|
|
return true, messageBuilder.KeepAlive(true)
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2018-01-12 18:31:47 +00:00
|
|
|
// ProcessEnableFeatures correctly handles a features enabled packet
|
2018-01-14 16:45:30 +00:00
|
|
|
func (ctrl *ControlChannel) ProcessEnableFeatures(handler Handler, ef *Protocol_Data_Control.EnableFeatures) []byte {
|
2018-01-12 18:31:47 +00:00
|
|
|
featuresToEnable := ef.GetFeature()
|
|
|
|
supportChannels := handler.GetSupportedChannelTypes()
|
|
|
|
result := []string{}
|
|
|
|
for _, v := range featuresToEnable {
|
|
|
|
for _, s := range supportChannels {
|
|
|
|
if v == s {
|
|
|
|
result = append(result, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
messageBuilder := new(utils.MessageBuilder)
|
|
|
|
raw := messageBuilder.FeaturesEnabled(result)
|
|
|
|
return raw
|
|
|
|
}
|