2017-11-22 07:02:11 +00:00
|
|
|
package libnmdc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AutodetectProtocol struct {
|
|
|
|
hc *HubConnection
|
|
|
|
|
|
|
|
realProtoMut sync.Mutex
|
|
|
|
realProto Protocol
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAutodetectProtocol(hc *HubConnection) Protocol {
|
|
|
|
proto := AutodetectProtocol{
|
|
|
|
hc: hc,
|
|
|
|
realProto: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
go proto.timeout()
|
|
|
|
|
|
|
|
return &proto
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *AutodetectProtocol) timeout() {
|
|
|
|
time.Sleep(AUTODETECT_ADC_NMDC_TIMEOUT)
|
|
|
|
|
|
|
|
this.realProtoMut.Lock()
|
|
|
|
defer this.realProtoMut.Unlock()
|
|
|
|
|
|
|
|
if this.realProto == nil {
|
|
|
|
this.realProto = NewAdcProtocol(this.hc)
|
|
|
|
this.hc.processEvent(HubEvent{EventType: EVENT_DEBUG_MESSAGE, Message: "Detected ADC protocol"})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *AutodetectProtocol) ProcessCommand(msg string) {
|
|
|
|
this.realProtoMut.Lock()
|
|
|
|
defer this.realProtoMut.Unlock()
|
|
|
|
|
|
|
|
if this.realProto == nil {
|
|
|
|
// We actually got some data using $ as the separator?
|
|
|
|
// Upgrade to a full NMDC protocol
|
|
|
|
this.realProto = NewNmdcProtocol(this.hc)
|
|
|
|
this.hc.processEvent(HubEvent{EventType: EVENT_DEBUG_MESSAGE, Message: "Detected NMDC protocol"})
|
|
|
|
}
|
|
|
|
|
|
|
|
this.realProto.ProcessCommand(msg)
|
|
|
|
}
|
|
|
|
|
2018-06-04 04:27:34 +00:00
|
|
|
func (this *AutodetectProtocol) SayPublic(msg string) error {
|
2017-11-22 07:02:11 +00:00
|
|
|
this.realProtoMut.Lock()
|
|
|
|
defer this.realProtoMut.Unlock()
|
|
|
|
|
|
|
|
if this.realProto == nil {
|
|
|
|
this.realProto = NewNmdcProtocol(this.hc)
|
|
|
|
}
|
2018-06-04 04:27:34 +00:00
|
|
|
return this.realProto.SayPublic(msg)
|
2017-11-22 07:02:11 +00:00
|
|
|
}
|
|
|
|
|
2018-06-04 04:27:34 +00:00
|
|
|
func (this *AutodetectProtocol) SayPrivate(user, message string) error {
|
2017-11-22 07:02:11 +00:00
|
|
|
this.realProtoMut.Lock()
|
|
|
|
defer this.realProtoMut.Unlock()
|
|
|
|
|
|
|
|
if this.realProto == nil {
|
|
|
|
this.realProto = NewNmdcProtocol(this.hc)
|
|
|
|
}
|
2018-06-04 04:27:34 +00:00
|
|
|
return this.realProto.SayPrivate(user, message)
|
2017-11-22 07:02:11 +00:00
|
|
|
}
|
|
|
|
|
2018-06-04 04:27:34 +00:00
|
|
|
func (this *AutodetectProtocol) SayInfo() error {
|
2018-03-24 00:21:49 +00:00
|
|
|
this.realProtoMut.Lock()
|
|
|
|
defer this.realProtoMut.Unlock()
|
|
|
|
|
|
|
|
if this.realProto == nil {
|
|
|
|
this.realProto = NewNmdcProtocol(this.hc)
|
|
|
|
}
|
2018-06-04 04:27:34 +00:00
|
|
|
return this.realProto.SayInfo()
|
2018-03-24 00:21:49 +00:00
|
|
|
}
|
|
|
|
|
2017-11-22 07:02:11 +00:00
|
|
|
func (this *AutodetectProtocol) ProtoMessageSeparator() string {
|
|
|
|
this.realProtoMut.Lock()
|
|
|
|
defer this.realProtoMut.Unlock()
|
|
|
|
|
|
|
|
if this.realProto == nil {
|
|
|
|
return "|"
|
|
|
|
}
|
|
|
|
return this.realProto.ProtoMessageSeparator()
|
|
|
|
}
|