From 126c31f0da53d1d9be67291feb41ebe4aaa2c704 Mon Sep 17 00:00:00 2001
From: TinWoodman92 <chrhodgden@gmail.com>
Date: Sun, 25 Jun 2023 07:55:58 -0500
Subject: Pass uuid and taget file path via system arguments. Establish socket
 connection by verifying matching uuid.

---
 dialoguer/context_script.r | 36 ++++++++++++++++++++++++------------
 1 file changed, 24 insertions(+), 12 deletions(-)

(limited to 'dialoguer/context_script.r')

diff --git a/dialoguer/context_script.r b/dialoguer/context_script.r
index bb2b133..9c600f5 100644
--- a/dialoguer/context_script.r
+++ b/dialoguer/context_script.r
@@ -3,13 +3,8 @@ PORT <- 6011
 SERVER <- "localhost"
 FORMAT <- "utf-8"
 DISCONNECT_MESSAGE <- "!DISSCONNECT"
-
-con <- socketConnection(
-	host = SERVER,
-	port = PORT,
-	server = FALSE,
-	open = "a+b"
-)
+UUID <- commandArgs(trailingOnly = TRUE)[1]
+TARGET_FILE <- commandArgs(trailingOnly = TRUE)[2]
 
 data_type_vect <- c(
 	character = 'character',
@@ -95,13 +90,30 @@ recv <- function(conn, recv_data_type = FALSE, set_data_type = "character") {
 	return(data)
 }
 
-#receive target file path
-# need to learn how to receive path as system argument.
-# may also receive package directory to source-import R modules
-file_path <- recv(con)
+find_connection <- function() {
+	connected <- FALSE
+	while (!connected) {
+		con <- socketConnection(
+			host = SERVER,
+			port = PORT,
+			server = FALSE,
+			open = "a+b"
+		)
+
+		send(con, UUID)
+		uuid_chk <- recv(con)
+		connected <- (UUID == uuid_chk)
+		if (!connected) {
+			close(con)
+		}
+	}
+	return(con)
+}
+
+con <- find_connection()
 
 #load target file
-source(file_path)
+source(TARGET_FILE)
 
 confirm <- TRUE
 msg <- 'confirm'
-- 
cgit v1.2.3