🔷 (File Transferring Utility) Transfer files through the Net 🔷
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

79 lines
2.1 KiB

package main
import (
"flag"
"fmt"
"os"
"strings"
"github.com/Unbewohnte/FTU/receiver"
"github.com/Unbewohnte/FTU/sender"
)
// flags
var PORT *int = flag.Int("port", 8080, "Specifies a port for a sender|port to connect to")
var SENDERADDR *string = flag.String("addr", "", "Specifies an IP for connection")
var DOWNLOADSFOLDER *string = flag.String("downloadto", "", "Specifies where the receiver will store downloaded file")
var SHAREDFILE *string = flag.String("sharefile", "", "Specifies what file sender will send")
var SENDING bool
// helpMessage
var HELPMSG string = `
"-port", default: 8080, Specifies a port for a sender|port to connect to
"-addr", default: "", Specifies an IP for connection
"-downloadto", default: "", Specifies where the receiver will store downloaded file
"-sharefile", default: "", Specifies what file sender will send`
// Input-validation
func processFlags() {
if *PORT < 0 {
fmt.Println("Invalid port !\n", HELPMSG)
os.Exit(-1)
}
// going to send file -> sending
if strings.TrimSpace(*SHAREDFILE) != "" {
SENDING = true
}
// specifying address to connect to -> receiving
if strings.TrimSpace(*SENDERADDR) != "" {
if SENDING {
fmt.Println("Cannot specify an address when sharing !\n", HELPMSG)
os.Exit(-1)
}
SENDING = false
}
// specifying path to download to -> receiving
if strings.TrimSpace(*DOWNLOADSFOLDER) != "" {
if SENDING {
fmt.Println("Cannot specify a downloads directory when sharing !\n", HELPMSG)
os.Exit(-1)
}
SENDING = false
}
}
// parse flags, validate given values
func init() {
flag.Parse()
processFlags()
}
func main() {
if SENDING {
// 1) create sender -> 2) wait for a connection ->|
// 3) send info about the file -> 4) if accepted - upload file
sender := sender.NewSender(*PORT, *SHAREDFILE)
sender.WaitForConnection()
sender.MainLoop()
} else {
// 1) create receiver -> 2) try to connect to a sender -> 3) wait for an info on the file ->|
// 4) accept or refuse -> 5) download|don`t_download file
receiver := receiver.NewReceiver(*DOWNLOADSFOLDER)
receiver.Connect(fmt.Sprintf("%s:%d", *SENDERADDR, *PORT))
receiver.MainLoop()
}
}