Module: Udat::Demo

Defined in:
lib/udat.rb

Overview

Module containing some demonstration functions.

Constant Summary collapse

ExampleDocument =

A string containing an UDAT example document.

"[sample config v1.0|\n\n  <network> [\n    <max connections> [256]\n    <reverse lookups> [yes]\n  ]\n\n  <locale> [\n    <language> [de]    german language\n    <timezone> [CET]   and CET timezone\n    Comments can appear almost anywhere.\n  ]\n\n  <access control> [\n    <allowed> [\n      [user|martin]\n      [user|max]\n      [group|admins]\n      [ip4network|\n        <address> [192.168.0.0]\n        <netmask> [255.255.255.0]\n      ]\n    ]\n    <blocked> [~]   The tilde symbol denotes an empty collection.\n  ]\n\n  <misc> [\n    <symbol for homedir> [\\~]   Not an empty collection but the scalar\n                                value \"~\".\n  ]\n\n  <address mappings> [\n\n    <email|\n      <user>   [jan.behrens]\n      <domain> [flexiguided.de]\n    >\n    [mbox|jan]\n\n    <email|\n      <user>   [glob|*]\n      <domain> [flexiguided.de]\n    >\n    [mbox|catchall]\n\n  ]\n\n  <logging> [\n    <verbosity>   [high]\n\\## <verbosity>   [debug] ##   Uncomment this for debug output.\n    <destination> [file|/var/log/sample.log]\n  ]\n\n]\n"

Class Method Summary collapse

Class Method Details

.rpc_selftest(port = 10330) ⇒ Object

Method to test the RPC mechanism, not for production use.



1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
# File 'lib/udat.rb', line 1265

def rpc_selftest(port = 10330)
  a = rand(100) + 1
  b = rand(100) + 1
  demo_server_thread = nil
  begin
    demo_server_thread = Thread.new do
      Udat::Demo::run_rpc_demo_server(port)
    end
    sleep 0.5
    udat_result = nil
    Timeout.timeout(15) do
      udat_result = {'operands' => [a, b], 'operator' => '+'}.
                    to_udat("calculation request").
                    rpc('localhost', port)
    end
    if udat_result.tag == "error"
      raise "RPC call returned with an error: " <<
        udat_result.fetch_scalar("message").to_s
    end
    result = udat_result.require_scalar("calculation result").to_f
  ensure
    demo_server_thread.kill
  end
  unless result == a + b
    raise "Calculation error during UDAT RPC selftest."
  end
  return "Test done: #{a} + #{b} == #{result}"
end

.run_rpc_demo_server(port, timeout = 180) ⇒ Object

Runs a demo server, not for production use.



1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
# File 'lib/udat.rb', line 1223

def run_rpc_demo_server(port, timeout = 180)
  Udat::run_rpc_server(port, timeout) do |query|
    begin
      case query.tag
      when "echo request"
        query.tag = "echo reply"
        next query
      when "calculation request"
        begin
          query.require_collection
          operands = query.fetch_collection("operands")
          operand1 = operands.fetch_scalar(0).to_f
          operand2 = operands.fetch_scalar(1).to_f
        end
        operand1 = operands[0].to_f
        operand2 = operands[1].to_f
        operator = query.fetch_scalar("operator")
        case operator
        when "+".to_udat
          next (operand1 + operand2).to_udat("calculation result")
        when "-".to_udat
          next (operand1 - operand2).to_udat("calculation result")
        when "*".to_udat
          next (operand1 * operand2).to_udat("calculation result")
        when "/".to_udat
          next (operand1.quo operand2).to_udat("calculation result")
        else
          next([["message", "Unknown operator."]].to_udat("error")
          )
        end
      when "time request"
        next Time.now.strftime("%Y-%m-%d %H:%M:%S %Z").to_udat('time')
      else
        raise Udat::UdatTagMismatch, "Unknown UDAT tag."
      end
    rescue Udat::UdatPropertyMismatch, UdatIndexError
      next [["message", $!.message]].to_udat("error")
    end
  end
end