여자처자 해서 간만에 서버 프로그램을 만들어야 했다. 예전 같으면 C나 C++로 만들었을테지만, 요즘엔 귀찮다. 왠만하면 HTTP 기반으로 하고 있는데, 이번엔 HTTP를 쓸 수 있는 상황이 아니다. Persistant한 통신채널을 유지해야 하기 때문이다.
최근 ruby 언어를 사용하고 있으니, ruby기반으로 서버 프로그램을 간단히 만들 수 있는 무언가 있을 거라고생각해서 찾은게 "GServer"이다.
GServer는 thread pool관리, 로깅, 멀티 스레드 관리와 같은 기능을 내장하고 있는 루비 라이브러리다.
Hello 서버 프로그램
Hello World를 출력하는 소켓 프로그램을 만들었다.
#!/usr/bin/ruby
require 'gserver'
class TestServer < GServer
def initialize(port=8000, *args)
super(port, *args)
end
def serve(io)
io.puts "What is your name?"
name = io.gets
io.puts "Hello #{name}"
server.stop
end
end
server = TestServer.new
server.audit = true
server.start
server.join
아 간단하다. 이걸로 멀티 스레드 기반의 Hello 서버 프로그램을 만들었다.
상수
DEFAULT_HOST
기본 bind 호스트 주소로 "127.0.0.1"이다. 위 예제는 bind 호스트를 설정하지 않았으므로, DEFAULT_HOST가 설정된다. netstate로 확인해 보자.
Contents
Gserver
Hello 서버 프로그램
상수
DEFAULT_HOST
Attribute
debug
host
port
stdlog
public instnce methods
connections
join
start(maxConnections = -1)
stop
stopped?
Protected Instance method
connection(client)
disconnectiong(clientPort)
error(detail)
log(msg)
starting()
stopping()
Recent Posts
Archive Posts
Tags