0
0
Fork 0
mirror of https://github.com/schollz/croc.git synced 2025-10-11 13:21:00 +02:00

add debug and progressbar

This commit is contained in:
Zack Scholl 2019-04-10 09:03:30 -07:00
parent 4d8041a69f
commit c45c7b00d7
5 changed files with 52 additions and 25 deletions

1
go.sum
View file

@ -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/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
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/go.mod h1:cl4UAOhUV0mkdjMj/QYaUZbZZdF8BnOqoz8rHMzwboY=
github.com/schollz/pake v1.1.0 h1:+tYqsPVkuirFpmeRePjYTUhIHHKLufdmd7QfuspaXCk=

View file

@ -19,9 +19,9 @@ import (
"github.com/mattn/go-colorable"
"github.com/pions/webrtc"
"github.com/schollz/croc/v5/src/utils"
common "github.com/schollz/croc/v5/src/webrtc/pkg/session/common"
recvSess "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/common"
"github.com/schollz/croc/v5/src/webrtc/pkg/session/receiver"
"github.com/schollz/croc/v5/src/webrtc/pkg/session/sender"
"github.com/schollz/pake"
"github.com/schollz/progressbar/v2"
"github.com/sirupsen/logrus"
@ -36,6 +36,8 @@ func init() {
log.SetFormatter(&logrus.TextFormatter{ForceColors: true})
log.SetOutput(colorable.NewColorableStdout())
log.SetLevel(logrus.DebugLevel)
receiver.Debug()
sender.Debug()
}
type Client struct {
@ -61,8 +63,8 @@ type Client struct {
CurrentFile *os.File
CurrentFileChunks []int64
sendSess *sendSess.Session
recvSess *recvSess.Session
sendSess *sender.Session
recvSess *receiver.Session
// channel data
incomingMessageChannel <-chan *redis.Message
@ -524,7 +526,7 @@ func (c *Client) updateState() (err error) {
}
func (c *Client) dataChannelReceive() (err error) {
c.recvSess = recvSess.NewWith(recvSess.Config{})
c.recvSess = receiver.NewWith(receiver.Config{})
err = c.recvSess.CreateConnection()
if err != nil {
return
@ -534,7 +536,7 @@ func (c *Client) dataChannelReceive() (err error) {
}
func (c *Client) dataChannelSend() (err error) {
c.sendSess = sendSess.NewWith(sendSess.Config{
c.sendSess = sender.NewWith(sender.Config{
Configuration: common.Configuration{
OnCompletion: func() {
},

View file

@ -7,6 +7,6 @@ import (
func (s *Session) onConnectionStateChange() 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())
}
}

View file

@ -5,12 +5,25 @@ import (
"io"
"os"
"github.com/mattn/go-colorable"
"github.com/pion/webrtc/v2"
internalSess "github.com/schollz/croc/v5/src/webrtc/internal/session"
"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
type Session struct {
sess internalSess.Session
@ -44,7 +57,7 @@ func NewWith(c Config) *Session {
func (s *Session) onConnectionStateChange() 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 {
log.Infoln("Starting to receive data...")
log.Infof("receiving %s", pathToFile)
log.Debugln("Starting to receive data...")
log.Debugf("receiving %s", pathToFile)
f, err := os.OpenFile(pathToFile, os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
return err
}
defer func() {
log.Infoln("Stopped receiving data...")
log.Debugln("Stopped receiving data...")
f.Close()
}()
// Consume the message channel, until done

View file

@ -5,12 +5,14 @@ import (
"io"
"os"
"sync"
"time"
colorable "github.com/mattn/go-colorable"
"github.com/pion/webrtc/v2"
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/stats"
"github.com/schollz/progressbar/v2"
"github.com/sirupsen/logrus"
)
@ -25,6 +27,10 @@ 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)
}
@ -49,6 +55,7 @@ type Session struct {
// Stats/infos
readingStats *stats.Stats
bar *progressbar.ProgressBar
}
// New creates a new sender session
@ -165,12 +172,20 @@ func (s *Session) readFile(pathToFile string) error {
log.Error(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()
defer func() {
f.Close()
s.readingStats.Pause()
log.Infof("Stopped reading data...")
log.Debugf("Stopped reading data...")
close(s.output)
}()
@ -210,8 +225,8 @@ func (s *Session) onBufferedAmountLow() func() {
return
}
currentSpeed := s.sess.NetworkStats.Bandwidth()
fmt.Printf("Transferring at %.2f MB/s\r", currentSpeed)
// currentSpeed := s.sess.NetworkStats.Bandwidth()
// fmt.Printf("Transferring at %.2f MB/s\r", currentSpeed)
for len(s.msgToBeSent) != 0 {
cur := s.msgToBeSent[0]
@ -221,6 +236,7 @@ func (s *Session) onBufferedAmountLow() func() {
return
}
s.sess.NetworkStats.AddBytes(uint64(cur.n))
s.bar.Add(cur.n)
s.msgToBeSent = s.msgToBeSent[1:]
}
}
@ -228,13 +244,10 @@ func (s *Session) onBufferedAmountLow() func() {
func (s *Session) writeToNetwork() {
// Set callback, as transfer may be paused
fmt.Println("\nwriting")
s.dataChannel.OnBufferedAmountLow(s.onBufferedAmountLow())
fmt.Println("\ndone")
<-s.stopSending
fmt.Println("\nstopped sending")
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()
}
@ -244,7 +257,7 @@ func (s *Session) StopSending() {
func (s *Session) onConnectionStateChange() 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 {
s.StopSending()
}
@ -255,8 +268,8 @@ func (s *Session) onOpenHandler() func() {
return func() {
s.sess.NetworkStats.Start()
log.Infof("Starting to send data...")
defer log.Infof("Stopped sending data...")
log.Debugf("Starting to send data...")
defer log.Debugf("Stopped sending data...")
s.writeToNetwork()
}