Merge pull request #30 from marcossegovia/master

Break from for loop if time exceeds an hour
This commit is contained in:
Zack 2017-10-21 12:25:35 -06:00 committed by GitHub
commit 3f34717a61
2 changed files with 19 additions and 3 deletions

View File

@ -166,6 +166,7 @@ func (c *Connection) runClient() error {
if !c.Debug {
c.bars = make([]*uiprogress.Bar, c.NumberOfConnections)
}
gotTimeout := false
gotOK := false
gotResponse := false
gotConnectionInUse := false
@ -201,8 +202,13 @@ func (c *Connection) runClient() error {
if c.IsSender { // this is a sender
logger.Debug("waiting for ok from relay")
message = receiveMessage(connection)
if message == "timeout" {
gotTimeout = true
fmt.Println("You've just exceeded limit waiting time.")
return
}
if message == "no" {
if id == 0 {
if id == 0 {
fmt.Println("The specifed code is already in use by a sender.")
}
gotConnectionInUse = true
@ -223,7 +229,7 @@ func (c *Connection) runClient() error {
logger.Debug("waiting for meta data from sender")
message = receiveMessage(connection)
if message == "no" {
if id == 0 {
if id == 0 {
fmt.Println("The specifed code is already in use by a sender.")
}
gotConnectionInUse = true
@ -291,7 +297,10 @@ func (c *Connection) runClient() error {
}
if c.IsSender {
// TODO: Add confirmation
if gotTimeout {
fmt.Println("Timeout waiting for receiver")
return nil
}
fmt.Println("\nFile sent.")
} else { // Is a Receiver
if notPresent {

View File

@ -12,6 +12,7 @@ import (
)
const MAX_NUMBER_THREADS = 8
const CONNECTION_TIMEOUT = time.Hour
type connectionMap struct {
receiver map[string]net.Conn
@ -135,7 +136,12 @@ func (r *Relay) clientCommuncation(id int, connection net.Conn) {
r.connections.Unlock()
// wait for receiver
receiversAddress := ""
isTimeout := time.Duration(0)
for {
if CONNECTION_TIMEOUT <= isTimeout {
sendMessage("timeout", connection)
break
}
r.connections.RLock()
if _, ok := r.connections.receiver[key]; ok {
receiversAddress = r.connections.receiver[key].RemoteAddr().String()
@ -145,6 +151,7 @@ func (r *Relay) clientCommuncation(id int, connection net.Conn) {
}
r.connections.RUnlock()
time.Sleep(100 * time.Millisecond)
isTimeout += 100 * time.Millisecond
}
logger.Debug("telling sender ok")
sendMessage(receiversAddress, connection)