Skip to content

Commit

Permalink
RSDK-4755: not quite working
Browse files Browse the repository at this point in the history
  • Loading branch information
ohEmily committed Sep 1, 2023
1 parent c4c079e commit 9cd1ff7
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 10 deletions.
2 changes: 1 addition & 1 deletion examples/src/ffi/cpp/ffi_echo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ using proto::rpc::examples::echo::v1::EchoService;
extern "C" void *init_rust_runtime();
extern "C" int free_rust_runtime(void *ptr);
extern "C" void free_string(char* s);
extern "C" char *dial(const char *uri, const char *type, const char *payload,
extern "C" char *dial(const char *uri, const char *entity, const char *type, const char *payload,
bool allow_insecure, void *ptr);

class EchoServiceClient {
Expand Down
2 changes: 1 addition & 1 deletion examples/src/ffi/cpp/ffi_robot.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ using viam::robot::v1::ResourceNamesResponse;
extern "C" void *init_rust_runtime();
extern "C" int free_rust_runtime(void *ptr);
extern "C" void free_string(char* s);
extern "C" char *dial(const char *uri, const char *type, const char *payload,
extern "C" char *dial(const char *uri, const char *entity, const char *type, const char *payload,
bool allow_insecure, void *ptr);

class RobotServiceClient {
Expand Down
21 changes: 20 additions & 1 deletion src/ffi/dial_ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,15 @@ fn dial_without_cred(

fn dial_with_cred(
uri: String,
entity: Option<&str>,
r#type: &str,
payload: &str,
allow_insec: bool,
disable_webrtc: bool,
) -> Result<DialBuilder<WithCredentials>> {
let creds = RPCCredentials::new(None, String::from(r#type), String::from(payload));
log::error!("dial_with_cred\n\n\n\n\n");
// let creds = RPCCredentials::new(None, String::from(r#type), String::from(payload));
let creds = RPCCredentials::new(entity, String::from(r#type), String::from(payload));
let c = DialOptions::builder().uri(&uri).with_credentials(creds);
let c = if disable_webrtc {
c.disable_webrtc()
Expand All @@ -110,18 +113,21 @@ fn dial_with_cred(
/// When falling to dial it will return a NULL pointer
/// # Arguments
/// * `c_uri` a C-style string representing the address of robot you want to connect to
/// * `c_entity` a C-style string representing the entity passed into the auth API
/// * `c_type` a C-style string representing the type of robot's secret you want to use, set to NULL if you don't need authentication
/// * `c_payload` a C-style string that is the robot's secret, set to NULL if you don't need authentication
/// * `c_allow_insecure` a bool, set to true when allowing insecure connection to your robot
/// * `rt_ptr` a pointer to a rust runtime previously obtained with init_rust_runtime
#[no_mangle]
pub unsafe extern "C" fn dial(
c_uri: *const c_char,
c_entity: *const c_char,
c_type: *const c_char,
c_payload: *const c_char,
c_allow_insec: bool,
rt_ptr: Option<&mut DialFfi>,
) -> *mut c_char {
log::error!("inside dial function in dial_ffi.rs\n\n\n\n\n");
let uri = {
if c_uri.is_null() {
return ptr::null_mut();
Expand Down Expand Up @@ -184,11 +190,24 @@ pub unsafe extern "C" fn dial(
false => Some(CStr::from_ptr(c_payload)),
}
};
let entity = {
match c_entity.is_null() {
true => None,
false => Some(CStr::from_ptr(c_entity)),
}
};

let (server, channel) = match runtime.block_on(async move {
let e = match entity {
Some(ent) => Some(ent.to_str()?),
None => None,
};
let channel = match (r#type, payload) {
(Some(t), Some(p)) => {
log::error!("EMILY before dial_with_cred\n\n\n\n\n");
dial_with_cred(
uri_str,
e,
t.to_str()?,
p.to_str()?,
allow_insec,
Expand Down
12 changes: 6 additions & 6 deletions src/rpc/dial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ pub enum ViamChannel {

#[derive(Debug)]
pub struct RPCCredentials {
entity: Option<String>,
entity: Option<&'static str>,
credentials: Credentials,
}

impl RPCCredentials {
pub fn new(entity: Option<String>, r#type: SecretType, payload: String) -> Self {
pub fn new(entity: Option<&str>, r#type: SecretType, payload: String) -> Self {
Self {
credentials: Credentials { r#type, payload },
entity,
Expand Down Expand Up @@ -569,11 +569,11 @@ impl DialBuilder<WithoutCredentials> {
async fn get_auth_token(
channel: &mut Channel,
creds: Credentials,
entity: String,
entity: &str,
) -> Result<String> {
let mut auth_service = AuthServiceClient::new(channel);
let req = AuthenticateRequest {
entity,
entity: entity.to_string(),
credentials: Some(creds),
};

Expand Down Expand Up @@ -647,7 +647,7 @@ impl DialBuilder<WithCredentials> {
.credentials
.unwrap()
.entity
.unwrap_or_else(|| domain.clone()),
.unwrap_or_else(|| &domain),
)
.await?;
log::debug!("{}", log_prefixes::ACQUIRED_AUTH_TOKEN);
Expand Down Expand Up @@ -1094,7 +1094,7 @@ fn infer_remote_uri_from_authority(uri: Uri) -> Uri {

fn uri_parts_with_defaults(uri: &str) -> Parts {
let mut uri_parts = uri.parse::<Uri>().unwrap().into_parts();
uri_parts.scheme = Some(Scheme::HTTPS);
uri_parts.scheme = Some(Scheme::HTTP);
uri_parts.path_and_query = Some(PathAndQuery::from_static(""));
uri_parts
}
Expand Down
3 changes: 2 additions & 1 deletion src/rpc/webrtc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ impl Options {
// TODO(RSDK-235): remove hard coding of signaling server address and prefer SRV lookup instead
let path = uri.to_string();
if path.contains(".viam.cloud") {
Some(("app.viam.com:443".to_string(), true))
// Some(("app.viam.com:443".to_string(), true))
Some(("localhost:8080".to_string(), true))
} else if path.contains(".robot.viaminternal") {
Some(("app.viaminternal:8089".to_string(), false))
} else {
Expand Down

0 comments on commit 9cd1ff7

Please sign in to comment.