-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
5 changed files
with
103 additions
and
14 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -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() | ||
} |
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 |
---|---|---|
|
@@ -12,4 +12,5 @@ type Peer interface { | |
type Transport interface { | ||
ListenAndAccept() error | ||
Consume() <-chan RPC | ||
Close() error | ||
} |
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