Skip to content
This repository has been archived by the owner on Aug 14, 2020. It is now read-only.

Commit

Permalink
Added SystemCallbacks module for 3rd party modules to set custom call…
Browse files Browse the repository at this point in the history
…backs when OverSIP is started, reloaded (HUP signal) or stopped.
  • Loading branch information
ibc committed Sep 1, 2012
1 parent d9561be commit df1389e
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 16 deletions.
18 changes: 9 additions & 9 deletions etc/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ module MyExampleApp
### OverSIP System Events:


# This callback is called once the OverSIP reactor has been started.
# This method is called once the OverSIP reactor has been started.
#
# def (OverSIP::SystemEvents).on_started
# [...]
# end


# This callback is called when a USR1 signal is received by OverSIP main
# This method is called when a USR1 signal is received by OverSIP main
# process and allows the user to set custom code to be executed
# or reloaded.
#
Expand All @@ -50,7 +50,7 @@ module MyExampleApp
# end


# This callback is called after OverSIP has been terminated. It's called
# This method is called after OverSIP has been terminated. It's called
# with argument "error" which is _true_ in case OverSIP has died in an
# unexpected way.
#
Expand All @@ -64,7 +64,7 @@ module MyExampleApp
### OverSIP SIP Events:


# This callback is called when a SIP request is received.
# This method is called when a SIP request is received.
#
def (OverSIP::SipEvents).on_request request

Expand Down Expand Up @@ -223,7 +223,7 @@ def (OverSIP::SipEvents).on_request request
end


# This callback is called when a client initiates a SIP TLS handshake.
# This method is called when a client initiates a SIP TLS handshake.
def (OverSIP::SipEvents).on_client_tls_handshake connection, pems

log_info "validating TLS connection from IP #{connection.remote_ip} and port #{connection.remote_port}"
Expand All @@ -241,7 +241,7 @@ def (OverSIP::SipEvents).on_client_tls_handshake connection, pems
end


# This callback is called when conntacting a SIP TLS server and the TLS handshake takes place.
# This method is called when conntacting a SIP TLS server and the TLS handshake takes place.
def (OverSIP::SipEvents).on_server_tls_handshake connection, pems

log_info "validating TLS connection to IP #{connection.remote_ip} and port #{connection.remote_port}"
Expand All @@ -264,7 +264,7 @@ def (OverSIP::SipEvents).on_server_tls_handshake connection, pems
### OverSIP WebSocket Events:


# This callback is called when a new WebSocket connection is being requested.
# This method is called when a new WebSocket connection is being requested.
# Here you can inspect the connection and the HTTP GET request. If you
# decide not to accept this connection then call to:
#
Expand All @@ -279,7 +279,7 @@ def (OverSIP::SipEvents).on_server_tls_handshake connection, pems
# end


# This callback is called when a WebSocket connection is closed. The connection
# This method is called when a WebSocket connection is closed. The connection
# is given as first argument along with a second argument "client_closed" which
# is _true_ in case the WebSocket connection was closed by the client.
#
Expand All @@ -288,7 +288,7 @@ def (OverSIP::SipEvents).on_server_tls_handshake connection, pems
# end


# This callback is called when a client initiates a WebSocket TLS handshake.
# This method is called when a client initiates a WebSocket TLS handshake.
def (OverSIP::WebSocketEvents).on_client_tls_handshake connection, pems

log_info "validating TLS connection from IP #{connection.remote_ip} and port #{connection.remote_port}"
Expand Down
1 change: 1 addition & 0 deletions lib/oversip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
require "oversip/utils.rb"
require "oversip/posix_mq.rb"
require "oversip/default_server.rb"
require "oversip/system_callbacks.rb"



Expand Down
45 changes: 39 additions & 6 deletions lib/oversip/launcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -279,13 +279,24 @@ def self.run(options)

log_system_info "reactor running"

# Run the user provided on_started callback.
log_system_info "calling OverSIP::SystemEvents.on_started()..."
# Run the user provided on_started method.
log_system_info "calling OverSIP::SystemEvents.on_started() method..."
begin
::OverSIP::SystemEvents.on_started
rescue ::Exception => e
log_system_crit "error calling OverSIP::SystemEvents.on_started():"
fatal e
log_system_crit e
end

# Run the on_started provided callbacks.
log_system_info "executing OverSIP::SystemCallbacks.on_started_callbacks..."
::OverSIP::SystemCallbacks.on_started_callbacks.each do |cb|
begin
cb.call
rescue ::Exception => e
log_system_crit "error executing a callback in OverSIP::SystemCallbacks.on_started_callbacks:"
log_system_crit e
end
end

log_system_info "master process (PID #{$$}) ready"
Expand Down Expand Up @@ -424,11 +435,22 @@ def self.trap_signals
trap :HUP do
log_system_notice "HUP signal received, reloading configuration files..."
::OverSIP::Config.system_reload

# Run the on_reload provided callbacks.
log_system_info "executing OverSIP::SystemCallbacks.on_reload_callbacks..."
::OverSIP::SystemCallbacks.on_reload_callbacks.each do |cb|
begin
cb.call
rescue ::Exception => e
log_system_crit "error executing a callback in OverSIP::SystemCallbacks.on_reload_callbacks:"
log_system_crit e
end
end
end

# Signal USR1 reloads custom code provided by the user.
trap :USR1 do
log_system_notice "USR1 signal received, calling OverSIP::SystemEvents.on_user_reload()..."
log_system_notice "USR1 signal received, calling OverSIP::SystemEvents.on_user_reload() method..."
# Run the user provided on_started callback.
begin
::OverSIP::SystemEvents.on_user_reload
Expand All @@ -448,14 +470,25 @@ def self.trap_signals

def self.terminate error=false, fatal=false
unless fatal
# Run the user provided on_terminated callback.
log_system_info "calling OverSIP::SystemEvents.on_terminated()..."
# Run the user provided on_terminated method.
log_system_info "calling OverSIP::SystemEvents.on_terminated() method..."
begin
::OverSIP::SystemEvents.on_terminated error
rescue ::Exception => e
log_system_crit "error calling OverSIP::SystemEvents.on_terminated():"
log_system_crit e
end

# Run the on_terminated provided callbacks.
log_system_info "executing OverSIP::SystemCallbacks.on_terminated_callbacks..."
::OverSIP::SystemCallbacks.on_terminated_callbacks.each do |cb|
begin
cb.call error
rescue ::Exception => e
log_system_crit "error executing a callback in OverSIP::SystemCallbacks.on_terminated_callbacks:"
log_system_crit e
end
end
end

unless error
Expand Down
1 change: 0 additions & 1 deletion lib/oversip/master_process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
require "oversip/sip/modules/user_assertion.rb"
require "oversip/sip/modules/registrar_without_path.rb"

require "oversip/default_server.rb"
require "oversip/fiber_pool.rb"
require "oversip/tls.rb"
require "oversip/stun.so"
Expand Down
43 changes: 43 additions & 0 deletions lib/oversip/system_callbacks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module OverSIP

# This module is intended for 3rd party modules that need custom code to be
# executed when OverSIP is started, reloaded or terminated.
#
module SystemCallbacks

extend ::OverSIP::Logger

class << self
attr_reader :on_started_callbacks
attr_reader :on_terminated_callbacks
attr_reader :on_reload_callbacks
end

@on_started_callbacks = []
@on_terminated_callbacks = []
@on_reload_callbacks = []

def self.on_started pr=nil, &bl
block = pr || bl
raise ::ArgumentError, "no block given" unless block.is_a? ::Proc

@on_started_callbacks << block
end

def self.on_terminated pr=nil, &bl
block = pr || bl
raise ::ArgumentError, "no block given" unless block.is_a? ::Proc

@on_terminated_callbacks << block
end

def self.on_reload pr=nil, &bl
block = pr || bl
raise ::ArgumentError, "no block given" unless block.is_a? ::Proc

@on_reload_callbacks << block
end

end

end

0 comments on commit df1389e

Please sign in to comment.