Class: JSTP::Engine

Inherits:
Object
  • Object
show all
Includes:
Discoverer::Reader
Defined in:
lib/jstp/engine.rb

Defined Under Namespace

Classes: ClientNotFoundError, NotAControllerError, NotPermittedError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(test = false) ⇒ Engine

Returns a new instance of Engine.



5
6
7
8
9
# File 'lib/jstp/engine.rb', line 5

def initialize test = false
  @config = Configuration.instance
  @logger = @config.logger
  from.send @config.strategy.inbound unless test
end

Class Method Details

.environment(argument = nil) ⇒ Object



114
115
116
# File 'lib/jstp/engine.rb', line 114

def self.environment argument = nil
  Configuration.instance.environment argument
end

.environment=(argument) ⇒ Object



118
119
120
# File 'lib/jstp/engine.rb', line 118

def self.environment= argument
  Configuration.instance.environment = argument
end

.gateway(argument = nil) ⇒ Object



122
123
124
# File 'lib/jstp/engine.rb', line 122

def self.gateway argument = nil
  Configuration.instance.gateway argument
end

.gateway=(argument) ⇒ Object



126
127
128
# File 'lib/jstp/engine.rb', line 126

def self.gateway= argument
  Configuration.instance.gateway = argument
end

.hostname(argument = nil) ⇒ Object



106
107
108
# File 'lib/jstp/engine.rb', line 106

def self.hostname argument = nil
  Configuration.instance.hostname argument
end

.hostname=(argument) ⇒ Object



110
111
112
# File 'lib/jstp/engine.rb', line 110

def self.hostname= argument
  Configuration.instance.hostname = argument
end

.logger(argument = nil) ⇒ Object



98
99
100
# File 'lib/jstp/engine.rb', line 98

def self.logger argument = nil
  Configuration.instance.logger argument
end

.logger=(argument) ⇒ Object



102
103
104
# File 'lib/jstp/engine.rb', line 102

def self.logger= argument
  Configuration.instance.logger = argument
end

.port(argument = nil) ⇒ Object

DSL for configuration



82
83
84
# File 'lib/jstp/engine.rb', line 82

def self.port argument = nil
  Configuration.instance.port argument
end

.port=(argument) ⇒ Object



86
87
88
# File 'lib/jstp/engine.rb', line 86

def self.port= argument
  Configuration.instance.port = argument
end

.strategy(argument = nil) ⇒ Object



90
91
92
# File 'lib/jstp/engine.rb', line 90

def self.strategy argument = nil
  Configuration.instance.strategy argument
end

.strategy=(argument) ⇒ Object



94
95
96
# File 'lib/jstp/engine.rb', line 94

def self.strategy= argument
  Configuration.instance.strategy = argument
end

Instance Method Details

#clientsObject



69
70
71
# File 'lib/jstp/engine.rb', line 69

def clients
  @clients ||= {}
end

#discover_resource(resource_stack) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/jstp/engine.rb', line 50

def discover_resource resource_stack
  response = []
  response << resource_stack.shift
  class_stack = "::#{self.class}::"
  query = []
  resource_stack.each do |item|
    begin
      capitalized = item[0].upcase + item[1..-1]
      eval(class_stack + capitalized)
      class_stack += "#{capitalized}::"
    rescue NameError, SyntaxError
      query << item
    end
  end
  response << eval(class_stack[0..-3])
  response << query
  return response
end

#dispatch(message, client) ⇒ Object

Processes the dispatch in the new REST engine way



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/jstp/engine.rb', line 12

def dispatch message, client
  host, the_class, query = discover_resource message["resource"].clone
  if @config.gateway.forward && host != @config.hostname && host != 'localhost'
    # May be we should gateway this message, if this wasn't aimed at us
    message.to.send @config.strategy.outbound
  else
    # Is there a reverse gateway token there?
    if @config.gateway.reverse && message.gateway == 'reverse'
      if clients.has_key? message.token.first
        begin 
          clients[message.token.first].send message.to.json
          @config.logger.debug message.to.string
        rescue Exception => exception
          @config.logger.error exception.message
          @config.logger.debug exception.backtrace.to_s
        end
      else
        if respond_to? :client_not_found
          client_not_found message.token.first, message
        else
          raise ClientNotFoundError, "Client #{message.token.first} is not registered in this server"
        end
      end
    else
      if the_class.ancestors.include? JSTP::Controller
        resource = the_class.new message, query, self, client
        resource.send message["method"].downcase.to_sym
      else
        if @config.environment == :development
          raise NotAControllerError, "The resource class #{the_class} for #{message["resource"].join("/")} was found, but is not a JSTP::Controller"
        else
          raise NotPermittedError, "The selected resource is forbidden for this type of request"
        end
      end
    end
  end
end

#loggerObject



73
74
75
# File 'lib/jstp/engine.rb', line 73

def logger
  @logger
end