author | Claus Gittinger <cg@exept.de> |
Wed, 21 May 2003 23:22:44 +0200 | |
changeset 1233 | 999feaf57b80 |
parent 1228 | b766b9873ba6 |
child 1259 | 0a557e59ea4a |
permissions | -rw-r--r-- |
100 | 1 |
" |
2 |
COPYRIGHT (c) 1995 by Claus Gittinger |
|
1164 | 3 |
All Rights Reserved |
100 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
1023 | 13 |
"{ Package: 'stx:libbasic2' }" |
14 |
||
818 | 15 |
SocketAddress variableByteSubclass:#IPSocketAddress |
1164 | 16 |
instanceVariableNames:'' |
1233 | 17 |
classVariableNames:'NameCache AddrCache' |
488 | 18 |
poolDictionaries:'' |
816 | 19 |
category:'OS-Sockets' |
100 | 20 |
! |
21 |
||
22 |
!IPSocketAddress class methodsFor:'documentation'! |
|
23 |
||
24 |
copyright |
|
25 |
" |
|
26 |
COPYRIGHT (c) 1995 by Claus Gittinger |
|
1164 | 27 |
All Rights Reserved |
100 | 28 |
|
29 |
This software is furnished under a license and may be used |
|
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
37 |
||
38 |
documentation |
|
39 |
" |
|
111 | 40 |
Instances of IPSocketAddress represent tcp/ip-domain socket addresses. |
41 |
These consist of an ip address (4 bytes) and a port number. |
|
42 |
||
100 | 43 |
ST-80 compatibility class. |
44 |
This may be required when existing code has to be ported to ST/X; |
|
45 |
however, it may not be complete and more protocol may be added in the future. |
|
46 |
The code here was created when public domain code (Manchester) had to |
|
47 |
be ported to ST/X and missing classes/methods were encountered, and code added |
|
48 |
by reasoning 'what the original class could probably do there'. |
|
49 |
||
50 |
This is an additional goody class; therefore: |
|
51 |
||
52 |
THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTOR ``AS IS'' AND |
|
53 |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
54 |
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
55 |
ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTOR BE LIABLE |
|
56 |
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|
57 |
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
|
58 |
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|
59 |
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|
60 |
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
|
61 |
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
|
62 |
SUCH DAMAGE. |
|
63 |
" |
|
64 |
! ! |
|
65 |
||
1233 | 66 |
!IPSocketAddress class methodsFor:'instance creation'! |
67 |
||
68 |
hostName:name serviceName:portNrOrName type:socketTypeSymbol |
|
69 |
"get a new instance given a hostname, port or service and type" |
|
70 |
||
71 |
|addrBytes sa| |
|
72 |
||
73 |
portNrOrName isString ifTrue:[ |
|
74 |
^ super hostName:name serviceName:portNrOrName type:socketTypeSymbol |
|
75 |
]. |
|
76 |
||
77 |
AddrCache notNil ifTrue:[ |
|
78 |
addrBytes := AddrCache at:name ifAbsent:nil. |
|
79 |
]. |
|
80 |
addrBytes notNil ifTrue:[ |
|
81 |
sa := self hostAddress:addrBytes. |
|
82 |
portNrOrName notNil ifTrue:[ |
|
83 |
sa port:portNrOrName. |
|
84 |
]. |
|
85 |
] ifFalse:[ |
|
86 |
sa := super hostName:name serviceName:portNrOrName type:socketTypeSymbol. |
|
87 |
||
88 |
(AddrCache isNil or:[AddrCache size > 30]) ifTrue:[ |
|
89 |
AddrCache := Dictionary new |
|
90 |
]. |
|
91 |
AddrCache at:name put:(sa hostAddress). |
|
92 |
]. |
|
93 |
^ sa |
|
94 |
||
95 |
" |
|
96 |
SocketAddress hostName:'localhost' serviceName:10 type:#stream |
|
97 |
IPSocketAddress hostName:'localhost' serviceName:'echo' type:#datagram |
|
98 |
IPSocketAddress hostName:'localhost' serviceName:'echo' type:nil |
|
99 |
" |
|
100 |
! ! |
|
101 |
||
1164 | 102 |
!IPSocketAddress class methodsFor:'addressing'! |
103 |
||
104 |
anyAddress |
|
105 |
"return the broadcast address" |
|
106 |
||
107 |
^ #[255 255 255 255] |
|
108 |
! |
|
109 |
||
110 |
anyPort |
|
111 |
"return the anon port number" |
|
112 |
||
113 |
^ 0 |
|
114 |
! |
|
100 | 115 |
|
1164 | 116 |
firstUnreservedPort |
117 |
"return the first unreserved port number" |
|
118 |
||
119 |
^ 1024 |
|
120 |
! |
|
121 |
||
122 |
local |
|
123 |
"return IN_ADDR_ANY, the address matching any local address" |
|
124 |
||
125 |
^ #[127 0 0 1] |
|
100 | 126 |
! |
127 |
||
1164 | 128 |
maxPort |
129 |
"return the maximum port number" |
|
100 | 130 |
|
1164 | 131 |
^ 16rffff |
816 | 132 |
! |
133 |
||
1164 | 134 |
thisHost |
1226
0114bd044aa9
vw compatible domainSymbols (afXXX)
Claus Gittinger <cg@exept.de>
parents:
1164
diff
changeset
|
135 |
"return the bytes of IN_ADDR_ANY, the address matching any local address" |
1164 | 136 |
|
137 |
^ #[0 0 0 0] |
|
138 |
! ! |
|
139 |
||
140 |
!IPSocketAddress class methodsFor:'queries'! |
|
816 | 141 |
|
1164 | 142 |
domainSymbol |
143 |
||
1226
0114bd044aa9
vw compatible domainSymbols (afXXX)
Claus Gittinger <cg@exept.de>
parents:
1164
diff
changeset
|
144 |
^ #'AF_INET' |
0114bd044aa9
vw compatible domainSymbols (afXXX)
Claus Gittinger <cg@exept.de>
parents:
1164
diff
changeset
|
145 |
! |
0114bd044aa9
vw compatible domainSymbols (afXXX)
Claus Gittinger <cg@exept.de>
parents:
1164
diff
changeset
|
146 |
|
0114bd044aa9
vw compatible domainSymbols (afXXX)
Claus Gittinger <cg@exept.de>
parents:
1164
diff
changeset
|
147 |
obsoleteDomainSymbol |
1164 | 148 |
^ #inet |
149 |
! |
|
150 |
||
1226
0114bd044aa9
vw compatible domainSymbols (afXXX)
Claus Gittinger <cg@exept.de>
parents:
1164
diff
changeset
|
151 |
vwDomainSymbol |
0114bd044aa9
vw compatible domainSymbols (afXXX)
Claus Gittinger <cg@exept.de>
parents:
1164
diff
changeset
|
152 |
^ #afInet |
100 | 153 |
! ! |
154 |
||
155 |
!IPSocketAddress methodsFor:'accessing'! |
|
156 |
||
818 | 157 |
hostAddress |
1164 | 158 |
^ (ByteArray new:4) replaceFrom:1 to:4 with:self startingAt:5 |
816 | 159 |
! |
160 |
||
1164 | 161 |
hostAddress:aByteArray |
162 |
^ self replaceFrom:5 to:8 with:aByteArray startingAt:1 |
|
818 | 163 |
! |
164 |
||
165 |
port |
|
1164 | 166 |
^ self unsignedShortAt:3 bigEndian:true |
818 | 167 |
! |
168 |
||
169 |
port:aPortNr |
|
1164 | 170 |
|
171 |
self unsignedShortAt:3 put:aPortNr bigEndian:true |
|
172 |
! ! |
|
173 |
||
174 |
!IPSocketAddress methodsFor:'obsolete'! |
|
175 |
||
176 |
address |
|
177 |
^ self hostAddress |
|
178 |
! |
|
179 |
||
180 |
portOrName |
|
181 |
^ self port |
|
100 | 182 |
! ! |
183 |
||
770 | 184 |
!IPSocketAddress methodsFor:'printing & storing'! |
185 |
||
186 |
printOn:aStream |
|
187 |
"append a user printed representation of the receiver to aStream. |
|
188 |
The format is suitable for a human - not meant to be read back." |
|
189 |
||
1233 | 190 |
|port i1 i2| |
100 | 191 |
|
1233 | 192 |
i1 := self adrBytesStart. |
193 |
i2 := i1 + self numAdrBytes - 1. |
|
194 |
i1 to:i2 do:[:i | |
|
195 |
i ~~ i1 ifTrue:[ |
|
196 |
aStream nextPut:$. |
|
197 |
]. |
|
198 |
(self at:i) printOn:aStream |
|
1164 | 199 |
]. |
200 |
(port := self port) ~~ 0 ifTrue:[ |
|
1233 | 201 |
aStream nextPut:$:. |
202 |
port printOn:aStream. |
|
1164 | 203 |
]. |
488 | 204 |
! ! |
205 |
||
1233 | 206 |
!IPSocketAddress methodsFor:'private'! |
207 |
||
208 |
adrBytesStart |
|
209 |
^ 5 |
|
210 |
! |
|
211 |
||
212 |
numAdrBytes |
|
213 |
^ 4 |
|
214 |
! ! |
|
215 |
||
216 |
!IPSocketAddress methodsFor:'queries'! |
|
217 |
||
218 |
hostName |
|
219 |
|name| |
|
220 |
||
221 |
NameCache notNil ifTrue:[ |
|
222 |
name := NameCache at:(self hostAddress) ifAbsent:nil. |
|
223 |
name notNil ifTrue:[^ name]. |
|
224 |
]. |
|
225 |
||
226 |
name := super hostName. |
|
227 |
name notNil ifTrue:[ |
|
228 |
(NameCache isNil or:[NameCache size > 30]) ifTrue:[ |
|
229 |
NameCache := Dictionary new |
|
230 |
]. |
|
231 |
NameCache at:(self hostAddress) put:name. |
|
232 |
]. |
|
233 |
^ name |
|
234 |
! ! |
|
235 |
||
488 | 236 |
!IPSocketAddress class methodsFor:'documentation'! |
100 | 237 |
|
488 | 238 |
version |
1233 | 239 |
^ '$Header: /cvs/stx/stx/libbasic2/IPSocketAddress.st,v 1.18 2003-05-21 21:22:44 cg Exp $' |
100 | 240 |
! ! |