root/apps/outlook/branches/1.6.1/DotTelSystem/Lookup/LookupTelname.cs
@
669
| Revision 669, 27.9 kB (checked in by jonmaycock, 3 years ago) |
|---|
| Line | |
|---|---|
| 1 | ///<summary> |
| 2 | /// Author: Ben Dowling & Nick Brunwin |
| 3 | ///------------------------ |
| 4 | ///Copyright (c) 2008, Telnic Ltd. |
| 5 | /// |
| 6 | ///All rights reserved. |
| 7 | /// |
| 8 | ///Redistribution and use in source and binary forms, with or |
| 9 | ///without modification, are permitted provided that the following |
| 10 | ///conditions are met: |
| 11 | /// |
| 12 | ///* Redistributions of source code must retain the above |
| 13 | /// copyright notice, this list of conditions and the |
| 14 | /// following disclaimer. |
| 15 | ///* Redistributions in binary form must reproduce the above |
| 16 | /// copyright notice, this list of conditions and the following |
| 17 | /// disclaimer in the documentation and/or other materials |
| 18 | /// provided with the distribution. |
| 19 | ///* Neither the name of the Telnic Ltd. nor the names of its |
| 20 | /// contributors may be used to endorse or promote products |
| 21 | /// derived from this software without specific prior written |
| 22 | /// permission. |
| 23 | /// |
| 24 | ///THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
| 25 | ///CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 26 | ///INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 27 | ///MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 28 | ///DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 29 | ///CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 30 | ///SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 31 | ///LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
| 32 | ///USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 33 | ///AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 34 | ///LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 35 | ///IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 36 | ///THE POSSIBILITY OF SUCH DAMAGE. |
| 37 | ///-------------------------- |
| 38 | ///</summary> |
| 39 | using System; |
| 40 | using System.Net; |
| 41 | using System.Net.Sockets; |
| 42 | using System.Threading; |
| 43 | |
| 44 | using DNS = org.telnic.outlook.dns.DNS; |
| 45 | using LookupWorkerCompletedListener = org.telnic.outlook.lookup.LookupWorkerCompletedListener; |
| 46 | using org.telnic.outlook.util; |
| 47 | |
| 48 | namespace org.telnic.outlook.lookup |
| 49 | { |
| 50 | |
| 51 | /// <summary> Lookup all NAPTR and TXT records for a given telname. |
| 52 | /// |
| 53 | /// </summary> |
| 54 | /// <author> Ben Dowling |
| 55 | /// </author> |
| 56 | /// <version> $id$ |
| 57 | /// </version> |
| 58 | public class LookupTelname : LookupWorkerCompletedListener |
| 59 | { |
| 60 | |
| 61 | /// <summary> Sets the listener. |
| 62 | /// |
| 63 | /// </summary> |
| 64 | /// <param name="l">the listener |
| 65 | /// </param> |
| 66 | virtual public LookupTelnameCompleteListener Listener |
| 67 | { |
| 68 | set |
| 69 | { |
| 70 | completeListener = value; |
| 71 | } |
| 72 | |
| 73 | } |
| 74 | /// <summary> Gets the status. |
| 75 | /// |
| 76 | /// </summary> |
| 77 | /// <returns> the status |
| 78 | /// </returns> |
| 79 | virtual public int Status |
| 80 | { |
| 81 | get |
| 82 | { |
| 83 | return status; |
| 84 | } |
| 85 | |
| 86 | } |
| 87 | |
| 88 | // is the lookup complete |
| 89 | private bool complete; |
| 90 | |
| 91 | /// <summary> The telname.</summary> |
| 92 | private System.String telname; |
| 93 | |
| 94 | /// <summary> The lw naptr.</summary> |
| 95 | private LookupWorker lwNaptr; |
| 96 | |
| 97 | /// <summary> The lw naptr.</summary> |
| 98 | private LookupWorker lwLoc; |
| 99 | |
| 100 | /// <summary> The lw txt.</summary> |
| 101 | private LookupWorker lwTxt; |
| 102 | |
| 103 | /// <summary> The lw ninfo.</summary> |
| 104 | private LookupWorker lwNInfo; |
| 105 | |
| 106 | /// <summary> The lw kw.</summary> |
| 107 | private LookupWorker lwKeyword; |
| 108 | |
| 109 | /// <summary> The lw version.</summary> |
| 110 | private LookupWorker lwVersion; |
| 111 | |
| 112 | /// <summary> The lookup result.</summary> |
| 113 | private LookupResult lookupResult; |
| 114 | |
| 115 | /// <summary> The complete listener.</summary> |
| 116 | private LookupTelnameCompleteListener completeListener; |
| 117 | |
| 118 | /// <summary> The cached result.</summary> |
| 119 | private LookupResult cachedResult; |
| 120 | |
| 121 | /// <summary> The cancelled.</summary> |
| 122 | private bool cancelled = false; |
| 123 | |
| 124 | /// <summary> The Constant STATUS_OK.</summary> |
| 125 | public const int STATUS_OK = 0; |
| 126 | |
| 127 | /// <summary> The Constant STATUS_NX_DOMAIN.</summary> |
| 128 | public const int STATUS_NX_DOMAIN = 1; |
| 129 | |
| 130 | /// <summary> The Constant STATUS_LOOKUP_ERROR.</summary> |
| 131 | public const int STATUS_LOOKUP_ERROR = 2; |
| 132 | |
| 133 | /// <summary> The Constant STATUS_NO_NETWORK.</summary> |
| 134 | public const int STATUS_NO_NETWORK = 3; |
| 135 | |
| 136 | /// <summary> The Constant STATUS_CANCELLED.</summary> |
| 137 | public const int STATUS_CANCELLED = 4; |
| 138 | |
| 139 | /// <summary> The Constant STATUS_TIMEOUT.</summary> |
| 140 | public const int STATUS_TIMEOUT = 5; |
| 141 | |
| 142 | /// <summary> The status.</summary> |
| 143 | private int status = STATUS_OK; |
| 144 | |
| 145 | /// <summary> The version at which we change lookups</summary> |
| 146 | private const int version_change = 12; |
| 147 | |
| 148 | private Thread naptrThread; |
| 149 | private Thread locThread; |
| 150 | private Thread txtThread; |
| 151 | private Thread nInfoThread; |
| 152 | private Thread keywordThread; |
| 153 | private Thread versionThread; |
| 154 | |
| 155 | public LookupWorker.LookupStatus parentStatus; |
| 156 | |
| 157 | private IPAddress address; |
| 158 | private int port; |
| 159 | |
| 160 | /// <summary> The Constructor.</summary> |
| 161 | public LookupTelname(IPAddress address, int port) |
| 162 | { |
| 163 | this.address = address; |
| 164 | this.port = port; |
| 165 | } |
| 166 | |
| 167 | |
| 168 | /// <summary> Perform lookup. |
| 169 | /// |
| 170 | /// </summary> |
| 171 | /// <param name="telname">the telname |
| 172 | /// </param> |
| 173 | public virtual void performLookup(string telname, string publicTelname) |
| 174 | { |
| 175 | if (completeListener == null) |
| 176 | { |
| 177 | // no point doing any work if nobody wants to know the result! |
| 178 | Logger.Log(Logger.Section.Lookups, "performLookup called without a listener, returning"); |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | this.telname = telname; |
| 183 | this.lookupResult = new LookupResult(telname, publicTelname); |
| 184 | |
| 185 | complete = false; |
| 186 | cancelled = false; |
| 187 | |
| 188 | |
| 189 | // check the cache for lookup results |
| 190 | if (!cancelled) |
| 191 | { |
| 192 | cachedResult = LookupCache.GetResult(telname); |
| 193 | } |
| 194 | |
| 195 | // if we have a valid cache result return it |
| 196 | if (!cancelled && cachedResult != null && cachedResult.Valid) |
| 197 | { |
| 198 | Logger.Log(Logger.Section.Lookups, "Using cached result for " + telname); |
| 199 | completeListener.LookupTelnameComplete(cachedResult); |
| 200 | } |
| 201 | // otherwise try and lookup a new result |
| 202 | else if (!cancelled) |
| 203 | { |
| 204 | int version = GetVersion(publicTelname); |
| 205 | |
| 206 | if (version < version_change) |
| 207 | { |
| 208 | Logger.Log(Logger.Section.Lookups, "version " + version ); |
| 209 | if (publicTelname == telname) |
| 210 | { |
| 211 | Logger.Log(Logger.Section.Lookups, "public"); |
| 212 | DoVersionOneLookupAndCache(publicTelname); |
| 213 | } |
| 214 | else |
| 215 | { |
| 216 | Logger.Log(Logger.Section.Lookups, "private "); |
| 217 | DoVersionOneLookupAndCachePrivate(publicTelname, telname); |
| 218 | } |
| 219 | } |
| 220 | else |
| 221 | { |
| 222 | Logger.Log(Logger.Section.Lookups, "version " + version); |
| 223 | DoLookupAndCache(publicTelname, telname); |
| 224 | } |
| 225 | |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | /// <summary> |
| 230 | /// Performs both public and private lookup and caching for version 1.1 onwards |
| 231 | /// </summary> |
| 232 | /// <param name="publicTelname">the public telname</param> |
| 233 | /// <param name="telname">the private telname</param> |
| 234 | private void DoLookupAndCache(string publicTelname, string telname) |
| 235 | { |
| 236 | // do txt and naptr lookups in separate threads |
| 237 | performTxtLookup(telname); |
| 238 | performLocLookup(telname); |
| 239 | performNaptrLookup(telname); |
| 240 | performNInfoLookup(telname); |
| 241 | performKeywordLookup("_kw." + publicTelname); |
| 242 | |
| 243 | // wait for those threads to finish |
| 244 | naptrThread.Join(); |
| 245 | locThread.Join(); |
| 246 | txtThread.Join(); |
| 247 | nInfoThread.Join(); |
| 248 | keywordThread.Join(); |
| 249 | |
| 250 | Logger.Log(Logger.Section.Lookups, "Lookup threads finished for " + telname); |
| 251 | Logger.Log(Logger.Section.Lookups, "NAPTR Status: " + lookupResult.NaptrStatus); |
| 252 | Logger.Log(Logger.Section.Lookups, "LOC Status: " + lookupResult.LocStatus); |
| 253 | Logger.Log(Logger.Section.Lookups, "TXT Status: " + lookupResult.TxtStatus); |
| 254 | Logger.Log(Logger.Section.Lookups, "NInfo Status: " + lookupResult.NInfoStatus); |
| 255 | Logger.Log(Logger.Section.Lookups, "Keyword Status: " + lookupResult.KeywordStatus); |
| 256 | Logger.Log(Logger.Section.Lookups, "Version Status: " + lookupResult.VersionStatus); |
| 257 | |
| 258 | // check if the lookup status was OK |
| 259 | if (!cancelled && lookupResult.LocStatus == LookupWorker.LookupStatus.Ok && |
| 260 | lookupResult.TxtStatus == LookupWorker.LookupStatus.Ok && |
| 261 | lookupResult.NaptrStatus == LookupWorker.LookupStatus.Ok && |
| 262 | lookupResult.NInfoStatus == LookupWorker.LookupStatus.Ok && |
| 263 | lookupResult.KeywordStatus == LookupWorker.LookupStatus.Ok && |
| 264 | lookupResult.VersionStatus == LookupWorker.LookupStatus.Ok) |
| 265 | { |
| 266 | // store result in cache |
| 267 | LookupCache.SetResult(telname, lookupResult); |
| 268 | complete = true; |
| 269 | completeListener.LookupTelnameComplete(lookupResult); |
| 270 | } |
| 271 | // if the result was NXDomain then remove the cache! |
| 272 | else if (!cancelled && (lookupResult.NaptrStatus == LookupWorker.LookupStatus.Nxdomain || |
| 273 | lookupResult.LocStatus == LookupWorker.LookupStatus.Nxdomain || |
| 274 | lookupResult.TxtStatus == LookupWorker.LookupStatus.Nxdomain || |
| 275 | lookupResult.KeywordStatus == LookupWorker.LookupStatus.Nxdomain || |
| 276 | lookupResult.VersionStatus == LookupWorker.LookupStatus.Nxdomain || |
| 277 | lookupResult.NInfoStatus == LookupWorker.LookupStatus.Nxdomain)) |
| 278 | { |
| 279 | LookupCache.RemoveResult(telname); |
| 280 | complete = true; |
| 281 | completeListener.LookupTelnameComplete(lookupResult); |
| 282 | } |
| 283 | // lookup result is not OK, return the cached result |
| 284 | else if (!cancelled && cachedResult != null) |
| 285 | { |
| 286 | // return the cached result, but set the status to the status of the actual lookup |
| 287 | // so that the user will know why they are seeing the cached results |
| 288 | complete = true; |
| 289 | cachedResult.TxtStatus = lookupResult.TxtStatus; |
| 290 | cachedResult.NaptrStatus = lookupResult.NaptrStatus; |
| 291 | cachedResult.LocStatus = lookupResult.LocStatus; |
| 292 | cachedResult.NInfoStatus = lookupResult.NInfoStatus; |
| 293 | cachedResult.KeywordStatus = lookupResult.KeywordStatus; |
| 294 | cachedResult.VersionStatus = lookupResult.VersionStatus; |
| 295 | completeListener.LookupTelnameComplete(cachedResult); |
| 296 | } |
| 297 | else if (!cancelled) |
| 298 | { |
| 299 | completeListener.LookupTelnameComplete(lookupResult); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | /// <summary> |
| 304 | /// Perform public lookup on version one |
| 305 | /// </summary> |
| 306 | /// <param name="publicTelname">the public telname</param> |
| 307 | private void DoVersionOneLookupAndCache(string publicTelname) |
| 308 | { |
| 309 | // do txt and naptr lookups in separate threads |
| 310 | performLocLookup(publicTelname); |
| 311 | performNaptrLookup(publicTelname); |
| 312 | |
| 313 | // wait for those threads to finish |
| 314 | naptrThread.Join(); |
| 315 | locThread.Join(); |
| 316 | |
| 317 | Logger.Log(Logger.Section.Lookups, "Lookup threads finished for " + telname); |
| 318 | Logger.Log(Logger.Section.Lookups, "NAPTR Status: " + lookupResult.NaptrStatus); |
| 319 | Logger.Log(Logger.Section.Lookups, "LOC Status: " + lookupResult.LocStatus); |
| 320 | Logger.Log(Logger.Section.Lookups, "Version Status: " + lookupResult.VersionStatus); |
| 321 | |
| 322 | // check if the lookup status was OK |
| 323 | if (!cancelled && lookupResult.LocStatus == LookupWorker.LookupStatus.Ok && |
| 324 | lookupResult.NaptrStatus == LookupWorker.LookupStatus.Ok && |
| 325 | lookupResult.VersionStatus == LookupWorker.LookupStatus.Ok) |
| 326 | { |
| 327 | // store result in cache |
| 328 | LookupCache.SetResult(telname, lookupResult); |
| 329 | complete = true; |
| 330 | completeListener.LookupTelnameComplete(lookupResult); |
| 331 | } |
| 332 | // if the result was NXDomain then remove the cache! |
| 333 | else if (!cancelled && (lookupResult.NaptrStatus == LookupWorker.LookupStatus.Nxdomain || |
| 334 | lookupResult.LocStatus == LookupWorker.LookupStatus.Nxdomain || |
| 335 | lookupResult.VersionStatus == LookupWorker.LookupStatus.Nxdomain)) |
| 336 | { |
| 337 | LookupCache.RemoveResult(telname); |
| 338 | complete = true; |
| 339 | completeListener.LookupTelnameComplete(lookupResult); |
| 340 | } |
| 341 | // lookup result is not OK, return the cached result |
| 342 | else if (!cancelled && cachedResult != null) |
| 343 | { |
| 344 | // return the cached result, but set the status to the status of the actual lookup |
| 345 | // so that the user will know why they are seeing the cached results |
| 346 | complete = true; |
| 347 | cachedResult.TxtStatus = lookupResult.TxtStatus; |
| 348 | cachedResult.NaptrStatus = lookupResult.NaptrStatus; |
| 349 | cachedResult.LocStatus = lookupResult.LocStatus; |
| 350 | cachedResult.VersionStatus = lookupResult.VersionStatus; |
| 351 | completeListener.LookupTelnameComplete(cachedResult); |
| 352 | } |
| 353 | else if (!cancelled) |
| 354 | { |
| 355 | completeListener.LookupTelnameComplete(lookupResult); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | /// <summary> |
| 360 | /// Performs private lookup on version 1 |
| 361 | /// </summary> |
| 362 | /// <param name="publicTelname"> the public telname</param> |
| 363 | /// <param name="telname">the private telname</param> |
| 364 | private void DoVersionOneLookupAndCachePrivate(string publicTelname, string telname) |
| 365 | { |
| 366 | // do txt and naptr lookups in separate threads |
| 367 | performTxtLookup(telname); |
| 368 | performLocLookup(publicTelname); |
| 369 | performNaptrLookup(telname); |
| 370 | |
| 371 | // wait for those threads to finish |
| 372 | naptrThread.Join(); |
| 373 | locThread.Join(); |
| 374 | txtThread.Join(); |
| 375 | |
| 376 | Logger.Log(Logger.Section.Lookups, "Lookup threads finished for " + telname); |
| 377 | Logger.Log(Logger.Section.Lookups, "NAPTR Status: " + lookupResult.NaptrStatus); |
| 378 | Logger.Log(Logger.Section.Lookups, "LOC Status: " + lookupResult.LocStatus); |
| 379 | Logger.Log(Logger.Section.Lookups, "TXT Status: " + lookupResult.TxtStatus); |
| 380 | Logger.Log(Logger.Section.Lookups, "Version Status: " + lookupResult.VersionStatus); |
| 381 | |
| 382 | // check if the lookup status was OK |
| 383 | if (!cancelled && lookupResult.LocStatus == LookupWorker.LookupStatus.Ok && |
| 384 | lookupResult.TxtStatus == LookupWorker.LookupStatus.Ok && |
| 385 | lookupResult.NaptrStatus == LookupWorker.LookupStatus.Ok && |
| 386 | lookupResult.VersionStatus == LookupWorker.LookupStatus.Ok) |
| 387 | { |
| 388 | // store result in cache |
| 389 | LookupCache.SetResult(telname, lookupResult); |
| 390 | complete = true; |
| 391 | completeListener.LookupTelnameComplete(lookupResult); |
| 392 | } |
| 393 | // if the result was NXDomain then remove the cache! |
| 394 | else if (!cancelled && (lookupResult.NaptrStatus == LookupWorker.LookupStatus.Nxdomain || |
| 395 | lookupResult.LocStatus == LookupWorker.LookupStatus.Nxdomain || |
| 396 | lookupResult.TxtStatus == LookupWorker.LookupStatus.Nxdomain || |
| 397 | lookupResult.VersionStatus == LookupWorker.LookupStatus.Nxdomain)) |
| 398 | { |
| 399 | LookupCache.RemoveResult(telname); |
| 400 | complete = true; |
| 401 | completeListener.LookupTelnameComplete(lookupResult); |
| 402 | } |
| 403 | // lookup result is not OK, return the cached result |
| 404 | else if (!cancelled && cachedResult != null) |
| 405 | { |
| 406 | // return the cached result, but set the status to the status of the actual lookup |
| 407 | // so that the user will know why they are seeing the cached results |
| 408 | complete = true; |
| 409 | cachedResult.TxtStatus = lookupResult.TxtStatus; |
| 410 | cachedResult.NaptrStatus = lookupResult.NaptrStatus; |
| 411 | cachedResult.LocStatus = lookupResult.LocStatus; |
| 412 | cachedResult.VersionStatus = lookupResult.VersionStatus; |
| 413 | completeListener.LookupTelnameComplete(cachedResult); |
| 414 | } |
| 415 | else if (!cancelled) |
| 416 | { |
| 417 | completeListener.LookupTelnameComplete(lookupResult); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | |
| 422 | |
| 423 | /// <summary> Perform naptr lookup. |
| 424 | /// |
| 425 | /// </summary> |
| 426 | /// <param name="telname">the telname |
| 427 | /// </param> |
| 428 | private void performNaptrLookup(string telnameVal) |
| 429 | { |
| 430 | if (!cancelled) |
| 431 | { |
| 432 | lwNaptr = new LookupWorker(telnameVal, DNS.DnsQueryType.Naptr, address, port, TelnicLookupType.Naptr); |
| 433 | } |
| 434 | if (!cancelled) |
| 435 | { |
| 436 | lwNaptr.LookupWorkerCompletedListener = this; |
| 437 | } |
| 438 | if (!cancelled) |
| 439 | { |
| 440 | naptrThread = new Thread(lwNaptr.Run); |
| 441 | naptrThread.Name = "NAPTR Thread"; |
| 442 | naptrThread.Start(); |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | /// <summary> Perform LOC lookup. |
| 447 | /// |
| 448 | /// </summary> |
| 449 | /// <param name="telname">the telname |
| 450 | /// </param> |
| 451 | private void performLocLookup(string telnameVal) |
| 452 | { |
| 453 | if (!cancelled) |
| 454 | { |
| 455 | lwLoc = new LookupWorker(telnameVal, DNS.DnsQueryType.Loc, address, port, TelnicLookupType.Location); |
| 456 | } |
| 457 | if (!cancelled) |
| 458 | { |
| 459 | lwLoc.LookupWorkerCompletedListener = this; |
| 460 | } |
| 461 | if (!cancelled) |
| 462 | { |
| 463 | locThread = new Thread(lwLoc.Run); |
| 464 | locThread.Name = "Loc Thread"; |
| 465 | locThread.Start(); |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | /// <summary> Perform txt lookup. |
| 470 | /// |
| 471 | /// </summary> |
| 472 | /// <param name="telname">the telname |
| 473 | /// </param> |
| 474 | private void performTxtLookup(string telnameVal) |
| 475 | { |
| 476 | if (!cancelled) |
| 477 | { |
| 478 | lwTxt = new LookupWorker(telnameVal, DNS.DnsQueryType.Txt, address, port, TelnicLookupType.LongLabelAndServiceMessage); |
| 479 | } |
| 480 | if (!cancelled) |
| 481 | { |
| 482 | lwTxt.LookupWorkerCompletedListener = this; |
| 483 | } |
| 484 | if (!cancelled) |
| 485 | { |
| 486 | txtThread = new Thread(lwTxt.Run); |
| 487 | txtThread.Name = "TXT Thread"; |
| 488 | txtThread.Start(); |
| 489 | |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | |
| 494 | /// <summary> Perform Version lookup. |
| 495 | /// |
| 496 | /// </summary> |
| 497 | /// <param name="telname">the telname |
| 498 | /// </param> |
| 499 | private void performVersionLookup(string telnameVal) |
| 500 | { |
| 501 | if (!cancelled) |
| 502 | { |
| 503 | lwVersion = new LookupWorker(telnameVal, DNS.DnsQueryType.Txt, address, port, TelnicLookupType.Version); |
| 504 | } |
| 505 | if (!cancelled) |
| 506 | { |
| 507 | lwVersion.LookupWorkerCompletedListener = this; |
| 508 | } |
| 509 | if (!cancelled) |
| 510 | { |
| 511 | versionThread = new Thread(lwVersion.Run); |
| 512 | versionThread.Name = "Version Thread"; |
| 513 | versionThread.Start(); |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | /// <summary> |
| 518 | /// Perform Ninfo Lookup |
| 519 | /// </summary> |
| 520 | /// <param name="telnameVal"> the telname</param> |
| 521 | private void performNInfoLookup(string telnameVal) |
| 522 | { |
| 523 | if (!cancelled) |
| 524 | { |
| 525 | lwNInfo = new LookupWorker(telnameVal, DNS.DnsQueryType.Ninfo, address, port, TelnicLookupType.HeaderText); |
| 526 | } |
| 527 | if (!cancelled) |
| 528 | { |
| 529 | lwNInfo.LookupWorkerCompletedListener = this; |
| 530 | } |
| 531 | if (!cancelled) |
| 532 | { |
| 533 | nInfoThread = new Thread(lwNInfo.Run); |
| 534 | nInfoThread.Name = "NInfo Thread"; |
| 535 | nInfoThread.Start(); |
| 536 | |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | /// <summary> |
| 541 | /// Perform keyword lookup |
| 542 | /// </summary> |
| 543 | /// <param name="telnameVal">the telname</param> |
| 544 | private void performKeywordLookup(string telnameVal) |
| 545 | { |
| 546 | if (!cancelled) |
| 547 | { |
| 548 | lwKeyword = new LookupWorker(telnameVal, DNS.DnsQueryType.Txt, address, port, TelnicLookupType.Keyword); |
| 549 | } |
| 550 | if (!cancelled) |
| 551 | { |
| 552 | lwKeyword.LookupWorkerCompletedListener = this; |
| 553 | } |
| 554 | if (!cancelled) |
| 555 | { |
| 556 | keywordThread = new Thread(lwKeyword.Run); |
| 557 | keywordThread.Name = "Keyword Thread"; |
| 558 | keywordThread.Start(); |
| 559 | |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | /// <summary> |
| 564 | /// Adds the results to the lookup results according to the type of the query |
| 565 | /// </summary> |
| 566 | /// <param name="statusVal">The status of the DNS lookup</param> |
| 567 | /// <param name="type">The type of lookup performed</param> |
| 568 | /// <param name="answers"></param> |
| 569 | public virtual void lookupWorkerCompleted(LookupWorker.LookupStatus statusVal, DNS.DnsQueryType type, System.Collections.ArrayList answers, TelnicLookupType telnicType) |
| 570 | { |
| 571 | if (type == DNS.DnsQueryType.Txt) |
| 572 | { |
| 573 | if (telnicType == TelnicLookupType.Keyword) |
| 574 | { |
| 575 | if (statusVal == LookupWorker.LookupStatus.Ok) |
| 576 | { |
| 577 | lookupResult.KeywordRecords = answers; |
| 578 | } |
| 579 | else |
| 580 | { |
| 581 | lookupResult.KeywordRecords = new System.Collections.ArrayList(); |
| 582 | } |
| 583 | |
| 584 | lookupResult.KeywordStatus = statusVal; |
| 585 | return; |
| 586 | } |
| 587 | else if (telnicType == TelnicLookupType.Version) |
| 588 | { |
| 589 | if (statusVal == LookupWorker.LookupStatus.Ok) |
| 590 | { |
| 591 | lookupResult.VersionRecords = answers; |
| 592 | } |
| 593 | else |
| 594 | { |
| 595 | lookupResult.VersionRecords = new System.Collections.ArrayList(); |
| 596 | } |
| 597 | |
| 598 | lookupResult.VersionStatus = statusVal; |
| 599 | return; |
| 600 | } |
| 601 | else |
| 602 | { |
| 603 | if (statusVal == LookupWorker.LookupStatus.Ok) |
| 604 | { |
| 605 | lookupResult.TxtRecords = answers; |
| 606 | } |
| 607 | else |
| 608 | { |
| 609 | lookupResult.TxtRecords = new System.Collections.ArrayList(); |
| 610 | } |
| 611 | |
| 612 | lookupResult.TxtStatus = statusVal; |
| 613 | return; |
| 614 | } |
| 615 | } |
| 616 | else if (type == DNS.DnsQueryType.Loc) |
| 617 | { |
| 618 | if (statusVal == LookupWorker.LookupStatus.Ok) |
| 619 | { |
| 620 | lookupResult.LocRecords = answers; |
| 621 | } |
| 622 | else |
| 623 | { |
| 624 | lookupResult.LocRecords = new System.Collections.ArrayList(); |
| 625 | } |
| 626 | lookupResult.LocStatus = statusVal; |
| 627 | return; |
| 628 | } |
| 629 | else if (type == DNS.DnsQueryType.Naptr) |
| 630 | { |
| 631 | if (statusVal == LookupWorker.LookupStatus.Ok) |
| 632 | { |
| 633 | lookupResult.NaptrRecords = answers; |
| 634 | } |
| 635 | else |
| 636 | { |
| 637 | lookupResult.NaptrRecords = new System.Collections.ArrayList(); |
| 638 | } |
| 639 | lookupResult.NaptrStatus = statusVal; |
| 640 | return; |
| 641 | } |
| 642 | else if (type == DNS.DnsQueryType.Ninfo) |
| 643 | { |
| 644 | if (statusVal == LookupWorker.LookupStatus.Ok) |
| 645 | { |
| 646 | lookupResult.NInfoRecords = answers; |
| 647 | } |
| 648 | else |
| 649 | { |
| 650 | lookupResult.NInfoRecords = new System.Collections.ArrayList(); |
| 651 | } |
| 652 | lookupResult.NInfoStatus = statusVal; |
| 653 | return; |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | /// <summary> Cancel the lookup. |
| 658 | /// |
| 659 | /// </summary> |
| 660 | /// <seealso cref="org.telnic.outlook.util.Cancellable.cancel()"> |
| 661 | /// </seealso> |
| 662 | public virtual void cancel() |
| 663 | { |
| 664 | cancelled = true; |
| 665 | if (lwNaptr != null) |
| 666 | { |
| 667 | //lwNaptr.Interrupt(); |
| 668 | lwNaptr = null; |
| 669 | } |
| 670 | if (lwTxt != null) |
| 671 | { |
| 672 | //lwTxt.Interrupt(); |
| 673 | lwTxt = null; |
| 674 | } |
| 675 | if (lwLoc != null) |
| 676 | { |
| 677 | lwLoc = null; |
| 678 | } |
| 679 | if (lwNInfo != null) |
| 680 | { |
| 681 | lwNInfo = null; |
| 682 | } |
| 683 | if (lwKeyword != null) |
| 684 | { |
| 685 | lwKeyword = null; |
| 686 | } |
| 687 | if (lwVersion != null) |
| 688 | { |
| 689 | lwVersion = null; |
| 690 | } |
| 691 | |
| 692 | this.status = STATUS_CANCELLED; |
| 693 | if (completeListener != null) |
| 694 | { |
| 695 | completeListener.LookupTelnameComplete(null); |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | /// <summary> |
| 700 | /// Do a public text lookup to get the pddx version to decide what further lookups to do |
| 701 | /// </summary> |
| 702 | /// <param name="telname">the public telname</param> |
| 703 | /// <returns>the pddx version eg "1" or "11"</returns> |
| 704 | private int GetVersion(string telname) |
| 705 | { |
| 706 | performVersionLookup(telname); |
| 707 | versionThread.Join(); |
| 708 | Logger.Log(Logger.Section.Lookups, "Version Status: " + lookupResult.VersionStatus); |
| 709 | if (lookupResult.VersionStatus == LookupWorker.LookupStatus.Ok) |
| 710 | { |
| 711 | Logger.Log(Logger.Section.Lookups, "PDDX Version (in getVersion) is: " + lookupResult.PddxVersion); |
| 712 | string pddx = lookupResult.PddxVersion; |
| 713 | if (pddx.Length == 1) |
| 714 | pddx = pddx + "0"; |
| 715 | try |
| 716 | { |
| 717 | return int.Parse(pddx); |
| 718 | } |
| 719 | catch (Exception e) |
| 720 | { |
| 721 | return version_change; |
| 722 | } |
| 723 | |
| 724 | } |
| 725 | return version_change; |
| 726 | } |
| 727 | } |
| 728 | } |
Note: See TracBrowser
for help on using the browser.








