my-process-server 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +0 -0
- data/Rakefile +14 -0
- data/VERSION +1 -0
- data/bin/process-server +7 -0
- data/lib/client.rb +0 -0
- data/lib/process_server.rb +5 -0
- data/lib/server.rb +75 -0
- data/spec/server_spec.rb +134 -0
- data/spec/spec_helper.rb +1 -0
- metadata +77 -0
data/README.rdoc
ADDED
File without changes
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
begin
|
2
|
+
require 'jeweler'
|
3
|
+
Jeweler::Tasks.new do |gemspec|
|
4
|
+
gemspec.name = "my-process-server"
|
5
|
+
gemspec.summary = "Daemon to run local system calls"
|
6
|
+
gemspec.description = "Daemon to run local system calls."
|
7
|
+
|
8
|
+
gemspec.email = "anylyne@gmail.com"
|
9
|
+
gemspec.homepage = ""
|
10
|
+
gemspec.authors = ["Pedro Dias", "Carolina Mascarenhas"]
|
11
|
+
end
|
12
|
+
rescue LoadError
|
13
|
+
puts "Jeweler not available. Install it with: gem install jeweler"
|
14
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/bin/process-server
ADDED
data/lib/client.rb
ADDED
File without changes
|
data/lib/server.rb
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
class Server
|
2
|
+
|
3
|
+
def start
|
4
|
+
@server = TCPServer.open(2000)
|
5
|
+
loop {
|
6
|
+
|
7
|
+
Thread.new(@server.accept) do |socket|
|
8
|
+
begin
|
9
|
+
|
10
|
+
unless localhost?(socket)
|
11
|
+
socket.puts "Error"
|
12
|
+
next
|
13
|
+
end
|
14
|
+
socket.puts "Welcome"
|
15
|
+
|
16
|
+
msg_client = socket.recvfrom( 10000 ) # recebe mensagem - 10000 bytes - do cliente
|
17
|
+
|
18
|
+
if stop_message?(msg_client.to_s)
|
19
|
+
socket.close
|
20
|
+
break
|
21
|
+
end
|
22
|
+
|
23
|
+
if valid_command?(msg_client.to_s) and execute_command(msg_client.to_s)
|
24
|
+
socket.puts "Success"
|
25
|
+
else
|
26
|
+
socket.puts "Error"
|
27
|
+
end
|
28
|
+
|
29
|
+
socket.close # fecha conexão
|
30
|
+
|
31
|
+
rescue => e
|
32
|
+
puts "Dando erro no loop #{e.backtrace}"
|
33
|
+
socket.close
|
34
|
+
end
|
35
|
+
end
|
36
|
+
}
|
37
|
+
end
|
38
|
+
|
39
|
+
def stop
|
40
|
+
begin
|
41
|
+
socket = TCPSocket.open('localhost', 2000)
|
42
|
+
socket.gets
|
43
|
+
socket.write "stop"
|
44
|
+
|
45
|
+
@server.close unless @server.closed?
|
46
|
+
rescue => e
|
47
|
+
puts "Dando erro no close #{e.backtrace}"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
def localhost? socket
|
53
|
+
socket.peeraddr[2] == 'localhost'
|
54
|
+
end
|
55
|
+
|
56
|
+
def stop_message? message
|
57
|
+
message == 'stop'
|
58
|
+
end
|
59
|
+
|
60
|
+
def valid_command? command
|
61
|
+
one_command?(command) and ffmpeg_command?(command)
|
62
|
+
end
|
63
|
+
|
64
|
+
def one_command? command
|
65
|
+
command !~ /;/
|
66
|
+
end
|
67
|
+
|
68
|
+
def ffmpeg_command? command
|
69
|
+
command =~ /ffmpeg/
|
70
|
+
end
|
71
|
+
|
72
|
+
def execute_command command
|
73
|
+
system "#{command} > out"
|
74
|
+
end
|
75
|
+
end
|
data/spec/server_spec.rb
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Server' do
|
4
|
+
|
5
|
+
it 'should be stoped' do
|
6
|
+
start_server
|
7
|
+
@process_server.stop
|
8
|
+
lambda { TCPSocket.open('localhost', 2000) }.should raise_error Errno::ECONNREFUSED
|
9
|
+
end
|
10
|
+
|
11
|
+
context 'when dealing with one connection' do
|
12
|
+
before do
|
13
|
+
start_server
|
14
|
+
start_client
|
15
|
+
end
|
16
|
+
|
17
|
+
after do
|
18
|
+
@process_server.stop
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'should send a welcome message and receive a system call' do
|
22
|
+
welcome = @socket.gets
|
23
|
+
welcome.should_not be_empty
|
24
|
+
welcome.should match /Welcome/
|
25
|
+
|
26
|
+
@socket.write "ffmpeg -version"
|
27
|
+
success = @socket.gets
|
28
|
+
success.should_not be_empty
|
29
|
+
success.should match /Success/
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should send a error message when receiving a invalid system call' do
|
33
|
+
@socket.gets
|
34
|
+
@socket.write "command_doesnt_exists"
|
35
|
+
error = @socket.gets
|
36
|
+
error.should_not be_empty
|
37
|
+
error.should match /Error/
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'should send a error message when receiving a non ffmpeg command' do
|
41
|
+
@socket.gets
|
42
|
+
@socket.write "ls"
|
43
|
+
error = @socket.gets
|
44
|
+
error.should_not be_empty
|
45
|
+
error.should match /Error/
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'should send a error message when receiving more than one command' do
|
49
|
+
@socket.gets
|
50
|
+
@socket.write "ffmpeg -version; ls"
|
51
|
+
error = @socket.gets
|
52
|
+
error.should_not be_empty
|
53
|
+
error.should match /Error/
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
context 'when dealing with more than one connection' do
|
58
|
+
before do
|
59
|
+
start_server
|
60
|
+
start_client
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'should receive receive a system call' do
|
64
|
+
@socket.gets
|
65
|
+
@socket.write "ffmpeg -version"
|
66
|
+
success = @socket.gets
|
67
|
+
success.should_not be_empty
|
68
|
+
success.should match /Success/
|
69
|
+
|
70
|
+
@socket = start_client
|
71
|
+
@socket.gets
|
72
|
+
@socket.write "ffmpeg -version"
|
73
|
+
success = @socket.gets
|
74
|
+
success.should_not be_empty
|
75
|
+
success.should match /Success/
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
context 'when dealing with concurrent connections' do
|
80
|
+
|
81
|
+
it 'should receive a system call' do
|
82
|
+
socket1 = start_client
|
83
|
+
socket2 = start_client
|
84
|
+
|
85
|
+
welcome = socket1.gets
|
86
|
+
welcome.should_not be_empty
|
87
|
+
|
88
|
+
welcome = socket2.gets
|
89
|
+
welcome.should_not be_empty
|
90
|
+
|
91
|
+
socket1.write "ffmpeg -version"
|
92
|
+
success = socket1.gets
|
93
|
+
success.should_not be_empty
|
94
|
+
success.should match /Success/
|
95
|
+
|
96
|
+
socket2.write "ffmpeg -version"
|
97
|
+
success = socket2.gets
|
98
|
+
success.should_not be_empty
|
99
|
+
success.should match /Success/
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
private
|
104
|
+
def start_client
|
105
|
+
@socket = TCPSocket.open('localhost', 2000)
|
106
|
+
@socket.should_not be_nil
|
107
|
+
@socket
|
108
|
+
end
|
109
|
+
def start_server
|
110
|
+
@process_server = Server.new
|
111
|
+
Thread.start {
|
112
|
+
@process_server.start
|
113
|
+
}
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
|
134
|
+
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'process_server'
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: my-process-server
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Pedro Dias
|
14
|
+
- Carolina Mascarenhas
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2011-03-04 00:00:00 -03:00
|
20
|
+
default_executable: process-server
|
21
|
+
dependencies: []
|
22
|
+
|
23
|
+
description: Daemon to run local system calls.
|
24
|
+
email: anylyne@gmail.com
|
25
|
+
executables:
|
26
|
+
- process-server
|
27
|
+
extensions: []
|
28
|
+
|
29
|
+
extra_rdoc_files:
|
30
|
+
- README.rdoc
|
31
|
+
files:
|
32
|
+
- README.rdoc
|
33
|
+
- Rakefile
|
34
|
+
- VERSION
|
35
|
+
- bin/process-server
|
36
|
+
- lib/client.rb
|
37
|
+
- lib/process_server.rb
|
38
|
+
- lib/server.rb
|
39
|
+
- spec/server_spec.rb
|
40
|
+
- spec/spec_helper.rb
|
41
|
+
has_rdoc: true
|
42
|
+
homepage: ""
|
43
|
+
licenses: []
|
44
|
+
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options: []
|
47
|
+
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
hash: 3
|
56
|
+
segments:
|
57
|
+
- 0
|
58
|
+
version: "0"
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
hash: 3
|
65
|
+
segments:
|
66
|
+
- 0
|
67
|
+
version: "0"
|
68
|
+
requirements: []
|
69
|
+
|
70
|
+
rubyforge_project:
|
71
|
+
rubygems_version: 1.4.2
|
72
|
+
signing_key:
|
73
|
+
specification_version: 3
|
74
|
+
summary: Daemon to run local system calls
|
75
|
+
test_files:
|
76
|
+
- spec/server_spec.rb
|
77
|
+
- spec/spec_helper.rb
|