merona 1.1.8 → 1.1.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,8 +1,3 @@
1
- require 'win32api'
2
-
3
- $t = Win32API.new("kernel32", "GetTickCount", nil, 'L')
4
- $t.call()
5
-
6
1
  class PingHandler < Handler
7
2
  def recv(server,connection,packet)
8
3
  case packet.id
@@ -2,8 +2,11 @@
2
2
 
3
3
  import 'merona'
4
4
 
5
+ import 'dbms/*'
6
+ import 'sharedmem/*'
5
7
  import 'handler/*'
6
8
  import 'protocol/*'
9
+
7
10
  import 'config/*'
8
11
 
9
12
  begin
@@ -26,5 +29,5 @@ begin
26
29
  end
27
30
 
28
31
  rescue
29
- Log.error "[Error] : #{$!}"
32
+ Log.error "#{$!}"
30
33
  end
@@ -1,4 +1,3 @@
1
- # 1400 ~ 1499
2
1
  class Protocol
3
2
  PING = 1
4
3
  end
@@ -18,8 +18,9 @@ class Channel
18
18
  end
19
19
  end
20
20
  def publish(packet)
21
+ freezed = packet.freeze
21
22
  @clients.each do |client|
22
- client.send packet
23
+ client.send_data freezed
23
24
  end
24
25
  end
25
26
  end
@@ -1,13 +1,19 @@
1
1
  class ChannelPool
2
+ # ChannelPool�� �ʱ�ȭ�Ѵ�.
2
3
  def initialize
3
4
  @channel = {}
4
5
  end
6
+
7
+ # ChannelPool�� �����Ѵ�.
5
8
  def dispose
6
9
  end
7
10
 
8
11
  # ä�� �̸��� ��ūȭ�ؼ� �ֻ��� ������ ä�ο� ������ �� ����
9
12
  # �ڵ� ������ �����Ų��.
10
13
  #
14
+ # name : ��ūȭ�� ä�� �̸�
15
+ # &block : ��ū �� ����� ����
16
+ #
11
17
  # ex ) "ch.map.party"
12
18
  # "ch.map"
13
19
  # "ch"
@@ -25,6 +31,9 @@ class ChannelPool
25
31
  end
26
32
 
27
33
  # ������ �̸��� ä���� �����Ѵ�
34
+ #
35
+ # name : ������ ä���� �̸�
36
+ # client : ������ Ŭ���̾�Ʈ
28
37
  def subscribe(name, client)
29
38
  tokenize(name) do |t|
30
39
  subscribe t, client
@@ -38,6 +47,9 @@ class ChannelPool
38
47
  end
39
48
 
40
49
  # ������ �̸��� ä���� ������ �����Ѵ�
50
+ #
51
+ # name : ���� ������ ä���� �̸�
52
+ # client : ���� ������ Ŭ���̾�Ʈ
41
53
  def unsubscribe(name, client)
42
54
  tokenize(name) do |t|
43
55
  unsubscribe t, client
@@ -51,19 +63,12 @@ class ChannelPool
51
63
  end
52
64
 
53
65
  # ������ �̸��� ä�ο� ��Ŷ�� �����Ѵ�
54
- #
55
- # to_super ���ڰ� true�� ��� ���� ä�� ��ο��� ��Ŷ�� ����
56
- def publish(name, packet, to_super = false)
57
- if to_super == true
58
- tokenize(name) do |t|
59
- publish t, packet
60
- end
61
- end
62
-
66
+ #
67
+ # name : ������ ä���� �̸�
68
+ # packet : ������ ��Ŷ
69
+ def publish(name, packet)
63
70
  return if @channel[name] == nil
64
71
 
65
- @channel[name].each do |client|
66
- client.publish packet
67
- end
72
+ @channel[name].publish packet
68
73
  end
69
74
  end
@@ -2,7 +2,7 @@ class ChatHandler < Handler
2
2
  def recv(server,connection,packet)
3
3
  case packet.id
4
4
  when Protocol::CHAT
5
- puts "[#{packet["sender"]}] : #{packet["msg"]}"
5
+ #puts "[#{packet["sender"]}] : #{packet["msg"]}"
6
6
 
7
7
  reply = Packet.new
8
8
  reply.id = Protocol::CHAT
@@ -4,6 +4,11 @@ class Handler
4
4
  def dispose
5
5
  end
6
6
 
7
+ # ��Ŷ�� �������� �� �ҷ����� �޼ҵ�
8
+ #
9
+ # server : ������ ����
10
+ # connection : ��Ŷ�� ���� Ŭ���̾�Ʈ
11
+ # packet : ������ ��Ŷ
7
12
  def recv(server,connection,packet)
8
13
  #case packet.id
9
14
  # when Protocol::PacketID
@@ -13,6 +13,11 @@ class Packet
13
13
  def dispose
14
14
  end
15
15
 
16
+ def freeze
17
+ data = Marshal.dump(self)
18
+ [data.respond_to?(:bytesize) ? data.bytesize : data.size, data].pack('Na*')
19
+ end
20
+
16
21
  # ��Ŷ�� �����͸� �ִ´�
17
22
  def push(name,data)
18
23
  if name == nil
@@ -18,12 +18,19 @@ class ProcessPool
18
18
  def dispose
19
19
  end
20
20
 
21
+ # ������ ������ŭ�� �ϲ��� �����Ѵ�
22
+ #
23
+ # n : ������ �ϲ��� ����
21
24
  def create_worker(n)
22
25
  n.times do
23
26
  thread = Thread.new do
24
27
  while true
25
28
  item = @queue.deq(false)
26
-
29
+
30
+ if item.sender.is_alive? == false
31
+ next
32
+ end
33
+
27
34
  @server.handler.each do |handler|
28
35
  handler.recv(@server, item.sender, item.packet)
29
36
  end
@@ -33,12 +40,18 @@ class ProcessPool
33
40
  @worker.push thread
34
41
  end
35
42
  end
43
+
44
+ # ��� �ϲ��� ������
36
45
  def kill
37
46
  @worker.each do |worker|
38
47
  worker.kill
39
48
  end
40
49
  end
41
50
 
51
+ # ť�� ��Ŷ�� �ִ´�
52
+ #
53
+ # sender : ��Ŷ�� ������ Ŭ���̾�Ʈ
54
+ # item : ���� ��Ŷ
42
55
  def enqueue(sender, item)
43
56
  @queue.push WorkItem.new(sender, item)
44
57
  end
@@ -18,6 +18,8 @@ class Connection < EM::Connection
18
18
  connect
19
19
 
20
20
  set_sock_opt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, CONNECTION_NODELAY)
21
+
22
+ @connected = true
21
23
  end
22
24
  # ������ ������ �� �ҷ����� �޼ҵ�
23
25
  def unbind
@@ -26,13 +28,8 @@ class Connection < EM::Connection
26
28
  @mem.dispose
27
29
 
28
30
  disconnect
29
- end
30
-
31
- # ���� Ŭ���̾�Ʈ�� ��Ŷ�� �����Ѵ�
32
- #
33
- # packet : ���� ��Ŷ
34
- def send(packet)
35
- send_object packet
31
+
32
+ @connected = false
36
33
  end
37
34
 
38
35
  # ���� Ŭ���̾�Ʈ�� ��Ŷ�� �޾��� �� �ҷ����� �޼ҵ�
@@ -51,4 +48,9 @@ class Connection < EM::Connection
51
48
  def disconnect
52
49
  Log.output("lost connection " + @ip)
53
50
  end
51
+
52
+ # ���� Ŭ���̾�Ʈ�� ���� ���¸� �����Ѵ�.
53
+ def is_alive?
54
+ @connected
55
+ end
54
56
  end
@@ -14,7 +14,7 @@ class Server
14
14
  # port : ������ ��Ʈ
15
15
  # klass : Connection �ڵ鷯 Ŭ����
16
16
  def initialize(name,port,klass=Connection)
17
- EventMachine.start_server("localhost", port, klass, self)
17
+ EventMachine.start_server("0.0.0.0", port, klass, self)
18
18
 
19
19
  @clients = Array.new
20
20
  @handler = Array.new
@@ -1,23 +1,23 @@
1
- require 'socket'
2
-
3
- load '../lib/protocol/chat_protocol.rb'
4
- load '../lib/packet/packet.rb'
5
- load '../lib/packet/chat_packet.rb'
6
-
7
- load 'test_util.rb'
8
-
9
- if ARGV.size == 2
10
- addr = ARGV[0]
11
- port = ARGV[1]
12
- else
13
- addr = "localhost"
14
- port = 9916
15
- end
16
- sock = TCPSocket.new addr, port
17
-
18
- packet = ChatPacket.new
19
- packet.id = Protocol::CHAT
20
- packet.sender = "Aqwer"
21
- packet.msg = "AEWF"
22
-
1
+ require 'socket'
2
+
3
+ load '../lib/protocol/chat_protocol.rb'
4
+ load '../lib/packet/packet.rb'
5
+ load '../lib/packet/chat_packet.rb'
6
+
7
+ load 'test_util.rb'
8
+
9
+ if ARGV.size == 2
10
+ addr = ARGV[0]
11
+ port = ARGV[1]
12
+ else
13
+ addr = "localhost"
14
+ port = 9916
15
+ end
16
+ sock = TCPSocket.new addr, port
17
+
18
+ packet = ChatPacket.new
19
+ packet.id = Protocol::CHAT
20
+ packet.sender = "Aqwer"
21
+ packet.msg = "AEWF"
22
+
23
23
  send_obj(sock, packet)
@@ -1,42 +1,42 @@
1
- require 'eventmachine'
2
-
3
- load '../lib/packet/packet.rb'
4
- load '../lib/protocol/pubsub_protocol.rb'
5
- load '../lib/protocol/sync_protocol.rb'
6
-
7
- load 'test_util.rb'
8
-
9
- class Connection < EM::Connection
10
- include EM::P::ObjectProtocol
11
-
12
- def initialize(*args)
13
- p = Packet.new
14
- p.id = Protocol::SUBSCRIBE
15
- send_object p
16
-
17
- p.id = Protocol::SYNC
18
- p["x"] = 1
19
- p["y"] = 2
20
- p["oid"] = 3
21
-
22
- 100.times do
23
- send_object p
24
- end
25
- end
26
- def unbind
27
- end
28
-
29
- def receive_object(obj)
30
- p obj
31
- end
32
- end
33
-
34
- EM::run do
35
- EM::connect "localhost", 9916, Connection
36
- end
37
-
38
- while true
39
- sleep 1
40
- end
41
-
1
+ require 'eventmachine'
2
+
3
+ load '../lib/packet/packet.rb'
4
+ load '../lib/protocol/pubsub_protocol.rb'
5
+ load '../lib/protocol/sync_protocol.rb'
6
+
7
+ load 'test_util.rb'
8
+
9
+ class Connection < EM::Connection
10
+ include EM::P::ObjectProtocol
11
+
12
+ def initialize(*args)
13
+ p = Packet.new
14
+ p.id = Protocol::SUBSCRIBE
15
+ send_object p
16
+
17
+ p.id = Protocol::SYNC
18
+ p["x"] = 1
19
+ p["y"] = 2
20
+ p["oid"] = 3
21
+
22
+ 100.times do
23
+ send_object p
24
+ end
25
+ end
26
+ def unbind
27
+ end
28
+
29
+ def receive_object(obj)
30
+ p obj
31
+ end
32
+ end
33
+
34
+ EM::run do
35
+ EM::connect "localhost", 9916, Connection
36
+ end
37
+
38
+ while true
39
+ sleep 1
40
+ end
41
+
42
42
 
@@ -1,42 +1,42 @@
1
- require 'eventmachine'
2
-
3
- load '../lib/packet/packet.rb'
4
- load '../lib/protocol/pubsub_protocol.rb'
5
- load '../lib/protocol/sync_protocol.rb'
6
-
7
- load 'test_util.rb'
8
-
9
- require 'Win32API'
10
-
11
- $t = Win32API.new("kernel32", "GetTickCount", nil, 'L')
12
- $t.call()
13
-
14
- class Connection < EM::Connection
15
- include EM::P::ObjectProtocol
16
-
17
- def initialize(*args)
18
- p = Packet.new
19
- p.id = Protocol::SUBSCRIBE
20
- send_object p
21
-
22
- p.id = 1
23
-
24
- 100.times do
25
- p["ping"] = $t.call()
26
- send_object p
27
- p "send " + ($t.call() - p["ping"]).to_s
28
- end
29
- end
30
- def unbind
31
- end
32
-
33
- def receive_object(obj)
34
- p "reply " + ($t.call() - obj["ping"]).to_s
35
- end
36
- end
37
-
38
- EM::run do
39
- EM::connect "localhost", 9916, Connection
40
- end
41
-
1
+ require 'eventmachine'
2
+
3
+ load '../lib/packet/packet.rb'
4
+ load '../lib/protocol/pubsub_protocol.rb'
5
+ load '../lib/protocol/sync_protocol.rb'
6
+
7
+ load 'test_util.rb'
8
+
9
+ require 'Win32API'
10
+
11
+ $t = Win32API.new("kernel32", "GetTickCount", nil, 'L')
12
+ $t.call()
13
+
14
+ class Connection < EM::Connection
15
+ include EM::P::ObjectProtocol
16
+
17
+ def initialize(*args)
18
+ p = Packet.new
19
+ p.id = Protocol::SUBSCRIBE
20
+ send_object p
21
+
22
+ p.id = 1
23
+
24
+ 100.times do
25
+ p["ping"] = $t.call()
26
+ send_object p
27
+ p "send " + ($t.call() - p["ping"]).to_s
28
+ end
29
+ end
30
+ def unbind
31
+ end
32
+
33
+ def receive_object(obj)
34
+ p "reply " + ($t.call() - obj["ping"]).to_s
35
+ end
36
+ end
37
+
38
+ EM::run do
39
+ EM::connect "localhost", 9916, Connection
40
+ end
41
+
42
42
 
@@ -1,29 +1,29 @@
1
- load '../lib/db/database.rb'
2
- load '../lib/db/sqlite.rb'
3
-
4
- load 'test_util.rb'
5
-
6
- begin
7
-
8
- db = SQLite.new("test.db")
9
-
10
- # create table
11
- db.execute "create table account(id TEXT, nick TEXT)"
12
- db.insert("account", "id, nick", "'hello', 'world'")
13
- db.insert("account", "id, nick", "'aaa', 'bbb'")
14
- db.insert("account", "id, nick", "'foo', 'bar'")
15
-
16
- # enum table data
17
- rs = db.select("account", "*")
18
-
19
- while (row = rs.next)
20
- p row
21
- end
22
-
23
- rs.close
24
-
25
- db.dispose
26
-
27
- rescue SQLite3::Exception => e
28
- puts "[error] " + e.to_s
1
+ load '../lib/db/database.rb'
2
+ load '../lib/db/sqlite.rb'
3
+
4
+ load 'test_util.rb'
5
+
6
+ begin
7
+
8
+ db = SQLite.new("test.db")
9
+
10
+ # create table
11
+ db.execute "create table account(id TEXT, nick TEXT)"
12
+ db.insert("account", "id, nick", "'hello', 'world'")
13
+ db.insert("account", "id, nick", "'aaa', 'bbb'")
14
+ db.insert("account", "id, nick", "'foo', 'bar'")
15
+
16
+ # enum table data
17
+ rs = db.select("account", "*")
18
+
19
+ while (row = rs.next)
20
+ p row
21
+ end
22
+
23
+ rs.close
24
+
25
+ db.dispose
26
+
27
+ rescue SQLite3::Exception => e
28
+ puts "[error] " + e.to_s
29
29
  end
@@ -1,6 +1,6 @@
1
- require 'socket'
2
-
3
- def send_obj(sock,obj)
4
- data = Marshal.dump(obj)
5
- sock.send [data.respond_to?(:bytesize) ? data.bytesize : data.size, data].pack('Na*'), 0
1
+ require 'socket'
2
+
3
+ def send_obj(sock,obj)
4
+ data = Marshal.dump(obj)
5
+ sock.send [data.respond_to?(:bytesize) ? data.bytesize : data.size, data].pack('Na*'), 0
6
6
  end
@@ -0,0 +1 @@
1
+ CONNECTION_NODELAY = 1
@@ -0,0 +1,3 @@
1
+ DB = SQLite
2
+
3
+ DB_ACCOUNT = "database/account.db"
@@ -0,0 +1 @@
1
+ SERVER_WORKERS = 5
@@ -0,0 +1,4 @@
1
+ SHAREDMEM = RedisMemory
2
+
3
+ SHAREDMEM_HOST = "localhost"
4
+ SHAREDMEM_PORT = 6379
@@ -0,0 +1,8 @@
1
+ class EchoHandler < Handler
2
+ def recv(server,connection,packet)
3
+ case packet.id
4
+ when Protocol::ECHO
5
+ connection.send packet
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,30 @@
1
+ # Merona main
2
+
3
+ import 'merona'
4
+
5
+ import 'dbms/*'
6
+ import 'sharedmem/*'
7
+ import 'handler/*'
8
+ import 'protocol/*'
9
+
10
+ import 'config/*'
11
+
12
+ begin
13
+ if meInitialize
14
+ Log.output "Merona engine started"
15
+ else
16
+ Log.error "Cannot initialize Merona!!"
17
+ end
18
+
19
+ polling do
20
+ # write code here
21
+
22
+ $oid = 0
23
+
24
+ echo = Server.new("echo_server", 9916)
25
+ echo.add_handler EchoHandler
26
+ end
27
+
28
+ rescue
29
+ Log.error "#{$!}"
30
+ end
@@ -0,0 +1,29 @@
1
+ # Merona entry
2
+
3
+ require 'rubygems'
4
+ require 'fileutils'
5
+
6
+ FileUtils.cd(File.dirname(File.expand_path(__FILE__)), :verbose => true)
7
+
8
+ $merona_path = Gem::bin_path('merona', 'merona')
9
+ $merona_path.reverse!
10
+ $merona_path = $merona_path.split("/", 3)[2]
11
+ $merona_path.reverse!
12
+ $merona_path += "/lib"
13
+
14
+ $LOAD_PATH << $merona_path
15
+ $LOAD_PATH << "."
16
+
17
+ def import(_path)
18
+ if _path[_path.length-1] == '*'
19
+ path = $merona_path + "/" + _path
20
+ Dir[path[0..path.length-1] + "*.rb"].each {|file| require file }
21
+ path = "./" + _path
22
+ Dir[path[0..path.length-1] + "*.rb"].each {|file| require file }
23
+ else
24
+ require _path
25
+ end
26
+ end
27
+
28
+ import 'main.rb'
29
+
@@ -0,0 +1,3 @@
1
+ class Protocol
2
+ ECHO = 1
3
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: merona
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.8
4
+ version: 1.1.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -97,16 +97,23 @@ files:
97
97
  - frame/config/config_sharedmem.rb
98
98
  - frame/database/account.db
99
99
  - frame/handler/ping_handler.rb
100
- - frame/log/2013.2.27.log
101
100
  - frame/main.rb
102
101
  - frame/merona.rb
103
102
  - frame/protocol/ping_protocol.rb
104
103
  - frame/run.bat
105
- - tests/test_chat.rb
106
- - tests/test_connect.rb
107
- - tests/test_ping.rb
108
- - tests/test_sqlite.rb
109
- - tests/test_util.rb
104
+ - tests/old/test_chat.rb
105
+ - tests/old/test_connect.rb
106
+ - tests/old/test_ping.rb
107
+ - tests/old/test_sqlite.rb
108
+ - tests/old/test_util.rb
109
+ - tests/server/echo_server/config/config_connection.rb
110
+ - tests/server/echo_server/config/config_db.rb
111
+ - tests/server/echo_server/config/config_server.rb
112
+ - tests/server/echo_server/config/config_sharedmem.rb
113
+ - tests/server/echo_server/handler/echo_handler.rb
114
+ - tests/server/echo_server/main.rb
115
+ - tests/server/echo_server/merona.rb
116
+ - tests/server/echo_server/protocol/ping_protocol.rb
110
117
  - bin/merona
111
118
  homepage: http://github.com/pjc0247/Merona
112
119
  licenses: []
@@ -1,16 +0,0 @@
1
- [2013:2:27:8:24:13] [LOG] : Merona engine started
2
- [2013:2:27:8:24:14] [LOG] : quit by user
3
- [2013:2:27:8:24:23] [LOG] : Merona engine started
4
- [2013:2:27:8:24:24] [LOG] : quit by user
5
- [2013:2:27:8:24:34] [LOG] : Merona engine started
6
- [2013:2:27:8:24:43] [LOG] : quit by user
7
- [2013:2:27:8:27:36] [LOG] : Merona engine started
8
- [2013:2:27:8:27:37] [LOG] : quit by user
9
- [2013:2:27:8:27:55] [LOG] : Merona engine started
10
- [2013:2:27:8:27:58] [LOG] : quit by user
11
- [2013:2:27:8:29:30] [LOG] : Merona engine started
12
- [2013:2:27:8:29:31] [LOG] : quit by user
13
- [2013:2:27:18:44:23] [LOG] : Merona engine started
14
- [2013:2:27:18:44:25] [LOG] : quit by user
15
- [2013:2:27:18:44:40] [LOG] : Merona engine started
16
- [2013:2:27:18:44:42] [LOG] : quit by user