Skip to content

Commit

Permalink
fix/scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
vivi108 committed Oct 22, 2023
1 parent 9aa3e05 commit 6591bd2
Show file tree
Hide file tree
Showing 14 changed files with 76 additions and 59 deletions.
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -7,7 +7,7 @@ require (
github.com/fsnotify/fsnotify v1.6.0
github.com/google/uuid v1.3.1
github.com/quic-go/quic-go v0.39.0
github.com/quic-s/quics v0.0.0-20231022050507-37265559d546
github.com/quic-s/quics v0.0.0-20231022064309-d84b4c889eae
github.com/quic-s/quics-protocol v0.0.0-20231007080707-81b358ef2f0b
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.17.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Expand Up @@ -260,6 +260,8 @@ github.com/quic-s/quics v0.0.0-20231017163803-d03854e9069a h1:160qewSyfKRtAcGwG8
github.com/quic-s/quics v0.0.0-20231017163803-d03854e9069a/go.mod h1:eQDkECDehkewssISgB16qONKKtJDxr62IV2R5ubXfyI=
github.com/quic-s/quics v0.0.0-20231022050507-37265559d546 h1:QZuDkVhU73HrV7qF3+uV/1Zznn8/4SWszEyEFIq4YZI=
github.com/quic-s/quics v0.0.0-20231022050507-37265559d546/go.mod h1:eQDkECDehkewssISgB16qONKKtJDxr62IV2R5ubXfyI=
github.com/quic-s/quics v0.0.0-20231022064309-d84b4c889eae h1:eLPCRocBa0u23Yp+GNac6exyhvTKsJ0l+VCZo/LZlUI=
github.com/quic-s/quics v0.0.0-20231022064309-d84b4c889eae/go.mod h1:eQDkECDehkewssISgB16qONKKtJDxr62IV2R5ubXfyI=
github.com/quic-s/quics-protocol v0.0.0-20231007080707-81b358ef2f0b h1:nYwaFH5UBkQFAjJnidVwmxZcD5d+fUQE2lWCLkh7zqc=
github.com/quic-s/quics-protocol v0.0.0-20231007080707-81b358ef2f0b/go.mod h1:nmSpvk/GC9lbgIPSvzKNbUwnkkacCe/caK0QQsuzQ5Q=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
Expand Down
36 changes: 36 additions & 0 deletions pkg/net/qclient/rsAdapter.go
@@ -1 +1,37 @@
package qclient

import (
qp "github.com/quic-s/quics-protocol"
qstypes "github.com/quic-s/quics/pkg/types"
)

func SendPing(stream *qp.Stream, UUID string) (*qstypes.Ping, error) {

req := qstypes.Ping{
UUID: UUID,
}

breq, err := req.Encode()
if err != nil {
return nil, err
}

err = stream.SendBMessage(breq)
if err != nil {
return nil, err
}

bres, err := stream.RecvBMessage()
if err != nil {
return nil, err
}

result := qstypes.Ping{}
err = result.Decode(bres)
if err != nil {
return nil, err
}

return &result, nil

}
4 changes: 2 additions & 2 deletions pkg/sync/conflict.go
Expand Up @@ -20,7 +20,7 @@ const (
func ConflictDownload(path string) error {
_, Afterpath := badger.SplitBeforeAfterRoot(path)

err := Conn.OpenTransaction("CONFLICTDOWNLOAD", func(stream *stream.Stream, transactionName string, transactionID []byte) error {
err := Conn.OpenTransaction(qstypes.CONFLICTDOWNLOAD, func(stream *stream.Stream, transactionName string, transactionID []byte) error {
UUID := badger.GetUUID()
res, err := qclient.SendConflictDownload(stream, UUID, Afterpath)
if err != nil {
Expand Down Expand Up @@ -97,7 +97,7 @@ func ChooseOne(path string, Side string) error {
UUID := badger.GetUUID()
_, AfterPath := badger.SplitBeforeAfterRoot(path)

err := Conn.OpenTransaction("CHOOSEONE", func(stream *stream.Stream, transactionName string, transactionID []byte) error {
err := Conn.OpenTransaction(qstypes.CHOOSEONE, func(stream *stream.Stream, transactionName string, transactionID []byte) error {

// Send ChooseOne
res, err := qclient.SendChooseOne(stream, UUID, AfterPath, Side)
Expand Down
3 changes: 2 additions & 1 deletion pkg/sync/forcesync.go
Expand Up @@ -9,11 +9,12 @@ import (
"github.com/quic-s/quics-client/pkg/types"
qp "github.com/quic-s/quics-protocol"
"github.com/quic-s/quics-protocol/pkg/stream"
qstypes "github.com/quic-s/quics/pkg/types"
)

func ForceSyncMain() {

err := QPClient.RecvTransactionHandleFunc("FORCESYNC", func(conn *qp.Connection, stream *stream.Stream, transactionName string, transactionID []byte) error {
err := QPClient.RecvTransactionHandleFunc(qstypes.FORCESYNC, func(conn *qp.Connection, stream *stream.Stream, transactionName string, transactionID []byte) error {
log.Println("quics-client: [FORCESYNC] transaction start")
UUID := badger.GetUUID()

Expand Down
2 changes: 1 addition & 1 deletion pkg/sync/fsnoti.go
Expand Up @@ -24,7 +24,7 @@ func DirWatchStart() {
// lock mutex by hash value of file path
// using hash value is to reduce the number of mutex

if event.Op&fsnotify.Remove == fsnotify.Remove {
if event.Op&fsnotify.Remove == fsnotify.Remove || event.Op&fsnotify.Rename == fsnotify.Rename {
log.Println("quics-client : REMOVE event ")
go PleaseSync(path)
continue
Expand Down
6 changes: 3 additions & 3 deletions pkg/sync/history.go
Expand Up @@ -14,7 +14,7 @@ func RollBack(path string, version uint64) error {

_, AfterPath := badger.SplitBeforeAfterRoot(path)

err := QPClient.RecvTransactionHandleFunc("ROLLBACK", func(conn *qp.Connection, stream *qp.Stream, transactionName string, transactionID []byte) error {
err := QPClient.RecvTransactionHandleFunc(qstypes.ROLLBACK, func(conn *qp.Connection, stream *qp.Stream, transactionName string, transactionID []byte) error {
rollbackres, err := qclient.SendRollBack(stream, badger.GetUUID(), AfterPath, version)
if err != nil {
return err
Expand All @@ -34,7 +34,7 @@ func RollBack(path string, version uint64) error {
// @URL /api/v1/history/show
func HistoryShow(path string, cntfromhead uint64) ([]qstypes.FileHistory, error) {
historyShowRes := []qstypes.FileHistory{}
err := QPClient.RecvTransactionHandleFunc("HISTORYSHOW", func(conn *qp.Connection, stream *qp.Stream, transactionName string, transactionID []byte) error {
err := QPClient.RecvTransactionHandleFunc(qstypes.HISTORYSHOW, func(conn *qp.Connection, stream *qp.Stream, transactionName string, transactionID []byte) error {
historyshowres, err := qclient.SendShowHistory(stream, badger.GetUUID(), path, cntfromhead)
if err != nil {
return err
Expand All @@ -54,7 +54,7 @@ func HistoryShow(path string, cntfromhead uint64) ([]qstypes.FileHistory, error)
// @URL /api/v1/history/download
func HistoryDownload(path string, version uint64) error {

err := QPClient.RecvTransactionHandleFunc("HISTORYDOWNLOAD", func(conn *qp.Connection, stream *qp.Stream, transactionName string, transactionID []byte) error {
err := QPClient.RecvTransactionHandleFunc(qstypes.HISTORYDOWNLOAD, func(conn *qp.Connection, stream *qp.Stream, transactionName string, transactionID []byte) error {
historydownloadres, err := qclient.SendDownloadHistory(stream, badger.GetUUID(), path, version)
if err != nil {
return err
Expand Down
5 changes: 3 additions & 2 deletions pkg/sync/mustsync.go
Expand Up @@ -12,12 +12,13 @@ import (

"github.com/quic-s/quics-client/pkg/types"
qp "github.com/quic-s/quics-protocol"
qstypes "github.com/quic-s/quics/pkg/types"
)

// TODO
func NeedContentMain() {

err := QPClient.RecvTransactionHandleFunc("NEEDCONTENT", func(conn *qp.Connection, stream *qp.Stream, transactionName string, transactionID []byte) error {
err := QPClient.RecvTransactionHandleFunc(qstypes.NEEDCONTENT, func(conn *qp.Connection, stream *qp.Stream, transactionName string, transactionID []byte) error {
req, err := qclient.NeedContentRecvHandler(stream)
if err != nil {
return err
Expand Down Expand Up @@ -49,7 +50,7 @@ func NeedContentMain() {

func MustSyncMain() {

err := QPClient.RecvTransactionHandleFunc("MUSTSYNC", func(conn *qp.Connection, stream *qp.Stream, transactionName string, transactionID []byte) error {
err := QPClient.RecvTransactionHandleFunc(qstypes.MUSTSYNC, func(conn *qp.Connection, stream *qp.Stream, transactionName string, transactionID []byte) error {
UUID := badger.GetUUID()
mustSyncReq, err := qclient.MustSyncRecvHandler(stream)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions pkg/sync/pleasesync.go
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/sha1"
"log"
"os"
"reflect"
"time"

"github.com/quic-s/quics-client/pkg/db/badger"
Expand Down Expand Up @@ -175,6 +176,9 @@ func PSwhenRemove(path string) {

// Update Sync Timestamp and hash as same as update Timestamp and hash
prevSyncMetadata := badger.GetSyncMetadata(path)
if reflect.ValueOf(prevSyncMetadata).IsZero() {
return
}

syncMetadata := types.SyncMetadata{
BeforePath: BeforePath,
Expand Down
1 change: 0 additions & 1 deletion pkg/sync/reconnect.go

This file was deleted.

21 changes: 19 additions & 2 deletions pkg/sync/registration.go
Expand Up @@ -15,9 +15,9 @@ import (

"github.com/quic-s/quics-client/pkg/db/badger"
"github.com/quic-s/quics-client/pkg/net/qclient"
"github.com/quic-s/quics-client/pkg/utils"
"github.com/quic-s/quics-client/pkg/viper"
qp "github.com/quic-s/quics-protocol"
"github.com/quic-s/quics-protocol/pkg/stream"
qstypes "github.com/quic-s/quics/pkg/types"
)

Expand Down Expand Up @@ -55,11 +55,28 @@ func ClientRegistration(ClientPassword string, SIP string, SPort string) error {
return nil
}

func CheckInternetConnection() bool {
err := Conn.OpenTransaction("PING", func(stream *stream.Stream, transactionName string, transactionID []byte) error {
res, err := qclient.SendPing(stream, badger.GetUUID())
if err != nil {
return err
}
if reflect.ValueOf(res).IsZero() {
return fmt.Errorf("ping do not success")
}
return nil
})
if err != nil {
return false
}
return true
}

func Reconnect() {
prevStatus := true
for {

isOnline := utils.CheckInternetConnection()
isOnline := CheckInternetConnection()
if !prevStatus && isOnline {
ip := viper.GetViperEnvVariables("QUICS_SERVER_IP")
if ip == "" {
Expand Down
4 changes: 3 additions & 1 deletion pkg/sync/setup.go
Expand Up @@ -29,10 +29,12 @@ func init() {
MustSyncMain()
ForceSyncMain()
FullScanMain()
NeedContentMain()

}
func InitQPClient() {
newClient, err := qp.New(qp.LOG_LEVEL_INFO)
//newClient, err := qp.New(qp.LOG_LEVEL_INFO)
newClient, err := qp.New(qp.LOG_LEVEL_ERROR)
if err != nil {
panic(err)
}
Expand Down
19 changes: 0 additions & 19 deletions pkg/utils/checkConn.go

This file was deleted.

26 changes: 0 additions & 26 deletions pkg/utils/getinfo.go

This file was deleted.

0 comments on commit 6591bd2

Please sign in to comment.