Skip to content

Commit 96cde27

Browse files
committed
Add IPC example.
1 parent 584440c commit 96cde27

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

examples/ipc/client.rb

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env async-service
2+
# frozen_string_literal: true
3+
4+
# This file demonstrates running just the client component
5+
# Run with: async-service examples/ipc/client.rb
6+
# (Make sure server.rb is running first)
7+
8+
require 'socket'
9+
10+
# Shared environment for IPC configuration
11+
environment(:ipc) do
12+
# IPC socket path - defaults to service.ipc in current directory
13+
ipc_socket_path File.expand_path("service.ipc", Dir.pwd)
14+
end
15+
16+
class IPCClient < Async::Service::Generic
17+
def setup(container)
18+
super
19+
20+
container.run(count: 1, restart: true) do |instance|
21+
socket_path = evaluator.ipc_socket_path
22+
23+
Console.info(self) { "IPC Client starting - will connect to #{socket_path}" }
24+
instance.ready!
25+
26+
while true
27+
begin
28+
# Connect to server
29+
client = UNIXSocket.new(socket_path)
30+
Console.info(self) { "Connected to server" }
31+
32+
# Read response
33+
response = client.readline.chomp
34+
puts "📨 Received: #{response}"
35+
36+
client.close
37+
Console.info(self) { "Connection closed" }
38+
39+
# Wait before next connection
40+
sleep(2)
41+
42+
rescue Errno::ENOENT
43+
Console.warn(self) { "Server socket not found at #{socket_path}, retrying..." }
44+
sleep(3)
45+
rescue Errno::ECONNREFUSED
46+
Console.warn(self) { "Connection refused, server may not be ready" }
47+
sleep(3)
48+
rescue => error
49+
Console.error(self, error)
50+
sleep(2)
51+
end
52+
end
53+
end
54+
end
55+
end
56+
57+
service "ipc-client" do
58+
service_class IPCClient
59+
environment :ipc
60+
end

examples/ipc/server.rb

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env async-service
2+
# frozen_string_literal: true
3+
4+
# This file demonstrates running just the server component
5+
# Run with: async-service examples/ipc/server.rb
6+
7+
require 'socket'
8+
9+
# Shared environment for IPC configuration
10+
environment(:ipc) do
11+
# IPC socket path - defaults to service.ipc in current directory
12+
ipc_socket_path File.expand_path("service.ipc", Dir.pwd)
13+
end
14+
15+
class IPCServer < Async::Service::Generic
16+
def setup(container)
17+
super
18+
19+
container.run(count: 1, restart: true) do |instance|
20+
socket_path = evaluator.ipc_socket_path
21+
22+
# Clean up any existing socket
23+
File.unlink(socket_path) if File.exist?(socket_path)
24+
25+
# Create Unix domain socket server
26+
server = UNIXServer.new(socket_path)
27+
28+
Console.info(self) { "IPC Server listening on #{socket_path}" }
29+
instance.ready!
30+
31+
begin
32+
while true
33+
# Accept incoming connections
34+
client = server.accept
35+
Console.info(self) { "Client connected from PID #{client.peereid[0]}" }
36+
37+
# Send greeting with timestamp
38+
timestamp = Time.now.strftime("%H:%M:%S")
39+
client.write("Hello World at #{timestamp}\n")
40+
client.close
41+
42+
Console.info(self) { "Sent greeting and closed connection" }
43+
end
44+
rescue => error
45+
Console.error(self, error)
46+
ensure
47+
server&.close
48+
File.unlink(socket_path) if File.exist?(socket_path)
49+
end
50+
end
51+
end
52+
end
53+
54+
service "ipc-server" do
55+
service_class IPCServer
56+
environment :ipc
57+
end

0 commit comments

Comments
 (0)