[#6862] Re: http_get.rb — 青山 和光 <PXN11625@...>

In-Reply-To: [ruby-list:6844] Re: http_get.rb

15 messages 1998/03/01

[#6906] ruby's Icon ? — 藤本尚邦 / FUJIMOTO Hisakuni <hisa@...>

藤本です、こんにちは。

25 messages 1998/03/03
[#6907] Re: ruby's Icon ? — matz@... (Yukihiro Matsumoto) 1998/03/03

まつもと ゆきひろです

[#6908] Re: ruby's Icon ? — 藤本尚邦 / FUJIMOTO Hisakuni <hisa@...> 1998/03/03

藤本です、こんにちは。

[#6911] Re: ruby's Icon ? — OZAWA Sakuro <ozawa@...> 1998/03/03

小澤さくです。

[#6912] Re: ruby's Icon ? — 藤本尚邦 / FUJIMOTO Hisakuni <hisa@...> 1998/03/03

藤本です、こんにちは。

[#6914] Re: ruby's Icon ? — 藤本尚邦 / FUJIMOTO Hisakuni <hisa@...> 1998/03/03

藤本です、こんばんは。

[#6918] manual 1.18b index — WATANABE Tetsuya <tetsu@...>

ruby-man-1.1b8 で、name タグがついているものを拾い集めて

17 messages 1998/03/04
[#6921] Re: manual 1.18b index — matz@... (Yukihiro Matsumoto) 1998/03/04

まつもと ゆきひろです

[#6954] Re: ruby's Icon ? — nosuzuki@... (Norio Suzuki)

こんばんは。鈴木教郎です。

18 messages 1998/03/04
[#6964] Re: ruby's Icon ? — matz@... (Yukihiro Matsumoto) 1998/03/05

まつもと ゆきひろです

[#7023] infinity — Tadayoshi Funaba <tadf@...>

ふなばです。

41 messages 1998/03/09
[#7029] Re: infinity — shugo@... (Shugo Maeda) 1998/03/09

前田です。

[#7033] Re: infinity — keiju@... (石塚圭樹 ) 1998/03/09

けいじゅ@日本ラショナルソフトウェアです.

[#7041] Re: infinity — Kazuhisa YANAGAWA <katze@...> 1998/03/10

In message <199803091741.CAA05774.keiju@cupmail0.rational.com>

[#7048] Re: infinity — keiju@... (Keiju ISHITSUKA) 1998/03/10

けいじゅ@日本ラショナルソフトウェアです.

[#7049] Re: infinity — matz@... (Yukihiro Matsumoto) 1998/03/10

まつもと ゆきひろです

[#7051] Re: infinity — keiju@... (石塚圭樹 ) 1998/03/10

けいじゅ@日本ラショナルソフトウェアです.

[#7054] Re: infinity — matz@... (Yukihiro Matsumoto) 1998/03/10

まつもと ゆきひろです

[#7050] Re: infinity — Kazuhisa YANAGAWA <katze@...> 1998/03/10

In message <199803100359.MAA08628.keiju@cupmail0.rational.com>

[#7259] Socket#shutdown — keiju@... (Keiju ISHITSUKA)

けいじゅ@日本ラショナルソフトウェアです.

16 messages 1998/03/28
[#7260] Re: Socket#shutdown — matz@... (Yukihiro Matsumoto) 1998/03/28

まつもと ゆきひろです

[#7265] Re: Socket#shutdown — keiju@... (石塚圭樹 ) 1998/03/29

けいじゅ@日本ラショナルソフトウェアです.

[ruby-list:7121] simple_chat_server.rb

From: 青山 和光 <PXN11625@...>
Date: 1998-03-15 23:16:05 UTC
List: ruby-list #7121
さらにシンプルにしました。

listen ウィンドウ用の Queue の使用をやめる事により、接続待ちのメインス
レッド以外のスレッドはすぐに消えて行くようになったので、効率もアップし
ました。 


#!/usr/local/bin/ruby
#
# simple_chat_server.rb
# Wakou Aoyama <pxn11625@niftyserve.or.jp>

require 'socket'
require 'thread'
require 'kconv'
require 'cgi-lib'

CR  = "\015"
LF  = "\012"
EOL = CR + LF

def add_http_header(content, option = '') # ===== add HTTP header
   'HTTP/1.0 200 OK' + EOL +
  ('Pragma: no-cache' + EOL if 'no-cache' == option) +
  ('Content-length: ' + content.length + EOL if not 'no-length' == option) +
   'Content-type: text/html' + EOL + EOL +
    content
end

def send_for_all_members(message) # ===== send message
  $members.each_value do |listener| # delivery for each listener
    begin
      listener['listen_window'] << message # delivery the message
    rescue # if listener was logouted
      handle = listener['handle']
      $members.delete(listener['no'])
      send_for_all_members( '(' + handle + " is logout.)<BR>\n" )
    end
  end
end

def talk_window(sock, my) # ===== talk window (post message)
  query = CGI.new(sock)
  if /\/HA (.+)/i =~ query.inputs['input'] # change handle
    send_for_all_members(
      '(change handle. "' + my['handle'] + '" --&gt; "' +
      Kconv.toeuc($1) + "\")<BR>\n" )
    my['handle'] = Kconv.toeuc($1)
  elsif query.inputs.include?('input') and '' != query.inputs['input']
    send_for_all_members(my['handle'] + ' &gt; ' +
      Kconv.toeuc( (query.inputs['input'] or '') ) + "<BR>\n" )
  end

  sock << add_http_header(
    %|<BODY BGCOLOR="white">
      <FORM METHOD="post" ACTION="/talk#{my['no']}.html" NAME="input">
        <B>#{my['handle']} &gt; </B>
        <INPUT TYPE="text" NAME="input" SIZE="60">
        <INPUT TYPE="submit" VALUE="send">
      </FORM>|.gsub(/^      /, ''))
  sock.shutdown(2)
end

def listen_window(sock, my) # ===== listen window
  my['listen_window'] = sock
  sock << add_http_header(
    %|<BODY BGCOLOR="white">
      <H1>Welcome!<BR>simple chat server.</H1>
      <P><B>Powerd by
      <A HREF="http://www.netlab.co.jp/ruby/jp/">
      Ruby</A>.</B></P>
      <P>change handle. input like this.<BR>
      <TT>/HA handle</TT></P>|.gsub(/^      /, ''), 'no-length')
  send_for_all_members( '(' + my['handle'] + " is login.)<BR>\n" )
end

def new_access(sock, my) # ===== new access
  sock << add_http_header(
    %|<TITLE>CHAT</TITLE>
      <FRAMESET ROWS="*, 60">
        <FRAME SRC="/listen#{my['no']}.html">
        <FRAME SRC="/talk#{my['no']}.html">
      </FRAMESET>|.gsub(/^      /, ''), 'no-cache')
  sock.shutdown(2)
end

def session(sock)
  head = ''
  head += sock.read(1) while /#{EOL}#{EOL}|#{LF}#{LF}|#{CR}#{CR}/n !~ head

  case head # analyze http header
  when /^GET\s+([^?\s]+)\??([^\s]*)/
    ENV['REQUEST_METHOD'] = 'GET'
    file_name = $1
    ENV['QUERY_STRING'] = ($2 or '').to_s
  when /^POST\s+([^\s]+)/
    ENV['REQUEST_METHOD'] = 'POST'
    file_name = $1
    ENV['CONTENT_LENGTH'] = head.scan(/^Content-length:\s*(\d+)/i)[0].to_s
  end

  case file_name
  when %r|^/$|                 # ===== new access
    $serial_no += 1
    $members[$serial_no] = { 'no'     => $serial_no,
                             'handle' => 'No.' + $serial_no.to_s}
    new_access(sock, $members[$serial_no])
  when %r|^/talk(\d+).html$|   # ===== talk window (post message)
    talk_window(sock, $members[$1.to_i])
  when %r|^/listen(\d+).html$| # ===== listen window
    listen_window(sock, $members[$1.to_i])
  else                         # ===== file not found
    sock << add_http_header('<H1>file not found</H1>')
    sock.shutdown(2)
  end
end

PORT = ARGV.shift || 8080
$serial_no = 0
$members = {}

port = TCPserver.open(PORT)
trap('PIPE', 'IGNORE')
STDERR.print (Time.now, "\tserver is on #{port.addr[1]}\n")

while TRUE
  sock = port.accept # stand by for accept

  Thread.start do # divide the work
    session(sock)
  end
end


青山 和光 Wakou Aoyama <pxn11625@niftyserve.or.jp>

In This Thread

Prev Next