指向性メモ::2005-12-21::今日のAda

ページ情報
制作日
2005-12-21T07:21:31+09:00
最終更新日
2005-12-21T19:43:26+09:00
ページ内目次

昨日作ったクライアントを少し改造して、IRCサーバに接続してみた。

with Ada.Text_IO;             use Ada.Text_IO;
with GNAT.Sockets;            use GNAT.Sockets;
with Ada.Strings.Unbounded;   use Ada.Strings.Unbounded;
with Ada.Command_Line;

with Ada.Streams;
use type Ada.Streams.Stream_Element_Count;

procedure Sock is

   task Connection_Send is
      entry Start (Connection : in out Stream_Access);
      entry Stop;
   end Connection_Send;

   task body Connection_Send is
      Connection_Stream : Stream_Access;
      Item              : String(1 .. 512);
      Last              : Natural;
   begin
      accept Start (Connection : in out Stream_Access) do
         Connection_Stream := Connection;
      end Start;
      loop
         Ada.Text_IO.Get_Line (Item => Item, Last => Last);
         String'Write(Connection_Stream, Item(1 .. Last) & ASCII.LF);
      end loop;
   end Connection_Send;

   task Connection_Recieve is
      entry Start (Connection : in out Stream_Access);
      entry Stop;
   end Connection_Recieve;

   task body Connection_Recieve is
      Connection_Stream : Stream_Access;
      Item              : Character;
   begin
      accept Start (Connection : in out Stream_Access) do
         Connection_Stream := Connection;
      end Start;
      loop
         Character'Read (Connection_Stream, Item);
         Ada.Text_IO.Put (Item);
      end loop;
   end Connection_Recieve;

   Server         : Socket_Type;
   Server_Address : Sock_Addr_Type;
   Connection     : Stream_Access;

begin
   Initialize;
   Server_Address.Addr := Addresses(Get_Host_By_Name(Ada.Command_Line.Argument(1)), 1);
   Server_Address.Port := 6667;

   Create_Socket (Server);
   Ada.Text_IO.Put_Line ("Connecting...");
   Connect_Socket (Server, Server_Address);
   Connection := Stream (Server);


   Ada.Text_IO.Put_Line ("Connection Established.");
   Ada.Text_IO.Put_Line ("-----------------------");

   Connection_Recieve.Start(Connection);
   Connection_Send.Start(Connection);
end Sock;

ホスト名からIPを解決できるようにしたので、./sock irc.tokyo.wide.ad.jpで繋がる。StringからIntegerへの変換方法が分からなかったので(型関係がいまいち理解できてない)、とりあえずポートは6667番固定にした。

接続できたらNICK hogeUSER hoge 0 hoge :hogeなどと打ったあと、JOIN #hogeすれば「#hoge」というチャンネルに入ることが出来るはず。詳細はRFC2812を参照のこと。

メッセージの受信はいただいたコメントに従ってCharacter'Read()を使用する方法に変えてみた。が、正直Attributeがよく分かっていない。Javaのstaticなメソッドに近い感じなのだろうか。

Comments

Name
YT
Datetime
2005-12-21T19:43:26+09:00
Message

StringからIntegerにする一番簡単な方法はInteger'Value("123")みたいな。Adaソース上でリテラルとして許される表記ならこれで変換してくれます。逆は'Imageです。

Attribute自体がどういう定義なのかは私もよくわからないですが、型に限らずいろんなもの諸々について、コンパイラが意味を把握している操作やらメタデータやらメモリレイアウトやら諸々みたいです。

'Readも、デフォルト実装をそのまま使うだけではなくてfor My_Type'Read use My_Read;みたいに挿げ替えることもできますし、そうしたらその型をレコードや配列の要素にした場合のレコードや配列の'Readのデフォルト実装も、My_Readを使ってくれるようになります。

あとリファレンスの件。

標準ライブラリのリファレンスは勿論規格に書いてある(http://adaic.com/standards/95lrm/html/RM-A.html)わけですが、GNAT以下は、単にgccが付けてくれてるだけのライブラリですのであまり詳しくないgccのマニュアル(http://gcc.gnu.org/onlinedocs/gcc-4.0.2/gnat_rm/)がいいとこじゃないですかね…。http://www.iuma.ulpgc.es/users/jmiranda/gnat-rts/rts/index.htmみたいなのもありますけれど。

Trackbacks

Trackback Ping URI

http://yudai.arielworks.com/memo/2005/12/21/072131.trackback

末尾に「1 + 9」の計算結果を繋げて下さい。例えば計算結果が「17」の場合、「072131.trackback17」です。これは機械的なトラックバックスパムを防止するための措置です。

Post a comment

Name (optional)
Email address or URI (optional)
Do the math below (required to filter comment spams)
1 + 9 + 2 =
Message (required)
Submit
連絡先、リンク、転載や複製などについては『サイト案内』をご覧ください。Powered by HIMMEL

I ♥ Validator