Class: Rho::RHO

Inherits:
Object
  • Object
show all
Defined in:
lib/rho/rho.rb

Constant Summary collapse

APPLICATIONS =
{}
APPNAME =
'app'
CR =
"\x0d"
LF =
"\x0a"
CRLF =
"\x0d\x0a"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_manifest_filename = nil) ⇒ RHO

Returns a new instance of RHO.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rho/rho.rb', line 13

def initialize(app_manifest_filename=nil)
  puts "Calling RHO.initialize"
  process_rhoconfig
  Rhom::RhomDbAdapter::open(Rho::RhoFSConnector::get_db_fullpathname)
  if app_manifest_filename
    process_model_dirs(app_manifest_filename)
  else
    process_model_dirs(Rho::RhoFSConnector::get_app_manifest_filename)
  end
  init_sources
end

Class Method Details

.finalizeObject

make sure we close the database file



26
27
28
# File 'lib/rho/rho.rb', line 26

def self.finalize
  Rhom::RhomDbAdapter::close
end

Instance Method Details

#get_app(appname) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/rho/rho.rb', line 34

def get_app(appname)
  if (APPLICATIONS[appname].nil?)
    require RhoApplication::get_app_path(appname)+'application'
    APPLICATIONS[appname] = Object.const_get('AppApplication').new
  end
  APPLICATIONS[appname]
end

#init_response(status = 200, message = "OK", body = "") ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/rho/rho.rb', line 153

def init_response(status=200,message="OK",body="")
  res = Hash.new
  res['status'] = status
  res['message'] = message
  res['headers'] = 
    {
    'Date' => Time.now.httpdate,
    'Content-Type' => 'text/html',
    'Content-Length' => 0,
    'Connection' => 'close' 
  }
  res['request-body'] = body
  res
end

#init_sourcesObject

setup the sources table and model attributes for all applications



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rho/rho.rb', line 79

def init_sources
  if defined? Rho::RhoConfig::sources
    
    # quick and dirty way to get unique array of hashes
    uniq_sources = Rho::RhoConfig::sources.values.inject([]) { |result,h| 
      result << h unless result.include?(h); result
    }
    
    # generate unique source list in database for sync
    uniq_sources.each do |source|
      
      src_id = source['source_id']
      url = source['url']
      name = source['name']
      if !self.source_initialized?(src_id)
        Rhom::RhomDbAdapter::insert_into_table('sources',
                                              {"source_id"=>src_id,"source_url"=>url,"name"=>name})
      end
    end
  end
end

#process_model_dirs(app_manifest_filename = nil) ⇒ Object

Return the directories where we need to load configuration files



43
44
45
46
47
48
49
50
# File 'lib/rho/rho.rb', line 43

def process_model_dirs(app_manifest_filename=nil)
  File.open(app_manifest_filename).each do |line|
    str = line.chomp
    if str != nil and str.length > 0 
        require File.join(File.dirname(app_manifest_filename), str )
    end    
  end
end

#process_rhoconfigObject

Return the directories where we need to load configuration files



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rho/rho.rb', line 53

def process_rhoconfig
  begin
    File.open(Rho::RhoFSConnector.get_rhoconfig_filename).each do |line|
      parts = line.chop.split('=')
      key = parts[0]
      value = nil
      if key and defined? RHO_ME
        value = rho_get_app_property(key.strip)
      end
      
      if !value
        value = parts[1] if parts[1]
      end
        
      if key and value
        val = value.strip.gsub(/\'|\"/,'')
        val = val == 'nil' ? nil : val
        Rho::RhoConfig.add_config(key.strip,val)
      end  
    end
  rescue Exception => e
    puts "Error opening rhoconfig.txt: #{e}, using defaults."
  end
end

#raise_rhoerror(errCode) ⇒ Object

Raises:



30
31
32
# File 'lib/rho/rho.rb', line 30

def raise_rhoerror(errCode)
    raise Rho::RhoError.new(errCode)
end

#send_error(exception = nil, status = 500, hash = false) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/rho/rho.rb', line 207

def send_error(exception=nil,status=500,hash=false)
  body=''
  body << "    <html>\n        <head>\n            <meta name=\"viewport\" content=\"width=320\"/>\n        </head>\n        <body>\n            <h2>Server Error</h2>\n            <p>\n  _HTML_STRING_\n  body << 'Error: ' << exception.message << \"<br/>\" if exception\n  body << 'Trace: ' << exception.backtrace.join(\"\\n\") if exception\n  body << <<-_HTML_STRING_\n            </p>    \n        </body>\n    </html>\n    \n  _HTML_STRING_\n  if ( hash )\n    send_response_hash(init_response(status,\"Server error\",body))\n  else\n    send_response(init_response(status,\"Server error\",body))\n  end\nend\n"

#send_response(res) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/rho/rho.rb', line 172

def send_response(res)
  res['headers']['Content-Length'] = res['request-body'].nil? ? 0 : res['request-body'].length
  data = "HTTP/1.1 #{res['status'].to_s} #{res['message']}" + CRLF
  res['headers'].each{|key, value|
    tmp = key.gsub(/\bwww|^te$|\b\w/){|s| s.upcase }
    data << "#{tmp}: #{value}" << CRLF
  }
data << "Pragma: no-cache" << CRLF
data << "Cache-Control: must-revalidate" << CRLF
data << "Cache-Control: no-cache" << CRLF
data << "Cache-Control: no-store" << CRLF
data << "Expires: 0" << CRLF

  data << CRLF
  if ( !res['request-body'].nil? )
    data << res['request-body']
  end
    
  data
end

#send_response_hash(res) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/rho/rho.rb', line 193

def send_response_hash(res)
    resp = Hash.new
    res['headers']['Content-Length'] = res['request-body'].nil? ? 0 : res['request-body'].length
    res['headers'].each{|key, value|
        tmp = key.gsub(/\bwww|^te$|\b\w/){|s| s.upcase }
        resp[tmp] = value
    }
    resp['request-body'] = res['request-body']
    resp['status'] = res['status']        
    resp['message'] = res['message']
    
    resp
end

#serve(req) ⇒ Object



105
106
107
108
109
110
111
112
113
114
# File 'lib/rho/rho.rb', line 105

def serve(req)
  begin
    puts 'inside RHO.serve...'
    res = init_response
    get_app(req['application']).send :serve, req, res
    return send_response(res)
  rescue Exception => e
    return send_error(e)
  end   
end

#serve_hash(req) ⇒ Object



116
117
118
119
120
121
122
123
124
125
# File 'lib/rho/rho.rb', line 116

def serve_hash(req)
    begin
        puts 'inside RHO.serve...'
        res = init_response
        get_app(req['application']).send :serve, req, res
        return send_response_hash(res)
    rescue Exception => e
        return send_error(e,500,true)
    end 
end

#serve_index(index_name) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/rho/rho.rb', line 127

def serve_index(index_name)
  # TODO: Removed hardcoded appname
  get_app(APPNAME).set_menu
    begin
        puts 'inside RHO.serve_index: ' + index_name
        res = init_response
        res['request-body'] = RhoController::renderfile(index_name)
        return send_response(res)
    rescue Exception => e
        return send_error(e)
    end
end

#serve_index_hash(index_name) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/rho/rho.rb', line 140

def serve_index_hash(index_name)
       # TODO: Removed hardcoded appname
  get_app(APPNAME).set_menu
    begin
        puts 'inside RHO.serve_index: ' + index_name
        res = init_response
        res['request-body'] = RhoController::renderfile(index_name)
        return send_response_hash(res)
    rescue Exception => e
        return send_error(e.message, 500, true)
    end
end

#source_initialized?(source_id) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/rho/rho.rb', line 101

def source_initialized?(source_id)
  Rhom::RhomDbAdapter::select_from_table('sources','*', 'source_id'=>source_id).size > 0 ? true : false
end