Module: Ji2p::Server::Database

Defined in:
lib/ji2p/server/database.rb

Constant Summary collapse

@@connections =
Hash.new
@@default_connection =
nil

Class Method Summary collapse

Class Method Details

.connect!(db = ':memory:', db_type = 'sqlite3') ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/ji2p/server/database.rb', line 8

def self.connect! db=':memory:', db_type='sqlite3'
  conn = ActiveRecord::Base.establish_connection(database: db, adapter: db_type)
  key = "#{db}/#{db_type}/#{Ji2p::Control.unique_id.to_s}"
  @@connections[key] = conn
  @@default_connection = conn if @@default_connection.nil?
  define_state_schema!
  conn
end

.connect_with_local_path!(name) ⇒ Object



17
18
19
20
21
22
# File 'lib/ji2p/server/database.rb', line 17

def self.connect_with_local_path! name
  java_import 'net.i2p.I2PAppContext'
  ctx = Java::NetI2p::I2PAppContext.getGlobalContext
  path = File.join ctx.getConfigDir.toString, name
  connect! path
end

.defaultObject



28
29
30
# File 'lib/ji2p/server/database.rb', line 28

def self.default
  @@default_connection
end

.define_state_schema!(b_force = false) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ji2p/server/database.rb', line 40

def self.define_state_schema! b_force=false
  ActiveRecord::Schema.define do
    create_table :keypairs, force: b_force do |t|
      t.string   :strid
      t.string   :name
      t.text     :destination
      t.string   :base32
      t.text     :private_key_data
      t.json     :metadata
      t.timestamps
    end unless ActiveRecord::Base.connection.table_exists? 'keypairs' or b_force
    create_table :tunnels, force: b_force do |t|
      t.string     :name
      t.text       :description
      t.json       :metadata
      t.references :keypair
      t.string     :tunnel_type
      t.integer    :listen_port
      t.integer    :target_port
      t.string     :listen_interface
      t.string     :target_host
      t.timestamps
    end unless ActiveRecord::Base.connection.table_exists? 'tunnels' or b_force
  end
end

.flushObject



24
25
26
# File 'lib/ji2p/server/database.rb', line 24

def self.flush
  @@default_connection.flush
end

.get_connectionsObject



36
37
38
# File 'lib/ji2p/server/database.rb', line 36

def self.get_connections
  @@connections
end

.is_connected?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/ji2p/server/database.rb', line 32

def self.is_connected?
  get_connections.size > 0
end