forked from blink-cdn/Swami-Delivery-Network
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
79 lines (63 loc) · 2.12 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"fmt"
"os"
"./blink"
"strconv"
"net"
"time"
)
func main() {
/* 4 Arguments (3 plus Arg[0]) needed to run the program (1) local address (2) blink node address
(3) destination address */
if len(os.Args) != 4 {
fmt.Println("Incorrect number of arguments!!!")
os.Exit(1)
}
/* Set up all needed addresses. The information will be sent from this node (localAddr)
to the blinkNode (blinkNodeAddr), which will then forward the packet to the final
destination (destAddr).
*/
localAddr, err := net.ResolveUDPAddr("udp", os.Args[1])
blink.CheckError(err)
oracleAddr, err := net.ResolveUDPAddr("udp", os.Args[2])
blink.CheckError(err)
destAddr, err := net.ResolveUDPAddr("udp", os.Args[3])
blink.CheckError(err)
conn, err := net.ListenUDP("udp", localAddr)
blink.CheckError(err)
// Setup With Oracle
buffer := blink.MakeBlinkPacket("0000000100000000", localAddr, destAddr, blink.HelloOracle, []byte(""))
_, err = conn.WriteToUDP(buffer, oracleAddr)
blink.CheckError(err)
fmt.Println("Sent to Oracle")
// Read Oracle's Response
_, _, err = conn.ReadFromUDP(buffer)
blink.CheckError(err)
packetData := blink.ExtractPacketData(buffer)
fmt.Println("Received From Oracle", string(packetData))
SID := string(packetData[:16])
var blinkAddrString string
for i:=16; i <=1024; i++ {
if string(packetData[i]) != "!" {
blinkAddrString += string(packetData[i])
} else {
break
}
}
fmt.Println("Connected to Oracle. SID:", SID, "Addr:", blinkAddrString)
blinkAddr, err := net.ResolveUDPAddr("udp", blinkAddrString)
blink.CheckError(err)
/* Loops through from 0 - 10,000 sending each number to the blinkNode. Each packet
is wrapped in a blink header, which will be used by the blink node to properly
forward the packet
*/
time.Sleep(3*time.Second)
for i := 1; i < 10000; i++ {
buffer := blink.MakeBlinkPacket(SID, localAddr, destAddr, blink.Iframe, []byte(strconv.Itoa(i)))
_, err := conn.WriteToUDP(buffer, blinkAddr)
blink.CheckError(err)
fmt.Println("Sent:", i)
time.Sleep(3333 * time.Microsecond)
}
}