more logic implemented
This commit is contained in:
parent
4098b47f41
commit
847f71d914
@ -43,7 +43,8 @@ func (cpc *ClientPeerConn) RunTask(wg *sync.WaitGroup) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//done:
|
cpc.route.cts.psc.Send(MakePeerStoppedPacket(cpc.route.id, cpc.conn_id)) // nothing much to do upon failure. no error check here
|
||||||
|
|
||||||
cpc.ReqStop()
|
cpc.ReqStop()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
32
client.go
32
client.go
@ -6,6 +6,7 @@ import "context"
|
|||||||
import "crypto/tls"
|
import "crypto/tls"
|
||||||
import "crypto/x509"
|
import "crypto/x509"
|
||||||
import "encoding/json"
|
import "encoding/json"
|
||||||
|
import "errors"
|
||||||
import "fmt"
|
import "fmt"
|
||||||
import "io"
|
import "io"
|
||||||
import "log"
|
import "log"
|
||||||
@ -164,8 +165,8 @@ func (r* ClientRoute) ConnectToPeer(pts_id uint32) {
|
|||||||
|
|
||||||
d.LocalAddr = nil // TOOD: use this if local address is specified
|
d.LocalAddr = nil // TOOD: use this if local address is specified
|
||||||
conn, err = d.DialContext(ctx, "tcp", r.peer_addr.String());
|
conn, err = d.DialContext(ctx, "tcp", r.peer_addr.String());
|
||||||
//conn, err = net.DialTCP("tcp", nil, r.peer_addr);
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
// TODO: make send peer started failure mesage?
|
||||||
fmt.Printf ("failed to connect to %s - %s\n", r.peer_addr.String(), err.Error())
|
fmt.Printf ("failed to connect to %s - %s\n", r.peer_addr.String(), err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -173,11 +174,18 @@ func (r* ClientRoute) ConnectToPeer(pts_id uint32) {
|
|||||||
ptc, err = r.AddNewClientPeerConn(conn)
|
ptc, err = r.AddNewClientPeerConn(conn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO: logging
|
// TODO: logging
|
||||||
|
// TODO: make send peer started failure mesage?
|
||||||
fmt.Printf("YYYYYYYY - %s\n", err.Error())
|
fmt.Printf("YYYYYYYY - %s\n", err.Error())
|
||||||
conn.Close()
|
conn.Close()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Printf("STARTED NEW SERVER PEER STAK\n")
|
fmt.Printf("STARTED NEW SERVER PEER STAK\n")
|
||||||
|
err = r.cts.psc.Send(MakePeerStartedPacket(r.id, ptc.conn_id))
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("CLOSING NEW SERVER PEER STAK - %s\n", err.Error())
|
||||||
|
conn.Close()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
r.ptc_wg.Add(1)
|
r.ptc_wg.Add(1)
|
||||||
go ptc.RunTask(&r.ptc_wg)
|
go ptc.RunTask(&r.ptc_wg)
|
||||||
@ -188,6 +196,22 @@ func (r* ClientRoute) ReportEvent (pts_id uint32, event_type PACKET_KIND, event_
|
|||||||
case PACKET_KIND_PEER_STARTED:
|
case PACKET_KIND_PEER_STARTED:
|
||||||
r.ConnectToPeer(pts_id)
|
r.ConnectToPeer(pts_id)
|
||||||
|
|
||||||
|
// TODO:
|
||||||
|
// case PACKET_KIND_PEER_STOPPED:
|
||||||
|
// r.DisconnectFromPeer(pts_id)
|
||||||
|
|
||||||
|
case PACKET_KIND_PEER_DATA:
|
||||||
|
var ptc *ClientPeerConn
|
||||||
|
var ok bool
|
||||||
|
var err error
|
||||||
|
ptc, ok = r.ptc_map[pts_id]
|
||||||
|
if ok {
|
||||||
|
_, err = ptc.conn.Write(event_data)
|
||||||
|
return err
|
||||||
|
} else {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: other types
|
// TODO: other types
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -367,12 +391,12 @@ fmt.Printf("[%v]\n", cts.route_map)
|
|||||||
goto done
|
goto done
|
||||||
|
|
||||||
default:
|
default:
|
||||||
// no other case is ready.
|
// no other case is ready. run the code below select.
|
||||||
// without the default case, the select construct would block
|
// without the default case, the select construct would block
|
||||||
}
|
}
|
||||||
|
|
||||||
pkt, err = psc.Recv()
|
pkt, err = psc.Recv()
|
||||||
if err == io.EOF {
|
if errors.Is(err, io.EOF) {
|
||||||
fmt.Printf("server disconnected\n")
|
fmt.Printf("server disconnected\n")
|
||||||
goto reconnect_to_server
|
goto reconnect_to_server
|
||||||
}
|
}
|
||||||
@ -448,12 +472,14 @@ fmt.Printf("[%v]\n", cts.route_map)
|
|||||||
|
|
||||||
case PACKET_KIND_PEER_DATA:
|
case PACKET_KIND_PEER_DATA:
|
||||||
// the connection from the client to a peer has been established
|
// the connection from the client to a peer has been established
|
||||||
|
fmt.Printf ("**** GOT PEER DATA\n")
|
||||||
var x *Packet_Data
|
var x *Packet_Data
|
||||||
var ok bool
|
var ok bool
|
||||||
x, ok = pkt.U.(*Packet_Data)
|
x, ok = pkt.U.(*Packet_Data)
|
||||||
if ok {
|
if ok {
|
||||||
err = cts.ReportEvent(x.Data.RouteId, x.Data.PeerId, PACKET_KIND_PEER_DATA, x.Data.Data)
|
err = cts.ReportEvent(x.Data.RouteId, x.Data.PeerId, PACKET_KIND_PEER_DATA, x.Data.Data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
fmt.Printf ("failed to report event - %s\n", err.Error())
|
||||||
// TODO:
|
// TODO:
|
||||||
} else {
|
} else {
|
||||||
// TODO:
|
// TODO:
|
||||||
|
26
s-peer.go
26
s-peer.go
@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "fmt"
|
import "fmt"
|
||||||
|
import "io"
|
||||||
import "net"
|
import "net"
|
||||||
import "sync/atomic"
|
import "sync/atomic"
|
||||||
import "time"
|
import "time"
|
||||||
@ -43,7 +44,7 @@ func (spc *ServerPeerConn) RunTask() error {
|
|||||||
err = pss.Send(MakePeerStartedPacket(spc.route.id, spc.conn_id))
|
err = pss.Send(MakePeerStartedPacket(spc.route.id, spc.conn_id))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO: include route id and conn id in the error message
|
// TODO: include route id and conn id in the error message
|
||||||
err = fmt.Errorf("unable to send start-pts - %s\n", err.Error())
|
fmt.Printf("unable to send start-pts - %s\n", err.Error())
|
||||||
goto done
|
goto done
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -72,23 +73,27 @@ wait_for_started:
|
|||||||
tmr.Stop()
|
tmr.Stop()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
fmt.Printf("******************* TRYING TO READ...\n")
|
||||||
n, err = spc.conn.Read(buf[:])
|
n, err = spc.conn.Read(buf[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if err != io.EOF {
|
||||||
fmt.Printf("read error - %s\n", err.Error())
|
fmt.Printf("read error - %s\n", err.Error())
|
||||||
break
|
}
|
||||||
|
goto done
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: this needs to be guarded
|
// TODO: this needs to be guarded
|
||||||
err = pss.Send(MakePeerDataPacket(spc.route.id, spc.conn_id, buf[:n]))
|
err = pss.Send(MakePeerDataPacket(spc.route.id, spc.conn_id, buf[:n]))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO: include route id and conn id in the error message
|
// TODO: include route id and conn id in the error message
|
||||||
err = fmt.Errorf("unable to send data - %s\n", err.Error())
|
fmt.Printf("unable to send data - %s\n", err.Error())
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
done:
|
done:
|
||||||
fmt.Printf("spc really ending..................\n")
|
// TODO: inform the client to close peer connection..
|
||||||
|
fmt.Printf("SPC really ending..................\n")
|
||||||
spc.ReqStop()
|
spc.ReqStop()
|
||||||
spc.route.RemoveServerPeerConn(spc)
|
spc.route.RemoveServerPeerConn(spc)
|
||||||
//spc.cts.wg.Done()
|
//spc.cts.wg.Done()
|
||||||
@ -118,16 +123,23 @@ func (spc *ServerPeerConn) ReportEvent (event_type PACKET_KIND, event_data []byt
|
|||||||
|
|
||||||
switch event_type {
|
switch event_type {
|
||||||
case PACKET_KIND_PEER_STARTED:
|
case PACKET_KIND_PEER_STARTED:
|
||||||
|
fmt.Printf("******************* AAAAAAAAAAAAAAAAAAAaaa\n")
|
||||||
if spc.client_peer_opened_received.CompareAndSwap(false, true) {
|
if spc.client_peer_opened_received.CompareAndSwap(false, true) {
|
||||||
spc.client_peer_status_chan <- true
|
spc.client_peer_status_chan <- true
|
||||||
}
|
}
|
||||||
|
|
||||||
case PACKET_KIND_PEER_STOPPED:
|
case PACKET_KIND_PEER_STOPPED:
|
||||||
if spc.client_peer_closed_received.CompareAndSwap(false, true) {
|
fmt.Printf("******************* BBBBBBBBBBBBBBBBBBBBBBBB\n")
|
||||||
spc.client_peer_status_chan <- false
|
//if spc.client_peer_closed_received.CompareAndSwap(false, true) {
|
||||||
}
|
// spc.client_peer_status_chan <- false
|
||||||
|
//}
|
||||||
|
// this event needs to close on the server-side peer connection.
|
||||||
|
// sending false to the client_peer_status_chan isn't good enough to break
|
||||||
|
// the Recv loop in RunTask().
|
||||||
|
spc.ReqStop()
|
||||||
|
|
||||||
case PACKET_KIND_PEER_DATA:
|
case PACKET_KIND_PEER_DATA:
|
||||||
|
fmt.Printf("******************* CCCCCCCCCCCCCCCCCCCCCCCccc\n")
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
_, err = spc.conn.Write(event_data)
|
_, err = spc.conn.Write(event_data)
|
||||||
|
12
server.go
12
server.go
@ -4,12 +4,14 @@ package main
|
|||||||
//import "bytes"
|
//import "bytes"
|
||||||
import "context"
|
import "context"
|
||||||
import "crypto/tls"
|
import "crypto/tls"
|
||||||
|
import "errors"
|
||||||
import "fmt"
|
import "fmt"
|
||||||
import "io"
|
import "io"
|
||||||
import "math/rand"
|
import "math/rand"
|
||||||
import "net"
|
import "net"
|
||||||
import "os"
|
import "os"
|
||||||
import "os/signal"
|
import "os/signal"
|
||||||
|
//import "strings"
|
||||||
import "sync"
|
import "sync"
|
||||||
import "sync/atomic"
|
import "sync/atomic"
|
||||||
import "syscall"
|
import "syscall"
|
||||||
@ -121,7 +123,13 @@ func (r *ServerRoute) RunTask() {
|
|||||||
conn, err = r.l.AcceptTCP()
|
conn, err = r.l.AcceptTCP()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO: logging
|
// TODO: logging
|
||||||
|
//if strings.Contains(err.Error(), "use of closed network connection") {
|
||||||
|
//if err == net.ErrClosed {
|
||||||
|
if errors.Is(err, net.ErrClosed) {
|
||||||
|
fmt.Printf("[%s,%d] END OF TASK...[%#v] [%#v]\n", r.cts.caddr.String(), r.id, err, net.ErrClosed)
|
||||||
|
} else {
|
||||||
fmt.Printf("[%s,%d] accept failure - %s\n", r.cts.caddr.String(), r.id, err.Error())
|
fmt.Printf("[%s,%d] accept failure - %s\n", r.cts.caddr.String(), r.id, err.Error())
|
||||||
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -145,7 +153,7 @@ func (r *ServerRoute) RunTask() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *ServerRoute) StopTask() {
|
func (r *ServerRoute) StopTask() {
|
||||||
fmt.Printf ("stoppping stak..\n")
|
fmt.Printf ("stoppping taak..\n")
|
||||||
// TODO: all pts stop...
|
// TODO: all pts stop...
|
||||||
r.l.Close();
|
r.l.Close();
|
||||||
// TODO: wait??
|
// TODO: wait??
|
||||||
@ -358,7 +366,7 @@ func (s *Server) PacketStream(strm Hodu_PacketStreamServer) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pkt, err = strm.Recv()
|
pkt, err = strm.Recv()
|
||||||
if err == io.EOF {
|
if errors.Is(err, io.EOF) {
|
||||||
// return will close stream from server side
|
// return will close stream from server side
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user