-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathport_publisher.rb
53 lines (38 loc) · 1.32 KB
/
port_publisher.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
################################################################
###
## File: PortPublisher.rb
## Desc: Send a byte stream to a specific IP / PORT
#
require 'socket'
require 'string_mods'
class PortPublisher
attr_reader :tcp_ip
attr_reader :tcp_port
attr_reader :connection
def initialize(tcp_ip='127.0.0.1', tcp_port=50002)
@tcp_ip = tcp_ip
@tcp_port = 'String' == tcp_port.class.to_s ? tcp_port.to_i : tcp_port
@connection = nil
begin
@connection = TCPSocket::new( @tcp_ip, @tcp_port )
puts "#{self.class} established TCP connection with #{@tcp_ip}:#{@tcp_port}" if $debug
rescue Exception => e
puts "ERROR: PortPublisher unable to open TCPSocket"
puts "TCP Socket Error: #{e}"
puts " tcp_ip: #{@tcp_ip}"
puts " tcp_port: #{@tcp_port}"
end
end ## end of def initialize(tcp_ip, tcp_port)
#################################
# SMELL: Assumes data is a string
def send_data(data)
return nil if @connection.nil? or data.nil? or data.empty?
begin
@connection.send(data, 0)
puts "#{self.class} send to #{@tcp_ip}:#{@tcp_port} this: #{data.to_hex}" if $debug
rescue
@connection = nil
return nil
end
end ## end of def send_data(data)
end ## end of class PortPublisher