Skip to content

Instantly share code, notes, and snippets.

@jfdm
Last active March 28, 2023 13:14
Show Gist options
  • Save jfdm/d89ffdb5ad1df19c9d120cb4d843a296 to your computer and use it in GitHub Desktop.
Save jfdm/d89ffdb5ad1df19c9d120cb4d843a296 to your computer and use it in GitHub Desktop.
Sample IPC between Idris and a process using Domain Sockets; an improvement would be to have `Client.idr` launch `Server.py` itself.
module Bar
import System
import System.File
import Data.String
namespace Main
export
main : IO ()
main
= do str <- getLine
Right str <- fPutStrLn stdout (trim str) | Left err => printLn err
exitSuccess
module Client
import System
import System.File
import Data.String
import Network.Socket
namespace Main
export
main : IO ()
main
= do Right sock <- socket AF_UNIX Stream 0
| Left err => putStrLn "Error creating \{show err}"
res <- connect sock (Hostname "/tmp/capable-test.sock") 0
if not (res == 0)
then putStrLn "Error connecting \{show res}"
else do Right res <- send sock "Echo\n"
| Left err => putStrLn "Error sending \{show err}"
putStrLn "Sent Echo"
Right msg <- recvAll sock
| Left err => putStrLn "Error receiving \{show err}"
putStrLn "Server says: \{msg}"
import socket as s
import subprocess
import tempfile
with s.socket(s.AF_UNIX, s.SOCK_STREAM) as sock:
sock.bind("/tmp/capable-test.sock")
sock.listen()
print("Listening")
conn, addr = sock.accept()
print("Spawing")
with conn:
with subprocess.Popen(
["./build/exec/bar"], # yes the actual process is an Idris one...
stdin =subprocess.PIPE,
stdout=subprocess.PIPE
) as proc:
i = conn.recv(1024)
# print("Received")
# print(i)
proc.stdin.write(i)
proc.stdin.flush()
o = proc.stdout.readline()
# print("Sending")
# print(o)
conn.sendall(o)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment