Don't report unknown events as errors

parent 0a1d45fa
......@@ -18,7 +18,6 @@
package katzenpost
import (
"errors"
"time"
"github.com/katzenpost/core/crypto/ecdh"
......@@ -33,11 +32,19 @@ const (
var identityKeyBytes = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
// TimeoutError is returned on timeouts
type TimeoutError struct{}
func (t TimeoutError) Error() string {
return "Timeout"
}
// Client is katzenpost object
type Client struct {
address string
proxy *mailproxy.Proxy
eventSink chan event.Event
recvCh chan bool
}
// New creates a katzenpost client
......@@ -70,8 +77,9 @@ func New(cfg Config) (Client, error) {
return Client{}, err
}
recvCh := make(chan bool, 10)
proxy, err := mailproxy.New(&proxyCfg)
return Client{cfg.getAddress(), proxy, eventSink}, err
return Client{cfg.getAddress(), proxy, eventSink, recvCh}, err
}
// Shutdown the client
......@@ -96,24 +104,31 @@ type Message struct {
// GetMessage from katzenpost
func (c Client) GetMessage(timeout int64) (Message, error) {
if timeout == 0 {
ev := <-c.eventSink
return c.handleEvent(ev)
<-c.recvCh
return c.getMsg()
}
select {
case ev := <-c.eventSink:
return c.handleEvent(ev)
case <-c.recvCh:
return c.getMsg()
case <-time.After(time.Second * time.Duration(timeout)):
return Message{}, errors.New("Timeout")
return Message{}, TimeoutError{}
}
}
func (c Client) handleEvent(ev event.Event) (Message, error) {
switch ev.(type) {
case *event.MessageReceivedEvent:
msg, err := c.proxy.ReceivePop(c.address)
return Message{msg.SenderID, string(msg.Payload)}, err
default:
return Message{}, errors.New("Another event arrived")
func (c Client) getMsg() (Message, error) {
msg, err := c.proxy.ReceivePop(c.address)
return Message{msg.SenderID, string(msg.Payload)}, err
}
func (c Client) eventHandler() {
for {
ev := <-c.eventSink
switch ev.(type) {
case *event.MessageReceivedEvent:
c.recvCh <- true
default:
continue
}
}
}
......@@ -63,6 +63,7 @@ func (c Config) getAccount() *config.Account {
Authority: pkiName,
LinkKey: c.LinkKey.priv,
IdentityKey: &identityKey,
StorageKey: nil,
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment