refactor gop2p

This commit is contained in:
Alexander Matson 2019-05-14 10:48:31 -04:00
parent 91327b1590
commit c655b6ca3f
12 changed files with 248 additions and 205 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
bin/
src/github.com
src/golang.org

View File

@ -1,35 +0,0 @@
package main
import (
"fmt"
"net"
"os"
"time"
)
func main() {
service := ":1200"
tcpAddr, err := net.ResolveTCPAddr("tcp4", service)
checkError(err)
listener, err := net.ListenTCP("tcp", tcpAddr)
checkError(err)
for {
conn, err := listener.Accept()
if err != nil {
continue
}
daytime := "Hahahaha virgin client vs chad server" + time.Now().String()
conn.Write([]byte(daytime))
conn.Close()
}
}
func checkError(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "Fatal error: %s", err.Error())
os.Exit(1)
}
}

53
echo.go
View File

@ -1,53 +0,0 @@
package main
import (
"net"
"os"
"fmt"
)
func main() {
service := ":1201"
tcpAddr, err := net.ResolveTCPAddr("tcp4", service)
checkError(err)
listener, err := net.ListenTCP("tcp", tcpAddr)
checkError(err)
for {
conn, err := listener.Accept()
if err != nil {
continue
}
go handleClient(conn)
}
}
func handleClient(conn net.Conn) {
// close connection on exit
defer conn.Close()
var buf [512]byte
for {
// read up to 512 bytes
n, err := conn.Read(buf[0:])
if err != nil {
return
}
// write the n bytes read
_, err2 := conn.Write(buf[0:n])
if err2 != nil {
return
}
}
}
func checkError(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "Fatal error: %s", err.Error())
os.Exit(1)
}
}

26
ip.go
View File

@ -1,26 +0,0 @@
/* IP
*/
package main
import (
"net"
"os"
"fmt"
)
func main() {
if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "Usage: %s ip-addr\n", os.Args[0])
os.Exit(1)
}
name := os.Args[1]
addr := net.ParseIP(name)
if addr == nil {
fmt.Println("Invalid address")
} else {
fmt.Println("The address is ", addr.String())
}
os.Exit(0)
}

BIN
p2p

Binary file not shown.

91
p2p.go
View File

@ -1,91 +0,0 @@
package main
import (
"net"
"os"
"fmt"
"strings"
)
func main() {
if len(os.Args) <= 1 {
fmt.Fprintf(os.Stderr, "Usage: %s <local node port> <remote node port>\n", os.Args[0])
os.Exit(1)
}
// Configure this local node's listening port.
service := ":" + os.Args[1]
tcpAddr, err := net.ResolveTCPAddr("tcp4", service)
checkError(err)
listener, err := net.ListenTCP("tcp", tcpAddr)
checkError(err)
// If a remote peer's port is specified, we want to connect and message it
if len(os.Args) == 3 {
fmt.Println("Found a remote peer")
remote := ":" + os.Args[2]
tcpRemote, err := net.ResolveTCPAddr("tcp4", remote)
checkError(err)
peerconn, err := net.DialTCP("tcp4", nil, tcpRemote)
checkError(err)
_, err = peerconn.Write([]byte("Hello from " + os.Args[1]))
checkError(err)
var buf [512]byte
n, err := peerconn.Read(buf[0:])
checkError(err)
result := string(buf[0:n])
fmt.Println(string(result))
}
fmt.Printf("(%s) Waiting for peer connections...\n", os.Args[1])
for {
conn, err := listener.Accept()
if err != nil {
continue
}
handleConnection(conn)
}
}
func handleConnection(conn net.Conn) {
// close connection on exit
defer conn.Close()
var buf [512]byte
for {
// read up to 512 bytes
n, err := conn.Read(buf[0:])
if err != nil {
return
}
message := string(buf[0:n])
fmt.Println(message)
tokens := strings.Split(message, " ")
if tokens[0] != "" {
response := "Hi " + tokens[2] + ", from " + os.Args[1]
// write the n bytes read
_, err2 := conn.Write([]byte(response))
if err2 != nil {
return
}
}
}
}
func checkError(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "Fatal error: %s", err.Error())
os.Exit(1)
}
}

13
src/errfn/errors.go Normal file
View File

@ -0,0 +1,13 @@
package errfn
import (
"fmt"
"os"
)
func Check(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "Fatal error: %s", err.Error())
os.Exit(1)
}
}

35
src/p2p/daytime.go Normal file
View File

@ -0,0 +1,35 @@
package main
// import (
// "fmt"
// "net"
// "os"
// "time"
// )
//
// func main() {
// service := ":1200"
// tcpAddr, err := net.ResolveTCPAddr("tcp4", service)
// checkError(err)
//
// listener, err := net.ListenTCP("tcp", tcpAddr)
// checkError(err)
//
// for {
// conn, err := listener.Accept()
// if err != nil {
// continue
// }
//
// daytime := "Hahahaha virgin client vs chad server" + time.Now().String()
// conn.Write([]byte(daytime))
// conn.Close()
// }
// }
//
// func checkError(err error) {
// if err != nil {
// fmt.Fprintf(os.Stderr, "Fatal error: %s", err.Error())
// os.Exit(1)
// }
// }

53
src/p2p/echo.go Normal file
View File

@ -0,0 +1,53 @@
package main
// import (
// "net"
// "os"
// "fmt"
// )
//
// func main() {
// service := ":1201"
// tcpAddr, err := net.ResolveTCPAddr("tcp4", service)
// checkError(err)
//
// listener, err := net.ListenTCP("tcp", tcpAddr)
// checkError(err)
//
// for {
// conn, err := listener.Accept()
// if err != nil {
// continue
// }
//
// go handleClient(conn)
// }
// }
//
// func handleClient(conn net.Conn) {
// // close connection on exit
// defer conn.Close()
//
// var buf [512]byte
//
// for {
// // read up to 512 bytes
// n, err := conn.Read(buf[0:])
// if err != nil {
// return
// }
//
// // write the n bytes read
// _, err2 := conn.Write(buf[0:n])
// if err2 != nil {
// return
// }
// }
// }
//
// func checkError(err error) {
// if err != nil {
// fmt.Fprintf(os.Stderr, "Fatal error: %s", err.Error())
// os.Exit(1)
// }
// }

26
src/p2p/ip.go Normal file
View File

@ -0,0 +1,26 @@
/* IP
*/
package main
// import (
// "net"
// "os"
// "fmt"
// )
//
// func main() {
// if len(os.Args) != 2 {
// fmt.Fprintf(os.Stderr, "Usage: %s ip-addr\n", os.Args[0])
// os.Exit(1)
// }
// name := os.Args[1]
//
// addr := net.ParseIP(name)
// if addr == nil {
// fmt.Println("Invalid address")
// } else {
// fmt.Println("The address is ", addr.String())
// }
// os.Exit(0)
// }

30
src/p2p/main.go Normal file
View File

@ -0,0 +1,30 @@
package main
import (
"os"
"fmt"
"peers"
)
var lpt []peers.Peer
func main() {
if len(os.Args) <= 1 {
fmt.Fprintf(os.Stderr, "Usage: %s <local node port> <remote node port>\n", os.Args[0])
os.Exit(1)
}
// Configure this local node's listening port.
local := peers.Peer{}
local.SetAddr(os.Args[1])
// If a remote peer's port is specified, we want to connect and message it
if len(os.Args) == 3 {
remote := peers.Peer{}
remote.SetAddr(os.Args[2])
local.Connect(remote)
}
local.StartListening()
}

88
src/peers/peers.go Normal file
View File

@ -0,0 +1,88 @@
package peers
import (
"net"
"os"
"fmt"
"strings"
"errfn"
)
type Peer struct {
address net.TCPAddr
listener net.TCPListener
connection net.TCPConn
}
func (p* Peer) SetAddr(port string) {
service := ":" + port
tcpAddr, err := net.ResolveTCPAddr("tcp4", service)
errfn.Check(err)
p.address = *tcpAddr
}
func (p* Peer) StartListening() {
listener, err := net.ListenTCP("tcp", &p.address)
errfn.Check(err)
p.listener = *listener
fmt.Printf("(%s) Waiting for peer connections...\n", os.Args[1])
for {
conn, err := p.listener.Accept()
if err != nil {
continue
}
handleConnection(conn)
}
}
func (p* Peer) Connect(remote Peer) {
peerconn, err := net.DialTCP("tcp4", nil, &remote.address)
errfn.Check(err)
fmt.Println("Found a remote peer")
p.connection = *peerconn
_, err = p.connection.Write([]byte("Hello from " + os.Args[1]))
errfn.Check(err)
var buf [512]byte
n, err := p.connection.Read(buf[0:])
errfn.Check(err)
result := string(buf[0:n])
fmt.Println(string(result))
}
func handleConnection(conn net.Conn) {
// close connection on exit
defer conn.Close()
var buf [512]byte
for {
// read up to 512 bytes
n, err := conn.Read(buf[0:])
if err != nil {
return
}
message := string(buf[0:n])
fmt.Println(message)
tokens := strings.Split(message, " ")
if tokens[0] != "" {
response := "Hi " + tokens[2] + ", from " + os.Args[1]
// write the n bytes read
_, err2 := conn.Write([]byte(response))
if err2 != nil {
return
}
}
}
}