Example #1
0
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()

}
Example #2
0
func MakeMartiniPing(dm *DryMartini, remoteHost net.IP, remotePort uint16) bool {

	dbg.Printf("MakeMartiniPing %s %d\n", Verbose, remoteHost, remotePort)
	//TODO: maybe should throw in a DoJoin(dm) here?
	return kademlia.MakePingCall(dm.KademliaInst, remoteHost, remotePort)

}
Example #3
0
func (kInst *KademliaInstruction) Execute(k *kademlia.Kademlia) (status bool) {
	var found bool
	var remoteContact *kademlia.Contact

	switch {
	case kInst.IsExit():
		if kademlia.RunningTests {
			log.Printf("Executing Exit Instruction\n")
		}
		return true
	case kInst.IsSkip():
		if kademlia.RunningTests {
			log.Printf("Executing Skip Instruction: _%s_\n", kInst.Data)
		}
		return true
	case kInst.IsPing():
		var success bool

		if kInst.Addr != "" { //ping host:port
			log.Printf("Executing Ping Instruction 'ping Addr:%s\n", kInst.Addr)
			remoteHost, remotePort, err := kademlia.AddrStrToHostPort(kInst.Addr)
			kademlia.Assert(err == nil, "Error converting AddrToHostPort")
			success = kademlia.MakePingCall(k, remoteHost, remotePort)
		} else { //ping nodeID
			log.Printf("Executing Ping Instruction 'ping nodeID:%s\n", kInst.NodeID.AsString())
			var searchRequest *kademlia.SearchRequest

			searchRequest = &kademlia.SearchRequest{kInst.NodeID, make(chan *kademlia.Contact)}
			k.SearchChannel <- searchRequest
			remoteContact = <-searchRequest.ReturnChan
			found = (remoteContact != nil)
			if found {
				success = kademlia.MakePingCall(k, remoteContact.Host, remoteContact.Port)
			} else {
				log.Printf("Error: Ping, nodeID %s could not be found\n", kInst.NodeID.AsString())
				return false
			}
		}
		return success
	case kInst.IsStore():
		var searchRequest *kademlia.SearchRequest
		var success bool
		if kademlia.RunningTests {
			log.Printf("Executing Store Instruction %s %s %s\n", kInst.NodeID.AsString(), kInst.Key.AsString(), kInst.Data)
		}

		searchRequest = &kademlia.SearchRequest{kInst.NodeID, make(chan *kademlia.Contact)}
		k.SearchChannel <- searchRequest
		remoteContact = <-searchRequest.ReturnChan
		found = (remoteContact != nil)
		if found {
			success = kademlia.MakeStore(k, remoteContact, kInst.Key, []byte(kInst.Data))
		} else {
			log.Printf("Error: Store, nodeID %s could not be found\n", kInst.NodeID.AsString())
			return false
		}
		return success
	case kInst.IsFindNode():
		var searchRequest *kademlia.SearchRequest
		var success bool
		if kademlia.RunningTests {
			log.Printf("Executing FindNode Instruction %s %s\n", kInst.NodeID.AsString(), kInst.Key.AsString())
		}
		searchRequest = &kademlia.SearchRequest{kInst.NodeID, make(chan *kademlia.Contact)}
		k.SearchChannel <- searchRequest
		remoteContact = <-searchRequest.ReturnChan
		found = (remoteContact != nil)
		if found {
			var fsResponseChan chan *kademlia.FindStarCallResponse
			var findStarResult *kademlia.FindStarCallResponse

			fsResponseChan = make(chan *kademlia.FindStarCallResponse, 1)
			go kademlia.MakeFindNodeCall(k, remoteContact, kInst.Key, fsResponseChan)
			findStarResult = <-fsResponseChan

			success = findStarResult.Responded
			if success {
				kademlia.Assert(findStarResult.ReturnedFNRes != nil, "findStarResult Struct error in FindNode")
				kademlia.PrintArrayOfFoundNodes(&(findStarResult.ReturnedFNRes.Nodes))
			}
		} else {
			log.Printf("Error: FindNode, nodeID %s could not be found\n", kInst.NodeID.AsString())
			return false
		}
		return success
	case kInst.IsFindValue():
		var searchRequest *kademlia.SearchRequest
		var success bool
		if kademlia.RunningTests {
			log.Printf("Executing FindValue Instruction %s %s\n", kInst.NodeID.AsString(), kInst.Key.AsString())
		}
		searchRequest = &kademlia.SearchRequest{kInst.NodeID, make(chan *kademlia.Contact)}
		k.SearchChannel <- searchRequest
		remoteContact = <-searchRequest.ReturnChan
		found = (remoteContact != nil)
		if found {
			var fsResponseChan chan *kademlia.FindStarCallResponse
			var findStarResult *kademlia.FindStarCallResponse

			fsResponseChan = make(chan *kademlia.FindStarCallResponse, 1)
			go kademlia.MakeFindValueCall(k, remoteContact, kInst.Key, fsResponseChan)
			findStarResult = <-fsResponseChan

			success = findStarResult.Responded
			if success {
				kademlia.Assert(findStarResult.ReturnedFVRes != nil, "findStarResult Struct error in FindValue")
				if findStarResult.ReturnedFVRes.Value != nil {
					log.Printf("FindValue: found [%s:%s]\n", kInst.Key.AsString(), string(findStarResult.ReturnedFVRes.Value))
				} else {
					log.Printf("FindValue: Could not locate value, printing closest nodes\n")
					kademlia.PrintArrayOfFoundNodes(&(findStarResult.ReturnedFVRes.Nodes))
				}
			}
		} else {
			log.Printf("Error: FindValue, nodeID %s could not be found\n", kInst.NodeID.AsString())
			return false
		}
		return success
	case kInst.IsWhoami():
		if kademlia.RunningTests {
			log.Printf("Executing Whoami Instruction\n")
			fmt.Printf("Local Node ID: %s\n", k.ContactInfo.NodeID.AsString())
		} else {
			fmt.Printf("%s\n", k.ContactInfo.NodeID.AsString())
		}
		return true
	case kInst.IsLocalFindValue():
		if kademlia.RunningTests {
			log.Printf("Executing LocalFindValue Instruction\n")
		}
		localvalue, found := k.ValueStore.Get(kInst.Key)
		if found {
			if kademlia.RunningTests {
				fmt.Printf("Value for key %s --> %s\n", kInst.Key.AsString(), string(localvalue))
			} else {
				fmt.Printf("%s\n", string(localvalue))
			}
		} else {
			if kademlia.RunningTests {
				fmt.Printf("Value for Key %s NOT found\n", kInst.Key.AsString())
			} else {
				fmt.Printf("ERR\n")
			}
		}
		return true
	case kInst.IsGetContact():
		var searchRequest *kademlia.SearchRequest
		if kademlia.RunningTests {
			log.Printf("Executing GetContact Instruction %s\n", kInst.NodeID.AsString())
		}

		searchRequest = &kademlia.SearchRequest{kInst.NodeID, make(chan *kademlia.Contact)}
		k.SearchChannel <- searchRequest
		remoteContact = <-searchRequest.ReturnChan
		found = (remoteContact != nil)
		if found {
			if kademlia.RunningTests {
				log.Printf("GetContact: Addr:%v, Port: %v\n", remoteContact.Host, remoteContact.Port)
			} else {
				fmt.Printf("%v %v\n", remoteContact.Host, remoteContact.Port)
			}
		} else {
			if kademlia.RunningTests {
				log.Printf("GetContact: Could not locate in local buckets nodeID %s\n", kInst.NodeID.AsString())
			} else {
				fmt.Printf("ERR\n")
			}
		}
		return true
	case kInst.IsIterativeStore():
		var success bool
		var nodes []kademlia.FoundNode
		var err error

		if kademlia.RunningTests {
			log.Printf("Executing iterativeStore Instruction %s %s\n", kInst.Key.AsString(), kInst.Data)
		}

		//NOTE: the third returned value is dropped on the assumption it would always be nil for this call
		success, nodes, _, err = kademlia.IterativeFind(k, kInst.Key, 1) //findType of 1 is FindNode
		if err != nil {
			log.Printf("IterativeFind: Error %s\n", err)
			return false
		}
		if success {
			if nodes != nil {
				for _, node := range nodes {
					kademlia.MakeStore(k, node.FoundNodeToContact(), kInst.Key, []byte(kInst.Data))
				}
				kademlia.PrintArrayOfFoundNodes(&nodes)
			} else {
				kademlia.Assert(false, "iterativeFindStore: TODO: This should probably never happen right?")
			}
		}
		return success
	case kInst.IsIterativeFindNode():
		var success bool
		var nodes []kademlia.FoundNode
		//var value []byte //This is probably not needed as iterativeFindNode should never return a value
		var err error

		if kademlia.RunningTests {
			log.Printf("Executing iterativeFindNode Instruction %s\n", kInst.NodeID.AsString())
		}

		//NOTE: the third returned value is dropped on the assumption it would always be nil for this call
		success, nodes, _, err = kademlia.IterativeFind(k, kInst.NodeID, 1) //findType of 1 is FindNode
		if err != nil {
			log.Printf("IterativeFind: Error %s\n", err)
			return false
		}
		if success {
			if nodes != nil {
				kademlia.PrintArrayOfFoundNodes(&nodes)
			} else {
				kademlia.Assert(false, "iterativeFindNode: TODO: This should probably never happen right?")
			}
		}
		return success
	case kInst.IsIterativeFindValue():
		var success bool
		var nodes []kademlia.FoundNode
		var value []byte
		var err error

		if kademlia.RunningTests {
			log.Printf("Executing iterativeFindValue Instruction %s\n", kInst.Key.AsString())
		}

		success, nodes, value, err = kademlia.IterativeFind(k, kInst.Key, 2) //findType of 2 is FindValue
		if err != nil {
			log.Printf("IterativeFind: Error %s\n", err)
			return false
		}
		if success {
			if value != nil {
				if kademlia.RunningTests {
					fmt.Printf("iterativeFindValue: Value for key %s --> %s\n", kInst.Key.AsString(), string(value))
				} else {
					fmt.Printf("%v %v\n", nodes[0].NodeID, value)
				}
			} else {
				if kademlia.RunningTests {
					fmt.Printf("iterativeFindValue: Value for key %s NOT FOUND\n", kInst.Key.AsString())
					kademlia.PrintArrayOfFoundNodes(&nodes)
				} else {
					fmt.Printf("ERR")
				}
			}
		}
		return success
	case kInst.IsRunTests():
		log.Printf("Executing RunTests!\n")
		kademlia.RunTests(kInst.Data)
		return true
	case kInst.IsPrintLocalBuckets():
		log.Printf("Print Local Buckets!\n")
		kademlia.PrintLocalBuckets(k)
		return true
	case kInst.IsPrintLocalData():
		log.Printf("Print Local Data!\n")
		kademlia.PrintLocalData(k)
		return true
	}

	return false
}