From 126c31f0da53d1d9be67291feb41ebe4aaa2c704 Mon Sep 17 00:00:00 2001 From: TinWoodman92 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/__init__.py | 21 +++++++++++++-------- dialoguer/context_script.r | 36 ++++++++++++++++++++++++------------ 2 files changed, 37 insertions(+), 20 deletions(-) (limited to 'dialoguer') diff --git a/dialoguer/__init__.py b/dialoguer/__init__.py index a1a3e85..f83e44a 100644 --- a/dialoguer/__init__.py +++ b/dialoguer/__init__.py @@ -2,6 +2,7 @@ import os import subprocess import threading import socket +import uuid from .binary_conversion import bin_conv from .data_type_ref import data_type_dict @@ -21,26 +22,30 @@ class Dialogue: self.file_name = file_name self.file_path = os.path.join(os.getcwd(), file_name) self.ext = file_name.split('.')[-1] + self.uuid = str(uuid.uuid4()) self.conn = None self.addr = None + self.connected = False self.active = False def execute_context_script(self): context_file = __file__.replace('__init__.py', 'context_script.r') - # I believe it is possible to pass the R target file path as a system argument. - # I might should pass the package directory instead/as-well so R can source-import modules from there - # perhaps setting the cwd to the package directory might work? - # If we cab use system-args, the most important arg should be the "key" that coordinates which sockets connection to find. subprocess.run( - f'Rscript {context_file}', + f'Rscript {context_file} {self.uuid} {self.file_path}', cwd = os.getcwd(), start_new_session = True ) def establish_connection(self): - server.listen() - self.conn, self.addr = server.accept() + while not self.connected: + server.listen() + self.conn, self.addr = server.accept() + uuid_chk = self.recv() + self.connected = self.uuid == uuid_chk + self.send(self.uuid) + if not self.connected: + self.conn.close() # I could call this method in the init method # perhaps use a wait arg? there are two points it could wait. @@ -52,7 +57,6 @@ class Dialogue: self._launch.start() self._connect.join() - self.send(self.file_path) data = self.recv(True) #This was not updated. self.active = (data == 'TRUE') @@ -103,6 +107,7 @@ class Dialogue: self.send('!DISCONNECT') self._launch.join() self.conn.close() + self.connected = False self.active = False def close(self, wait = True): 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