Skip to content
This repository has been archived by the owner on Jan 9, 2024. It is now read-only.

Commit

Permalink
Factors out messagesender
Browse files Browse the repository at this point in the history
  • Loading branch information
lbarman committed Jan 8, 2017
1 parent a8bd722 commit 6726e1f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 41 deletions.
2 changes: 1 addition & 1 deletion prifi-lib/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (p *PriFiLibInstance) Received_ALL_CLI_PARAMETERS(msg net.ALL_ALL_PARAMETER

//start the broadcast-listener goroutine
log.Lvl2("Client " + strconv.Itoa(p.clientState.ID) + " : starting the broadcast-listener goroutine")
go p.messageSender.ClientSubscribeToBroadcast(p.clientState.Name, p, p.clientState.StartStopReceiveBroadcast)
go p.messageSender.ClientSubscribeToBroadcast(p.clientState.Name, p.ReceivedMessage, p.clientState.StartStopReceiveBroadcast)

//after receiving this message, we are done with the state CLIENT_STATE_BEFORE_INIT, and are ready for initializing
p.clientState.currentState = CLIENT_STATE_INITIALIZING
Expand Down
30 changes: 30 additions & 0 deletions prifi-lib/net/message_sender.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package net

// MessageSender is the interface that abstracts the network
// interactions.
type MessageSender interface {
// SendToClient tries to deliver the message "msg" to the client i.
SendToClient(i int, msg interface{}) error

// SendToTrustee tries to deliver the message "msg" to the trustee i.
SendToTrustee(i int, msg interface{}) error

// SendToRelay tries to deliver the message "msg" to the relay.
SendToRelay(msg interface{}) error

/*
BroadcastToAllClients tries to deliver the message "msg"
to every client, possibly using broadcast.
*/
BroadcastToAllClients(msg interface{}) error

/*
ClientSubscribeToBroadcast should be called by the Clients
in order to receive the Broadcast messages.
Calling the function starts the handler but does not actually
listen for broadcast messages.
Sending true to startStopChan starts receiving the broadcasts.
Sending false to startStopChan stops receiving the broadcasts.
*/
ClientSubscribeToBroadcast(clientName string, messageReceived func(interface{}) error, startStopChan chan bool) error
}
45 changes: 8 additions & 37 deletions prifi-lib/prifi.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Then, it runs the PriFi anonymous communication network among those entities.
// PriFiLibInstance contains the mutable state of a PriFi entity.
type PriFiLibInstance struct {
role int16
messageSender MessageSender
messageSender net.MessageSender
// TODO: combine states into a single interface
clientState ClientState //only one of those will be set
relayState RelayState //only one of those will be set
Expand All @@ -35,35 +35,6 @@ const (
PRIFI_ROLE_TRUSTEE
)

// MessageSender is the interface that abstracts the network
// interactions.
type MessageSender interface {
// SendToClient tries to deliver the message "msg" to the client i.
SendToClient(i int, msg interface{}) error

// SendToTrustee tries to deliver the message "msg" to the trustee i.
SendToTrustee(i int, msg interface{}) error

// SendToRelay tries to deliver the message "msg" to the relay.
SendToRelay(msg interface{}) error

/*
BroadcastToAllClients tries to deliver the message "msg"
to every client, possibly using broadcast.
*/
BroadcastToAllClients(msg interface{}) error

/*
ClientSubscribeToBroadcast should be called by the Clients
in order to receive the Broadcast messages.
Calling the function starts the handler but does not actually
listen for broadcast messages.
Sending true to startStopChan starts receiving the broadcasts.
Sending false to startStopChan stops receiving the broadcasts.
*/
ClientSubscribeToBroadcast(clientName string, protocolInstance *PriFiLibInstance, startStopChan chan bool) error
}

/*
call the functions below on the appropriate machine on the network.
if you call *without state* (one of the first 3 methods), IT IS NOT SUFFICIENT FOR PRIFI to start; this entity will expect a ALL_ALL_PARAMETERS as a
Expand All @@ -75,7 +46,7 @@ Otherwise, the 3 last methods fully initialize the entity.
// Note: the returned state is not sufficient for the PrFi protocol
// to start; this entity will expect a ALL_ALL_PARAMETERS message as
// first received message to complete it's state.
func NewPriFiRelay(msgSender MessageSender) *PriFiLibInstance {
func NewPriFiRelay(msgSender net.MessageSender) *PriFiLibInstance {
prifi := PriFiLibInstance{
role: PRIFI_ROLE_RELAY,
messageSender: msgSender,
Expand All @@ -88,7 +59,7 @@ func NewPriFiRelay(msgSender MessageSender) *PriFiLibInstance {
// Note: the returned state is not sufficient for the PrFi protocol
// to start; this entity will expect a ALL_ALL_PARAMETERS message as
// first received message to complete it's state.
func NewPriFiClient(msgSender MessageSender) *PriFiLibInstance {
func NewPriFiClient(msgSender net.MessageSender) *PriFiLibInstance {
prifi := PriFiLibInstance{
role: PRIFI_ROLE_CLIENT,
messageSender: msgSender,
Expand All @@ -100,7 +71,7 @@ func NewPriFiClient(msgSender MessageSender) *PriFiLibInstance {
// Note: the returned state is not sufficient for the PrFi protocol
// to start; this entity will expect a ALL_ALL_PARAMETERS message as
// first received message to complete it's state.
func NewPriFiTrustee(msgSender MessageSender) *PriFiLibInstance {
func NewPriFiTrustee(msgSender net.MessageSender) *PriFiLibInstance {
prifi := PriFiLibInstance{
role: PRIFI_ROLE_TRUSTEE,
messageSender: msgSender,
Expand All @@ -109,7 +80,7 @@ func NewPriFiTrustee(msgSender MessageSender) *PriFiLibInstance {
}

// NewPriFiRelayWithState creates a new PriFi relay entity state.
func NewPriFiRelayWithState(msgSender MessageSender, state *RelayState) *PriFiLibInstance {
func NewPriFiRelayWithState(msgSender net.MessageSender, state *RelayState) *PriFiLibInstance {
prifi := PriFiLibInstance{
role: PRIFI_ROLE_RELAY,
messageSender: msgSender,
Expand All @@ -121,7 +92,7 @@ func NewPriFiRelayWithState(msgSender MessageSender, state *RelayState) *PriFiLi
}

// NewPriFiClientWithState creates a new PriFi client entity state.
func NewPriFiClientWithState(msgSender MessageSender, state *ClientState) *PriFiLibInstance {
func NewPriFiClientWithState(msgSender net.MessageSender, state *ClientState) *PriFiLibInstance {
prifi := PriFiLibInstance{
role: PRIFI_ROLE_CLIENT,
messageSender: msgSender,
Expand All @@ -130,12 +101,12 @@ func NewPriFiClientWithState(msgSender MessageSender, state *ClientState) *PriFi
log.Lvl1("Client has been initialized by function call. ")

log.Lvl2("Client " + strconv.Itoa(prifi.clientState.ID) + " : starting the broadcast-listener goroutine")
go prifi.messageSender.ClientSubscribeToBroadcast(prifi.clientState.Name, &prifi, prifi.clientState.StartStopReceiveBroadcast)
go prifi.messageSender.ClientSubscribeToBroadcast(prifi.clientState.Name, prifi.ReceivedMessage, prifi.clientState.StartStopReceiveBroadcast)
return &prifi
}

// NewPriFiTrusteeWithState creates a new PriFi trustee entity state.
func NewPriFiTrusteeWithState(msgSender MessageSender, state *TrusteeState) *PriFiLibInstance {
func NewPriFiTrusteeWithState(msgSender net.MessageSender, state *TrusteeState) *PriFiLibInstance {
prifi := PriFiLibInstance{
role: PRIFI_ROLE_TRUSTEE,
messageSender: msgSender,
Expand Down
6 changes: 3 additions & 3 deletions sda/protocols/message_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/dedis/cothority/log"
"github.com/dedis/cothority/sda"
prifi_lib "github.com/lbarman/prifi/prifi-lib"
"github.com/lbarman/prifi/prifi-lib/net"
)

Expand Down Expand Up @@ -65,7 +64,7 @@ func (ms MessageSender) BroadcastToAllClients(msg interface{}) error {
}

//ClientSubscribeToBroadcast allows a client to subscribe to UDP broadcast
func (ms MessageSender) ClientSubscribeToBroadcast(clientName string, prifiLibInstance *prifi_lib.PriFiLibInstance, startStopChan chan bool) error {
func (ms MessageSender) ClientSubscribeToBroadcast(clientName string, messageReceived func(interface{}) error, startStopChan chan bool) error {

log.Lvl3(clientName, " started UDP-listener helper.")
listening := false
Expand Down Expand Up @@ -101,7 +100,8 @@ func (ms MessageSender) ClientSubscribeToBroadcast(clientName string, prifiLibIn
}

//forward to PriFi
prifiLibInstance.ReceivedMessage(filledMessage)
//prifiLibInstance.ReceivedMessage(filledMessage)
messageReceived(filledMessage)

}

Expand Down

0 comments on commit 6726e1f

Please sign in to comment.