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