/Ice-3.4.2/java/src/IceInternal/UdpEndpointI.java

# · Java · 796 lines · 632 code · 78 blank · 86 comment · 136 complexity · 27a2cd8433739aca3edc670db4b7e28a MD5 · raw file

  1. // **********************************************************************
  2. //
  3. // Copyright (c) 2003-2011 ZeroC, Inc. All rights reserved.
  4. //
  5. // This copy of Ice is licensed to you under the terms described in the
  6. // ICE_LICENSE file included in this distribution.
  7. //
  8. // **********************************************************************
  9. package IceInternal;
  10. final class UdpEndpointI extends EndpointI
  11. {
  12. public
  13. UdpEndpointI(Instance instance, String ho, int po, String mif, int mttl, byte pma, byte pmi, byte ema, byte emi,
  14. boolean conn, String conId, boolean co)
  15. {
  16. _instance = instance;
  17. _host = ho;
  18. _port = po;
  19. _mcastInterface = mif;
  20. _mcastTtl = mttl;
  21. _protocolMajor = pma;
  22. _protocolMinor = pmi;
  23. _encodingMajor = ema;
  24. _encodingMinor = emi;
  25. _connect = conn;
  26. _connectionId = conId;
  27. _compress = co;
  28. calcHashValue();
  29. }
  30. public
  31. UdpEndpointI(Instance instance, String str, boolean oaEndpoint)
  32. {
  33. _instance = instance;
  34. _host = null;
  35. _port = 0;
  36. _protocolMajor = Protocol.protocolMajor;
  37. _protocolMinor = Protocol.protocolMinor;
  38. _encodingMajor = Protocol.encodingMajor;
  39. _encodingMinor = Protocol.encodingMinor;
  40. _connect = false;
  41. _compress = false;
  42. String[] arr = str.split("[ \t\n\r]+");
  43. int i = 0;
  44. while(i < arr.length)
  45. {
  46. if(arr[i].length() == 0)
  47. {
  48. i++;
  49. continue;
  50. }
  51. String option = arr[i++];
  52. if(option.charAt(0) != '-')
  53. {
  54. throw new Ice.EndpointParseException("expected an endpoint option but found `" + option +
  55. "' in endpoint `udp " + str + "'");
  56. }
  57. String argument = null;
  58. if(i < arr.length && arr[i].charAt(0) != '-')
  59. {
  60. argument = arr[i++];
  61. if(argument.charAt(0) == '\"' && argument.charAt(argument.length() - 1) == '\"')
  62. {
  63. argument = argument.substring(1, argument.length() - 1);
  64. }
  65. }
  66. if(option.equals("-v"))
  67. {
  68. if(argument == null)
  69. {
  70. throw new Ice.EndpointParseException("no argument provided for -v option in endpoint `udp "
  71. + str + "'");
  72. }
  73. int pos = argument.indexOf('.');
  74. if(pos == -1)
  75. {
  76. throw new Ice.EndpointParseException("malformed protocol version `" + argument +
  77. "' in endpoint `udp " + str + "'");
  78. }
  79. String majStr = argument.substring(0, pos);
  80. String minStr = argument.substring(pos + 1, argument.length());
  81. int majVersion;
  82. int minVersion;
  83. try
  84. {
  85. majVersion = Integer.parseInt(majStr);
  86. minVersion = Integer.parseInt(minStr);
  87. }
  88. catch(NumberFormatException ex)
  89. {
  90. throw new Ice.EndpointParseException("invalid protocol version `" + argument +
  91. "' in endpoint `udp " + str + "'");
  92. }
  93. if(majVersion < 1 || majVersion > 255 || minVersion < 0 || minVersion > 255)
  94. {
  95. throw new Ice.EndpointParseException("range error in protocol version `" + argument +
  96. "' in endpoint `udp " + str + "'");
  97. }
  98. if(majVersion != Protocol.protocolMajor)
  99. {
  100. Ice.UnsupportedProtocolException e = new Ice.UnsupportedProtocolException();
  101. e.badMajor = majVersion < 0 ? majVersion + 255 : majVersion;
  102. e.badMinor = minVersion < 0 ? minVersion + 255 : minVersion;
  103. e.major = Protocol.protocolMajor;
  104. e.minor = Protocol.protocolMinor;
  105. throw e;
  106. }
  107. _protocolMajor = (byte)majVersion;
  108. _protocolMinor = (byte)minVersion;
  109. }
  110. else if(option.equals("-e"))
  111. {
  112. if(argument == null)
  113. {
  114. throw new Ice.EndpointParseException("no argument provided for -e option in endpoint `udp "
  115. + str + "'");
  116. }
  117. int pos = argument.indexOf('.');
  118. if(pos == -1)
  119. {
  120. throw new Ice.EndpointParseException("malformed encoding version `" + argument +
  121. "' in endpoint `udp " + str + "'");
  122. }
  123. String majStr = argument.substring(0, pos);
  124. String minStr = argument.substring(pos + 1, argument.length());
  125. int majVersion;
  126. int minVersion;
  127. try
  128. {
  129. majVersion = Integer.parseInt(majStr);
  130. minVersion = Integer.parseInt(minStr);
  131. }
  132. catch(NumberFormatException ex)
  133. {
  134. throw new Ice.EndpointParseException("invalid encoding version `" + argument +
  135. "' in endpoint `udp " + str + "'");
  136. }
  137. if(majVersion < 1 || majVersion > 255 || minVersion < 0 || minVersion > 255)
  138. {
  139. throw new Ice.EndpointParseException("range error in encoding version `" + argument +
  140. "' in endpoint `udp " + str + "'");
  141. }
  142. if(majVersion != Protocol.encodingMajor)
  143. {
  144. Ice.UnsupportedEncodingException e = new Ice.UnsupportedEncodingException();
  145. e.badMajor = majVersion < 0 ? majVersion + 255 : majVersion;
  146. e.badMinor = minVersion < 0 ? minVersion + 255 : minVersion;
  147. e.major = Protocol.encodingMajor;
  148. e.minor = Protocol.encodingMinor;
  149. throw e;
  150. }
  151. _encodingMajor = (byte)majVersion;
  152. _encodingMinor = (byte)minVersion;
  153. }
  154. else if(option.equals("-h"))
  155. {
  156. if(argument == null)
  157. {
  158. throw new Ice.EndpointParseException("no argument provided for -h option in endpoint `udp "
  159. + str + "'");
  160. }
  161. _host = argument;
  162. }
  163. else if(option.equals("-p"))
  164. {
  165. if(argument == null)
  166. {
  167. throw new Ice.EndpointParseException("no argument provided for -p option in endpoint `udp "
  168. + str + "'");
  169. }
  170. try
  171. {
  172. _port = Integer.parseInt(argument);
  173. }
  174. catch(NumberFormatException ex)
  175. {
  176. throw new Ice.EndpointParseException("invalid port value `" + argument + "' in endpoint `udp " +
  177. str + "'");
  178. }
  179. if(_port < 0 || _port > 65535)
  180. {
  181. throw new Ice.EndpointParseException("port value `" + argument +
  182. "' out of range in endpoint `udp " + str + "'");
  183. }
  184. }
  185. else if(option.equals("-c"))
  186. {
  187. if(argument != null)
  188. {
  189. throw new Ice.EndpointParseException("unexpected argument `" + argument +
  190. "' provided for -c option in `udp " + str + "'");
  191. }
  192. _connect = true;
  193. break;
  194. }
  195. else if(option.equals("-z"))
  196. {
  197. if(argument != null)
  198. {
  199. throw new Ice.EndpointParseException("unexpected argument `" + argument +
  200. "' provided for -z option in `udp " + str + "'");
  201. }
  202. _compress = true;
  203. break;
  204. }
  205. else if(option.equals("--interface"))
  206. {
  207. if(argument == null)
  208. {
  209. throw new Ice.EndpointParseException("no argument provided for --interface option in endpoint `udp "
  210. + str + "'");
  211. }
  212. _mcastInterface = argument;
  213. }
  214. else if(option.equals("--ttl"))
  215. {
  216. if(argument == null)
  217. {
  218. throw new Ice.EndpointParseException("no argument provided for --ttl option in endpoint `udp "
  219. + str + "'");
  220. }
  221. try
  222. {
  223. _mcastTtl = Integer.parseInt(argument);
  224. }
  225. catch(NumberFormatException ex)
  226. {
  227. throw new Ice.EndpointParseException("invalid TTL value `" + argument + "' in endpoint `udp " +
  228. str + "'");
  229. }
  230. if(_mcastTtl < 0)
  231. {
  232. throw new Ice.EndpointParseException("TTL value `" + argument +
  233. "' out of range in endpoint `udp " + str + "'");
  234. }
  235. }
  236. else
  237. {
  238. throw new Ice.EndpointParseException("unknown option `" + option + "' in `udp " + str + "'");
  239. }
  240. }
  241. if(_host == null)
  242. {
  243. _host = _instance.defaultsAndOverrides().defaultHost;
  244. }
  245. else if(_host.equals("*"))
  246. {
  247. if(oaEndpoint)
  248. {
  249. _host = null;
  250. }
  251. else
  252. {
  253. throw new Ice.EndpointParseException("`-h *' not valid for proxy endpoint `udp " + str + "'");
  254. }
  255. }
  256. if(_host == null)
  257. {
  258. _host = "";
  259. }
  260. calcHashValue();
  261. }
  262. public
  263. UdpEndpointI(BasicStream s)
  264. {
  265. _instance = s.instance();
  266. s.startReadEncaps();
  267. _host = s.readString();
  268. _port = s.readInt();
  269. _protocolMajor = s.readByte();
  270. _protocolMinor = s.readByte();
  271. _encodingMajor = s.readByte();
  272. _encodingMinor = s.readByte();
  273. if(_protocolMajor != Protocol.protocolMajor)
  274. {
  275. Ice.UnsupportedProtocolException e = new Ice.UnsupportedProtocolException();
  276. e.badMajor = _protocolMajor < 0 ? _protocolMajor + 255 : _protocolMajor;
  277. e.badMinor = _protocolMinor < 0 ? _protocolMinor + 255 : _protocolMinor;
  278. e.major = Protocol.protocolMajor;
  279. e.minor = Protocol.protocolMinor;
  280. throw e;
  281. }
  282. if(_encodingMajor != Protocol.encodingMajor)
  283. {
  284. Ice.UnsupportedEncodingException e = new Ice.UnsupportedEncodingException();
  285. e.badMajor = _encodingMajor < 0 ? _encodingMajor + 255 : _encodingMajor;
  286. e.badMinor = _encodingMinor < 0 ? _encodingMinor + 255 : _encodingMinor;
  287. e.major = Protocol.encodingMajor;
  288. e.minor = Protocol.encodingMinor;
  289. throw e;
  290. }
  291. // Not transmitted.
  292. //_connect = s.readBool();
  293. _connect = false;
  294. _compress = s.readBool();
  295. s.endReadEncaps();
  296. calcHashValue();
  297. }
  298. //
  299. // Marshal the endpoint
  300. //
  301. public void
  302. streamWrite(BasicStream s)
  303. {
  304. s.writeShort(Ice.UDPEndpointType.value);
  305. s.startWriteEncaps();
  306. s.writeString(_host);
  307. s.writeInt(_port);
  308. s.writeByte(_protocolMajor);
  309. s.writeByte(_protocolMinor);
  310. s.writeByte(_encodingMajor);
  311. s.writeByte(_encodingMinor);
  312. // Not transmitted.
  313. //s.writeBool(_connect);
  314. s.writeBool(_compress);
  315. s.endWriteEncaps();
  316. }
  317. //
  318. // Convert the endpoint to its string form
  319. //
  320. public String
  321. _toString()
  322. {
  323. //
  324. // WARNING: Certain features, such as proxy validation in Glacier2,
  325. // depend on the format of proxy strings. Changes to toString() and
  326. // methods called to generate parts of the reference string could break
  327. // these features. Please review for all features that depend on the
  328. // format of proxyToString() before changing this and related code.
  329. //
  330. String s = "udp";
  331. if((int)_protocolMajor != 1 || (int)_protocolMinor != 0)
  332. {
  333. s += " -v " + (_protocolMajor < 0 ? (int)_protocolMajor + 255 : _protocolMajor)
  334. + "." + (_protocolMinor < 0 ? (int)_protocolMinor + 255 : _protocolMinor);
  335. }
  336. if((int)_encodingMajor != 1 || (int)_encodingMinor != 0)
  337. {
  338. s += " -e " + (_encodingMajor < 0 ? (int)_encodingMajor + 255 : _encodingMajor)
  339. + "." + (_encodingMinor < 0 ? (int)_encodingMinor + 255 : _encodingMinor);
  340. }
  341. if(_host != null && _host.length() > 0)
  342. {
  343. s += " -h ";
  344. boolean addQuote = _host.indexOf(':') != -1;
  345. if(addQuote)
  346. {
  347. s += "\"";
  348. }
  349. s += _host;
  350. if(addQuote)
  351. {
  352. s += "\"";
  353. }
  354. }
  355. s += " -p " + _port;
  356. if(_mcastInterface.length() != 0)
  357. {
  358. s += " --interface " + _mcastInterface;
  359. }
  360. if(_mcastTtl != -1)
  361. {
  362. s += " --ttl " + _mcastTtl;
  363. }
  364. if(_connect)
  365. {
  366. s += " -c";
  367. }
  368. if(_compress)
  369. {
  370. s += " -z";
  371. }
  372. return s;
  373. }
  374. //
  375. // Return the endpoint information.
  376. //
  377. public Ice.EndpointInfo
  378. getInfo()
  379. {
  380. return new Ice.UDPEndpointInfo(-1, _compress, _host, _port, _protocolMajor, _protocolMinor, _encodingMajor,
  381. _encodingMinor, _mcastInterface, _mcastTtl)
  382. {
  383. public short type()
  384. {
  385. return Ice.UDPEndpointType.value;
  386. }
  387. public boolean datagram()
  388. {
  389. return true;
  390. }
  391. public boolean secure()
  392. {
  393. return false;
  394. }
  395. };
  396. }
  397. //
  398. // Return the endpoint type
  399. //
  400. public short
  401. type()
  402. {
  403. return Ice.UDPEndpointType.value;
  404. }
  405. //
  406. // Return the timeout for the endpoint in milliseconds. 0 means
  407. // non-blocking, -1 means no timeout.
  408. //
  409. public int
  410. timeout()
  411. {
  412. return -1;
  413. }
  414. //
  415. // Return true if the endpoints support bzip2 compress, or false
  416. // otherwise.
  417. //
  418. public boolean
  419. compress()
  420. {
  421. return _compress;
  422. }
  423. //
  424. // Return a new endpoint with a different compression value,
  425. // provided that compression is supported by the
  426. // endpoint. Otherwise the same endpoint is returned.
  427. //
  428. public EndpointI
  429. compress(boolean compress)
  430. {
  431. if(compress == _compress)
  432. {
  433. return this;
  434. }
  435. else
  436. {
  437. return new UdpEndpointI(_instance, _host, _port, _mcastInterface, _mcastTtl, _protocolMajor,
  438. _protocolMinor, _encodingMajor, _encodingMinor, _connect, _connectionId,
  439. compress);
  440. }
  441. }
  442. //
  443. // Return a new endpoint with a different connection id.
  444. //
  445. public EndpointI
  446. connectionId(String connectionId)
  447. {
  448. if(connectionId.equals(_connectionId))
  449. {
  450. return this;
  451. }
  452. else
  453. {
  454. return new UdpEndpointI(_instance, _host, _port, _mcastInterface, _mcastTtl, _protocolMajor,
  455. _protocolMinor, _encodingMajor, _encodingMinor, _connect, connectionId,
  456. _compress);
  457. }
  458. }
  459. //
  460. // Return a new endpoint with a different timeout value, provided
  461. // that timeouts are supported by the endpoint. Otherwise the same
  462. // endpoint is returned.
  463. //
  464. public EndpointI
  465. timeout(int timeout)
  466. {
  467. return this;
  468. }
  469. //
  470. // Return true if the endpoint is datagram-based.
  471. //
  472. public boolean
  473. datagram()
  474. {
  475. return true;
  476. }
  477. //
  478. // Return true if the endpoint is secure.
  479. //
  480. public boolean
  481. secure()
  482. {
  483. return false;
  484. }
  485. //
  486. // Return a server side transceiver for this endpoint, or null if a
  487. // transceiver can only be created by an acceptor. In case a
  488. // transceiver is created, this operation also returns a new
  489. // "effective" endpoint, which might differ from this endpoint,
  490. // for example, if a dynamic port number is assigned.
  491. //
  492. public Transceiver
  493. transceiver(EndpointIHolder endpoint)
  494. {
  495. UdpTransceiver p = new UdpTransceiver(_instance, _host, _port, _mcastInterface, _connect);
  496. endpoint.value = new UdpEndpointI(_instance, _host, p.effectivePort(), _mcastInterface, _mcastTtl,
  497. _protocolMajor, _protocolMinor, _encodingMajor, _encodingMinor, _connect,
  498. _connectionId, _compress);
  499. return p;
  500. }
  501. //
  502. // Return connectors for this endpoint, or empty list if no connector
  503. // is available.
  504. //
  505. public java.util.List<Connector>
  506. connectors()
  507. {
  508. return connectors(Network.getAddresses(_host, _port, _instance.protocolSupport()));
  509. }
  510. public void
  511. connectors_async(EndpointI_connectors callback)
  512. {
  513. _instance.endpointHostResolver().resolve(_host, _port, this, callback);
  514. }
  515. //
  516. // Return an acceptor for this endpoint, or null if no acceptors
  517. // is available. In case an acceptor is created, this operation
  518. // also returns a new "effective" endpoint, which might differ
  519. // from this endpoint, for example, if a dynamic port number is
  520. // assigned.
  521. //
  522. public Acceptor
  523. acceptor(EndpointIHolder endpoint, String adapterName)
  524. {
  525. endpoint.value = this;
  526. return null;
  527. }
  528. //
  529. // Expand endpoint out in to separate endpoints for each local
  530. // host if listening on INADDR_ANY.
  531. //
  532. public java.util.List<EndpointI>
  533. expand()
  534. {
  535. java.util.ArrayList<EndpointI> endps = new java.util.ArrayList<EndpointI>();
  536. java.util.ArrayList<String> hosts =
  537. Network.getHostsForEndpointExpand(_host, _instance.protocolSupport(), false);
  538. if(hosts == null || hosts.isEmpty())
  539. {
  540. endps.add(this);
  541. }
  542. else
  543. {
  544. for(String host : hosts)
  545. {
  546. endps.add(new UdpEndpointI(_instance, host, _port, _mcastInterface, _mcastTtl,
  547. _protocolMajor, _protocolMinor, _encodingMajor, _encodingMinor,
  548. _connect, _connectionId, _compress));
  549. }
  550. }
  551. return endps;
  552. }
  553. //
  554. // Check whether the endpoint is equivalent to another one.
  555. //
  556. public boolean
  557. equivalent(EndpointI endpoint)
  558. {
  559. UdpEndpointI udpEndpointI = null;
  560. try
  561. {
  562. udpEndpointI = (UdpEndpointI)endpoint;
  563. }
  564. catch(ClassCastException ex)
  565. {
  566. return false;
  567. }
  568. return udpEndpointI._host.equals(_host) && udpEndpointI._port == _port;
  569. }
  570. public int
  571. hashCode()
  572. {
  573. return _hashCode;
  574. }
  575. //
  576. // Compare endpoints for sorting purposes
  577. //
  578. public boolean
  579. equals(java.lang.Object obj)
  580. {
  581. try
  582. {
  583. return compareTo((EndpointI)obj) == 0;
  584. }
  585. catch(ClassCastException ee)
  586. {
  587. assert(false);
  588. return false;
  589. }
  590. }
  591. public int
  592. compareTo(EndpointI obj) // From java.lang.Comparable
  593. {
  594. UdpEndpointI p = null;
  595. try
  596. {
  597. p = (UdpEndpointI)obj;
  598. }
  599. catch(ClassCastException ex)
  600. {
  601. return type() < obj.type() ? -1 : 1;
  602. }
  603. if(this == p)
  604. {
  605. return 0;
  606. }
  607. if(_port < p._port)
  608. {
  609. return -1;
  610. }
  611. else if(p._port < _port)
  612. {
  613. return 1;
  614. }
  615. if(!_connect && p._connect)
  616. {
  617. return -1;
  618. }
  619. else if(!p._connect && _connect)
  620. {
  621. return 1;
  622. }
  623. if(!_connectionId.equals(p._connectionId))
  624. {
  625. return _connectionId.compareTo(p._connectionId);
  626. }
  627. if(!_compress && p._compress)
  628. {
  629. return -1;
  630. }
  631. else if(!p._compress && _compress)
  632. {
  633. return 1;
  634. }
  635. if(_protocolMajor < p._protocolMajor)
  636. {
  637. return -1;
  638. }
  639. else if(p._protocolMajor < _protocolMajor)
  640. {
  641. return 1;
  642. }
  643. if(_protocolMinor < p._protocolMinor)
  644. {
  645. return -1;
  646. }
  647. else if(p._protocolMinor < _protocolMinor)
  648. {
  649. return 1;
  650. }
  651. if(_encodingMajor < p._encodingMajor)
  652. {
  653. return -1;
  654. }
  655. else if(p._encodingMajor < _encodingMajor)
  656. {
  657. return 1;
  658. }
  659. if(_encodingMinor < p._encodingMinor)
  660. {
  661. return -1;
  662. }
  663. else if(p._encodingMinor < _encodingMinor)
  664. {
  665. return 1;
  666. }
  667. if(_mcastTtl < p._mcastTtl)
  668. {
  669. return -1;
  670. }
  671. else if(p._mcastTtl < _mcastTtl)
  672. {
  673. return 1;
  674. }
  675. int rc = _mcastInterface.compareTo(p._mcastInterface);
  676. if(rc != 0)
  677. {
  678. return rc;
  679. }
  680. return _host.compareTo(p._host);
  681. }
  682. public java.util.List<Connector>
  683. connectors(java.util.List<java.net.InetSocketAddress> addresses)
  684. {
  685. java.util.ArrayList<Connector> connectors = new java.util.ArrayList<Connector>();
  686. for(java.net.InetSocketAddress p : addresses)
  687. {
  688. connectors.add(
  689. new UdpConnector(_instance, p, _mcastInterface, _mcastTtl, _protocolMajor, _protocolMinor,
  690. _encodingMajor, _encodingMinor, _connectionId));
  691. }
  692. return connectors;
  693. }
  694. private void
  695. calcHashValue()
  696. {
  697. _hashCode = _host.hashCode();
  698. _hashCode = 5 * _hashCode + _port;
  699. _hashCode = 5 * _hashCode + _mcastInterface.hashCode();
  700. _hashCode = 5 * _hashCode + _mcastTtl;
  701. _hashCode = 5 * _hashCode + (_connect ? 1 : 0);
  702. _hashCode = 5 * _hashCode + _connectionId.hashCode();
  703. _hashCode = 5 * _hashCode + (_compress ? 1 : 0);
  704. }
  705. private Instance _instance;
  706. private String _host;
  707. private int _port;
  708. private String _mcastInterface = "";
  709. private int _mcastTtl = -1;
  710. private byte _protocolMajor;
  711. private byte _protocolMinor;
  712. private byte _encodingMajor;
  713. private byte _encodingMinor;
  714. private boolean _connect;
  715. private String _connectionId = "";
  716. private boolean _compress;
  717. private int _hashCode;
  718. }