Class: FSPSocket::PSocket
- Inherits:
-
Object
- Object
- FSPSocket::PSocket
- Includes:
- FSPSocket
- Defined in:
- lib/fspsocket.rb
Constant Summary collapse
- @@id_count =
0
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Class Method Summary collapse
Instance Method Summary collapse
- #close ⇒ Object
-
#connect(dst_id) ⇒ Object
used to connect to multiple destinations.
- #connect_channel(dst_fullpath) ⇒ Object
- #delete_channel ⇒ Object
-
#initialize(*args) ⇒ PSocket
constructor
A new instance of PSocket.
- #puts(data) ⇒ Object (also: #_puts)
- #received(&block) ⇒ Object
- #update(msg) ⇒ Object
Methods included from FSPSocket
Constructor Details
#initialize(*args) ⇒ PSocket
Returns a new instance of PSocket.
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/fspsocket.rb', line 39 def initialize(*args) @host = Socket.gethostname @pid = Process.pid @id = [@host, @pid, @@id_count].join('_') @@id_count += 1 if args.length == 1 @block = args[0] end @sock_buf = [] Manager.instance.add_observer self init_channel end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
37 38 39 |
# File 'lib/fspsocket.rb', line 37 def id @id end |
Class Method Details
.open(dst_id, block) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/fspsocket.rb', line 125 def PSocket.open(dst_id, block) sock = PSocket.new(block) if (dst_id.kind_of?(Enumerable)) dst_id.each do |item| sock.connect_channel([@@base, item].join(File::SEPARATOR)) end else sock.connect_channel([@@base, dst_id].join(File::SEPARATOR)) end return sock end |
.write_to(path, data) ⇒ Object
162 163 164 165 166 167 168 |
# File 'lib/fspsocket.rb', line 162 def PSocket.write_to(path, data) @@log.info "write_to #{path} #{data}" File::open(path, "a+") do |f| h = {:time=>Time.now, :data=>URI.encode(data)} f.puts(h.to_json) end end |
Instance Method Details
#close ⇒ Object
148 149 150 151 |
# File 'lib/fspsocket.rb', line 148 def close delete_channel PSocket.write_to(@dst_cpath, "BYE #{@id}") end |
#connect(dst_id) ⇒ Object
used to connect to multiple destinations
138 139 140 141 142 143 144 145 146 |
# File 'lib/fspsocket.rb', line 138 def connect(dst_id) if (dst_id.kind_of?(Enumerable)) dst_id.each do |item| connect_channel([@@base, item].join(File::SEPARATOR)) end else connect_channel([@@base, dst_id].join(File::SEPARATOR)) end end |
#connect_channel(dst_fullpath) ⇒ Object
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/fspsocket.rb', line 100 def connect_channel(dst_fullpath) # 1. create my controls channel in the other's controls dir # /socks/dstid/controls/myid mycpath = [dst_fullpath, :controls, @id].join(File::SEPARATOR) @connected << mycpath FileUtils.touch(mycpath) sleep 1 # XXX for timing purpose # 2. let the other know my path PSocket.write_to(mycpath, "HELLO #{@id}") end |
#delete_channel ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/fspsocket.rb', line 111 def delete_channel @connected.each do |path| if FileTest.exist? path File::delete path end end path = [@@base, @id].join(File::SEPARATOR) if FileTest.exist? path File::delete [path, :data].join(File::SEPARATOR) FileUtils.rm_rf path end end |
#puts(data) ⇒ Object Also known as: _puts
157 158 159 160 |
# File 'lib/fspsocket.rb', line 157 def puts(data) #@@log.debug "XXX puts: #{@dpath} #{data}" PSocket.write_to(@dpath, data) end |
#received(&block) ⇒ Object
153 154 155 |
# File 'lib/fspsocket.rb', line 153 def received(&block) @recv_block = block end |
#update(msg) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/fspsocket.rb', line 71 def update(msg) @@log.info "--- update #{msg}" # msg[1]:data part can contain more than one line msg[1].each_line do |item| h = JSON.parse(item) d = h.fetch(:data.to_s) ud = URI.unescape(d) ar = ud.split if ar[0] == "HELLO" @@log.info "---got HELLO: #{ar[1]}" Manager.instance.add_file([@@base, ar[1], :data].join(File::SEPARATOR)) # XXX mycpath = [@@base, ar[1], :controls, @id].join(File::SEPARATOR) FileUtils.touch(mycpath) sleep 1 # XXX for timing purpose PSocket.write_to(mycpath, "OK #{@id}") elsif ar[0] == "OK" @@log.info "---got OK: #{ar[1]}" Manager.instance.add_file([@@base, ar[1], :data].join(File::SEPARATOR)) @block.call(method(:received)) elsif ar[0] == "BYE" @@log.info "---got BYE: #{ar[1]}" Manager.instance.remove_file([@@base, ar[1], :data].join(File::SEPARATOR)) else @recv_block.call(msg[0], ud) end end # each end |