Module: Udat

Defined in:
lib/udat.rb

Overview

Copyright © 2007 FlexiGuided GmbH, Berlin

Author: Jan Behrens

Website: www.flexiguided.de/publications.udat.en.html


This module provides data structures to represent nodes of UDAT documents, a data format offering a generic basis for data storage and transmission, while being both easily readable by humans and machines. The format is comparable to formats like XML or YAML, but due to its simplicity is much more easy to parse.

UDAT documents can be generated by converting an Object to a Udat::Node, by calling Object#to_udat, and then encoding them by calling Udat::Node#encode.

UDAT documents can be parsed by calling String#parse_udat.

There are methods providing useful IO functions for UDAT documents: IO#read_udat, IO#write_udat, Udat::Node#rpc and Udat.run_rpc_server.

Defined Under Namespace

Modules: Demo Classes: Collection, Node, ParseError, Scalar, UdatIndexError, UdatPropertyMismatch, UdatTagMismatch, UdatTypeMismatch

Constant Summary collapse

AnyTag =

Special argument used as an argument default, do not use it directly.

Object.new

Class Method Summary collapse

Class Method Details

.escape_string(string) ⇒ Object

Returns an escaped version of a string, where backslashes are preceding certain reserved characters.



54
55
56
# File 'lib/udat.rb', line 54

def escape_string(string)
  string.to_s.gsub /([<>\[\]|~\\])/, "\\\\\\1"
end

.run_rpc_server(port, timeout = 180) ⇒ Object

Waits in an endless loop for incoming TCP connections on the given port and runs a new thread for each incoming connection, reads and parses UDAT objects, passes them each to a given block (by calling yield) and writes the result of the called block in form of an encoded UDAT object back to the TCP socket. A timeout can be specified, to determine how long it may take to read an object.



1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
# File 'lib/udat.rb', line 1098

def run_rpc_server(port, timeout = 180)
  server = nil
  begin
    server = TCPServer.new(port)
    while true
      Thread.new(server.accept) do |socket|
        begin
          while true
            query = nil
            begin
              Timeout.timeout(timeout) do
                query = socket.read_udat
              end
            rescue Timeout::Error
            end
            break unless query
            socket.write_udat(yield(query))
          end
        ensure
          socket.close
        end
      end
    end
  ensure
    server.close if server
  end
end