aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--launcher.py52
-rw-r--r--source_file.py11
-rw-r--r--source_file.r37
3 files changed, 73 insertions, 27 deletions
diff --git a/launcher.py b/launcher.py
index 23e3000..32ec12b 100644
--- a/launcher.py
+++ b/launcher.py
@@ -2,6 +2,7 @@ import os
import subprocess
import threading
import socket
+import time
HEADER = 64
PORT = 6011
@@ -29,54 +30,67 @@ def recv_msg(conn):
msg = conn.recv(HEADER).strip()
return msg
-def terminate_connection(conn):
- conn.close()
-
def execute_source_file(file_name):
ext = file_name.split('.')
if ext[1] == 'py': cmd = 'Python'
- elif ext[1] == 'r': cmd = 'RScript'
+ elif ext[1] == 'r': cmd = 'Rscript'
subprocess.run(
f'{cmd} {file_name}',
cwd = os.getcwd(),
start_new_session = True
)
+ print(f'{file_name} executed')
def launch_and_connect(file_name, CONN):
- mutable = {}
- connect_ = threading.Thread(target=establish_connection, args=[mutable])
+ connect_ = threading.Thread(target=establish_connection, args=[CONN])
launch_ = threading.Thread(target=execute_source_file, args=[file_name])
connect_.start()
launch_.start()
connect_.join()
- conn = mutable['conn']
- addr = mutable['addr']
- CONN['conn'] = mutable['conn']
- CONN['addr'] = mutable['addr']
+ conn = CONN['conn']
+ addr = CONN['addr']
+ CONN['launch_'] = launch_
return conn, addr
-def disconnect_and_close(file_name):
- pass
+def disconnect_and_close(CONN):
+ conn = CONN['conn']
+ launch_ = CONN['launch_']
+ send_msg(conn, '!DISCONNECT')
+ print(f'closing {CONN["addr"]}')
+ launch_.join()
+ print(f'closed {CONN["addr"]}')
+ conn.close()
+CONN_R = {}
CONN_PY = {}
+src_fil_r = threading.Thread(target=launch_and_connect, args=['source_file.r', CONN_R])
src_fil_py = threading.Thread(target=launch_and_connect, args=['source_file.py', CONN_PY])
-#src_fil_r = threading.Thread(target=execute_source_file, args=['source_file.r'])
+disc_r = threading.Thread(target=disconnect_and_close, args=[CONN_R])
+disc_py = threading.Thread(target=disconnect_and_close, args=[CONN_PY])
-#src_fil_r.start()
-src_fil_py.start()
+src_fil_r.run()
+src_fil_py.run()
-#src_fil_r.join()
-src_fil_py.join()
+conn_r = CONN_R['conn']
conn_py = CONN_PY['conn']
+msg = recv_msg(conn_r)
+print(msg)
msg = recv_msg(conn_py)
print(msg)
-msg = "goodbye"
-send_msg(conn_py, msg)
+for i in range(255):
+ msg = f'message {i}'
+ send_msg(conn_r, msg)
+ send_msg(conn_py, msg)
+
+disc_r.start()
+disc_py.start()
+disc_r.join()
+disc_py.join()
print("end launcher")
diff --git a/source_file.py b/source_file.py
index 9d10aed..672139c 100644
--- a/source_file.py
+++ b/source_file.py
@@ -28,17 +28,14 @@ def recv_msg(conn):
msg = msg.decode(FORMAT)
return msg
-def terminate_connection(conn):
- conn.close()
-
-
msg = "Initializing Client - Python"
display_msg(msg)
msg = "Initialized Client - Python"
send_msg(CONN, msg)
-msg = recv_msg(CONN)
-display_msg(msg)
+while msg != '!DISCONNECT':
+ msg = recv_msg(CONN)
+ display_msg(msg)
-terminate_connection(CONN) \ No newline at end of file
+CONN.close() \ No newline at end of file
diff --git a/source_file.r b/source_file.r
index b0bd8d9..ce7e85d 100644
--- a/source_file.r
+++ b/source_file.r
@@ -1,3 +1,15 @@
+HEADER <- 64
+PORT <- 6011
+SERVER <- "localhost"
+FORMAT <- "utf-8"
+DISCONNECT_MESSAGE <- "!DISSCONNECT"
+
+con <- socketConnection(
+ host = SERVER,
+ port = PORT,
+ server = FALSE
+)
+
display_msg <- function(msg) {
cat(
'\033[94m',
@@ -7,6 +19,29 @@ display_msg <- function(msg) {
)
}
-msg <- "hello world - R"
+send_msg <- function(conn, msg) {
+ sendme <- paste(msg, strrep(" ", HEADER - nchar(msg)), sep = "")
+ writeChar(sendme, conn)
+}
+
+recv_msg <- function(conn) {
+ suppressWarnings(msg <- trimws(readChar(conn, HEADER)))
+ while (length(msg) == 0) {
+ #cat('ALERT\n')
+ suppressWarnings(msg <- trimws(readChar(conn, HEADER)))
+ }
+ return(msg)
+}
+msg <- "Initializing Client - R"
display_msg(msg)
+
+msg <- "Initialized Client - R"
+send_msg(con, msg)
+
+while (msg != '!DISCONNECT') {
+ msg <- recv_msg(con)
+ display_msg(msg)
+}
+
+close(con) \ No newline at end of file