1 | # ---------------------------------------------------------------------- |
---|
2 | # LIBRARY: handles the client side of the connection in the p2p |
---|
3 | # infrastructure |
---|
4 | # ---------------------------------------------------------------------- |
---|
5 | # Michael McLennan (mmclennan@purdue.edu) |
---|
6 | # ====================================================================== |
---|
7 | # Copyright (c) 2008 Purdue Research Foundation |
---|
8 | # ====================================================================== |
---|
9 | package require Itcl |
---|
10 | |
---|
11 | itcl::class Client { |
---|
12 | inherit Handler |
---|
13 | |
---|
14 | private variable _addr "" ;# address that this client is connected to |
---|
15 | private variable _sid "" ;# file handle for server connection |
---|
16 | |
---|
17 | constructor {addr args} { |
---|
18 | eval configure $args |
---|
19 | |
---|
20 | # |
---|
21 | # Connect to the server at the specified address, which is |
---|
22 | # specified as "host:port". |
---|
23 | # |
---|
24 | set alist [split $addr :] |
---|
25 | if {[llength $alist] != 2} { |
---|
26 | error "bad address \"$addr\": should be host:port" |
---|
27 | } |
---|
28 | foreach {host port} $alist break |
---|
29 | set _sid [socket $host $port] |
---|
30 | connectionName $_sid $host:$port |
---|
31 | connectionSpeaks $_sid DEFAULT |
---|
32 | |
---|
33 | fileevent $_sid readable [itcl::code $this handle $_sid] |
---|
34 | fconfigure $_sid -buffering line |
---|
35 | |
---|
36 | set _addr $addr |
---|
37 | } |
---|
38 | |
---|
39 | destructor { |
---|
40 | catch {close $_sid} |
---|
41 | } |
---|
42 | |
---|
43 | public method send {message} |
---|
44 | public method address {} |
---|
45 | |
---|
46 | protected method handlerType {} |
---|
47 | } |
---|
48 | |
---|
49 | # ---------------------------------------------------------------------- |
---|
50 | # USAGE: send <message> |
---|
51 | # |
---|
52 | # Used to send a <message> off to the server. If the connection |
---|
53 | # was unexpectedly closed, then this method does nothing. |
---|
54 | # ---------------------------------------------------------------------- |
---|
55 | itcl::body Client::send {message} { |
---|
56 | if {"" != $_sid} { |
---|
57 | if {[eof $_sid]} { |
---|
58 | set _sid "" |
---|
59 | } else { |
---|
60 | log debug "sending: $message" |
---|
61 | puts $_sid $message |
---|
62 | } |
---|
63 | } |
---|
64 | } |
---|
65 | |
---|
66 | # ---------------------------------------------------------------------- |
---|
67 | # USAGE: address |
---|
68 | # |
---|
69 | # Returns the address that this client is connected to. This is |
---|
70 | # the host:port passed in when the client was created. |
---|
71 | # ---------------------------------------------------------------------- |
---|
72 | itcl::body Client::address {} { |
---|
73 | return $_addr |
---|
74 | } |
---|
75 | |
---|
76 | # ---------------------------------------------------------------------- |
---|
77 | # USAGE: handlerType |
---|
78 | # |
---|
79 | # Returns a descriptive string describing this handler. Derived |
---|
80 | # classes override this method to provide their own string. Used |
---|
81 | # for debug messages. |
---|
82 | # ---------------------------------------------------------------------- |
---|
83 | itcl::body Client::handlerType {} { |
---|
84 | return "client" |
---|
85 | } |
---|