Skip to content

Commit

Permalink
Code refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
mstmdev committed Nov 25, 2021
1 parent 00264fb commit a9e78af
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 25 deletions.
5 changes: 2 additions & 3 deletions server/remote_file.go → contract/file_info.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package server
package contract

// RemoteFile the remote file info
type RemoteFile struct {
type FileInfo struct {
// Path file path
Path string `json:"path"`
// IsDir is a dir the path
Expand Down
9 changes: 3 additions & 6 deletions server/info.go → contract/file_server_info.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package server
package contract

import "github.com/no-src/gofs/contract"

// Info file server info
type Info struct {
contract.Status
type FileServerInfo struct {
Status
ServerAddr string `json:"server_addr"`
SrcPath string `json:"src_path"`
TargetPath string `json:"target_path"`
Expand Down
6 changes: 4 additions & 2 deletions monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ type Monitor interface {
// syncOnce tag a sync once command, the sync once command will execute when call the Start
func NewMonitor(syncer sync.Sync, retry retry.Retry, syncOnce bool) (Monitor, error) {
src := syncer.Source()
if src.IsDisk() || (src.Is(core.RemoteDisk) && src.Server()) {
if src.IsDisk() {
return NewFsNotifyMonitor(syncer, retry, syncOnce)
} else if src.Is(core.RemoteDisk) && src.Server() {
return NewFsNotifyMonitor(syncer, retry, syncOnce)
} else if src.Is(core.RemoteDisk) && !src.Server() {
return NewRemoteMonitor(syncer, retry, syncOnce, src.Host(), src.Port(), src.MessageQueue())
return NewRemoteClientMonitor(syncer, retry, syncOnce, src.Host(), src.Port(), src.MessageQueue())
}
return nil, fmt.Errorf("file system unsupported ! src=>%s", src.Type().String())

Expand Down
17 changes: 8 additions & 9 deletions monitor/remote_monitor.go → monitor/remote_client_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"github.com/no-src/gofs/contract"
"github.com/no-src/gofs/retry"
"github.com/no-src/gofs/server"
"github.com/no-src/gofs/sync"
"github.com/no-src/gofs/tran"
"github.com/no-src/gofs/util"
Expand All @@ -14,7 +13,7 @@ import (
"strings"
)

type remoteMonitor struct {
type remoteClientMonitor struct {
syncer sync.Sync
retry retry.Retry
client tran.Client
Expand All @@ -27,13 +26,13 @@ type message struct {
data []byte
}

// NewRemoteMonitor create an instance of remoteMonitor to monitor the remote file change
func NewRemoteMonitor(syncer sync.Sync, retry retry.Retry, syncOnce bool, host string, port int, messageQueue int) (m Monitor, err error) {
// NewRemoteClientMonitor create an instance of remoteClientMonitor to monitor the remote file change
func NewRemoteClientMonitor(syncer sync.Sync, retry retry.Retry, syncOnce bool, host string, port int, messageQueue int) (m Monitor, err error) {
if syncer == nil {
err = errors.New("syncer can't be nil")
return nil, err
}
m = &remoteMonitor{
m = &remoteClientMonitor{
syncer: syncer,
retry: retry,
client: tran.NewClient(host, port),
Expand All @@ -43,7 +42,7 @@ func NewRemoteMonitor(syncer sync.Sync, retry retry.Retry, syncOnce bool, host s
return m, nil
}

func (m *remoteMonitor) Start() error {
func (m *remoteClientMonitor) Start() error {
if m.client == nil {
return errors.New("remote sync client is nil")
}
Expand All @@ -61,7 +60,7 @@ func (m *remoteMonitor) Start() error {
}
}()

var info server.Info
var info contract.FileServerInfo
m.retry.Do(func() error {
infoResult, err := m.client.ReadAll()
if err != nil {
Expand Down Expand Up @@ -114,7 +113,7 @@ func (m *remoteMonitor) Start() error {
return nil
}

func (m *remoteMonitor) processingMessage() {
func (m *remoteClientMonitor) processingMessage() {
for {
message := <-m.messages
log.Info("client read request => %s", string(message.data))
Expand Down Expand Up @@ -159,7 +158,7 @@ func (m *remoteMonitor) processingMessage() {
}
}

func (m *remoteMonitor) Close() error {
func (m *remoteClientMonitor) Close() error {
m.closed = true
return m.client.Close()
}
10 changes: 10 additions & 0 deletions monitor/remote_server_monitor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package monitor

import (
"github.com/no-src/gofs/retry"
"github.com/no-src/gofs/sync"
)

func NewRemoteServerMonitor(syncer sync.Sync, retry retry.Retry, syncOnce bool) (m Monitor, err error) {
return NewFsNotifyMonitor(syncer, retry, syncOnce)
}
6 changes: 3 additions & 3 deletions server/handler/file_api_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (h *fileApiHandler) ServeHTTP(writer http.ResponseWriter, request *http.Req
}
}()
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
var remoteFiles []server.RemoteFile
var fileList []contract.FileInfo
path := request.FormValue(contract.FsPath)
srcPrefix := strings.Trim(server.SrcRoutePrefix, "/")
targetPrefix := strings.Trim(server.TargetRoutePrefix, "/")
Expand Down Expand Up @@ -73,7 +73,7 @@ func (h *fileApiHandler) ServeHTTP(writer http.ResponseWriter, request *http.Req
aTime = cTime
mTime = cTime
}
remoteFiles = append(remoteFiles, server.RemoteFile{
fileList = append(fileList, contract.FileInfo{
Path: file.Name(),
IsDir: file.IsDir(),
Size: file.Size(),
Expand All @@ -83,7 +83,7 @@ func (h *fileApiHandler) ServeHTTP(writer http.ResponseWriter, request *http.Req
})
}
}
bytes, err := util.Marshal(server.NewApiResult(0, "success", remoteFiles))
bytes, err := util.Marshal(server.NewApiResult(0, "success", fileList))
if err != nil {
log.Error(err, "file server marshal error")
writer.Write(server.NewErrorApiResultBytes(-6, "marshal error"))
Expand Down
2 changes: 1 addition & 1 deletion sync/remote_client_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ func (rs *remoteClientSync) sync(serverAddr, path string) error {
if err != nil {
return err
}
var files []server.RemoteFile
var files []contract.FileInfo
err = util.Unmarshal(dataBytes, &files)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion sync/remote_server_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func (rs *remoteServerSync) start() error {
log.Debug("receive message [%s] => %s", client.RemoteAddr().String(), string(data))
writer := bufio.NewWriter(client)
if bytes.Equal(data, contract.InfoCommand) {
info := server.Info{
info := contract.FileServerInfo{
Status: contract.SuccessStatus(contract.InfoApi),
ServerAddr: rs.serverAddr,
SrcPath: server.SrcRoutePrefix,
Expand Down

0 comments on commit a9e78af

Please sign in to comment.