本文整理汇总了Golang中net/rpc.DialHTTPPath函数的典型用法代码示例。如果您正苦于以下问题:Golang DialHTTPPath函数的具体用法?Golang DialHTTPPath怎么用?Golang DialHTTPPath使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了DialHTTPPath函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: DoFindValueIter
func (k *Kademlia) DoFindValueIter(contact Contact, searchKey ID, chnl chan FindValueResult) string {
// The recipient must return k triples if at all possible.
// It may only return fewer than k if it is returning all of the contacts that it has knowledge of.
port_str := strconv.Itoa(int(contact.Port))
client, err := rpc.DialHTTPPath("tcp", contact.Host.String()+":"+port_str, rpc.DefaultRPCPath+port_str)
if err != nil {
log.Fatal("DialHTTP: ", err)
}
defer client.Close()
request := new(FindValueRequest)
result := new(FindValueResult)
request.MsgID = NewRandomID()
request.Sender = k.SelfContact
request.Key = searchKey
err = client.Call("KademliaCore.FindValue", request, &result)
if err != nil {
log.Fatal("Call: ", err)
return "ERR:DoFindValue call err"
}
if request.MsgID == result.MsgID {
result.SenderContact = contact
k.SelfTable.Update(contact)
chnl <- *result
return "OK: Findvalue"
} else {
return "ERR: FindValue: req.MsgID != res.MsgID"
}
}
示例2: DoFindNodeIter
//-----------------------------------------------------------------//
func (k *Kademlia) DoFindNodeIter(contact Contact, searchKey ID, chnl chan FindNodeResult) string {
// TODO: Implement
// Warining:The recipient must return k triples if at all possible.
// It may only return fewer than k if it is returning all of the contacts that it has knowledge of. -----FIXME
port_str := strconv.Itoa(int(contact.Port))
client, err := rpc.DialHTTPPath("tcp", contact.Host.String()+":"+port_str, rpc.DefaultRPCPath+port_str)
if err != nil {
return "DialHTTP:err"
}
defer client.Close()
request := new(FindNodeRequest)
result := new(FindNodeResult)
request.MsgID = NewRandomID()
request.Sender = k.SelfContact
request.NodeID = searchKey
err = client.Call("KademliaCore.FindNode", request, &result)
if err != nil {
return "ERR:DoFindValue call err"
}
if request.MsgID == result.MsgID {
/* put returned contact into channel */
result.SenderContact = contact
chnl <- *result
return "OK, Found Nodes"
}
return "ERR: FindNOde->req.MsgID!=res.MsgID"
// If all goes well, return "OK: <output>", otherwise print "ERR: <messsage>"
}
示例3: DoGetVDO
func (k *Kademlia) DoGetVDO(contact Contact, vdoId ID) (string, error) {
port_str := strconv.Itoa(int(contact.Port))
client, err := rpc.DialHTTPPath("tcp", contact.Host.String()+":"+port_str, rpc.DefaultRPCPath+port_str)
if err != nil {
log.Fatal("DialHTTP: ", err)
}
defer client.Close()
req := new(GetVDORequest)
res := new(GetVDOResult)
req.Sender = k.SelfContact
req.MsgID = NewRandomID()
req.VdoID = vdoId
err = client.Call("KademliaCore.GetVDO", req, &res)
if err != nil {
log.Fatal("Call: ", err)
return "ERR:Doping call err", nil
}
// If all goes well, return "OK: <output>", otherwise print "ERR: <messsage>"
if req.MsgID == res.MsgID {
k.SelfTable.Update(res.SenderContact)
secret := UnvanishData(k, res.VDO)
return string(secret), nil
} else {
return "ERR: req.MsgID != res.MsgID", nil
}
}
示例4: DoFindNode
func (k *Kademlia) DoFindNode(contact *Contact, searchKey ID) (string, FindNodeResult) {
// TODO: Implement
// Warining:The recipient must return k triples if at all possible.
// It may only return fewer than k if it is returning all of the contacts that it has knowledge of. -----FIXME
port_str := strconv.Itoa(int(contact.Port))
client, err := rpc.DialHTTPPath("tcp", contact.Host.String()+":"+port_str, rpc.DefaultRPCPath+port_str)
if err != nil {
log.Fatal("DialHTTP: ", err)
}
defer client.Close()
request := new(FindNodeRequest)
result := new(FindNodeResult)
request.MsgID = NewRandomID()
request.Sender = k.SelfContact
request.NodeID = searchKey
err = client.Call("KademliaCore.FindNode", request, &result)
if err != nil {
log.Fatal("Call: ", err)
return "ERR:DoFindNode call err", *result
}
if request.MsgID == result.MsgID {
k.SelfTable.Update(*contact)
//print the contacts
fmt.Println("--------Closest Nodes to", searchKey.AsString(), "---------")
for i := 0; i < len(result.Nodes); i++ {
fmt.Println(result.Nodes[i].NodeID.AsString())
}
return "OK : FindNode", *result
}
return "ERR: FindNOde->req.MsgID!=res.MsgID", *result
// If all goes well, return "OK: <output>", otherwise print "ERR: <messsage>"
}
示例5: Test_Proto_HTTP_Large_Timeout
func Test_Proto_HTTP_Large_Timeout(t *testing.T) {
t.Skip()
srv := &httpTestServer{&baseTestServer{}}
port, err := srv.listen()
assert.NoError(t, err)
srv.start()
defer srv.Stop()
client, err := rpc.DialHTTPPath("tcp",
fmt.Sprintf("127.0.0.1:%d", port), "/v1/rpc")
assert.NoError(t, err)
defer client.Close()
var res, res1 TestMessage
req := TestMessage{
"12345678910",
[]byte(strings.Repeat("0", 1024*1024)),
}
err = client.Call("ServiceFixture.Ping", &req, &res)
assert.NoError(t, err)
assert.Equal(t, req, res)
time.Sleep(time.Second)
err = client.Call("ServiceFixture.Ping", &req, &res1)
assert.NoError(t, err)
assert.Equal(t, req, res1)
}
示例6: DoPing
// This is the function to perform the RPC
func (k *Kademlia) DoPing(host net.IP, port uint16) string {
// TODO: Implement
// dstHostPort := HostPortGenerator(host, port)
port_str := strconv.Itoa(int(port))
client, err := rpc.DialHTTPPath("tcp", host.String()+":"+port_str, rpc.DefaultRPCPath+port_str)
if err != nil {
log.Fatal("DialHTTP: ", err)
}
defer client.Close()
ping := new(PingMessage)
pong := new(PongMessage)
ping.MsgID = NewRandomID()
ping.Sender = k.SelfContact
err = client.Call("KademliaCore.Ping", ping, &pong)
if err != nil {
log.Fatal("Call: ", err)
return "ERR:Doping call err"
}
// If all goes well, return "OK: <output>", otherwise print "ERR: <messsage>"
if pong.MsgID == ping.MsgID {
k.SelfTable.Update(pong.Sender)
return "OK: PONG"
} else {
return "ERR: PONG.MsgID != ping.MsgID"
}
}
示例7: Start
// Start starts an agreement on new instance.
func (p *Paxos) Start(seq int, v Value) {
var clients []*rpc.Client
for _, peer := range p.peers {
addrAndPath := strings.Split(peer, "/")
if len(addrAndPath) != 2 {
panic(fmt.Sprintf("got: %v, want: ${HOSTNAME}:${PORT}/${RPC_PATH}", addrAndPath))
}
addr := addrAndPath[0]
rpcPath := addrAndPath[1]
client, err := rpc.DialHTTPPath("tcp", addr, rpcPath)
if err == nil {
clients = append(clients, client)
} else {
fmt.Printf("Error: %v\n", err)
clients = append(clients, nil)
}
}
p.maxSeq++
for _, c := range clients {
if c != nil {
c.Go("Handler.OnReceiveProposal",
&Request{
FromID: p.ID(),
Seq: p.Max(),
},
&Response{}, nil)
}
}
}
示例8: pingToRemove
/***
* if the least recently used contact has response, then
* ignore the new element. If the least recently used contact
* doesn't have response, then delete it and add the new contact.
*/
func pingToRemove(bucket *[]Contact, contact *Contact, self *Contact) {
ping := PingMessage{*self, NewRandomID()}
var pong PongMessage
remove := 0
port_str := strconv.Itoa(int((*bucket)[0].Port))
client, err := rpc.DialHTTPPath("tcp", Dest((*bucket)[0].Host, (*bucket)[0].Port), rpc.DefaultRPCPath+port_str)
if err != nil {
remove = 1
}
defer client.Close()
err = client.Call("KademliaCore.Ping", ping, &pong)
if err != nil {
remove = 1
}
if remove == 1 {
*bucket = (*bucket)[1:]
*bucket = append(*bucket, *contact)
} else {
tmp := (*bucket)[0]
*bucket = (*bucket)[1:]
*bucket = append(*bucket, tmp)
}
}
示例9: main
func main() {
// By default, Go seeds its RNG with 1. This would cause every program to
// generate the same sequence of IDs. Use the current nano time to
// random numbers
rand.Seed(time.Now().UnixNano())
// Get the bind and connect connection strings from command-line arguments.
flag.Parse()
args := flag.Args()
if len(args) != 2 {
log.Fatal("Must be invoked with exactly two arguments!\n")
}
listenStr := args[0]
firstPeerStr := args[1]
// Create the Kademlia instance
fmt.Printf("kademlia starting up!\n")
kadem := kademlia.NewKademlia(listenStr)
// 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.
_, port, _ := net.SplitHostPort(firstPeerStr)
client, err := rpc.DialHTTPPath("tcp", firstPeerStr, rpc.DefaultRPCPath+port)
if err != nil {
log.Fatal("DialHTTP: ", err)
}
ping := new(kademlia.PingMessage)
ping.MsgID = kademlia.NewRandomID()
ping.Sender = kadem.Routes.SelfContact
var pong kademlia.PongMessage
err = client.Call("KademliaCore.Ping", ping, &pong)
if err != nil {
log.Fatal("Call: ", err)
}
log.Printf("ping msgID: %s\n", ping.MsgID.AsString())
log.Printf("pong msgID: %s\n", pong.MsgID.AsString())
kadem.Routes.Update(&pong.Sender)
in := bufio.NewReader(os.Stdin)
quit := false
for !quit {
line, err := in.ReadString('\n')
if err != nil {
log.Fatal(err)
}
line = strings.TrimSpace(line)
if line == "" {
continue
}
resp := executeLine(kadem, line)
if resp == "quit" {
quit = true
} else if resp != "" {
fmt.Printf("%v\n", resp)
}
}
}
示例10: DoStore
func (k *Kademlia) DoStore(contact *Contact, key ID, value []byte) string {
// TODO: Implement
// If all goes well, return "OK: <output>", otherwise print "ERR: <messsage>"
port_str := strconv.Itoa(int(contact.Port))
client, err := rpc.DialHTTPPath("tcp", contact.Host.String()+":"+port_str, rpc.DefaultRPCPath+port_str)
if err != nil {
log.Fatal("DialHTTP: ", err)
}
defer client.Close()
storeRequest := new(StoreRequest)
storeResult := new(StoreResult)
storeRequest.MsgID = NewRandomID()
storeRequest.Sender = k.SelfContact
storeRequest.Key = key
storeRequest.Value = value
err = client.Call("KademliaCore.Store", storeRequest, &storeResult)
if err != nil {
log.Fatal("Call: ", err)
return "ERR: Dostore call err"
}
// If all goes well, return "OK: <output>", otherwise print "ERR: <messsage>"
if storeRequest.MsgID == storeResult.MsgID {
k.SelfTable.Update(*contact)
fmt.Println("\"", string(value), "\" stored in \"", contact.NodeID.AsString(), "\"")
return "OK: Store"
} else {
return "ERR: storeRequest.MsgID != storeResult.MsgID"
}
return "ERR: Not implemented"
}
示例11: sendQuery
func sendQuery(c Contact, active *ConcurrMap, waitChan chan int, nodeChan chan Contact) {
args := FindNodeRequest{c, NewRandomID(), c.NodeID}
var reply FindNodeResult
active.Lock()
active.m[c.NodeID] = 1
active.Unlock()
port_str := strconv.Itoa(int(c.Port))
client, err := rpc.DialHTTPPath("tcp", Dest(c.Host, c.Port), rpc.DefaultRPCPath+port_str)
if err != nil {
log.Fatal("DialHTTP", err)
active.Lock()
active.m[c.NodeID] = 0
active.Unlock()
}
defer client.Close()
err = client.Call("KademliaCore.FindNode", args, &reply)
if err != nil {
log.Fatal("Call: ", err)
active.Lock()
active.m[c.NodeID] = 0
active.Unlock()
}
active.RLock()
a := active.m[c.NodeID]
active.RUnlock()
if a == 1 {
for _, node := range reply.Nodes {
nodeChan <- node
}
}
waitChan <- 1
}
示例12: Benchmark_Proto_HTTP_Large
func Benchmark_Proto_HTTP_Large(b *testing.B) {
b.Skip()
srv := &httpTestServer{&baseTestServer{}}
port, err := srv.listen()
assert.NoError(b, err)
srv.start()
defer srv.Stop()
client, err := rpc.DialHTTPPath("tcp",
fmt.Sprintf("127.0.0.1:%d", port), "/v1/rpc")
assert.NoError(b, err)
defer client.Close()
b.Log(port, b.N)
b.ResetTimer()
for i := 0; i < b.N; i++ {
b.StartTimer()
var res TestMessage
req := TestMessage{
fmt.Sprintf("%d", i),
[]byte(strings.Repeat("0", 1024*1024)),
}
err = client.Call("ServiceFixture.Ping", &req, &res)
b.StopTimer()
assert.NoError(b, err)
assert.Equal(b, req, res)
b.SetBytes(int64(len(req.Data) * 2))
}
}
示例13: BenchmarkRPCGet
func BenchmarkRPCGet(b *testing.B) {
b.StopTimer()
client, _ := rpc.DialHTTPPath("tcp", "localhost:2000", "/rpc")
b.StartTimer()
var reply string
for i := 0; i < b.N; i++ {
client.Call("Responder.Get", "test", &reply)
}
}
示例14: Connect
func (c *RPCClient) Connect() (err error) {
if c.client == nil {
c.client, err = rpc.DialHTTPPath(
"tcp",
c.endpoint.Host,
c.endpoint.Path+"/rpc")
}
return
}
示例15: NewRunner
func NewRunner(rpcService string, port int) *Run {
client, err := rpc.DialHTTPPath("tcp", fmt.Sprintf(":%d", port), "/")
if err != nil {
panic(err.Error())
}
return &Run{
client: client,
rpcService: rpcService,
}
}