View sockets.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'socket' | |
server1 = TCPServer.new('0.0.0.0', 1337) | |
server2 = TCPServer.new('0.0.0.0', 1338) | |
servers = [server1, server2] | |
clients = [] | |
loop do | |
readables, * = IO.select(servers+clients) |
View s.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'socket' | |
class Page | |
attr_reader :body, :method, :path | |
def initialize(m, p, b) | |
@body, @method, @path = b, m, p | |
end | |
end | |
class Server |
View flatten.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Fluent | |
class TextParser | |
class StringifiedValuesJSONParser < JSONParser | |
Plugin.register_parser("stringified_values_json", self) | |
def parse(text) | |
time, record = super(text) | |
# replace all Hash (like a dict) values with stringified JSON | |
record = record.inject({}) { | new_hash, (key, value) | |
View waiter.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Waiter | |
def initialize | |
@m, @cv = Mutex.new, ConditionVariable.new | |
@signalled = false | |
end | |
def wait | |
@m.synchronize{ @cv.wait(@m) until @signalled; @signalled = false } | |
end | |
def signal | |
@m.synchronize{ @signalled = true; @cv.signal } |
View open3.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'open3' | |
Open3.popen3 'bash' do |stdin, stdout, stderr, wait| | |
t=Thread.new{ stdout.each{|l| print l } } | |
stdin.puts 'ls' | |
stdin.puts 'exit' | |
wait.join | |
t.join | |
end |
View get_data.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GetData | |
@@logger = Logger.new('log.log') | |
def initialize | |
@yesterday = Date.today.prev_day | |
@token = xxx | |
end | |
def save_actions | |
with_database do |db| |
View act.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
w_r, w_w = IO.pipe | |
t_r, t_w = IO.pipe | |
work = Thread.new { block.call; w_w.write('.') } | |
timeout = Thread.new{ sleep 10; t_w.write('.') } | |
io = IO.select [w_r, t_r] | |
if io == t_r | |
#Timeout |
View get_transactions.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /^(\d{2}\/\d{2}) (.*) (\-?\d+\.\d+)/ | |
File.open ARGV[0], 'r' do |file| | |
file.each_line do |line| | |
if m = line.match(/^(\d{2}\/\d{2}) (.*) (\-?\d+\.\d+)/) | |
puts [m[1], m[2], m[3]].join(', ') | |
end | |
end |
View conn.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'thread' | |
queues = Array.new(10){ Queue.new } | |
list_of_hosts.each_slice(10).map{|s| s.each_with_index{|h,i| queues[i] << h } } | |
threads = 10.times.map do |i| | |
Thread.new do | |
while host = queues[i].pop | |
check host |
View rak.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Server | |
def call(env) | |
req = Request.new('http://www.example.com/') | |
headers = nil | |
req.on_headers{|h| headers = h } | |
req.start | |
sleep 0.1 until headers | |
[200, headers, Enumerator.new(streamer, :on_body)] | |
end |