totem 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.swp
19
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in totem.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Chad Remesch
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # Totem
2
+
3
+ Totem is a framework for writing actor model based servers using the Ruby language.
4
+ Currently it is in an experimental state, but you are welcome to play with it.
5
+
6
+ Features:
7
+ - Ruby on Rails inspired folder structure.
8
+ - Includes Active Record and Active Support.
9
+ - Generators for database migrations.
10
+ - Integrated console.
11
+ - Designed for MRI and JRuby.
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ gem 'totem'
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install totem
26
+
27
+ ## Contributing
28
+
29
+ 1. Fork it
30
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
31
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
32
+ 4. Push to the branch (`git push origin my-new-feature`)
33
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ desc 'Start an IRB console with Workers loaded'
4
+ task :console do
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'lib'))
6
+
7
+ require 'totem'
8
+ require 'irb'
9
+
10
+ ARGV.clear
11
+
12
+ IRB.start
13
+ end
data/lib/totem.rb ADDED
@@ -0,0 +1,147 @@
1
+ require 'tribe'
2
+ require 'tribe_em'
3
+
4
+ require 'totem/version'
5
+ require 'totem/tasks/console'
6
+ require 'totem/tasks/database'
7
+ require 'totem/tasks/generator'
8
+ require 'totem/actable'
9
+ require 'totem/connection'
10
+ require 'totem/tcp_server'
11
+
12
+ module Totem
13
+ def self.initialize(root)
14
+ raise 'Already initialized.' if @setup
15
+
16
+ @setup = true
17
+ @root = root
18
+
19
+ Bundler.require(Totem.env.to_sym)
20
+ Time.zone = 'UTC'
21
+ db_connect
22
+ load_app
23
+
24
+ return true
25
+ end
26
+
27
+ def self.root
28
+ return @root
29
+ end
30
+
31
+ def self.env
32
+ return (@env ||= (ENV['GAME_SRV_ENV'] || 'development'))
33
+ end
34
+
35
+ def self.settings
36
+ return @settings if @settings
37
+
38
+ begin
39
+ @settings = Fiona::Settings.new do |s|
40
+ eval(File.read(File.join(root, 'config', 'settings.rb')))
41
+ end
42
+ rescue Exception => e
43
+ puts "Failed to initialize settings: #{e.message}\n#{e.backtrace.join("\n")}"
44
+ end
45
+
46
+ return @settings
47
+ end
48
+
49
+ def self.db_config
50
+ return (@db_config ||= YAML.load_file(File.join(root, 'config', 'database.yml'))[env])
51
+ end
52
+
53
+ def self.db_connect
54
+ return false if db_connected?
55
+
56
+ begin
57
+ ActiveRecord::Base.establish_connection(Totem.db_config)
58
+ rescue Exception => e
59
+ puts "Failed to establish DB connection: #{e.message}\n#{e.backtrace.join("\n")}"
60
+ return false
61
+ end
62
+
63
+ return true
64
+ end
65
+
66
+ def self.db_disconnect
67
+ return false unless db_connected?
68
+
69
+ ActiveRecord::Base.connection_pool.disconnect!
70
+
71
+ return true
72
+ end
73
+
74
+ def self.db_reconnect
75
+ db_disconnect
76
+ db_connect
77
+
78
+ return true
79
+ end
80
+
81
+ def self.db_connected?
82
+ return !!ActiveRecord::Base.connected?
83
+ end
84
+
85
+ def self.load_app
86
+ Dir.chdir("#{Totem.root}/app")
87
+ load("loader.rb")
88
+ Dir.chdir(Totem.root)
89
+ end
90
+
91
+ def self.component
92
+ return @component || 'default'
93
+ end
94
+
95
+ def self.component=(val)
96
+ return @component = val
97
+ end
98
+
99
+ def self.instance
100
+ return @instance || 0
101
+ end
102
+
103
+ def self.instance=(val)
104
+ return @instance = val
105
+ end
106
+
107
+ def self.logger=(val)
108
+ return @logger = val
109
+ end
110
+
111
+ def self.logger
112
+ return @logger if @logger
113
+
114
+ case env
115
+ when 'development'
116
+ log_to_stdout
117
+ when 'production'
118
+ log_to_file
119
+ else
120
+ log_to_stdout
121
+ end
122
+
123
+ return @logger
124
+ end
125
+
126
+ def self.log_to_stdout
127
+ init_logger($stdout)
128
+
129
+ return nil
130
+ end
131
+
132
+ def self.log_to_file
133
+ init_logger(File.join(root, 'log', "#{env}_#{component}_#{instance}.log"))
134
+
135
+ return nil
136
+ end
137
+
138
+ def self.init_logger(output)
139
+ @logger = Logger.new(output)
140
+
141
+ @logger.formatter = proc do |severity, datetime, progname, msg|
142
+ "#{datetime} :: #{msg}\n"
143
+ end
144
+
145
+ return nil
146
+ end
147
+ end
@@ -0,0 +1,9 @@
1
+ module Totem
2
+ module Actable
3
+ def process_event(event)
4
+ ActiveRecord::Base.connection_pool.with_connection do
5
+ super
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,13 @@
1
+ module Totem
2
+ class Connection < Tribe::EM::Connection
3
+ include Totem::Actable
4
+
5
+ private
6
+
7
+ def initialize(options = {})
8
+ options[:logger] = Totem.logger
9
+
10
+ super
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,10 @@
1
+ module Totem
2
+ module Tasks
3
+ class Console
4
+ def start
5
+ ARGV.clear
6
+ IRB.start
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,68 @@
1
+ module Totem
2
+ module Tasks
3
+ class Database
4
+ def initialize
5
+ @config = Totem.db_config.clone
6
+ end
7
+
8
+ def create
9
+ options = { :charset => 'utf8', :collation => 'utf8_unicode_ci' }
10
+
11
+ create_db = lambda do |config|
12
+ ActiveRecord::Base.establish_connection(config.merge('database' => nil))
13
+ ActiveRecord::Base.connection.create_database(config['database'], options)
14
+ ActiveRecord::Base.establish_connection(config)
15
+ end
16
+
17
+ begin
18
+ create_db.call(@config)
19
+ rescue Mysql::Error => sqlerr
20
+ if sqlerr.errno == 1405
21
+ print "#{sqlerr.error}. \nPlease provide the root password for your mysql installation\n>"
22
+ root_password = $stdin.gets.strip
23
+
24
+ grant_statement = <<-SQL
25
+ GRANT ALL PRIVILEGES ON #{config['database']}.*
26
+ TO '#{config['username']}'@'localhost'
27
+ IDENTIFIED BY '#{config['password']}' WITH GRANT OPTION;
28
+ SQL
29
+
30
+ create_db.call(@config.merge('database' => nil, 'username' => 'root', 'password' => root_password))
31
+ else
32
+ $stderr.puts sqlerr.error
33
+ $stderr.puts "Couldn't create database for #{config.inspect}, charset: utf8, collation: utf8_unicode_ci"
34
+ $stderr.puts "(if you set the charset manually, make sure you have a matching collation)" if config['charset']
35
+ end
36
+ end
37
+
38
+ Totem.db_reconnect
39
+
40
+ return true
41
+ end
42
+
43
+ def drop
44
+ ActiveRecord::Base.connection.drop_database(@config['database'])
45
+
46
+ return true
47
+ end
48
+
49
+ def migrate
50
+ ActiveRecord::Migration.verbose = true
51
+ ActiveRecord::Migrator.migrate('db/migrate', ENV['VERSION'] ? ENV['VERSION'].to_i : nil)
52
+
53
+ Totem.db_reconnect
54
+
55
+ return true
56
+ end
57
+
58
+ def rollback
59
+ step = ENV['STEP'] ? ENV['STEP'].to_i : 1
60
+ ActiveRecord::Migrator.rollback('db/migrate', step)
61
+
62
+ Totem.db_reconnect
63
+
64
+ return true
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,29 @@
1
+ module Totem
2
+ module Tasks
3
+ class Generator
4
+ def migration(name)
5
+ tstamp = Time.now.utc.strftime("%Y%m%d%H%M%S")
6
+ fname = "#{tstamp}_#{name}.rb"
7
+ path = File.join(Totem.root, 'db', 'migrate', fname)
8
+
9
+ name || raise('You must specify a name')
10
+
11
+ puts "Creating migration: #{path}"
12
+
13
+ if File.exists?(path)
14
+ puts 'ERROR: File already exists.'
15
+ return
16
+ end
17
+
18
+ content = <<-EOS.unindent
19
+ class #{name.camelize} < ActiveRecord::Migration
20
+ end
21
+ EOS
22
+
23
+ File.open(path, 'w') { |f| f.write(content) }
24
+
25
+ return nil
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,13 @@
1
+ module Totem
2
+ class TcpServer < Tribe::EM::TcpServer
3
+ include Totem::Actable
4
+
5
+ private
6
+
7
+ def initialize(ip, port, conn_class, options = {})
8
+ options[:logger] = Totem.logger
9
+
10
+ super
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module Totem
2
+ VERSION = '0.0.1'
3
+ end
data/totem.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'totem/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = 'totem'
8
+ gem.version = Totem::VERSION
9
+ gem.authors = ['Chad Remesch']
10
+ gem.email = ['chad@remesch.com']
11
+ gem.description = %q{The Totem framework.}
12
+ gem.summary = %q{An actor model based framework for rapid server server side development.}
13
+ gem.homepage = 'https://github.com/chadrem/totem'
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency('tribe', '0.0.8')
21
+ gem.add_dependency('tribe_em', '0.0.2')
22
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: totem
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Chad Remesch
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: tribe
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - '='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.0.8
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - '='
28
+ - !ruby/object:Gem::Version
29
+ version: 0.0.8
30
+ - !ruby/object:Gem::Dependency
31
+ name: tribe_em
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - '='
36
+ - !ruby/object:Gem::Version
37
+ version: 0.0.2
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - '='
44
+ - !ruby/object:Gem::Version
45
+ version: 0.0.2
46
+ description: The Totem framework.
47
+ email:
48
+ - chad@remesch.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - lib/totem.rb
59
+ - lib/totem/actable.rb
60
+ - lib/totem/connection.rb
61
+ - lib/totem/tasks/console.rb
62
+ - lib/totem/tasks/database.rb
63
+ - lib/totem/tasks/generator.rb
64
+ - lib/totem/tcp_server.rb
65
+ - lib/totem/version.rb
66
+ - totem.gemspec
67
+ homepage: https://github.com/chadrem/totem
68
+ licenses: []
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ! '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubyforge_project:
87
+ rubygems_version: 1.8.24
88
+ signing_key:
89
+ specification_version: 3
90
+ summary: An actor model based framework for rapid server server side development.
91
+ test_files: []