mirror of
https://github.com/schollz/croc.git
synced 2025-10-11 21:30:16 +02:00
add debug and progressbar
This commit is contained in:
parent
4d8041a69f
commit
c45c7b00d7
5 changed files with 52 additions and 25 deletions
1
go.sum
1
go.sum
|
@ -98,7 +98,6 @@ github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
|
||||||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/schollz/croc v3.0.6+incompatible h1:rCfc8MGgcGjNW2/qSoulPh8CRGH+Ej4i3RWYOwhX9pE=
|
|
||||||
github.com/schollz/mnemonicode v1.0.1 h1:LiH5hwADZwjwnfXsaD4xgnMyTAtaKHN+e5AyjRU6WSU=
|
github.com/schollz/mnemonicode v1.0.1 h1:LiH5hwADZwjwnfXsaD4xgnMyTAtaKHN+e5AyjRU6WSU=
|
||||||
github.com/schollz/mnemonicode v1.0.1/go.mod h1:cl4UAOhUV0mkdjMj/QYaUZbZZdF8BnOqoz8rHMzwboY=
|
github.com/schollz/mnemonicode v1.0.1/go.mod h1:cl4UAOhUV0mkdjMj/QYaUZbZZdF8BnOqoz8rHMzwboY=
|
||||||
github.com/schollz/pake v1.1.0 h1:+tYqsPVkuirFpmeRePjYTUhIHHKLufdmd7QfuspaXCk=
|
github.com/schollz/pake v1.1.0 h1:+tYqsPVkuirFpmeRePjYTUhIHHKLufdmd7QfuspaXCk=
|
||||||
|
|
|
@ -19,9 +19,9 @@ import (
|
||||||
"github.com/mattn/go-colorable"
|
"github.com/mattn/go-colorable"
|
||||||
"github.com/pions/webrtc"
|
"github.com/pions/webrtc"
|
||||||
"github.com/schollz/croc/v5/src/utils"
|
"github.com/schollz/croc/v5/src/utils"
|
||||||
common "github.com/schollz/croc/v5/src/webrtc/pkg/session/common"
|
"github.com/schollz/croc/v5/src/webrtc/pkg/session/common"
|
||||||
recvSess "github.com/schollz/croc/v5/src/webrtc/pkg/session/receiver"
|
"github.com/schollz/croc/v5/src/webrtc/pkg/session/receiver"
|
||||||
sendSess "github.com/schollz/croc/v5/src/webrtc/pkg/session/sender"
|
"github.com/schollz/croc/v5/src/webrtc/pkg/session/sender"
|
||||||
"github.com/schollz/pake"
|
"github.com/schollz/pake"
|
||||||
"github.com/schollz/progressbar/v2"
|
"github.com/schollz/progressbar/v2"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
@ -36,6 +36,8 @@ func init() {
|
||||||
log.SetFormatter(&logrus.TextFormatter{ForceColors: true})
|
log.SetFormatter(&logrus.TextFormatter{ForceColors: true})
|
||||||
log.SetOutput(colorable.NewColorableStdout())
|
log.SetOutput(colorable.NewColorableStdout())
|
||||||
log.SetLevel(logrus.DebugLevel)
|
log.SetLevel(logrus.DebugLevel)
|
||||||
|
receiver.Debug()
|
||||||
|
sender.Debug()
|
||||||
}
|
}
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
|
@ -61,8 +63,8 @@ type Client struct {
|
||||||
CurrentFile *os.File
|
CurrentFile *os.File
|
||||||
CurrentFileChunks []int64
|
CurrentFileChunks []int64
|
||||||
|
|
||||||
sendSess *sendSess.Session
|
sendSess *sender.Session
|
||||||
recvSess *recvSess.Session
|
recvSess *receiver.Session
|
||||||
|
|
||||||
// channel data
|
// channel data
|
||||||
incomingMessageChannel <-chan *redis.Message
|
incomingMessageChannel <-chan *redis.Message
|
||||||
|
@ -524,7 +526,7 @@ func (c *Client) updateState() (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) dataChannelReceive() (err error) {
|
func (c *Client) dataChannelReceive() (err error) {
|
||||||
c.recvSess = recvSess.NewWith(recvSess.Config{})
|
c.recvSess = receiver.NewWith(receiver.Config{})
|
||||||
err = c.recvSess.CreateConnection()
|
err = c.recvSess.CreateConnection()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
|
@ -534,7 +536,7 @@ func (c *Client) dataChannelReceive() (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) dataChannelSend() (err error) {
|
func (c *Client) dataChannelSend() (err error) {
|
||||||
c.sendSess = sendSess.NewWith(sendSess.Config{
|
c.sendSess = sender.NewWith(sender.Config{
|
||||||
Configuration: common.Configuration{
|
Configuration: common.Configuration{
|
||||||
OnCompletion: func() {
|
OnCompletion: func() {
|
||||||
},
|
},
|
||||||
|
|
|
@ -7,6 +7,6 @@ import (
|
||||||
|
|
||||||
func (s *Session) onConnectionStateChange() func(connectionState webrtc.ICEConnectionState) {
|
func (s *Session) onConnectionStateChange() func(connectionState webrtc.ICEConnectionState) {
|
||||||
return func(connectionState webrtc.ICEConnectionState) {
|
return func(connectionState webrtc.ICEConnectionState) {
|
||||||
log.Infof("ICE Connection State has changed: %s\n", connectionState.String())
|
log.Debugf("ICE Connection State has changed: %s\n", connectionState.String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,12 +5,25 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/mattn/go-colorable"
|
||||||
"github.com/pion/webrtc/v2"
|
"github.com/pion/webrtc/v2"
|
||||||
internalSess "github.com/schollz/croc/v5/src/webrtc/internal/session"
|
internalSess "github.com/schollz/croc/v5/src/webrtc/internal/session"
|
||||||
"github.com/schollz/croc/v5/src/webrtc/pkg/session/common"
|
"github.com/schollz/croc/v5/src/webrtc/pkg/session/common"
|
||||||
log "github.com/sirupsen/logrus"
|
logrus "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var log = logrus.New()
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
log.SetFormatter(&logrus.TextFormatter{ForceColors: true})
|
||||||
|
log.SetOutput(colorable.NewColorableStdout())
|
||||||
|
log.SetLevel(logrus.WarnLevel)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Debug() {
|
||||||
|
log.SetLevel(logrus.DebugLevel)
|
||||||
|
}
|
||||||
|
|
||||||
// Session is a receiver session
|
// Session is a receiver session
|
||||||
type Session struct {
|
type Session struct {
|
||||||
sess internalSess.Session
|
sess internalSess.Session
|
||||||
|
@ -44,7 +57,7 @@ func NewWith(c Config) *Session {
|
||||||
|
|
||||||
func (s *Session) onConnectionStateChange() func(connectionState webrtc.ICEConnectionState) {
|
func (s *Session) onConnectionStateChange() func(connectionState webrtc.ICEConnectionState) {
|
||||||
return func(connectionState webrtc.ICEConnectionState) {
|
return func(connectionState webrtc.ICEConnectionState) {
|
||||||
log.Infof("ICE Connection State has changed: %s\n", connectionState.String())
|
log.Debugf("ICE Connection State has changed: %s\n", connectionState.String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -111,14 +124,14 @@ func (s *Session) ReceiveData(pathToFile string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) receiveData(pathToFile string) error {
|
func (s *Session) receiveData(pathToFile string) error {
|
||||||
log.Infoln("Starting to receive data...")
|
log.Debugln("Starting to receive data...")
|
||||||
log.Infof("receiving %s", pathToFile)
|
log.Debugf("receiving %s", pathToFile)
|
||||||
f, err := os.OpenFile(pathToFile, os.O_RDWR|os.O_CREATE, 0755)
|
f, err := os.OpenFile(pathToFile, os.O_RDWR|os.O_CREATE, 0755)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
log.Infoln("Stopped receiving data...")
|
log.Debugln("Stopped receiving data...")
|
||||||
f.Close()
|
f.Close()
|
||||||
}()
|
}()
|
||||||
// Consume the message channel, until done
|
// Consume the message channel, until done
|
||||||
|
|
|
@ -5,12 +5,14 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
colorable "github.com/mattn/go-colorable"
|
colorable "github.com/mattn/go-colorable"
|
||||||
"github.com/pion/webrtc/v2"
|
"github.com/pion/webrtc/v2"
|
||||||
internalSess "github.com/schollz/croc/v5/src/webrtc/internal/session"
|
internalSess "github.com/schollz/croc/v5/src/webrtc/internal/session"
|
||||||
"github.com/schollz/croc/v5/src/webrtc/pkg/session/common"
|
"github.com/schollz/croc/v5/src/webrtc/pkg/session/common"
|
||||||
"github.com/schollz/croc/v5/src/webrtc/pkg/stats"
|
"github.com/schollz/croc/v5/src/webrtc/pkg/stats"
|
||||||
|
"github.com/schollz/progressbar/v2"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -25,6 +27,10 @@ var log = logrus.New()
|
||||||
func init() {
|
func init() {
|
||||||
log.SetFormatter(&logrus.TextFormatter{ForceColors: true})
|
log.SetFormatter(&logrus.TextFormatter{ForceColors: true})
|
||||||
log.SetOutput(colorable.NewColorableStdout())
|
log.SetOutput(colorable.NewColorableStdout())
|
||||||
|
log.SetLevel(logrus.WarnLevel)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Debug() {
|
||||||
log.SetLevel(logrus.DebugLevel)
|
log.SetLevel(logrus.DebugLevel)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,6 +55,7 @@ type Session struct {
|
||||||
|
|
||||||
// Stats/infos
|
// Stats/infos
|
||||||
readingStats *stats.Stats
|
readingStats *stats.Stats
|
||||||
|
bar *progressbar.ProgressBar
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new sender session
|
// New creates a new sender session
|
||||||
|
@ -165,12 +172,20 @@ func (s *Session) readFile(pathToFile string) error {
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
log.Infof("Starting to read data from '%s'", pathToFile)
|
stat, _ := f.Stat()
|
||||||
|
s.bar = progressbar.NewOptions64(
|
||||||
|
stat.Size(),
|
||||||
|
progressbar.OptionSetRenderBlankState(true),
|
||||||
|
progressbar.OptionSetBytes64(stat.Size()),
|
||||||
|
progressbar.OptionSetWriter(os.Stderr),
|
||||||
|
progressbar.OptionThrottle(1/60*time.Second),
|
||||||
|
)
|
||||||
|
log.Debugf("Starting to read data from '%s'", pathToFile)
|
||||||
s.readingStats.Start()
|
s.readingStats.Start()
|
||||||
defer func() {
|
defer func() {
|
||||||
f.Close()
|
f.Close()
|
||||||
s.readingStats.Pause()
|
s.readingStats.Pause()
|
||||||
log.Infof("Stopped reading data...")
|
log.Debugf("Stopped reading data...")
|
||||||
close(s.output)
|
close(s.output)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
@ -210,8 +225,8 @@ func (s *Session) onBufferedAmountLow() func() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
currentSpeed := s.sess.NetworkStats.Bandwidth()
|
// currentSpeed := s.sess.NetworkStats.Bandwidth()
|
||||||
fmt.Printf("Transferring at %.2f MB/s\r", currentSpeed)
|
// fmt.Printf("Transferring at %.2f MB/s\r", currentSpeed)
|
||||||
|
|
||||||
for len(s.msgToBeSent) != 0 {
|
for len(s.msgToBeSent) != 0 {
|
||||||
cur := s.msgToBeSent[0]
|
cur := s.msgToBeSent[0]
|
||||||
|
@ -221,6 +236,7 @@ func (s *Session) onBufferedAmountLow() func() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
s.sess.NetworkStats.AddBytes(uint64(cur.n))
|
s.sess.NetworkStats.AddBytes(uint64(cur.n))
|
||||||
|
s.bar.Add(cur.n)
|
||||||
s.msgToBeSent = s.msgToBeSent[1:]
|
s.msgToBeSent = s.msgToBeSent[1:]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -228,13 +244,10 @@ func (s *Session) onBufferedAmountLow() func() {
|
||||||
|
|
||||||
func (s *Session) writeToNetwork() {
|
func (s *Session) writeToNetwork() {
|
||||||
// Set callback, as transfer may be paused
|
// Set callback, as transfer may be paused
|
||||||
fmt.Println("\nwriting")
|
|
||||||
s.dataChannel.OnBufferedAmountLow(s.onBufferedAmountLow())
|
s.dataChannel.OnBufferedAmountLow(s.onBufferedAmountLow())
|
||||||
fmt.Println("\ndone")
|
|
||||||
<-s.stopSending
|
<-s.stopSending
|
||||||
fmt.Println("\nstopped sending")
|
|
||||||
s.dataChannel.OnBufferedAmountLow(nil)
|
s.dataChannel.OnBufferedAmountLow(nil)
|
||||||
log.Infof("Pausing network I/O... (remaining at least %v packets)\n", len(s.output))
|
log.Debugf("Pausing network I/O... (remaining at least %v packets)\n", len(s.output))
|
||||||
s.sess.NetworkStats.Pause()
|
s.sess.NetworkStats.Pause()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -244,7 +257,7 @@ func (s *Session) StopSending() {
|
||||||
|
|
||||||
func (s *Session) onConnectionStateChange() func(connectionState webrtc.ICEConnectionState) {
|
func (s *Session) onConnectionStateChange() func(connectionState webrtc.ICEConnectionState) {
|
||||||
return func(connectionState webrtc.ICEConnectionState) {
|
return func(connectionState webrtc.ICEConnectionState) {
|
||||||
log.Infof("ICE Connection State has changed: %s\n", connectionState.String())
|
log.Debugf("ICE Connection State has changed: %s\n", connectionState.String())
|
||||||
if connectionState == webrtc.ICEConnectionStateDisconnected {
|
if connectionState == webrtc.ICEConnectionStateDisconnected {
|
||||||
s.StopSending()
|
s.StopSending()
|
||||||
}
|
}
|
||||||
|
@ -255,8 +268,8 @@ func (s *Session) onOpenHandler() func() {
|
||||||
return func() {
|
return func() {
|
||||||
s.sess.NetworkStats.Start()
|
s.sess.NetworkStats.Start()
|
||||||
|
|
||||||
log.Infof("Starting to send data...")
|
log.Debugf("Starting to send data...")
|
||||||
defer log.Infof("Stopped sending data...")
|
defer log.Debugf("Stopped sending data...")
|
||||||
|
|
||||||
s.writeToNetwork()
|
s.writeToNetwork()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue