Fleyhe 1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/Fleyhe.rb +191 -0
  2. metadata +46 -0
@@ -0,0 +1,191 @@
1
+ require 'socket'
2
+ require 'thread'
3
+ require 'timeout'
4
+ require 'sqlite3'
5
+
6
+
7
+ # Made with love <3 by:
8
+ # Marlon Henry Schweigert
9
+
10
+ module Fleyhe
11
+
12
+ module Network
13
+
14
+
15
+ # Accept Bridges in the server, and configure it
16
+ class Listener
17
+
18
+ def initialize port=3030,event=Event
19
+ # Config the event
20
+ @event = event
21
+ @event.generateHash
22
+
23
+
24
+ # Config the TCPServer
25
+ @tcp = TCPServer.new port
26
+
27
+ #Config the thread for working
28
+ @thread = Thread.new {
29
+ self.work
30
+ }
31
+ end
32
+
33
+ def close
34
+ @thread.kill
35
+ @tcp.close
36
+ end
37
+
38
+ protected
39
+ def work
40
+ loop {
41
+ Bridge.new(@tcp.accept, @event)
42
+ sleep 1
43
+ }
44
+ end
45
+
46
+
47
+
48
+ end
49
+
50
+ # Made a bridge (Server <-> Client) in TCP to call Events
51
+ class Bridge
52
+
53
+ def initialize tcp, event=Event
54
+ @tcp = tcp
55
+ @event = event
56
+ @thread = Thread.new {
57
+ self.work
58
+ }
59
+ end
60
+
61
+ def close
62
+ # Close your work
63
+ @tcp.close
64
+ @thread.kill
65
+ end
66
+
67
+ def call event="Event",args=[]
68
+ @tcp.puts event.to_s
69
+ @tcp.puts args.size.to_s
70
+
71
+ for i in args
72
+ @tcp.puts i.to_s
73
+ end
74
+
75
+ end
76
+
77
+ def read
78
+ event = nil
79
+ args = []
80
+
81
+ event = @tcp.gets.to_s.chomp
82
+ n = @tcp.gets.to_i
83
+
84
+ n.times do
85
+ args << @tcp.gets.to_s
86
+ end
87
+
88
+ return event,args
89
+
90
+
91
+ end
92
+
93
+ protected
94
+ def work
95
+
96
+ begin
97
+ loop {
98
+ event = nil
99
+ args = []
100
+
101
+ Timeout.timeout(60) {event, args = self.read}
102
+ @event.getHash[event].new(self,args)
103
+
104
+ }
105
+ rescue => e
106
+ puts "Error on Bridge: #{ self.to_s }"
107
+ puts " \t #{ e.to_s }"
108
+ self.close
109
+ end
110
+
111
+ end
112
+
113
+
114
+ end
115
+
116
+
117
+ # Make and generate events for Bridges
118
+ class Event
119
+
120
+ def initialize bridge, args
121
+
122
+ @bridge = bridge
123
+ @args = args
124
+ @event = nil
125
+ @form = []
126
+
127
+ self.solve
128
+
129
+ unless @event == nil
130
+ @bridge.call(@event, @form)
131
+ end
132
+
133
+ end
134
+
135
+ def solve
136
+ puts "Noting here..."
137
+ end
138
+
139
+ def self.generateHash
140
+ @@hash = Hash.new
141
+ obj = ObjectSpace.each_object(Class).select { |klass| klass < self }
142
+ for i in obj
143
+ @@hash[i.to_s] = i
144
+ end
145
+ end
146
+
147
+ def self.getHash
148
+ return @@hash
149
+ end
150
+
151
+ end
152
+
153
+ end
154
+
155
+ module Database
156
+
157
+ class Database
158
+ def initialize file
159
+ @database = SQLite3::Database.open file
160
+ @database.results_as_hash = true
161
+ end
162
+
163
+ def execute command
164
+ return @database.execute command
165
+ end
166
+
167
+ def close
168
+ @database.close
169
+ end
170
+ end
171
+
172
+ end
173
+
174
+
175
+ end
176
+
177
+
178
+ class Evento < Fleyhe::Network::Event
179
+ def solve
180
+ puts @args
181
+ end
182
+ end
183
+
184
+ Fleyhe::Network::Listener.new
185
+
186
+ a = Fleyhe::Network::Bridge.new(TCPSocket.new("localhost",3030))
187
+ a.call "Evento", [1,2,3,4,5,6,7,8]
188
+
189
+ loop {
190
+
191
+ }
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: Fleyhe
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Marlon Henry Schweigert
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2016-06-08 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A simple gem for made game servers
15
+ email: fleyhe0@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/Fleyhe.rb
21
+ homepage: http://www.fleyhe.com
22
+ licenses:
23
+ - GPL 3.0
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 1.8.23
43
+ signing_key:
44
+ specification_version: 3
45
+ summary: Fleyhe Gem
46
+ test_files: []