blob: 0af2d465cffb6f99f72a6181156ea07a2da2f390 [file] [log] [blame]
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build java android
package v23_go_runner
import (
"fmt"
"strconv"
"time"
"v.io/v23"
"v.io/v23/context"
"v.io/v23/naming"
"v.io/v23/rpc"
"v.io/v23/security"
)
const (
tcpServerName = "tmp/tcpServerName"
// TODO(suharshs): Currently we hard code the server phone's MAC address, email blessing, and port number because:
// (1) We haven't plugged in discovery yet.
// (2) Android APIs have removed a way to get the MAC address of a server.
btAddress = "a0:91:69:99:00:f0"
btPortNum = 13
btBlessings = "dev.v.io:o:608941808256-43vtfndets79kf5hac8ieujto8837660.apps.googleusercontent.com:vanadium.testphone@gmail.com"
)
var btServerName = naming.Endpoint{
Protocol: "bt",
Address: btAddress + "/" + strconv.Itoa(btPortNum),
}.WithBlessingNames([]string{btBlessings}).Name()
// v23GoRunnerFuncs is a map containing go functions keys by unique strings
// intended to be run by java/android applications using V23GoRunner.run(key).
// Users must add function entries to this map and rebuild lib/android-lib in
// the vanadium java repository.
var v23GoRunnerFuncs = map[string]func(*context.T) error{
"tcp-server": tcpServerFunc,
"tcp-client": tcpClientFunc,
"bt-client": btClientFunc,
"bt-server": btServerFunc,
}
func tcpServerFunc(ctx *context.T) error {
ctx = v23.WithListenSpec(ctx, rpc.ListenSpec{Proxy: "proxy"})
return runServer(ctx, tcpServerName)
}
func tcpClientFunc(ctx *context.T) error {
return runClient(ctx, tcpServerName)
}
func btServerFunc(ctx *context.T) error {
ctx = v23.WithListenSpec(ctx, rpc.ListenSpec{Addrs: rpc.ListenAddrs{{Protocol: "bt", Address: "/" + strconv.Itoa(btPortNum)}}})
return runServer(ctx, "")
}
func btClientFunc(ctx *context.T) error {
return runClient(ctx, btServerName)
}
func runServer(ctx *context.T, name string) error {
_, server, err := v23.WithNewServer(ctx, name, &echoServer{}, security.AllowEveryone())
if err != nil {
return err
}
ctx.Infof("Server listening on %v", server.Status().Endpoints)
ctx.Infof("Server listen errors: %v", server.Status().ListenErrors)
return nil
}
func runClient(ctx *context.T, name string) error {
elapsed, err := runTimedCall(ctx, name)
if err != nil {
return err
}
ctx.Infof("Client successfully executed rpc on new connection in %s.", elapsed.String())
elapsed, err = runTimedCall(ctx, name)
if err != nil {
return err
}
ctx.Infof("Client successfully executed rpc on cached connection in %s.", elapsed.String())
return nil
}
func runTimedCall(ctx *context.T, name string) (time.Duration, error) {
message := "hi there"
var got string
start := time.Now()
if err := v23.GetClient(ctx).Call(ctx, name, "Echo", []interface{}{message}, []interface{}{&got}); err != nil {
return 0, err
}
elapsed := time.Now().Sub(start)
if want := message; got != want {
return 0, fmt.Errorf("got %s, want %s", got, want)
}
return elapsed, nil
}