-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alexandre Shinebourne
committed
Dec 9, 2023
1 parent
d89d2aa
commit 79bdfdd
Showing
16 changed files
with
190 additions
and
213 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,29 @@ | ||
package betterNode | ||
|
||
// TODO: complete the test node (pipe) - server/client behaviour | ||
import "net" | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
mock_conn "github.com/jordwest/mock-conn" | ||
"net" | ||
) | ||
type PipeCommunication struct{} | ||
|
||
type Pipe struct { | ||
//conn mock_conn.Conn | ||
func NewPipeCommunication() (*PipeCommunication, error) { | ||
return &PipeCommunication{}, nil | ||
} | ||
|
||
func (p *Pipe) Listen() { | ||
listener := bufio.NewReader(p.conn.Server) | ||
for { | ||
var buffer bytes.Buffer | ||
for { | ||
b, err := listener.ReadByte() | ||
if err != nil { | ||
break | ||
} | ||
if b == EOF { | ||
break | ||
} | ||
buffer.WriteByte(b) | ||
} | ||
// Handle connection | ||
func (pc *PipeCommunication) Listen() (net.Listener, error) { | ||
listener, err := net.Listen("unix", "/tmp/pipe.sock") | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
func (p *Pipe) Request(commInterface CommunicationInterface, route []byte, payload []byte) ([]byte, error) { | ||
//pipeInterface := commInterface.(*Pipe) | ||
// create a new conn | ||
conn := mock_conn.NewConn() | ||
conn. | ||
return nil, nil | ||
} | ||
defer listener.Close() | ||
|
||
func Read(conn *net.Conn) ([]byte, error) { | ||
reader := bufio.NewReader(*conn) | ||
var buffer bytes.Buffer | ||
for { | ||
b, err := reader.ReadByte() | ||
if err != nil { | ||
return nil, err | ||
} | ||
if b == EOF { | ||
break | ||
} | ||
buffer.WriteByte(b) | ||
} | ||
return buffer.Bytes(), nil | ||
return listener, nil | ||
} | ||
|
||
func Write(conn *net.Conn, packet []byte) error { | ||
writer := bufio.NewWriter(*conn) | ||
appended := append(packet, EOF) | ||
_, err := writer.Write(appended) | ||
func (pc *PipeCommunication) Connect(addr string) (net.Conn, error) { | ||
conn, err := net.Dial("unix", addr) | ||
if err != nil { | ||
return err | ||
return nil, err | ||
} | ||
err = writer.Flush() | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
|
||
return conn, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,98 +1,29 @@ | ||
package betterNode | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"log" | ||
"net" | ||
) | ||
import "net" | ||
|
||
type TCP struct { | ||
addr string | ||
port int | ||
listener net.Listener | ||
} | ||
|
||
func (n *TCP) Listen() { | ||
for { | ||
_, err := n.listener.Accept() | ||
if err != nil { | ||
// Avoid fatal errors at all costs - we want to maximise node availability | ||
log.Println("Node is unable to accept incoming connections due to: ", err.Error()) | ||
continue // forces next iteration of the loop skipping any code in between | ||
} | ||
type TCPCommunication struct{} | ||
|
||
// Pass connection to request handler in a new goroutine - allows a node to handle multiple connections at once | ||
//go node.HandleRequest(conn, overlay) | ||
} | ||
func NewTCPCommunication() (*TCPCommunication, error) { | ||
return &TCPCommunication{}, nil | ||
} | ||
|
||
func (n *TCP) Request(commInterface CommunicationInterface, route []byte, payload []byte) ([]byte, error) { | ||
tcpInterface := commInterface.(*TCP) | ||
conn, err := createConnections(tcpInterface.addr, tcpInterface.port) | ||
func (tc *TCPCommunication) Listen() (net.Listener, error) { | ||
listener, err := net.Listen("tcp", ":8080") | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer conn.Close() | ||
|
||
packet := append(route, payload...) | ||
packet = append(packet, EOF) | ||
|
||
err = Write(&conn, packet) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
response, err := Read(&conn) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return response, nil | ||
defer listener.Close() | ||
|
||
return listener, nil | ||
} | ||
|
||
func createConnections(address string, port int) (net.Conn, error) { | ||
socketAddr := address + ":" + string(port) | ||
tcpAddr, err := net.ResolveTCPAddr("tcp", socketAddr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
conn, err := net.DialTCP("tcp", nil, tcpAddr) | ||
func (tc *TCPCommunication) Connect(addr string) (net.Conn, error) { | ||
conn, err := net.Dial("tcp", addr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return conn, nil | ||
} | ||
|
||
func Read(conn *net.Conn) ([]byte, error) { | ||
reader := bufio.NewReader(*conn) | ||
var buffer bytes.Buffer | ||
for { | ||
b, err := reader.ReadByte() | ||
if err != nil { | ||
return nil, err | ||
} | ||
if b == EOF { | ||
break | ||
} | ||
buffer.WriteByte(b) | ||
} | ||
return buffer.Bytes(), nil | ||
} | ||
|
||
func Write(conn *net.Conn, packet []byte) error { | ||
writer := bufio.NewWriter(*conn) | ||
appended := append(packet, EOF) | ||
_, err := writer.Write(appended) | ||
if err != nil { | ||
return err | ||
} | ||
err = writer.Flush() | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
Oops, something went wrong.