|
@@ -47,6 +47,9 @@ var clientData = make(map[net.Addr]clientKeys)
|
|
|
|
|
|
const follower string = "127.0.0.1:4442"
|
|
|
|
|
|
+//Maximum Transport Unit
|
|
|
+const mtu int = 1100
|
|
|
+
|
|
|
var leaderPrivateKey *[32]byte
|
|
|
var leaderPublicKey *[32]byte
|
|
|
var followerPublicKey *[32]byte
|
|
@@ -59,15 +62,18 @@ var archiveTopicAmount int
|
|
|
|
|
|
// every roundsBeforeUpdate the client updates his pirQuery
|
|
|
const roundsBeforeUpdate = 1
|
|
|
-const neededSubscriptions = 10
|
|
|
+const neededSubscriptions = 1
|
|
|
const numThreads = 12
|
|
|
const dataLength = 128
|
|
|
|
|
|
-var dbWriteSize int = 10
|
|
|
+var dbWriteSize int = 100
|
|
|
|
|
|
const minDBWriteSize int = 10
|
|
|
|
|
|
-var maxTimePerRound time.Duration = 1000 * time.Millisecond
|
|
|
+var maxTimePerRound time.Duration = 5 * time.Second
|
|
|
+
|
|
|
+var clientsServedPhase1 int = 0
|
|
|
+var clientsServedPhase3 int = 0
|
|
|
|
|
|
//counts the number of rounds
|
|
|
var round int = 0
|
|
@@ -248,6 +254,13 @@ func main() {
|
|
|
|
|
|
round++
|
|
|
|
|
|
+ fmt.Println("clientsServedPhase1", clientsServedPhase1)
|
|
|
+ fmt.Println("clientsServedPhase3", clientsServedPhase3)
|
|
|
+ fmt.Println("dbWriteSize", dbWriteSize)
|
|
|
+
|
|
|
+ clientsServedPhase1 = 0
|
|
|
+ clientsServedPhase3 = 0
|
|
|
+
|
|
|
fmt.Println("Phase 1 Round", round)
|
|
|
|
|
|
//creates a new write Db for this round
|
|
@@ -332,6 +345,7 @@ func phase1(id int, phase []byte, followerConnection net.Conn, wg *sync.WaitGrou
|
|
|
}
|
|
|
|
|
|
for clientConnection := range phase1Channel {
|
|
|
+ clientsServedPhase1++
|
|
|
|
|
|
gotClient[0] = 1
|
|
|
//tells follower that this worker got a clientConnection
|
|
@@ -709,6 +723,7 @@ func phase3(id int, phase []byte, followerConnection net.Conn, wg *sync.WaitGrou
|
|
|
}
|
|
|
|
|
|
for clientConnection := range phase3Channel {
|
|
|
+ clientsServedPhase3++
|
|
|
|
|
|
gotClient[0] = 1
|
|
|
//tells follower that this worker got a clientConnection
|
|
@@ -820,7 +835,7 @@ func phase3(id int, phase []byte, followerConnection net.Conn, wg *sync.WaitGrou
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- errorBool = getSendTweets(clientKeys, nil, clientConnection, followerConnection)
|
|
|
+ errorBool = getSendTweets(clientKeys, nil, clientConnection, followerConnection, m)
|
|
|
if errorBool {
|
|
|
contBool := handleClientDC(wg, followerConnection, phase3Channel)
|
|
|
if contBool {
|
|
@@ -852,7 +867,7 @@ func phase3(id int, phase []byte, followerConnection net.Conn, wg *sync.WaitGrou
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
- errorBool = getSendTweets(clientKeys, archiveQuerys, clientConnection, followerConnection)
|
|
|
+ errorBool = getSendTweets(clientKeys, archiveQuerys, clientConnection, followerConnection, m)
|
|
|
if errorBool {
|
|
|
contBool := handleClientDC(wg, followerConnection, phase3Channel)
|
|
|
if contBool {
|
|
@@ -968,7 +983,7 @@ func getSendVirtualAddress(pirQuery []byte, virtualAddresses []int, sharedSecret
|
|
|
return errorBool
|
|
|
}
|
|
|
|
|
|
-func getSendTweets(clientKeys clientKeys, archiveQuerys [][]byte, clientConnection, followerConnection net.Conn) bool {
|
|
|
+func getSendTweets(clientKeys clientKeys, archiveQuerys [][]byte, clientConnection, followerConnection net.Conn, m *sync.RWMutex) bool {
|
|
|
//todo! repeat for archive
|
|
|
tmpNeededSubscriptions := neededSubscriptions
|
|
|
if tmpNeededSubscriptions > topicAmount {
|
|
@@ -983,9 +998,9 @@ func getSendTweets(clientKeys clientKeys, archiveQuerys [][]byte, clientConnecti
|
|
|
for i := 0; i < tmpNeededSubscriptions; i++ {
|
|
|
//gets all requested tweets
|
|
|
if archiveQuerys == nil {
|
|
|
- tweets[i] = lib.GetTweets(clientKeys.PirQuery[i], dataLength, 0)
|
|
|
+ tweets[i] = lib.GetTweets(clientKeys.PirQuery[i], dataLength, 0, *clientKeys.PublicKey)
|
|
|
} else {
|
|
|
- tweets[i] = lib.GetTweets(archiveQuerys[i], dataLength, 1)
|
|
|
+ tweets[i] = lib.GetTweets(archiveQuerys[i], dataLength, 1, *clientKeys.PublicKey)
|
|
|
}
|
|
|
|
|
|
//expand sharedSecret so it is of right length
|
|
@@ -1000,18 +1015,15 @@ func getSendTweets(clientKeys clientKeys, archiveQuerys [][]byte, clientConnecti
|
|
|
|
|
|
//fmt.Println(tweets[0])
|
|
|
|
|
|
- //receives tweets from follower and Xor's them in
|
|
|
- tweetsLengthBytes, _ := readFrom(followerConnection, 4, nil, 0)
|
|
|
-
|
|
|
- tweetsReceivedLength := byteToInt(tweetsLengthBytes)
|
|
|
+ blockLength := len(tweets[i])
|
|
|
|
|
|
- receivedTweets, _ := readFrom(followerConnection, tweetsReceivedLength, nil, 0)
|
|
|
+ receivedTweets, _ := readFrom(followerConnection, blockLength, nil, 0)
|
|
|
|
|
|
+ //fmt.Println("receivedTweets", blockLength, len(receivedTweets))
|
|
|
+ //fmt.Println("pubKey", *clientKeys.PublicKey, "Bytes", tweets, "follower", receivedTweets)
|
|
|
lib.Xor(receivedTweets, tweets[i])
|
|
|
}
|
|
|
|
|
|
- //fmt.Println("Bytes", tweets[0][:10])
|
|
|
-
|
|
|
//sends tweets to client
|
|
|
for i := 0; i < tmpNeededSubscriptions; i++ {
|
|
|
tweetsLengthBytes := intToByte(len(tweets[i]))
|
|
@@ -1179,25 +1191,75 @@ func sendTopicLists(clientConnection, followerConnection net.Conn, setup bool) b
|
|
|
|
|
|
//sends the array to the connection
|
|
|
func writeTo(connection net.Conn, array []byte) {
|
|
|
- _, err := connection.Write(array)
|
|
|
- if err != nil {
|
|
|
- panic(err)
|
|
|
+ remainingLength := len(array)
|
|
|
+ for remainingLength > 0 {
|
|
|
+ if remainingLength >= mtu {
|
|
|
+ _, err := connection.Write(array[:mtu])
|
|
|
+ if err != nil {
|
|
|
+ panic(err)
|
|
|
+ }
|
|
|
+ array = array[mtu:]
|
|
|
+ remainingLength -= mtu
|
|
|
+ } else {
|
|
|
+ _, err := connection.Write(array)
|
|
|
+ if err != nil {
|
|
|
+ panic(err)
|
|
|
+ }
|
|
|
+ remainingLength = 0
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func writeToWError(connection net.Conn, array []byte, followerConnection net.Conn, size int) bool {
|
|
|
- var err error
|
|
|
if connection.RemoteAddr().String() == follower {
|
|
|
arrayWError := make([]byte, 1)
|
|
|
arrayWError = append(arrayWError, array[:]...)
|
|
|
- _, err = connection.Write(arrayWError)
|
|
|
+ remainingLength := len(arrayWError)
|
|
|
+ for remainingLength > 0 {
|
|
|
+ if remainingLength >= mtu {
|
|
|
+ _, err := connection.Write(arrayWError[:mtu])
|
|
|
+ if err != nil {
|
|
|
+ return handleError(connection, followerConnection, size, err)
|
|
|
+ }
|
|
|
+ arrayWError = arrayWError[mtu:]
|
|
|
+ remainingLength -= mtu
|
|
|
+ } else {
|
|
|
+ _, err := connection.Write(arrayWError)
|
|
|
+ if err != nil {
|
|
|
+ return handleError(connection, followerConnection, size, err)
|
|
|
+ }
|
|
|
+ remainingLength = 0
|
|
|
+ }
|
|
|
+ }
|
|
|
} else {
|
|
|
- _, err = connection.Write(array)
|
|
|
+ remainingLength := len(array)
|
|
|
+ for remainingLength > 0 {
|
|
|
+ if remainingLength >= mtu {
|
|
|
+ _, err := connection.Write(array[:mtu])
|
|
|
+ if err != nil {
|
|
|
+ return handleError(connection, followerConnection, size, err)
|
|
|
+ }
|
|
|
+ array = array[mtu:]
|
|
|
+ remainingLength -= mtu
|
|
|
+ } else {
|
|
|
+ _, err := connection.Write(array)
|
|
|
+ if err != nil {
|
|
|
+ return handleError(connection, followerConnection, size, err)
|
|
|
+ }
|
|
|
+ remainingLength = 0
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
+ return false
|
|
|
+}
|
|
|
|
|
|
+func handleError(connection, followerConnection net.Conn, size int, err error) bool {
|
|
|
if err != nil {
|
|
|
//lets follower know that client has disconnected unexpectedly
|
|
|
if connection.RemoteAddr().String() != follower {
|
|
|
+ if size > mtu {
|
|
|
+ fmt.Println("have a look here")
|
|
|
+ }
|
|
|
fmt.Println("error", err)
|
|
|
array := make([]byte, size)
|
|
|
array[0] = 1
|
|
@@ -1216,23 +1278,37 @@ func writeToWError(connection net.Conn, array []byte, followerConnection net.Con
|
|
|
//reads an array which is returned and of size "size" from the connection
|
|
|
//returns true if error occured
|
|
|
func readFrom(connection net.Conn, size int, followerConnection net.Conn, sizeError int) ([]byte, bool) {
|
|
|
- array := make([]byte, size)
|
|
|
- _, err := connection.Read(array)
|
|
|
- if err != nil {
|
|
|
- //lets follower know that client has disconnected unexpectedly
|
|
|
- if connection.RemoteAddr().String() != follower {
|
|
|
- fmt.Println("error", err)
|
|
|
- array := make([]byte, sizeError)
|
|
|
- array[0] = 1
|
|
|
- _, err = followerConnection.Write(array)
|
|
|
- if err != nil {
|
|
|
+ var array []byte
|
|
|
+ remainingSize := size
|
|
|
+ for remainingSize > 0 {
|
|
|
+ var err error
|
|
|
+ toAppend := make([]byte, mtu)
|
|
|
+ if remainingSize > mtu {
|
|
|
+ _, err = connection.Read(toAppend)
|
|
|
+ array = append(array, toAppend...)
|
|
|
+ remainingSize -= mtu
|
|
|
+ } else {
|
|
|
+ _, err = connection.Read(toAppend[:remainingSize])
|
|
|
+ array = append(array, toAppend[:remainingSize]...)
|
|
|
+ remainingSize = 0
|
|
|
+ }
|
|
|
+ if err != nil {
|
|
|
+ //lets follower know that client has disconnected unexpectedly
|
|
|
+ if connection.RemoteAddr().String() != follower {
|
|
|
+ fmt.Println(err)
|
|
|
+ array := make([]byte, sizeError)
|
|
|
+ array[0] = 1
|
|
|
+ _, err = followerConnection.Write(array)
|
|
|
+ if err != nil {
|
|
|
+ panic(err)
|
|
|
+ }
|
|
|
+ return nil, true
|
|
|
+ } else {
|
|
|
panic(err)
|
|
|
}
|
|
|
- return nil, true
|
|
|
- } else {
|
|
|
- panic(err)
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
return array, false
|
|
|
}
|
|
|
|