Module: Voyager::SIP::BasicSipClient

Included in:
Client
Defined in:
lib/voyager/sip/basic_client.rb

Constant Summary collapse

VERBOSE =
true
TIMESTAMP_FORMAT =
"%Y%m%d    %H%M%S"

Instance Method Summary collapse

Instance Method Details

#closeObject



50
51
52
53
# File 'lib/voyager/sip/basic_client.rb', line 50

def close
  Rails::logger.info "SIPClient: Connection closed." if VERBOSE
  @socket.close
end

#initialize(host, port) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/voyager/sip/basic_client.rb', line 12

def initialize(host, port)
  Rails::logger.info "SIPClient: Connecting with #{host}:#{port}" if VERBOSE
  @socket = TCPSocket::new(host, port)

  if block_given?
    begin
      yield self
    ensure
      # When given a socket, we automatically close connection
      close
    end
  end
end

#recvObject



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/voyager/sip/basic_client.rb', line 36

def recv
  msg = ""
  until msg =~ /\r/
    result = IO::select([@socket], nil, nil)

    for inp in result[0]
      msg << @socket.recv(300);
    end
  end

  Rails::logger.info "SIPClient: Recieved #{msg.strip}" if VERBOSE
  msg
end

#send(msg) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/voyager/sip/basic_client.rb', line 26

def send(msg)
  Rails::logger.info "SIPClient: Sending #{msg}" if VERBOSE
  @socket.write("#{msg.strip}\r")

  # If a block is given, yield the response
  if block_given?
    yield recv
  end
end

#timestampObject



55
56
57
# File 'lib/voyager/sip/basic_client.rb', line 55

def timestamp
  Time.now.strftime(TIMESTAMP_FORMAT)
end