diff --git a/README.md b/README.md index 7bfe5f5..f67a0cf 100644 --- a/README.md +++ b/README.md @@ -76,7 +76,7 @@ docker run -it -d -v /path/to/your/dir:/dirs --name quics-client -p 6120:6120 - | connect | `qic connect root` | `--remote` `--password` | connect to the remote root directory | POST | /api/v1/connect/root/remote | | connect | `qic connect list-remote` | | get the list of remote root directory | GET | /api/v1/connect/list/remote | | disconnect | `qic disconnect root` | `--path` | disconnect to the root directory | POST | /api/v1/disconnect/root | -| sync | `qic sync status` | `--path` | get the status of the root directory | POST | /api/v1/sync/status | +| sync | `qic sync status` |y `--path` | get the status of the root directory | POST | /api/v1/sync/status | | sync | `qic sync rescan` | | rescan the all of root directory | POST | /api/v1/sync/rescan | | conflict | `qic conflict list` | | get the list of the root directory | GET | /api/v1/conflict/list | | conflict | `qic conflict choose` | `--path` `--candidate` | choose the file of the root directory | POST | /api/v1/conflict/choose | diff --git a/go.mod b/go.mod index 81741e7..452bcd3 100644 --- a/go.mod +++ b/go.mod @@ -29,6 +29,7 @@ require ( github.com/golang/snappy v0.0.4 // indirect github.com/google/flatbuffers v23.5.26+incompatible // indirect github.com/google/pprof v0.0.0-20231023181126-ff6d637d2a7b // indirect + github.com/google/wire v0.5.0 // indirect github.com/hashicorp/hcl v1.0.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/klauspost/compress v1.17.2 // indirect diff --git a/go.sum b/go.sum index dd7efaf..81c2877 100644 --- a/go.sum +++ b/go.sum @@ -179,9 +179,12 @@ github.com/google/pprof v0.0.0-20201218002935-b9804c9f04c2/go.mod h1:kpwsk12EmLe github.com/google/pprof v0.0.0-20231023181126-ff6d637d2a7b h1:RMpPgZTSApbPf7xaVel+QkoGPRLFLrwFO89uDUHEGf0= github.com/google/pprof v0.0.0-20231023181126-ff6d637d2a7b/go.mod h1:czg5+yv1E0ZGTi6S6vVK1mke0fV+FaUhNGcd6VRS9Ik= github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= +github.com/google/subcommands v1.0.1/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4= github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/google/wire v0.5.0 h1:I7ELFeVBr3yfPIcc8+MWvrjk+3VjbcSzoXm3JVa+jD8= +github.com/google/wire v0.5.0/go.mod h1:ngWDr9Qvq3yZA10YrxfyGELY/AFWGVpy9c1LTRi1EoU= github.com/googleapis/gax-go v2.0.0+incompatible/go.mod h1:SFVmujtThgffbyetf+mdk2eWhX2bMyUtNHzFKcPA9HY= github.com/googleapis/gax-go/v2 v2.0.3/go.mod h1:LLvjysVCY1JZeum8Z6l8qUty8fiNwE08qbEPm1M08qg= github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= @@ -535,6 +538,7 @@ golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3 golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190312151545-0bb0c0a6e846/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190312170243-e65039ee4138/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190422233926-fe54fb35175b/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/tools v0.0.0-20190506145303-2d16b83fe98c/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= diff --git a/pkg/app/setup.go b/pkg/app/setup.go new file mode 100644 index 0000000..efb379c --- /dev/null +++ b/pkg/app/setup.go @@ -0,0 +1,19 @@ +package app + +import ( + "github.com/google/wire" + "github.com/quic-s/quics-client/pkg/db/badger" + "github.com/quic-s/quics-client/pkg/sync" +) + +type App struct { + db sync.DB +} + +// TODO : DB -> APP + +var DBSet = wire.NewSet( + badger.NewBadger, + + wire.Bind(new(sync.DB), new(*badger.Badger)), +) diff --git a/pkg/db/badger/common.go b/pkg/db/badger/common.go index c66839e..6bbec69 100644 --- a/pkg/db/badger/common.go +++ b/pkg/db/badger/common.go @@ -8,11 +8,14 @@ import ( "strings" "github.com/dgraph-io/badger/v3" + "github.com/quic-s/quics-client/pkg/types" ) -func GetUUID() string { - bUUID, err := View("UUID") +/*--------------USER INFO ----------------*/ + +func (db *Badger) GetUUID() string { + bUUID, err := db.view("UUID") if err != nil { log.Println(err) } @@ -23,8 +26,8 @@ func GetUUID() string { /*--------------SYNC METADATA ----------------*/ // TODO NIL 반환 , 즉 아래 두개 함수를 합쳐야함 -func GetSyncMetadata(path string) types.SyncMetadata { - bsyncMetadata, err := View(path) +func (db *Badger) GetSyncMetadata(path string) types.SyncMetadata { + bsyncMetadata, err := db.view(path) if err != nil { return types.SyncMetadata{} } @@ -34,8 +37,8 @@ func GetSyncMetadata(path string) types.SyncMetadata { } -func IsSyncMetadataExisted(path string) bool { - syncMetadata := GetSyncMetadata(path) +func (db *Badger) IsSyncMetadataExisted(path string) bool { + syncMetadata := db.GetSyncMetadata(path) if reflect.ValueOf(syncMetadata).IsZero() { return false } @@ -44,13 +47,13 @@ func IsSyncMetadataExisted(path string) bool { // Get All SyncMetadata in certain rootpath // e.g. GetAllSyncMetadataInRoot("/home/username/rootdir") -func GetAllSyncMetadataInRoot(rootpath string) ([]*types.SyncMetadata, error) { +func (db *Badger) GetAllSyncMetadataInRoot(rootpath string) ([]*types.SyncMetadata, error) { syncMetadataList := []*types.SyncMetadata{} // get all file path in rootpath prefix := rootpath - err := badgerdb.View(func(txn *badger.Txn) error { + err := db.BadgerDB.View(func(txn *badger.Txn) error { opts := badger.DefaultIteratorOptions opts.PrefetchSize = 10 it := txn.NewIterator(opts) @@ -80,11 +83,11 @@ func GetAllSyncMetadataInRoot(rootpath string) ([]*types.SyncMetadata, error) { // Get All SyncMetadata in all rootpath // e.g. GetAllSyncMetadataAmongRoot() -func GetAllSyncMetadataAmongRoot() ([]*types.SyncMetadata, error) { +func (db *Badger) GetAllSyncMetadataAmongRoot() ([]*types.SyncMetadata, error) { syncMetadataList := []*types.SyncMetadata{} - rootDirList := GetRootDirList() + rootDirList := db.GetRootDirList() for _, rootDir := range rootDirList { - syncMetadata, err := GetAllSyncMetadataInRoot(rootDir.Path) + syncMetadata, err := db.GetAllSyncMetadataInRoot(rootDir.Path) if err != nil { return nil, err } @@ -96,8 +99,8 @@ func GetAllSyncMetadataAmongRoot() ([]*types.SyncMetadata, error) { /*------------ROOT DIRECTORY---------------- */ // key : value == string : []RootDir -func GetRootDirList() []types.RootDir { - bRootDirList, err := View("RootDirList") +func (db *Badger) GetRootDirList() []types.RootDir { + bRootDirList, err := db.view("RootDirList") if err != nil { return []types.RootDir{} } @@ -109,8 +112,8 @@ func GetRootDirList() []types.RootDir { } -func GetRootDir(path string) types.RootDir { - rootDirList := GetRootDirList() +func (db *Badger) GetRootDir(path string) types.RootDir { + rootDirList := db.GetRootDirList() for _, rootDir := range rootDirList { if rootDir.Path == path { return rootDir @@ -119,8 +122,8 @@ func GetRootDir(path string) types.RootDir { return types.RootDir{} } -func GetBeforePathWithAfterPath(afterPath string) string { - rootDirList := GetRootDirList() +func (db *Badger) GetBeforePathWithAfterPath(afterPath string) string { + rootDirList := db.GetRootDirList() rootbase := strings.Split(afterPath, "/")[1] for _, rootDir := range rootDirList { if rootDir.AfterPath == "/"+rootbase { @@ -130,8 +133,8 @@ func GetBeforePathWithAfterPath(afterPath string) string { return "" } -func SplitBeforeAfterRoot(path string) (string, string) { - rootDirList := GetRootDirList() +func (db *Badger) SplitBeforeAfterRoot(path string) (string, string) { + rootDirList := db.GetRootDirList() for _, rootDir := range rootDirList { if strings.HasPrefix(path, rootDir.BeforePath) { return rootDir.BeforePath, strings.TrimPrefix(path, rootDir.BeforePath) @@ -140,11 +143,11 @@ func SplitBeforeAfterRoot(path string) (string, string) { return "", "" } -func AddRootDir(path string) error { +func (db *Badger) AddRootDir(path string) error { //If Same Absolute Path is already exist, return //If Same Nickname is already taken , return - rootDirList := GetRootDirList() + rootDirList := db.GetRootDirList() for _, rootDir := range rootDirList { if rootDir.Path == path || rootDir.NickName == filepath.Base(path) { return fmt.Errorf("this Directory is already exist as Root") @@ -163,15 +166,15 @@ func AddRootDir(path string) error { newRootDirList := types.RootDirList{} newRootDirList = append(rootDirList, rootDir) - err := Update("RootDirList", newRootDirList.Encode()) + err := db.update("RootDirList", newRootDirList.Encode()) if err != nil { return err } return nil } -func UpdateRootdirToRegistered(path string) error { - rootDirList := GetRootDirList() +func (db *Badger) UpdateRootdirToRegistered(path string) error { + rootDirList := db.GetRootDirList() // Update IsRegistered newRootDirList := types.RootDirList{} for _, rootDir := range rootDirList { @@ -190,15 +193,15 @@ func UpdateRootdirToRegistered(path string) error { } newRootDirList = append(newRootDirList, rootDir) } - err := Update("RootDirList", newRootDirList.Encode()) + err := db.update("RootDirList", newRootDirList.Encode()) if err != nil { return err } return nil } -func DeleteRootDir(path string) { - rootDirList := GetRootDirList() +func (db *Badger) DeleteRootDir(path string) { + rootDirList := db.GetRootDirList() newRootDirList := types.RootDirList{} for i, rootDir := range rootDirList { if rootDir.Path == path { @@ -207,5 +210,5 @@ func DeleteRootDir(path string) { } } - Update("RootDirList", newRootDirList.Encode()) + db.update("RootDirList", newRootDirList.Encode()) } diff --git a/pkg/db/badger/badger.go b/pkg/db/badger/crud.go similarity index 59% rename from pkg/db/badger/badger.go rename to pkg/db/badger/crud.go index 8dd40ae..99a6f66 100644 --- a/pkg/db/badger/badger.go +++ b/pkg/db/badger/crud.go @@ -4,16 +4,11 @@ import ( "github.com/dgraph-io/badger/v3" ) -type CRUD interface { - Update(key string, value []byte) error - View(key string) ([]byte, error) - Delete(key string) error -} - -func Update(key string, value []byte) error { +func (bg *Badger) update(key string, value []byte) error { + mutex := &bg.Mutex mutex.Lock() defer mutex.Unlock() - return badgerdb.Update(func(txn *badger.Txn) error { + return bg.BadgerDB.Update(func(txn *badger.Txn) error { if err := txn.Set([]byte(key), value); err != nil { return err } @@ -21,12 +16,13 @@ func Update(key string, value []byte) error { }) } -func View(key string) ([]byte, error) { +func (bg *Badger) view(key string) ([]byte, error) { + mutex := &bg.Mutex mutex.Lock() defer mutex.Unlock() var value []byte - err := badgerdb.View(func(txn *badger.Txn) error { + err := bg.BadgerDB.View(func(txn *badger.Txn) error { item, err := txn.Get([]byte(key)) if err != nil { @@ -44,10 +40,11 @@ func View(key string) ([]byte, error) { return value, err } -func Delete(key string) error { +func (bg *Badger) delete(key string) error { + mutex := &bg.Mutex mutex.Lock() defer mutex.Unlock() - err := badgerdb.Update(func(txn *badger.Txn) error { + err := bg.BadgerDB.Update(func(txn *badger.Txn) error { err := txn.Delete([]byte(key)) return err }) diff --git a/pkg/db/badger/setup.go b/pkg/db/badger/setup.go index 502891a..a50b8fa 100644 --- a/pkg/db/badger/setup.go +++ b/pkg/db/badger/setup.go @@ -9,23 +9,27 @@ import ( ) // Declare a global variable for the DB. -var badgerdb *badger.DB -var mutex sync.Mutex +type Badger struct { + BadgerDB *badger.DB + Mutex sync.Mutex `wire:"-"` // ignore this field +} -// Define a function to open the DB. -func OpenDB() { +func NewBadger() *Badger { // Open the Badger database located in the ./badger directory. // It will be created if it doesn't exist. opts := badger.DefaultOptions(utils.GetQuicsDirPath() + "/badger") db, err := badger.Open(opts) - badgerdb = db if err != nil { log.Fatal(err) } + return &Badger{ + BadgerDB: db, + } + } -func CloseDB() { - if err := badgerdb.Close(); err != nil { +func (bg *Badger) CloseDB() { + if err := bg.BadgerDB.Close(); err != nil { log.Fatal(err) } } diff --git a/pkg/net/http3/http.go b/pkg/net/http3/http.go index dcbae15..3a7df99 100644 --- a/pkg/net/http3/http.go +++ b/pkg/net/http3/http.go @@ -16,6 +16,9 @@ import ( "github.com/quic-s/quics-client/pkg/viper" ) +func Http3ServerProvider() { + +} func RestServerStart(port string) { fmt.Println("\t-----------------------------------------\n") @@ -23,6 +26,8 @@ func RestServerStart(port string) { fmt.Println("\t-----------------------------------------\n") log.Println("quics-client : starting port " + viper.GetViperEnvVariables("REST_SERVER_PORT")) + + //TODO bagder injection badger.OpenDB() sync.InitWatcher() diff --git a/pkg/net/qclient/cfAdapter.go b/pkg/net/qclient/cfAdapter.go index fb171db..2a5a072 100644 --- a/pkg/net/qclient/cfAdapter.go +++ b/pkg/net/qclient/cfAdapter.go @@ -9,7 +9,7 @@ import ( qstypes "github.com/quic-s/quics/pkg/types" ) -func SendChooseOne(stream *qp.Stream, UUID string, AfterPath string, side string) (*qstypes.PleaseFileRes, error) { +func (qc *QPClient) SendChooseOne(stream *qp.Stream, UUID string, AfterPath string, side string) (*qstypes.PleaseFileRes, error) { breq := qstypes.PleaseFileReq{ UUID: UUID, @@ -35,7 +35,7 @@ func SendChooseOne(stream *qp.Stream, UUID string, AfterPath string, side string } -func SendAskConflictList(stream *qp.Stream, UUID string) (*qstypes.AskConflictListRes, error) { +func (qc *QPClient) SendAskConflictList(stream *qp.Stream, UUID string) (*qstypes.AskConflictListRes, error) { breq := qstypes.AskConflictListReq{ UUID: UUID, } @@ -59,7 +59,7 @@ func SendAskConflictList(stream *qp.Stream, UUID string) (*qstypes.AskConflictLi } -func SendConflictDownload(stream *qp.Stream, UUID string, AfterPath string) ([]*qstypes.ConflictDownloadReq, error) { +func (qc *QPClient) SendConflictDownload(stream *qp.Stream, UUID string, AfterPath string) ([]*qstypes.ConflictDownloadReq, error) { breq := qstypes.AskStagingNumReq{ UUID: UUID, diff --git a/pkg/net/qclient/fcAdapter.go b/pkg/net/qclient/fcAdapter.go index a86b673..095201f 100644 --- a/pkg/net/qclient/fcAdapter.go +++ b/pkg/net/qclient/fcAdapter.go @@ -12,10 +12,9 @@ import ( qstypes "github.com/quic-s/quics/pkg/types" ) -func ForceSyncRecvHandler(stream *qp.Stream) (*qstypes.MustSyncReq, string, error) { +func (qc *QPClient) ForceSyncRecvHandler(stream *qp.Stream, badger *badger.Badger) (*qstypes.MustSyncReq, string, error) { data, fileInfo, fileContent, err := stream.RecvFileBMessage() - if err != nil { log.Println("", err) return nil, "", err @@ -69,7 +68,7 @@ func ForceSyncRecvHandler(stream *qp.Stream) (*qstypes.MustSyncReq, string, erro } -func ForceSyncHandler(stream *qp.Stream, UUID string, AfterPath string, LastSyncTimestamp uint64, LastSyncHash string) error { +func (qc *QPClient) ForceSyncHandler(stream *qp.Stream, UUID string, AfterPath string, LastSyncTimestamp uint64, LastSyncHash string) error { bres := qstypes.MustSyncRes{ UUID: UUID, AfterPath: AfterPath, diff --git a/pkg/net/qclient/fsAdapter.go b/pkg/net/qclient/fsAdapter.go index 771995d..fe97bd5 100644 --- a/pkg/net/qclient/fsAdapter.go +++ b/pkg/net/qclient/fsAdapter.go @@ -1,11 +1,11 @@ package qclient import ( - "github.com/quic-s/quics-protocol/pkg/stream" + qp "github.com/quic-s/quics-protocol" qstypes "github.com/quic-s/quics/pkg/types" ) -func AskAllMetaRecvHandler(stream *stream.Stream) (*qstypes.AskAllMetaReq, error) { +func (qc *QPClient) AskAllMetaRecvHandler(stream *qp.Stream) (*qstypes.AskAllMetaReq, error) { data, err := stream.RecvBMessage() if err != nil { return nil, err @@ -18,7 +18,7 @@ func AskAllMetaRecvHandler(stream *stream.Stream) (*qstypes.AskAllMetaReq, error } -func AskAllMetaHandler(stream *stream.Stream, UUID string, syncMetaList []qstypes.SyncMetadata) error { +func (qc *QPClient) AskAllMetaHandler(stream *qp.Stream, UUID string, syncMetaList []qstypes.SyncMetadata) error { bres := qstypes.AskAllMetaRes{ UUID: UUID, @@ -35,7 +35,7 @@ func AskAllMetaHandler(stream *stream.Stream, UUID string, syncMetaList []qstype return nil } -func SendRescan(stream *stream.Stream, UUID string, RootAfterPath []string) (*qstypes.RescanRes, error) { +func (qc *QPClient) SendRescan(stream *qp.Stream, UUID string, RootAfterPath []string) (*qstypes.RescanRes, error) { bres := qstypes.RescanReq{ UUID: UUID, RootAfterPath: RootAfterPath, diff --git a/pkg/net/qclient/htAdapter.go b/pkg/net/qclient/htAdapter.go index ea2bf42..3ecd695 100644 --- a/pkg/net/qclient/htAdapter.go +++ b/pkg/net/qclient/htAdapter.go @@ -9,7 +9,7 @@ import ( qstypes "github.com/quic-s/quics/pkg/types" ) -func SendRollBack(stream *qp.Stream, UUID string, AfterPath string, Version uint64) (*qstypes.RollBackRes, error) { +func (qc *QPClient) SendRollBack(stream *qp.Stream, UUID string, AfterPath string, Version uint64) (*qstypes.RollBackRes, error) { breq := qstypes.RollBackReq{ UUID: UUID, AfterPath: AfterPath, @@ -35,7 +35,7 @@ func SendRollBack(stream *qp.Stream, UUID string, AfterPath string, Version uint return rollbackRes, nil } -func SendShowHistory(stream *qp.Stream, UUID string, Afterpath string, CntFromHead uint64) (*qstypes.ShowHistoryRes, error) { +func (qc *QPClient) SendShowHistory(stream *qp.Stream, UUID string, Afterpath string, CntFromHead uint64) (*qstypes.ShowHistoryRes, error) { breq := qstypes.ShowHistoryReq{ UUID: UUID, @@ -62,7 +62,7 @@ func SendShowHistory(stream *qp.Stream, UUID string, Afterpath string, CntFromHe return showHistoryRes, nil } -func SendDownloadHistory(stream *qp.Stream, UUID string, AfterPath string, Version uint64) (*qstypes.DownloadHistoryRes, error) { +func (qc *QPClient) SendDownloadHistory(stream *qp.Stream, UUID string, AfterPath string, Version uint64) (*qstypes.DownloadHistoryRes, error) { breq := qstypes.DownloadHistoryReq{ UUID: UUID, diff --git a/pkg/net/qclient/http_port.go b/pkg/net/qclient/http_port.go new file mode 100644 index 0000000..d7858e5 --- /dev/null +++ b/pkg/net/qclient/http_port.go @@ -0,0 +1,57 @@ +package qclient + +import ( + "github.com/quic-s/quics-client/pkg/db/badger" + qp "github.com/quic-s/quics-protocol" + qstypes "github.com/quic-s/quics/pkg/types" +) + +type NetPort interface { +} + +type QPC struct { + qpport QPPort +} + +func NewQPC(qpport QPPort) *QPC { + return &QPC{qpport: qpport} +} + +type QPPort interface { + // cfAdpater + SendChooseOne(stream *qp.Stream, UUID string, AfterPath string, side string) (*qstypes.PleaseFileRes, error) + SendAskConflictList(stream *qp.Stream, UUID string) (*qstypes.AskConflictListRes, error) + SendConflictDownload(stream *qp.Stream, UUID string, AfterPath string) ([]*qstypes.ConflictDownloadReq, error) + // fcAdapter + ForceSyncRecvHandler(stream *qp.Stream, badger *badger.Badger) (*qstypes.MustSyncReq, string, error) + ForceSyncHandler(stream *qp.Stream, UUID string, AfterPath string, LastSyncTimestamp uint64, LastSyncHash string) error + // fsAdapter + AskAllMetaRecvHandler(stream *qp.Stream) (*qstypes.AskAllMetaReq, error) + AskAllMetaHandler(stream *qp.Stream, UUID string, syncMetaList []qstypes.SyncMetadata) error + SendRescan(stream *qp.Stream, UUID string, RootAfterPath []string) (*qstypes.RescanRes, error) + // htAdapter + SendRollBack(stream *qp.Stream, UUID string, AfterPath string, Version uint64) (*qstypes.RollBackRes, error) + SendShowHistory(stream *qp.Stream, UUID string, Afterpath string, CntFromHead uint64) (*qstypes.ShowHistoryRes, error) + SendDownloadHistory(stream *qp.Stream, UUID string, AfterPath string, Version uint64) (*qstypes.DownloadHistoryRes, error) + // msAdapter + MustSyncRecvHandler(stream *qp.Stream) (*qstypes.MustSyncReq, error) + MustSyncHandler(stream *qp.Stream, UUID string, AfterPath string, LastSyncTimestamp uint64, LastSyncHash string) error + GiveYouRecvHandler(stream *qp.Stream, path string, afterPath string, hash string, Isremoved bool) (*qstypes.GiveYouReq, error) + GiveYouHandler(stream *qp.Stream, UUID string, AfterPath string, LastSyncTimestamp uint64, LastSyncHash string) error + NeedContentRecvHandler(stream *qp.Stream) (*qstypes.NeedContentReq, error) + NeedContentHandler(stream *qp.Stream, path string, UUID string, AfterPath string, LastUpdateTimestamp uint64, LastUpdateHash string) error + // psAdapter + SendPleaseSync(stream *qp.Stream, UUID string, Event string, AfterPath string, LastUpdateTimestamp uint64, LastUpdateHash string, LastSyncHash string, fileMetadata qstypes.FileMetadata) (*qstypes.PleaseSyncRes, error) + SendPleaseTake(stream *qp.Stream, UUID string, AfterPath string, path string) (*qstypes.PleaseTakeRes, error) + // rgAdapter + SendClientRegister(stream *qp.Stream, UUID string, ClientPassword string) (qstypes.ClientRegisterRes, error) + SendAskRootList(stream *qp.Stream, UUID string) (*qstypes.AskRootDirRes, error) + SendRootDirRegister(stream *qp.Stream, UUID string, RootDirPassword string, BeforePath string, AfterPath string) (qstypes.RootDirRegisterRes, error) + SendDisconnectRootDir(stream *qp.Stream, UUID string, AfterPath string) (*qstypes.DisconnectRootDirRes, error) + SendDisconnectClient(stream *qp.Stream, UUID string) (*qstypes.DisconnectClientRes, error) + // rsAdapter + SendPing(stream *qp.Stream, UUID string) (*qstypes.Ping, error) + // shAdapter + SendLinkShare(stream *qp.Stream, UUID string, AfterPath string, MaxCnt uint64) (*qstypes.ShareRes, error) + SendStopShare(stream *qp.Stream, UUID string, Link string) (*qstypes.StopShareRes, error) +} diff --git a/pkg/net/qclient/msAdapter.go b/pkg/net/qclient/msAdapter.go index b24d087..adeb1c2 100644 --- a/pkg/net/qclient/msAdapter.go +++ b/pkg/net/qclient/msAdapter.go @@ -12,7 +12,7 @@ import ( qstypes "github.com/quic-s/quics/pkg/types" ) -func MustSyncRecvHandler(stream *qp.Stream) (*qstypes.MustSyncReq, error) { +func (qc *QPClient) MustSyncRecvHandler(stream *qp.Stream) (*qstypes.MustSyncReq, error) { data, err := stream.RecvBMessage() if err != nil { @@ -24,7 +24,7 @@ func MustSyncRecvHandler(stream *qp.Stream) (*qstypes.MustSyncReq, error) { } -func MustSyncHandler(stream *qp.Stream, UUID string, AfterPath string, LastSyncTimestamp uint64, LastSyncHash string) error { +func (qc *QPClient) MustSyncHandler(stream *qp.Stream, UUID string, AfterPath string, LastSyncTimestamp uint64, LastSyncHash string) error { bres := qstypes.MustSyncRes{ UUID: UUID, AfterPath: AfterPath, @@ -46,7 +46,7 @@ func MustSyncHandler(stream *qp.Stream, UUID string, AfterPath string, LastSyncT } -func GiveYouRecvHandler(stream *qp.Stream, path string, afterPath string, hash string, Isremoved bool) (*qstypes.GiveYouReq, error) { +func (qc *QPClient) GiveYouRecvHandler(stream *qp.Stream, path string, afterPath string, hash string, Isremoved bool) (*qstypes.GiveYouReq, error) { data, fileInfo, fileContent, err := stream.RecvFileBMessage() if err != nil { @@ -128,7 +128,7 @@ func GiveYouRecvHandler(stream *qp.Stream, path string, afterPath string, hash s return &req, nil } -func GiveYouHandler(stream *qp.Stream, UUID string, AfterPath string, LastSyncTimestamp uint64, LastSyncHash string) error { +func (qc *QPClient) GiveYouHandler(stream *qp.Stream, UUID string, AfterPath string, LastSyncTimestamp uint64, LastSyncHash string) error { bres := qstypes.GiveYouRes{ UUID: UUID, @@ -149,7 +149,7 @@ func GiveYouHandler(stream *qp.Stream, UUID string, AfterPath string, LastSyncTi return nil } -func NeedContentRecvHandler(stream *qp.Stream) (*qstypes.NeedContentReq, error) { +func (qc *QPClient) NeedContentRecvHandler(stream *qp.Stream) (*qstypes.NeedContentReq, error) { data, err := stream.RecvBMessage() if err != nil { @@ -161,7 +161,7 @@ func NeedContentRecvHandler(stream *qp.Stream) (*qstypes.NeedContentReq, error) } -func NeedContentHandler(stream *qp.Stream, path string, UUID string, AfterPath string, LastUpdateTimestamp uint64, LastUpdateHash string) error { +func (qc *QPClient) NeedContentHandler(stream *qp.Stream, path string, UUID string, AfterPath string, LastUpdateTimestamp uint64, LastUpdateHash string) error { bres := qstypes.NeedContentRes{ UUID: UUID, diff --git a/pkg/net/qclient/psAdapter.go b/pkg/net/qclient/psAdapter.go index 25002de..c78b57b 100644 --- a/pkg/net/qclient/psAdapter.go +++ b/pkg/net/qclient/psAdapter.go @@ -5,7 +5,7 @@ import ( qstypes "github.com/quic-s/quics/pkg/types" ) -func SendPleaseSync(stream *qp.Stream, UUID string, Event string, AfterPath string, LastUpdateTimestamp uint64, LastUpdateHash string, LastSyncHash string, fileMetadata qstypes.FileMetadata) (*qstypes.PleaseSyncRes, error) { +func (qc *QPClient) SendPleaseSync(stream *qp.Stream, UUID string, Event string, AfterPath string, LastUpdateTimestamp uint64, LastUpdateHash string, LastSyncHash string, fileMetadata qstypes.FileMetadata) (*qstypes.PleaseSyncRes, error) { breq := qstypes.PleaseSyncReq{ UUID: UUID, @@ -36,7 +36,7 @@ func SendPleaseSync(stream *qp.Stream, UUID string, Event string, AfterPath stri } -func SendPleaseTake(stream *qp.Stream, UUID string, AfterPath string, path string) (*qstypes.PleaseTakeRes, error) { +func (qc *QPClient) SendPleaseTake(stream *qp.Stream, UUID string, AfterPath string, path string) (*qstypes.PleaseTakeRes, error) { res := qstypes.PleaseTakeRes{} breq := qstypes.PleaseTakeReq{ UUID: UUID, diff --git a/pkg/net/qclient/rgAdapter.go b/pkg/net/qclient/rgAdapter.go index 346325c..341b809 100644 --- a/pkg/net/qclient/rgAdapter.go +++ b/pkg/net/qclient/rgAdapter.go @@ -5,7 +5,7 @@ import ( qstypes "github.com/quic-s/quics/pkg/types" ) -func SendClientRegister(stream *qp.Stream, UUID string, ClientPassword string) (qstypes.ClientRegisterRes, error) { +func (qc *QPClient) SendClientRegister(stream *qp.Stream, UUID string, ClientPassword string) (qstypes.ClientRegisterRes, error) { breq := qstypes.ClientRegisterReq{ UUID: UUID, ClientPassword: ClientPassword, @@ -27,7 +27,7 @@ func SendClientRegister(stream *qp.Stream, UUID string, ClientPassword string) ( } -func SendAskRootList(stream *qp.Stream, UUID string) (*qstypes.AskRootDirRes, error) { +func (qc *QPClient) SendAskRootList(stream *qp.Stream, UUID string) (*qstypes.AskRootDirRes, error) { breq := qstypes.AskRootDirReq{ UUID: UUID, } @@ -51,7 +51,7 @@ func SendAskRootList(stream *qp.Stream, UUID string) (*qstypes.AskRootDirRes, er } -func SendRootDirRegister(stream *qp.Stream, UUID string, RootDirPassword string, BeforePath string, AfterPath string) (qstypes.RootDirRegisterRes, error) { +func (qc *QPClient) SendRootDirRegister(stream *qp.Stream, UUID string, RootDirPassword string, BeforePath string, AfterPath string) (qstypes.RootDirRegisterRes, error) { breq := qstypes.RootDirRegisterReq{ UUID: UUID, RootDirPassword: RootDirPassword, @@ -78,7 +78,7 @@ func SendRootDirRegister(stream *qp.Stream, UUID string, RootDirPassword string, return res, nil } -func SendDisconnectRootDir(stream *qp.Stream, UUID string, AfterPath string) (qstypes.DisconnectRootDirRes, error) { +func (qc *QPClient) SendDisconnectRootDir(stream *qp.Stream, UUID string, AfterPath string) (*qstypes.DisconnectRootDirRes, error) { breq := qstypes.DisconnectRootDirReq{ UUID: UUID, AfterPath: AfterPath, @@ -86,48 +86,48 @@ func SendDisconnectRootDir(stream *qp.Stream, UUID string, AfterPath string) (qs req, err := breq.Encode() if err != nil { - return qstypes.DisconnectRootDirRes{}, err + return &qstypes.DisconnectRootDirRes{}, err } err = stream.SendBMessage(req) if err != nil { - return qstypes.DisconnectRootDirRes{}, err + return &qstypes.DisconnectRootDirRes{}, err } bres, err := stream.RecvBMessage() if err != nil { - return qstypes.DisconnectRootDirRes{}, err + return &qstypes.DisconnectRootDirRes{}, err } res := qstypes.DisconnectRootDirRes{} res.Decode(bres) - return res, nil + return &res, nil } -func SendDisconnectClient(stream *qp.Stream, UUID string) (qstypes.DisconnectClientRes, error) { +func (qc *QPClient) SendDisconnectClient(stream *qp.Stream, UUID string) (*qstypes.DisconnectClientRes, error) { breq := qstypes.DisconnectClientReq{ UUID: UUID, } req, err := breq.Encode() if err != nil { - return qstypes.DisconnectClientRes{}, err + return &qstypes.DisconnectClientRes{}, err } err = stream.SendBMessage(req) if err != nil { - return qstypes.DisconnectClientRes{}, err + return &qstypes.DisconnectClientRes{}, err } bres, err := stream.RecvBMessage() if err != nil { - return qstypes.DisconnectClientRes{}, err + return &qstypes.DisconnectClientRes{}, err } res := qstypes.DisconnectClientRes{} res.Decode(bres) - return res, nil + return &res, nil } diff --git a/pkg/net/qclient/rsAdapter.go b/pkg/net/qclient/rsAdapter.go index 01d8e51..eff5920 100644 --- a/pkg/net/qclient/rsAdapter.go +++ b/pkg/net/qclient/rsAdapter.go @@ -7,7 +7,7 @@ import ( qstypes "github.com/quic-s/quics/pkg/types" ) -func SendPing(stream *qp.Stream, UUID string) (*qstypes.Ping, error) { +func (qc *QPClient) SendPing(stream *qp.Stream, UUID string) (*qstypes.Ping, error) { req := qstypes.Ping{ UUID: UUID, diff --git a/pkg/net/qclient/setup.go b/pkg/net/qclient/setup.go index 1edfb79..9398b25 100644 --- a/pkg/net/qclient/setup.go +++ b/pkg/net/qclient/setup.go @@ -1 +1,24 @@ package qclient + +import ( + qp "github.com/quic-s/quics-protocol" +) + +type QPClient struct { + Conn *qp.Connection `wire:"-"` // ignore this field when inject + QPClient *qp.QP +} + +func NewQPClient() *QPClient { + //newClient, err := qp.New(qp.LOG_LEVEL_INFO) + newClient, err := qp.New(qp.LOG_LEVEL_ERROR) + if err != nil { + panic(err) + } + return &QPClient{QPClient: newClient} +} + +func (qc *QPClient) closeConnnect() { + qc.Conn.Close() + qc.Conn = nil +} diff --git a/pkg/net/qclient/shAdapter.go b/pkg/net/qclient/shAdapter.go index a93f359..40ab94a 100644 --- a/pkg/net/qclient/shAdapter.go +++ b/pkg/net/qclient/shAdapter.go @@ -5,7 +5,7 @@ import ( qstypes "github.com/quic-s/quics/pkg/types" ) -func SendLinkShare(stream *qp.Stream, UUID string, AfterPath string, MaxCnt uint64) (*qstypes.ShareRes, error) { +func (qc *QPClient) SendLinkShare(stream *qp.Stream, UUID string, AfterPath string, MaxCnt uint64) (*qstypes.ShareRes, error) { breq := qstypes.ShareReq{ UUID: UUID, AfterPath: AfterPath, @@ -30,7 +30,7 @@ func SendLinkShare(stream *qp.Stream, UUID string, AfterPath string, MaxCnt uint return &res, nil } -func SendStopShare(stream *qp.Stream, UUID string, Link string) (*qstypes.StopShareRes, error) { +func (qc *QPClient) SendStopShare(stream *qp.Stream, UUID string, Link string) (*qstypes.StopShareRes, error) { breq := qstypes.StopShareReq{ UUID: UUID, Link: Link, diff --git a/pkg/net/qclient/wire.go b/pkg/net/qclient/wire.go new file mode 100644 index 0000000..aa71db7 --- /dev/null +++ b/pkg/net/qclient/wire.go @@ -0,0 +1,23 @@ +//go:build wireinject +// +build wireinject + +package qclient + +import ( + "github.com/google/wire" +) + +var NetSet = wire.NewSet( + NewQPClient, + wire.Bind(new(QPPort), new(*QPClient)), + NewQPC, +) + +// func injectDB() (*DB, error) { +// panic(wire.Build(DBSet)) + +// } + +func injectQPC() (*QPC, error) { + panic(wire.Build(NetSet)) +} diff --git a/pkg/sync/db_port.go b/pkg/sync/db_port.go new file mode 100644 index 0000000..4c201f8 --- /dev/null +++ b/pkg/sync/db_port.go @@ -0,0 +1,20 @@ +package sync + +import ( + "github.com/quic-s/quics-client/pkg/types" +) + +type Repository interface { + GetUUID() string + GetSyncMetadata(path string) types.SyncMetadata + IsSyncMetadataExisted(path string) bool + GetAllSyncMetadataInRoot(rootpath string) ([]*types.SyncMetadata, error) + GetAllSyncMetadataAmongRoot() ([]*types.SyncMetadata, error) + GetRootDirList() []types.RootDir + GetRootDir(path string) types.RootDir + GetBeforePathWithAfterPath(afterpath string) string + SplitBeforeAfterRoot(path string) (string, string) + AddRootDir(path string) error + UpdateRootdirToRegistered(path string) error + DeleteRootDir(path string) +} diff --git a/pkg/sync/setup.go b/pkg/sync/setup.go index 0476cf2..8912725 100644 --- a/pkg/sync/setup.go +++ b/pkg/sync/setup.go @@ -5,51 +5,45 @@ import ( "sync" "github.com/fsnotify/fsnotify" - - qp "github.com/quic-s/quics-protocol" + "github.com/google/wire" ) -var ( - QPClient *qp.QP - Conn *qp.Connection - Watcher *fsnotify.Watcher - PSMut map[byte]*sync.Mutex - PSMutModNum uint8 = 64 +const PSMutModNum uint8 = 16 + +var ServiceSet = wire.NewSet( +// TODO ) -func init() { +type Service struct { + Watcher *fsnotify.Watcher + PSMut map[byte]*sync.Mutex + db Repository + qclient QPPort + //TODO http +} - // InitQPClient() +//TODO ServiceProvider -} -func InitQPClient() { - //newClient, err := qp.New(qp.LOG_LEVEL_INFO) - newClient, err := qp.New(qp.LOG_LEVEL_ERROR) - if err != nil { - panic(err) - } - QPClient = newClient +func NewMutex() map[byte]*sync.Mutex { - PSMut = make(map[byte]*sync.Mutex) + pSMut := make(map[byte]*sync.Mutex) for i := uint8(0); i < PSMutModNum; i++ { - PSMut[i] = &sync.Mutex{} + pSMut[i] = &sync.Mutex{} } + // TODO when initiate MustSyncMain() ForceSyncMain() FullScanMain() NeedContentMain() + + return pSMut } -func InitWatcher() { - // Create a new watcher. - err := error(nil) - Watcher, err = fsnotify.NewWatcher() + +func NewWatcher() *fsnotify.Watcher { + watcher, err := fsnotify.NewWatcher() if err != nil { log.Fatal(err) } -} - -func CloseConnect() { - Conn.Close() - Conn = nil + return watcher }