func main() { kListenStr := flag.String("l", "localhost:8000", "the address:port the kademlia instance will operate over") htmlDirPath := flag.String("d", "/home/jch570/public_html/", "the path to the directory where html files are served. file is written out to this dir") htmlFileName := flag.String("f", "dms.html", "filename where drymartini info will be written out") flag.Parse() listenStr := *kListenStr log.Printf("dmTracker listening on:%s\n", listenStr) var junk = "junk" kademInst, _ := kademlia.NewKademlia(listenStr, &junk) log.Printf("dmTracker checking querying initial core: %+v\n", coreKadems) // commenting out, for testing just going to use localhost:8000-8004 for _, apPair := range coreKadems { ipList, err := net.LookupIP(apPair.addr) if err != nil { log.Printf("error looking up addr:%s. Err:%s\n", apPair.addr, err) } kademlia.MakePingCall(kademInst, ipList[0], apPair.port) } //test should trash soon for i := 0; i < 5; i++ { ipList, err := net.LookupIP("localhost") if err != nil { log.Printf("error looking up addr\n") } kademlia.MakePingCall(kademInst, ipList[0], uint16(8000+i)) } //end junk kademlia.DoJoin(kademInst) var contacts []kademlia.Contact contacts = kademlia.BucketsAsArray(kademInst) log.Printf("local buckets as array:\n %+v\n", contacts) f, err := os.Create(*htmlDirPath + *htmlFileName) if err != nil { //"./testoutput"); if(err!=nil){// log.Printf("error creating file:%s\n", err) os.Exit(1) } f.Write([]byte("DryMartinis found:\n")) var c kademlia.Contact for _, c = range contacts { f.Write([]byte(c.AsString() + "\n")) } f.Write([]byte("last updated: " + time.Now().String())) f.Close() }
//more arguments for a later time //remoteAddr net.IP, remotePort uint16, doPing bool func DoJoin(dm *DryMartini) bool { var success bool var secToWait time.Duration = 1 dbg.Printf("drymartini.DoJoin()\n", Verbose) success = kademlia.DoJoin(dm.KademliaInst) if !success { return false } dm.DoJoinFlag = false dbg.Printf("doJoin in %d sec\n", Verbose, secToWait) time.Sleep(secToWait) //Store our contact information //TODO StoreContactInfo(dm) return true }
func main() { var err error var args []string var listenStr string var kadem *kademlia.Kademlia var stdInReader *bufio.Reader // By default, Go seeds its RNG with 1. This would cause every program to // generate the same sequence of IDs. rand.Seed(time.Now().UnixNano()) // Get the bind and connect connection strings from command-line arguments. flag.Parse() args = flag.Args() if len(args) != 1 { log.Fatal("Must be invoked with exactly one arguments!\n") } listenStr = args[0] //firstPeerStr = args[1] //log.Printf("First Peer: %s\n", firstPeerStr); kadem, _ = kademlia.NewKademlia(listenStr, nil) stdInReader = bufio.NewReader(os.Stdin) //input, _ := reader.ReadString('\n') /* //REMOVE: part of the initial skeleton // Confirm our server is up with a PING request and then exit. // Your code should loop forever, reading instructions from stdin and // printing their results to stdout. See README.txt for more details. client, err := rpc.DialHTTP("tcp", firstPeerStr) if err != nil { log.Fatal("DialHTTP: ", err) } ping := new(kademlia.Ping) ping.MsgID = kademlia.NewRandomID() ping.Sender = kademlia.GetNodeContactInfo(kadem) var pong kademlia.Pong err = client.Call("Kademlia.Ping", ping, &pong) if err != nil { log.Fatal("Call: ", err) } log.Printf("ping msgID: %s %s\n", ping.MsgID.AsString(), ping.Sender.AsString()) log.Printf("pong msgID: %s\n", pong.MsgID.AsString()) */ /* //REMOVE: this is just to check if the map is working tmpKey, _ := kademlia.FromString("abcd") var tmpVal []byte = make([]byte, 3) tmpVal[0] = 'f' tmpVal[1] = 'o' tmpVal[2] = 'o' kadem.ValueStore.HashMap[tmpKey] = tmpVal //~REMOVE */ var instStr string var inst *KademliaInstruction for { fmt.Printf("δώσε:") //Print prompt //read new instruction //ret, err := fmt.Scanln(&instStr) instStr, err = stdInReader.ReadString('\n') if err != nil { log.Printf("Error at Scanf: %s\n", err) panic(1) } //parse line input and create command struct inst = NewKademliaInstruction(instStr) if inst.IsExit() { log.Printf("Kademlia exiting...\n") break } //execute new instruction inst.Execute(kadem) if kadem.DoJoinFlag { go kademlia.DoJoin(kadem) } } //finalizer() }