Obsolete
/
ipc.cr-old
Archived
3
0
Fork 0

High level API: first take.

poll
Karchnu 2020-07-02 21:13:54 +02:00
parent 6f5aa676b5
commit ac7bb07d95
3 changed files with 98 additions and 91 deletions

View File

@ -3,18 +3,22 @@ require "./message"
require "./event" require "./event"
class IPC::Connection class IPC::Connection
getter ctx : LibIPC::Ctx
getter connection : LibIPC::Connection getter connection : LibIPC::Connection
getter closed = false getter closed = false
# connection already established # connection already established
def initialize(c : LibIPC::Connection) def initialize(c : LibIPC::Ctx)
@connection = c @ctx = c
end end
# def initialize(c : LibIPC::Connection)
# @connection = c
# end
def initialize(service_name : String) def initialize(service_name : String)
@connection = LibIPC::Connection.new @ctx = LibIPC::Ctx.new
r = LibIPC.ipc_connection(LibC.environ, self.pointer, service_name) r = LibIPC.ipc_connection(self.pointer, service_name)
if r.error_code != 0 if r.error_code != 0
m = String.new r.error_message.to_slice m = String.new r.error_message.to_slice
raise Exception.new "error during connection establishment: #{m}" raise Exception.new "error during connection establishment: #{m}"
@ -148,15 +152,10 @@ class IPC::Connections
end end
end end
def wait_event(server : IPC::Connection | Nil, &block) : Tuple(LibIPC::EventType, IPC::Message, IPC::Connection) | Tuple(LibIPC::EventType, Nil, Nil) def wait_event(&block) : IPC::Event::Events | Exception
event = LibIPC::Event.new event = LibIPC::Event.new
serverp = nil r = LibIPC.ipc_events_loop self.pointer, pointerof(event), pointerof(@timer)
unless server.nil?
serverp = server.pointer
end
r = LibIPC.ipc_wait_event self.pointer, serverp, pointerof(event), pointerof(@timer)
if r.error_code != 0 if r.error_code != 0
m = String.new r.error_message.to_slice m = String.new r.error_message.to_slice
yield IPC::Exception.new "error waiting for a new event: #{m}" yield IPC::Exception.new "error waiting for a new event: #{m}"
@ -166,52 +165,41 @@ class IPC::Connections
# if event type is Timer, there is no connection nor message # if event type is Timer, there is no connection nor message
case eventtype case eventtype
when LibIPC::EventType::Timer when LibIPC::EventType::NotSet
return eventtype, nil, nil return Exception.new "'Event type: not set"
end when LibIPC::EventType::Error
return IPC::Event::Error.new event.index, event.origin
connection = IPC::Connection.new event.origin.unsafe_as(Pointer(LibIPC::Connection)).value when LibIPC::EventType::ExtraSocket # Message received from a non IPC socket.
return IPC::Event::ExtraSocket.new event.origin, event.index
when LibIPC::EventType::Switch # Message to send to a corresponding fd.
return IPC::Event::Switch.new event.origin, event.index
when LibIPC::EventType::Connection # New user.
return IPC::Event::Connection.new event.origin, event.index
when LibIPC::EventType::Disconnection # User disconnected.
return IPC::Event::Disconnection.new event.origin, event.index
when LibIPC::EventType::Message # New message.
message = event.message.unsafe_as(Pointer(LibIPC::Message)) message = event.message.unsafe_as(Pointer(LibIPC::Message))
return IPC::Event::MessageReceived.new event.origin, event.index, message
return eventtype, IPC::Message.new(message), connection when LibIPC::EventType::LookUp # Client asking for a service through ipcd.
# for now, the libipc does not provide lookup events
# ipcd uses a simple LibIPC::EventType::Message
return IPC::Event::LookUp.new event.origin, event.index
when LibIPC::EventType::Timer # Timeout in the poll(2) function.
return IPC::Event::Timer.new
when LibIPC::EventType::Tx # Message sent.
return IPC::Event::Tx.new event.origin, event.index
end end
def loop(server : IPC::Connection | IPC::Server | ::Nil, &block : Proc(IPC::Event::Events|Exception, Nil)) yield Exception.new "Cannot understand the event type: #{eventtype}"
end
def loop(&block : Proc(IPC::Event::Events|Exception, Nil))
if @base_timer > 0 && @timer == 0 if @base_timer > 0 && @timer == 0
@timer = @base_timer @timer = @base_timer
end end
::loop do ::loop do
type, message, connection = wait_event server, &block yield wait_event &block
case type
when LibIPC::EventType::Timer
# reset timer
@timer = @base_timer
yield IPC::Event::Timer.new
when LibIPC::EventType::Connection
yield IPC::Event::Connection.new connection.not_nil!
when LibIPC::EventType::NotSet
yield IPC::Exception.new "even type not set"
when LibIPC::EventType::Error
yield IPC::Exception.new "even type indicates an error"
when LibIPC::EventType::ExtraSocket
yield IPC::Event::ExtraSocket.new message.not_nil!, connection.not_nil!
when LibIPC::EventType::Switch
yield IPC::Event::Switch.new message.not_nil!, connection.not_nil!
when LibIPC::EventType::Message
yield IPC::Event::Message.new message.not_nil!, connection.not_nil!
# for now, the libipc does not provide lookup events
# networkd uses a simple LibIPC::EventType::Message
# when LibIPC::EventType::LookUp
# yield IPC::Event::LookUp.new message, connection
when LibIPC::EventType::Disconnection when LibIPC::EventType::Disconnection
yield IPC::Event::Disconnection.new connection.not_nil! yield IPC::Event::Disconnection.new connection.not_nil!
@ -221,10 +209,10 @@ class IPC::Connections
# sanitizer # sanitizer
def pointer def pointer
pointerof(@connections) pointerof(@ctx)
end end
def pp # def pp
LibIPC.ipc_connections_print @connections # LibIPC.ipc_connections_print @connections
end # end
end end

View File

@ -3,37 +3,55 @@ require "./message"
require "./connection" require "./connection"
class IPC::Event class IPC::Event
alias Events = IPC::Event::Timer | IPC::Event::Connection | IPC::Event::Disconnection | IPC::Event::Message | IPC::Event::ExtraSocket | IPC::Event::Switch | IPC::Event::LookUp alias Events = IPC::Event::Timer |
IPC::Event::Error |
IPC::Event::Connection |
IPC::Event::Disconnection |
IPC::Event::MessageReceived |
IPC::Event::ExtraSocket |
IPC::Event::Switch |
IPC::Event::LookUp |
IPC::Event::MessageSent
end
class Timer class IPC::Event::Timer < IPC::Event
end def initialize
class Connection
getter connection : IPC::Connection
def initialize(@connection)
end
end
class Disconnection
getter connection : IPC::Connection
def initialize(@connection)
end
end
class Message
getter message : ::IPC::Message
getter connection : IPC::Connection
def initialize(@message, @connection)
end
end
class ExtraSocket < IPC::Event::Message
end
class Switch < IPC::Event::Message
end
class LookUp < IPC::Event::Message
end end
end end
class IPC::Event::Base < IPC::Event
getter fd : Int
getter index : UInt64
def initialize(@fd, @index)
end
end
class IPC::Event::Connection < IPC::Event::Base
end
class IPC::Event::Disconnection < IPC::Event::Base
end
class IPC::Event::Error < IPC::Event::Base
end
class IPC::Event::MessageReceived < IPC::Event::Base
getter message : ::IPC::Message
def initialize(@fd, @index, @message)
end
end
class IPC::Event::ExtraSocket < IPC::Event::Base
end
class IPC::Event::Switch < IPC::Event::Base
end
class IPC::Event::LookUp < IPC::Event::Base
end
class IPC::Event::MessageSent < IPC::Event::Base
end

View File

@ -51,15 +51,16 @@ lib LibIPC
end end
enum EventType enum EventType
NotSet NotSet #
Error Error #
ExtraSocket ExtraSocket # Message received from a non IPC socket.
Switch Switch # Message to send to a corresponding fd.
Connection Connection # New user.
Disconnection Disconnection # User disconnected.
Message Message # New message.
LookUp LookUp # Client asking for a service through ipcd.
Timer Timer # Timeout in the poll(2) function.
Tx # Message sent.
end end
struct Event struct Event