Class: Rho::RHO

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

Constant Summary collapse

APPLICATIONS =
{}
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.



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

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



24
25
26
# File 'lib/rho/rho.rb', line 24

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

Instance Method Details

#get_app(appname) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/rho/rho.rb', line 78

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

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



131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/rho/rho.rb', line 131

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



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

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']
      if !self.source_initialized?(src_id)
        Rhom::RhomDbAdapter::insert_into_table('sources',
                                              {"source_id"=>src_id,"source_url"=>url})
      end
    end
  end
end

#process_model_dirs(app_manifest_filename = nil) ⇒ Object

Return the directories where we need to load configuration files



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

def process_model_dirs(app_manifest_filename=nil)
  File.open(app_manifest_filename).each do |line|
    require File.join(File.dirname(app_manifest_filename), line.chop)
  end
end

#process_rhoconfigObject

Return the directories where we need to load configuration files



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rho/rho.rb', line 36

def process_rhoconfig
  begin
    File.open(Rho::RhoFSConnector.get_rhoconfig_filename).each do |line|
      parts = line.chop.split('=')
      key = parts[0]
      value = parts[1..parts.length-1].join('=') if parts and parts.length > 1
      if key and value
        tmp = value.strip!
        Rho::RhoConfig.config[key.strip] = tmp.gsub(/'|"/,'') if tmp
      end  
    end
  rescue Exception => e
    puts "Error opening rhoconfig.txt: #{e}, using defaults."
  end
end

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



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/rho/rho.rb', line 185

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

#send_response(res) ⇒ Object



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

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



171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/rho/rho.rb', line 171

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



87
88
89
90
91
92
93
94
95
96
# File 'lib/rho/rho.rb', line 87

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



98
99
100
101
102
103
104
105
106
107
# File 'lib/rho/rho.rb', line 98

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



109
110
111
112
113
114
115
116
117
118
# File 'lib/rho/rho.rb', line 109

def serve_index(index_name)
    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



120
121
122
123
124
125
126
127
128
129
# File 'lib/rho/rho.rb', line 120

def serve_index_hash(index_name)
    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)


74
75
76
# File 'lib/rho/rho.rb', line 74

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