|
@@ -31,12 +31,13 @@ import (
|
|
|
"net"
|
|
|
"os"
|
|
|
|
|
|
- "github.com/campoy/whispering-gophers/util"
|
|
|
+ "code.osinet.fr/fgm/whispering_gophers/util"
|
|
|
)
|
|
|
|
|
|
var (
|
|
|
- peerAddr = flag.String("peer", "", "peer host:port")
|
|
|
- self string
|
|
|
+ listenAddr = flag.String("listen", "", "peer host:port")
|
|
|
+ peerAddr = flag.String("peer", "", "peer host:port")
|
|
|
+ self string
|
|
|
)
|
|
|
|
|
|
type Message struct {
|
|
@@ -46,10 +47,22 @@ type Message struct {
|
|
|
|
|
|
func main() {
|
|
|
flag.Parse()
|
|
|
+ var l net.Listener
|
|
|
+ var err error
|
|
|
+ // Create a new listener using util.Listen and put it in a variable named l.
|
|
|
+ if *listenAddr == "" {
|
|
|
+ l, err = util.ListenOnFirstUsableInterface()
|
|
|
+ } else {
|
|
|
+ l, err = net.Listen("tcp4", *listenAddr)
|
|
|
+ }
|
|
|
+ if err != nil {
|
|
|
+ log.Fatal(err)
|
|
|
+ }
|
|
|
+ // Set the global variable self with the address of the listener.
|
|
|
+ self = l.Addr().String()
|
|
|
|
|
|
- // TODO: Create a new listener using util.Listen and put it in a variable named l.
|
|
|
- // TODO: Set the global variable self with the address of the listener.
|
|
|
- // TODO: Print the address to the standard output
|
|
|
+ // Print the address to the standard output
|
|
|
+ fmt.Printf("Listening on %s\", ", self)
|
|
|
|
|
|
go dial(*peerAddr)
|
|
|
|
|
@@ -86,7 +99,8 @@ func dial(addr string) {
|
|
|
e := json.NewEncoder(c)
|
|
|
for s.Scan() {
|
|
|
m := Message{
|
|
|
- // TODO: Put the self variable in the new Addr field.
|
|
|
+ // Put the self variable in the new Addr field.
|
|
|
+ Addr: self,
|
|
|
Body: s.Text(),
|
|
|
}
|
|
|
err := e.Encode(m)
|
|
@@ -97,4 +111,5 @@ func dial(addr string) {
|
|
|
if err := s.Err(); err != nil {
|
|
|
log.Fatal(err)
|
|
|
}
|
|
|
+ os.Exit(0)
|
|
|
}
|