1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
HEADER <- 2048
PORT <- 6011
SERVER <- "localhost"
FORMAT <- "utf-8"
DISCONNECT_MESSAGE <- "!DISSCONNECT"
con <- socketConnection(
host = SERVER,
port = PORT,
server = FALSE,
open = "a+b"
)
display_msg <- function(msg) {
cat(
'\033[94m',
msg,
sep = '',
end = '\033[0m\n'
)
}
add_missing_bits <- function(bin_data, base = 8) {
missing_bits <- length(bin_data) %% base
if (missing_bits > 0) {
missing_bits <- rep(as.raw(00), times = base - missing_bits)
bin_data <- c(bin_data, missing_bits)
}
return(bin_data)
}
convert_from_binary <- function(bin_data, to_type) {
data <- NA
bin_data <- add_missing_bits(bin_data)
if (to_type == "character") {
data <- packBits(bin_data, "raw")
data <- rawToChar(data)
} else if (to_type == "integer") {
data <- packBits(bin_data, "raw")
data <- as.integer(data)
}
return(data)
}
convert_to_binary <- function(data) {
bin_data <- NA
if (is.character(data)) {
bin_data <- charToRaw(data)
bin_data <- rawToBits(bin_data)
} else if (is.integer(data)) {
bin_data <- as.raw(data)
bin_data <- rawToBits(bin_data)
}
return(bin_data)
}
# Add optional data_type argument that sends data_type as string
send <- function(conn, data) {
writeBin(data, conn)
}
# Add optional expect_data_type argument that will receive and convert data type as string
# should there be an expected data type arg that specifies a known data type?
recv <- function(conn, silent = TRUE) {
if (silent) {
suppressWarnings(data <- readBin(conn, "raw", HEADER))
while (length(data) == 0) {
suppressWarnings(data <- readBin(conn, "raw", HEADER))
}
} else {
data <- readBin(conn, "raw", HEADER)
while (length(data) == 0) {
cat('ALERT\n')
data <- readBin(conn, "raw", HEADER)
}
}
return(data)
}
#receive target file path
file_path <- recv(con)
file_path <- convert_from_binary(file_path, "character")
#load target file
# need to learn how to receive path as system argument.
source(file_path)
confirm <- TRUE
msg <- 'confirm'
while (msg != '!DISCONNECT') {
# there should be several handeling methods
# returning variables
# evaluating expressions
display_msg(msg)
val <- get(msg)
val <- as.character(val)
val <- convert_to_binary(val)
send(con, val)
msg <- recv(con)
msg <- convert_from_binary(msg, "character")
}
close(con)
|