Giter Club home page Giter Club logo

demogorgon's Introduction

The following program waits for various events to occur, then executes the callback blocks specified for each one.

require "demogorgon"

Demogorgon.new do

  # executes after everything is setup but before any other event
  on_boot do
    puts "demon online"
  end

  # do this for each line of standard input, nothing will happen at EOF
  stdin do |msg|
    puts "stdin: #{msg}"
  end

  # do this for the first line of each connection on port 12346, then disconnect them
  # use the tell action to send data to them before disconnecting
  on_message 12346 do |msg, tell|
    puts "message: #{msg}"
    tell["yeah I heard\n"]
  end

  # same as on_message but don't wait for any input, disconnect immediately afterward
  on_connect 12345 do |tell|
    puts "connect"
    tell["hello world\n"]
  end

  # an extended two-way session
  dialog 12347, {
    :connect => lambda do |id, tell, kick|
      puts "#{id} connected"
      tell["hello\n"]
    end,
    :message => lambda do |id, msg|
      puts "#{id}: #{msg}"
    end,
    :disconnect => lambda do |id|
      puts "#{id} disconnect"
    end
  }

  # do this in response to inotify filesystem events, such as a touch command
  monitor 'some/file', [:all_events] do |filename, events|
    puts "#{filename} #{events.inspect}"
  end

  # do on a cron-like schedule (sec, min, hour, month, day, day-of-week)
  on_schedule "0 */15 * * * *" do |now|
    puts "BING BONG"
  end

  # think of this as "tail -f", use it on a named pipe
  each_line_in_file 'bar-stream' do |msg|
    puts "tail: #{msg}"
  end

  # These INT and TERM signal callbacks will run before the program ends.
  # Warning, these may occur *in the middle of* some other operation, though
  # you can consider that operation to be cancelled at this point mid-way

  on_ctrl_c do
    puts "CTRL C PRESSED"
  end

  on_terminate do
    puts "I WAS KILLED"
  end

end

Demogorgon can be equipped with an ACID memory which will survive crashes and restarts.

require "demogorgon"
require "acid"

acid = Acid.new "state.log" do

  init do
    0
  end

  view :show do |s|
    s.to_s
  end

  update :bump! do |s|
    s + 1
  end

  update :set! do |s, n|
    n
  end

end

Demogorgon.new do

  on_connect 12345 do |tell|
    tell[acid.show + "\n"]
    acid.bump!
  end

  stdin do |msg|
    acid.set! msg.to_i
  end

end

demogorgon's People

Contributors

evanrinehart avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.