Skip to content

Commit fc1ca0d

Browse files
committed
make connection public on receive
Signed-off-by: Ghanshyam Thakkar <shyamthakkar001@gmail.com>
1 parent ddc4c4e commit fc1ca0d

File tree

6 files changed

+26
-26
lines changed

6 files changed

+26
-26
lines changed

cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var rootCmd = &cobra.Command{
1313
Use: "fs-cli",
1414
Short: "Peer-to-Peer filesharing CLI application",
1515
Long: `A Peer-to-Peer multi-threaded filesharing CLI app using WebRTC.`,
16-
Version: "v0.5.1",
16+
Version: "v0.5.2",
1717
}
1818

1919
// Execute adds all child commands to the root command and sets flags appropriately.

session/receive/handlers.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
// Handle all the listeners.
1515
func (s *Session) HandleState() {
1616
//print the state change
17-
s.peerConnection.OnICEConnectionStateChange(func(state webrtc.ICEConnectionState) {
17+
s.PeerConnection.OnICEConnectionStateChange(func(state webrtc.ICEConnectionState) {
1818
if state == webrtc.ICEConnectionStateFailed {
1919
fmt.Printf("\nICE Connection State has changed: %s\n\n", state.String())
2020
s.done <- struct{}{}
@@ -27,7 +27,7 @@ func (s *Session) HandleState() {
2727
})
2828

2929
//On new DataChannel being created by sender.
30-
s.peerConnection.OnDataChannel(func(dc *webrtc.DataChannel) {
30+
s.PeerConnection.OnDataChannel(func(dc *webrtc.DataChannel) {
3131
if dc.Label() == "control" {
3232
s.controlChannel = dc
3333
//add listeners to control channel.
@@ -62,7 +62,7 @@ func (s *Session) HandleState() {
6262
s.Channels[i].File, err = os.OpenFile(s.Channels[i].Name, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
6363

6464
//increment the channelsDone counter
65-
//atomic write is used avoid race conditions due to multiple channels being intiallized at once.
65+
//atomic write is used avoid race conditions due to multiple channels being initiallized at once.
6666
atomic.AddInt32(&s.channelsDone, 1)
6767
if err != nil {
6868
panic(err)

session/receive/methods.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ func (s *Session) CreateConnection() error {
2626
if err != nil {
2727
return err
2828
}
29-
s.peerConnection = peerConnection
30-
s.gatherDone = webrtc.GatheringCompletePromise(s.peerConnection)
29+
s.PeerConnection = peerConnection
30+
s.gatherDone = webrtc.GatheringCompletePromise(s.PeerConnection)
3131
s.HandleState()
3232
return nil
3333
}
@@ -62,13 +62,13 @@ func (s *Session) transfer() {
6262
mpb.PrependDecorators(
6363
decor.Name(fmt.Sprintf("Receiving '%s': ", s.Channels[i].Name), decor.WCSyncSpaceR),
6464

65-
//Make the size counter disapper on completion.
65+
//Make the size counter disappear on completion.
6666
// decor.OnComplete(decor.Counters(decor.SizeB1024(0), "% .2f / % .2f", decor.WCSyncSpaceR), ""),
6767

6868
//display the received amount
6969
//decor.SizeB1024 converts the amount into appropriate units of data (KiB,MiB,Gib)
7070
decor.OnComplete(decor.Any(func(st decor.Statistics) string {
71-
stats, _ := s.peerConnection.GetStats().GetDataChannelStats(doc.DC)
71+
stats, _ := s.PeerConnection.GetStats().GetDataChannelStats(doc.DC)
7272
return fmt.Sprintf("% .2f ", decor.SizeB1024(int64(stats.BytesReceived)))
7373
}, decor.WCSyncSpaceR), ""),
7474

@@ -78,7 +78,7 @@ func (s *Session) transfer() {
7878
period := float64(time.Now().UnixMilli()-doc.StartTime) / 1000.0
7979

8080
// If the clients are disconnected, do not update speed.
81-
if s.peerConnection.ICEConnectionState() == webrtc.ICEConnectionStateDisconnected {
81+
if s.PeerConnection.ICEConnectionState() == webrtc.ICEConnectionStateDisconnected {
8282
return fmt.Sprintf("%.2f MiB/s", 0.0)
8383
}
8484
return fmt.Sprintf("%.2f MiB/s", amount/period)
@@ -149,24 +149,24 @@ func (s *Session) fileWrite(proxyWriter io.WriteCloser, wg *sync.WaitGroup, i in
149149

150150
func (s *Session) GenSDP(offer webrtc.SessionDescription) (string, error) {
151151
var sdp string
152-
err := s.peerConnection.SetRemoteDescription(offer)
152+
err := s.PeerConnection.SetRemoteDescription(offer)
153153
if err != nil {
154154
return sdp, err
155155
}
156156

157-
answer, err := s.peerConnection.CreateAnswer(nil)
157+
answer, err := s.PeerConnection.CreateAnswer(nil)
158158
if err != nil {
159159
return sdp, err
160160
}
161161

162-
err = s.peerConnection.SetLocalDescription(answer)
162+
err = s.PeerConnection.SetLocalDescription(answer)
163163
if err != nil {
164164
return sdp, err
165165
}
166166
<-s.gatherDone
167167

168168
//Encode the SDP to base64
169-
sdp, err = lib.Encode(s.peerConnection.LocalDescription())
169+
sdp, err = lib.Encode(s.PeerConnection.LocalDescription())
170170
return sdp, err
171171
}
172172

session/receive/session.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
// Receiver's session struct to manage Datachannels, PeerConnection, Go Signaling Channels etc.
99
type Session struct {
10-
peerConnection *webrtc.PeerConnection
10+
PeerConnection *webrtc.PeerConnection
1111

1212
controlChannel *webrtc.DataChannel //handling consent and metadata
1313
Channels []struct {

session/send/handlers.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616

1717
// Prints the state change.
1818
func (s *Session) handleState() {
19-
s.peerConnection.OnICEConnectionStateChange(func(state webrtc.ICEConnectionState) {
19+
s.PeerConnection.OnICEConnectionStateChange(func(state webrtc.ICEConnectionState) {
2020
if state != webrtc.ICEConnectionStateClosed {
2121
fmt.Printf("\nICE Connection State has changed: %s\n\n", state.String())
2222
}
@@ -75,7 +75,7 @@ func (s *Session) handleopen() func() {
7575
//display the sent amount
7676
//decor.SizeB1024 converts the amount into appropriate units of data (KiB,MiB,Gib)
7777
decor.OnComplete(decor.Any(func(st decor.Statistics) string {
78-
stats, _ := s.peerConnection.GetStats().GetDataChannelStats(doc.DC)
78+
stats, _ := s.PeerConnection.GetStats().GetDataChannelStats(doc.DC)
7979
return fmt.Sprintf("% .2f ", decor.SizeB1024(int64(stats.BytesSent-doc.DC.BufferedAmount())))
8080
}, decor.WCSyncSpaceR), ""),
8181

@@ -85,7 +85,7 @@ func (s *Session) handleopen() func() {
8585
period := float64(time.Now().UnixMilli()-doc.StartTime) / 1000.0
8686

8787
//If the clients are disconnected, do not update the speed.
88-
if s.peerConnection.ICEConnectionState() == webrtc.ICEConnectionStateDisconnected {
88+
if s.PeerConnection.ICEConnectionState() == webrtc.ICEConnectionStateDisconnected {
8989
return fmt.Sprintf("%.2f MiB/s", 0.0)
9090
}
9191
return fmt.Sprintf("%.2f MiB/s", amount/period)
@@ -176,7 +176,7 @@ func (s *Session) close(closehandler bool) {
176176
}
177177
}
178178
s.controlChannel.Close()
179-
err := s.peerConnection.Close()
179+
err := s.PeerConnection.Close()
180180
if err != nil {
181181
panic(err)
182182
}

session/send/methods.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (s *Session) SetupConnection(paths []string) error {
3232

3333
// Connects clients.
3434
func (s *Session) Connect(answer webrtc.SessionDescription) error {
35-
err := s.peerConnection.SetRemoteDescription(answer)
35+
err := s.PeerConnection.SetRemoteDescription(answer)
3636
if err != nil {
3737
return err
3838
}
@@ -55,7 +55,7 @@ func (s *Session) createConnection() error {
5555
if err != nil {
5656
return err
5757
}
58-
s.peerConnection = peerConnection
58+
s.PeerConnection = peerConnection
5959
s.handleState()
6060
return nil
6161
}
@@ -94,7 +94,7 @@ func (s *Session) createTransferChannel(path string, i int) error {
9494
//mplt means MaxPacketLifeTime.
9595
//It is the time in Miliseconds during which if the sender does not receive acknowledgement of the packet, it will retransmit.
9696
mplt := uint16(5000)
97-
s.channels[i].DC, err = s.peerConnection.CreateDataChannel(fmt.Sprintf("dc%d", i), &webrtc.DataChannelInit{
97+
s.channels[i].DC, err = s.PeerConnection.CreateDataChannel(fmt.Sprintf("dc%d", i), &webrtc.DataChannelInit{
9898
Ordered: &ordered,
9999
MaxPacketLifeTime: &mplt,
100100
})
@@ -129,7 +129,7 @@ func (s *Session) createTransferChannel(path string, i int) error {
129129
func (s *Session) createControlChannel() error {
130130
ordered := true
131131
mplt := uint16(5000)
132-
channel, err := s.peerConnection.CreateDataChannel("control", &webrtc.DataChannelInit{
132+
channel, err := s.PeerConnection.CreateDataChannel("control", &webrtc.DataChannelInit{
133133
Ordered: &ordered,
134134
MaxPacketLifeTime: &mplt,
135135
})
@@ -160,14 +160,14 @@ func (s *Session) createControlChannel() error {
160160
}
161161

162162
func (s *Session) GenOffer() (string, error) {
163-
offer, err := s.peerConnection.CreateOffer(nil)
163+
offer, err := s.PeerConnection.CreateOffer(nil)
164164
if err != nil {
165165
return "", err
166166
}
167-
s.gatherDone = webrtc.GatheringCompletePromise(s.peerConnection)
168-
err = s.peerConnection.SetLocalDescription(offer)
167+
s.gatherDone = webrtc.GatheringCompletePromise(s.PeerConnection)
168+
err = s.PeerConnection.SetLocalDescription(offer)
169169
<-s.gatherDone
170-
offer2 := s.peerConnection.LocalDescription()
170+
offer2 := s.PeerConnection.LocalDescription()
171171

172172
if err != nil {
173173
return "", err

0 commit comments

Comments
 (0)