Skip to content

Commit

Permalink
started working on fileserver
Browse files Browse the repository at this point in the history
  • Loading branch information
gnana997 committed Jan 26, 2024
1 parent 7864945 commit 1820429
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 14 deletions.
69 changes: 69 additions & 0 deletions file-server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package main

import (
"fmt"

"github.com/gnana997/decentralised-storage-go/p2p"
)

type FileServerOpts struct {
RootFolder string

Transport p2p.Transport
PathTransformFunc PathTransformFunc
}

type FileServer struct {
FileServerOpts

store *Store
quitch chan struct{}
}

func NewFileServer(opts FileServerOpts) *FileServer {
return &FileServer{
FileServerOpts: opts,
store: NewStore(StoreOpts{
Root: opts.RootFolder,
PathTransformFunc: opts.PathTransformFunc,
}),
quitch: make(chan struct{}),
}
}

func (fs *FileServer) Stop() {
close(fs.quitch)
}

func (fs *FileServer) loop() {
defer func() {
if err := fs.Transport.Close(); err != nil {
fmt.Println(err)
}
if err := fs.Close(); err != nil {
fmt.Println(err)
}
}()
for {
select {
case msg := <-fs.Transport.Consume():
fmt.Println(msg)
case <-fs.quitch:
return
}
}
}

func (fs *FileServer) Start() error {
if err := fs.Transport.ListenAndAccept(); err != nil {
return err
}

fs.loop()

return nil
}

func (fs *FileServer) Close() error {
return fs.store.Close()
}
30 changes: 16 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"fmt"
"log"
"time"

"github.com/gnana997/decentralised-storage-go/p2p"
)
Expand All @@ -15,27 +15,29 @@ func OnPeer(peer p2p.Peer) error {

func main() {

tcpOpts := p2p.TCPTransportOptions{
tcpTransportOpts := p2p.TCPTransportOptions{
ListenAddr: ":3000",
HandshakeFunc: p2p.NOPHandshakeFunc,
Decoder: p2p.NOPDecoder{},
OnPeer: OnPeer,
}
tcptransport := p2p.NewTCPTransport(tcpTransportOpts)

tr := p2p.NewTCPTransport(tcpOpts)
fileServerOpts := FileServerOpts{
RootFolder: "3000_network",
PathTransformFunc: CASPathTransformFunc,

Transport: tcptransport,
}

fs := NewFileServer(fileServerOpts)

go func() {
for {
rpc := <-tr.Consume()
log.Printf("received rpc: %+v", rpc)
}
time.Sleep(time.Second * 3)
fs.Stop()
}()

if err := tr.ListenAndAccept(); err != nil {
log.Fatal(err)
if err := fs.Start(); err != nil {
fmt.Println(err)
return
}

fmt.Println("building decentralised storage system")

select {}
}
13 changes: 13 additions & 0 deletions p2p/tcp_transport.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package p2p

import (
"errors"
"fmt"
"log"
"net"
"sync"
)
Expand Down Expand Up @@ -67,12 +69,17 @@ func (t *TCPTransport) ListenAndAccept() error {

go t.startAcceptLoop()

log.Printf("TCP transport listening on %s", t.ListenAddr)

return nil
}

func (t *TCPTransport) startAcceptLoop() {
for {
conn, err := t.listener.Accept()
if errors.Is(err, net.ErrClosed) {
return
}
if err != nil {
fmt.Printf("TCP accept error: %v\n", err)
continue
Expand Down Expand Up @@ -123,3 +130,9 @@ func (t *TCPTransport) handleConn(conn net.Conn) {
}

}

// Close implements the Transport interface.
func (t *TCPTransport) Close() error {
close(t.rpcch)
return t.listener.Close()
}
1 change: 1 addition & 0 deletions p2p/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ type Peer interface {
type Transport interface {
ListenAndAccept() error
Consume() <-chan RPC
Close() error
}
4 changes: 4 additions & 0 deletions storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ func (s *Store) Delete(key string) error {
return os.RemoveAll(firstPathWithRoot)
}

func (s *Store) Write(key string, r io.Reader) error {
return s.writeStream(key, r)
}

func (s *Store) Read(key string) (io.Reader, error) {
f, err := s.readStream(key)
if err != nil {
Expand Down

0 comments on commit 1820429

Please sign in to comment.