From cf5e47e3030a4a4705c853783789214a1b359670 Mon Sep 17 00:00:00 2001
From: Benjamin Wenger <ben@elixxir.ioo>
Date: Thu, 18 Feb 2021 11:18:55 -0800
Subject: [PATCH] implemented bindigns

---
 api/messages.go      |  8 +++-----
 bindings/callback.go |  2 +-
 bindings/client.go   | 37 ++++++++++++++-----------------------
 3 files changed, 18 insertions(+), 29 deletions(-)

diff --git a/api/messages.go b/api/messages.go
index 933af975b..a44ca4ce0 100644
--- a/api/messages.go
+++ b/api/messages.go
@@ -38,9 +38,7 @@ const (
 //	  Returns false if all rounds statuses were returned
 // rounds contains a mapping of all previously requested rounds to
 //   their respective round results
-type RoundEventCallback interface {
-	Report(allRoundsSucceeded, timedOut bool, rounds map[id.Round]RoundResult)
-}
+type RoundEventCallback func(allRoundsSucceeded, timedOut bool, rounds map[id.Round]RoundResult)
 
 // Comm interface for RequestHistoricalRounds.
 // Constructed for testability with getRoundResults
@@ -129,14 +127,14 @@ func (c *Client) getRoundResults(roundList []id.Round, timeout time.Duration,
 
 			// If we know about all rounds, return
 			if numResults == 0 {
-				roundCallback.Report(allRoundsSucceeded, false, roundsResults)
+				roundCallback(allRoundsSucceeded, false, roundsResults)
 				return
 			}
 
 			// Wait for info about rounds or the timeout to occur
 			select {
 			case <-timer.C:
-				roundCallback.Report(false, true, roundsResults)
+				roundCallback(false, true, roundsResults)
 				return
 			case roundReport := <-sendResults:
 				numResults--
diff --git a/bindings/callback.go b/bindings/callback.go
index 68ca5646e..4fc0431a4 100644
--- a/bindings/callback.go
+++ b/bindings/callback.go
@@ -38,7 +38,7 @@ type RoundEventCallback interface {
 
 // RoundEventHandler handles round events happening on the cMix network.
 type MessageDeliveryCallback interface {
-	EventCallback(msgID []byte, delivered, timedOut bool)
+	EventCallback(msgID []byte, delivered, timedOut bool, roundResults []byte)
 }
 
 // AuthRequestCallback notifies the register whenever they receive an auth
diff --git a/bindings/client.go b/bindings/client.go
index 7402c02ec..866721af2 100644
--- a/bindings/client.go
+++ b/bindings/client.go
@@ -16,9 +16,7 @@ import (
 	"gitlab.com/elixxir/client/interfaces/contact"
 	"gitlab.com/elixxir/client/interfaces/message"
 	"gitlab.com/elixxir/client/interfaces/params"
-	"gitlab.com/elixxir/client/interfaces/utility"
 	"gitlab.com/elixxir/comms/mixmessages"
-	ds "gitlab.com/elixxir/comms/network/dataStructures"
 	"gitlab.com/elixxir/primitives/states"
 	"gitlab.com/xx_network/primitives/id"
 	"time"
@@ -307,37 +305,30 @@ func (c *Client) RegisterRoundEventsHandler(rid int, cb RoundEventCallback,
 // This function takes the marshaled send report to ensure a memory leak does
 // not occur as a result of both sides of the bindings holding a reference to
 // the same pointer.
-func (c *Client) RegisterMessageDeliveryCB(marshaledSendReport []byte,
-	mdc MessageDeliveryCallback, timeoutMS int) (*Unregister, error) {
+func (c *Client) WaitForRoundCompletion(marshaledSendReport []byte,
+	mdc MessageDeliveryCallback, timeoutMS int) error {
 
 	sr, err := UnmarshalSendReport(marshaledSendReport)
 	if err != nil {
-		return nil, errors.New(fmt.Sprintf("Failed to "+
-			"RegisterMessageDeliveryCB: %+v", err))
+		 return errors.New(fmt.Sprintf("Failed to "+
+			"WaitForRoundCompletion callback due to bad Send Report: %+v", err))
 	}
 
-	/*check message delivery*/
-	sendResults := make(chan ds.EventReturn, len(sr.rl.list))
-	roundEvents := c.api.GetRoundEvents()
+	f := func(allRoundsSucceeded, timedOut bool, rounds map[id.Round]api.RoundResult){
+		results := make([]byte, len(sr.rl.list))
 
-	reventObjs := make([]*ds.EventCallback, len(sr.rl.list))
+		for i, r := range sr.rl.list{
+			if result, exists := rounds[r]; exists{
+				results[i] = byte(result)
+			}
+		}
 
-	for i, r := range sr.rl.list {
-		reventObjs[i] = roundEvents.AddRoundEventChan(r, sendResults,
-			time.Duration(timeoutMS)*time.Millisecond, states.COMPLETED,
-			states.FAILED)
+		mdc.EventCallback(sr.mid.Marshal(), allRoundsSucceeded, timedOut, results)
 	}
 
-	go func() {
-		success, _, numTmeout := utility.TrackResults(sendResults, len(sr.rl.list))
-		if !success {
-			mdc.EventCallback(sr.mid[:], false, numTmeout > 0)
-		} else {
-			mdc.EventCallback(sr.mid[:], true, false)
-		}
-	}()
+	timeout := time.Duration(timeoutMS)*time.Millisecond
 
-	return newRoundListUnregister(sr.rl.list, reventObjs, roundEvents), nil
+	return c.api.GetRoundResults(sr.rl.list, timeout, f)
 }
 
 // Returns a user object from which all information about the current user
-- 
GitLab