Skip to content

Instantly share code, notes, and snippets.

@GiuseppeChillemi
Last active July 11, 2022 23:34
Show Gist options
  • Save GiuseppeChillemi/214482b96832c44a77ea3cf848362554 to your computer and use it in GitHub Desktop.
Save GiuseppeChillemi/214482b96832c44a77ea3cf848362554 to your computer and use it in GitHub Desktop.
An experimental server which waits and executes the Rebol commands it receives taking them from a command block
SERVER-DB: "myserver"
DB: "mydb"
port-number: 55551
server-address: "localhost"
;--- These are the commands we could execute
commands: make object! [
get-product: func [] [ctx-munge/sqlcmd server-DB db "select CD_AR, descrizione from AR"]
test: funct [] [
;Generates a big chunk of 'chunk-size' with identifiable data and control string!
res: copy ""
str-ln: 25
chunk-size: 500000
check-phrase: "!THIS-SHOULD-START/END-WITH-AN-ESCALAMATION!"
ln-check-prase: length? check-phrase
repeat-times: to-integer chunk-size / (str-ln + ln-check-prase)
repeat idx repeat-times [
idx2: to-string idx
times: str-ln - (length? to-string idx2)
str-to-append: copy ""
str-to-append: loop times [insert head str-to-append "0"]
change str-to-append rejoin ["LAST:" repeat-times":"]
append str-to-append idx 2
append res rejoin [str-to-append check-phrase]
]
;probe skip tail res -200
res
]
]
waiting-for: copy []
Print "You can input the following commands: "
probe commands-list: words-of commands
print "Opening the server"
server: open/direct/no-wait to-url rejoin ["tcp://:" port-number]
;Adding it to a block with all the server
append waiting-for server
print "We have opened the server"
forever [
new-connection: wait waiting-for
;(Investigate on the use of FIRST on server)
print "Something Happened on the connection"
either new-connection = server [
Print "A new client has arrived!!!"
append waiting-for first new-connection
] [
print "We have an event!!!"
;==== Wait for the server to return the client connection
;
; wait client
Print "pre-get-data"
;Returns everything is on that port,
; or it waits for the end of connetction
; I don't know what happens if the internal Rebol buffer ends.
; Will return too and you have to join the previous to the new chunck of data
;Getting the data arrived from port or give to copy and empty block to not generate an errore
data: copy either port? new-connection
[
Print "A new event has happened, the connection is a port"
new-connection
]
[
Print "A new connection has happened, there is NO port"
[]
;It is used when the connection does not return anything like:
; it is in TIMEOUT STATE
; Or you do something like WAIT [PORT 10]
]
Prin "----------- Data Type of DATA is: --------------: " probe type? data
;--- If the port is opened in a mode it returns a string, insert it in a block
;
if string? data [data: append copy [] data]
either (type? data) = block! [
print ["Data is a block and queue length is: " length? data]
] [
Print ["Connection has been closed, data type is: " type? data]
]
case [
;TBD: Unterstand how to work in case of network error
;TBD: Test TRY
data = none [print ["Connection has been closed"] remove find waiting-for new-connection]
empty? data [print ["Connection in timeout!!"] remove find waiting-for new-connection]
block? data [
print "------------ DATA: ------------"
print ["data: " data]
print "------------ END DATA: --------"
print "------------ First string od DATA ------------"
;probe incoming-commands: attempt [first data]
probe incoming-commands: first data ;Later I will parse it
print "------------ Loading the commands to execute ------------"
;--- Commands are passed as [COMMAND-NAME [ARGS]]
probe the-commands: attempt [append copy [] load incoming-commands]
print "------------ End load phase ------------"
;close new-connection ; It correctly goes on error. Input 55551 here we have 55630
print "----------------- Evaluate the commands ----------------"
response: case [
;--- Commands are passed as [COMMAND-NAME [ARGS]], here we are getting the COMMAND-NAME
; in the methods of the object
all [found? find commands-list first the-commands] [
;TBD:
; Manage non existing commands
; Wrap the data to send with leading and trailing identificator, to debug it
; Analize how to manage a server crash at destination
;--- Here we run a command with a single block argument
return-value: mold/all
commands/(the-commands/1) ;pick the-commands 2
Print "---------- The command has been executed -----------"
return-value
]
true [
rejoin ["--- DATA server echo ----> " data]
]
]
Print ["xx- Response length: " length? response]
;Sending the result
;
insert new-connection response
] ;Se lo metto prima intercetto anche il blocco vuoto!
]
Print "after-get-data"
]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment