Frederic G. MARAND 4 years ago
parent
commit
c470b19dfc
2 changed files with 46 additions and 7 deletions
  1. 24 0
      .idea/runConfigurations/WG_Step_5.xml
  2. 22 7
      part5/main.go

+ 24 - 0
.idea/runConfigurations/WG_Step_5.xml

@@ -0,0 +1,24 @@
+<component name="ProjectRunConfigurationManager">
+  <configuration default="false" name="WG Step 5" type="GoApplicationRunConfiguration" factoryName="Go Application" editBeforeRun="true">
+    <module name="whispering_gophers" />
+    <working_directory value="$PROJECT_DIR$/part5" />
+    <go_parameters value="-o wg" />
+    <parameters value="-peer 127.0.0.1:8000" />
+    <EXTENSION ID="net.ashald.envfile">
+      <option name="IS_ENABLED" value="false" />
+      <option name="IS_SUBST" value="false" />
+      <option name="IS_PATH_MACRO_SUPPORTED" value="false" />
+      <option name="IS_IGNORE_MISSING_FILES" value="false" />
+      <option name="IS_ENABLE_EXPERIMENTAL_INTEGRATIONS" value="false" />
+      <ENTRIES>
+        <ENTRY IS_ENABLED="true" PARSER="runconfig" />
+      </ENTRIES>
+    </EXTENSION>
+    <kind value="DIRECTORY" />
+    <filePath value="$PROJECT_DIR$/" />
+    <package value="code.osinet.fr/fgm/whispering_gophers" />
+    <directory value="$PROJECT_DIR$/part5" />
+    <output_directory value="$PROJECT_DIR$/part5" />
+    <method v="2" />
+  </configuration>
+</component>

+ 22 - 7
part5/main.go

@@ -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)
 }