Compare commits

..

3 Commits

Author SHA1 Message Date
vaclavt
3695a54e9a tcp net wip 2022-05-05 18:01:57 +02:00
vaclavt
4dfdd76b05 no cache in docker file 2022-05-05 18:01:37 +02:00
vaclavt
8085397744 move test to its files 2022-05-05 18:01:19 +02:00
7 changed files with 62 additions and 29 deletions

View File

@@ -71,10 +71,7 @@ int TcpNet::server(int portno, const std::function<std::pair<bool, std::string>(
shutdown = response.first;
std::string response_str = response.second;
auto response_len = response_str.size();
auto n = write(newsockfd, response_str.c_str(), response_len);
if (n < 0 || response_len != n)
error("ERROR writing to socket");
write_to_socket(newsockfd, response_str);
requests_processed++;
}
@@ -134,10 +131,20 @@ std::string TcpNet::client(const std::string &address, int portno, const std::st
return response[0];
}
std::string TcpNet::read_from_socket(int sockfd) {
char buffer[TCPNET_BUFFER_SIZE];
std::string request;
// read length header
unsigned long long bytesLen = 0;
if (USE_LENGTH_HEADER) {
long n = read(sockfd, &bytesLen, sizeof(bytesLen));
if (n != 0 && n != sizeof(bytesLen))
error("ERROR reading length header failed");
}
// read data
long n;
do {
memset(buffer, 0, TCPNET_BUFFER_SIZE);
@@ -153,7 +160,7 @@ std::string TcpNet::read_from_socket(int sockfd) {
std::string part{buffer};
request.append(part);
} while (n == TCPNET_BUFFER_SIZE - 1); // TODO what if data exactly of this size
} while ((USE_LENGTH_HEADER && n < bytesLen) || n == TCPNET_BUFFER_SIZE - 1); // TODO what if data exactly of this size
return request;
}
@@ -162,6 +169,16 @@ void TcpNet::write_to_socket(int sockfd, const std::string &str) {
const char *buffer = str.c_str();
int pos = 0;
long n;
// write length header
unsigned long long bytesLen = str.length();
if (USE_LENGTH_HEADER) {
n = write(sockfd, &bytesLen, (int) sizeof(bytesLen));
if (n < 0)
error("ERROR writing size number to socket");
}
// write data
do {
n = write(sockfd, buffer + pos, (int) (str.length() - pos));
if (n < 0)

View File

@@ -17,6 +17,7 @@ public:
[[nodiscard]] std::vector<std::string> client(const std::string &address, int portno, const std::vector<std::string> &requests) const;
private:
static const bool USE_LENGTH_HEADER = true;
static std::string read_from_socket(int sockfd) ;
static void write_to_socket(int sockfd, const std::string &str) ;
};

View File

@@ -1,4 +1,19 @@
; (system-cmd-fork "ml" "-c" "(print 123) (sleep 1) (print \"aaa\")")
(print (reduce (lambda (acc e) (+ acc (string (+ "<th>" e "</th>")))) "" '("h1" "h2" "h3"))
(thread-create
(tcp-server 7777 (lambda (str) (list #f (+ "(print \"" (string-upcase str) "\")"))))
)
(thread-sleep 1)
(thread-create
(def code (tcp-client "127.0.0.1" 7777 ("abcd" "xyz")))
(for c code
(print "executing code:" c)
(eval (parse c))
)
(def code (tcp-client "127.0.0.1" 7777 "abcd"))
(print "executing code:" code)
(eval (parse code))
)
(threads-join)
(print "ok")

View File

@@ -21,7 +21,7 @@ RUN cd /mlisp \
# Create image and copy compiled installation into it
FROM alpine:3.15.4
RUN apk add openssl libstdc++
RUN apk add --no-cache openssl libstdc++
CMD mkdir -p /usr/local/var/mlisp/
COPY --from=builder /mlisp/stdlib/*.lsp /usr/local/var/mlisp/

1
tests/cmd_fork.lsp Normal file
View File

@@ -0,0 +1 @@
(system-cmd-fork "ml" "-c" "(print 123) (sleep 1) (print \"aaa\")")

19
tests/test_tcp.lsp Normal file
View File

@@ -0,0 +1,19 @@
(thread-create
(tcp-server 7777 (lambda (str) (list #f (+ "(print \"" (string-upcase str) "\")"))))
)
(thread-sleep 1)
(thread-create
(def code (tcp-client "127.0.0.1" 7777 ("abcd" "xyz")))
(for c code
(print "executing code:" c)
(eval (parse c))
)
(def code (tcp-client "127.0.0.1" 7777 "abcd"))
(print "executing code:" code)
(eval (parse code))
)
(threads-join)
(print "ok")

20
wip.lsp
View File

@@ -3,26 +3,6 @@
(print (filter (lambda (eval e) (print e)) l))
(thread-create
(tcp-server 7777 (lambda (str) (list #f (+ "(print \"" (string-upcase str) "\")"))))
)
(thread-sleep 1)
(thread-create
(def code (tcp-client "127.0.0.1" 7777 ("abcd" "xyz")))
(for c code
(print "executing code:" c)
(eval (parse c))
)
(def code (tcp-client "127.0.0.1" 7777 "abcd"))
(print "executing code:" code)
(eval (parse code))
)
(threads-join)
(print "ok")
;; (usql "create table sf1 (symbol varchar(8), dimension varchar(3), calendar_date date, date_key date, report_period date, last_updated date, accoci float, assets float, assetsavg float, assetsc float, assetsnc float, assetturnover float, bvps float, capex float, cashneq float, cashnequsd float, cor float, consolinc float, currentratio float, de float, debt float, debtc float, debtnc float, debtusd float, deferredrev float, depamor float, deposits float, divyield float, dps float, ebit float, ebitda float, ebitdamargin float, ebitdausd float, ebitusd float, ebt float, eps float, epsdil float, epsusd float, equity float, equityavg float, equityusd float, ev float, evebit float, evebitda float, fcf float, fcfps float, fxusd float, gp float, grossmargin float, intangibles float, intexp float, invcap float, invcapavg float, inventory float, investments float, investmentsc float, investmentsnc float, liabilities float, liabilitiesc float, liabilitiesnc float, marketcap float, ncf float, ncfbus float, ncfcommon float, ncfdebt float, ncfdiv float, ncff float, ncfi float, ncfinv float, ncfo float, ncfx float, netinc float, netinccmn float, netinccmnusd float, netincdis float, netincnci float, netmargin float, opex float, opinc float, payables float, payoutratio float, pb float, pe float, pe1 float, ppnenet float, prefdivis float, price float, ps float, ps1 float, receivables float, retearn float, revenue float, revenueusd float, rnd float, roa float, roe float, roic float, ros float, sbcomp float, sgna float, sharefactor float, sharesbas float, shareswa float, shareswadil float, sps float, tangibles float, taxassets float, taxexp float, taxliabilities float, tbvps float, workingcapital float)")
;; (usql "set 'DATE_FORMAT' = '%Y-%m-%d'")