ResultCode.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. package com.zerotier.sdk;
  28. /**
  29. * Function return code: OK (0) or error results
  30. *
  31. * <p>Use {@link ResultCode#isFatal) to check for a fatal error. If a fatal error
  32. * occurs, the node should be considered to not be working correctly. These
  33. * indicate serious problems like an inaccessible data store or a compile
  34. * problem.</p>
  35. *
  36. * Defined in ZeroTierOne.h as ZT_ResultCode
  37. */
  38. public enum ResultCode {
  39. /**
  40. * Operation completed normally
  41. */
  42. RESULT_OK(0),
  43. // Fatal errors (>=100, <1000)
  44. /**
  45. * Ran out of memory
  46. */
  47. RESULT_FATAL_ERROR_OUT_OF_MEMORY(100),
  48. /**
  49. * Data store is not writable or has failed
  50. */
  51. RESULT_FATAL_ERROR_DATA_STORE_FAILED(101),
  52. /**
  53. * Internal error (e.g. unexpected exception indicating bug or build problem)
  54. */
  55. RESULT_FATAL_ERROR_INTERNAL(102),
  56. // non-fatal errors
  57. /**
  58. * Network ID not valid
  59. */
  60. RESULT_ERROR_NETWORK_NOT_FOUND(1000),
  61. RESULT_ERROR_UNSUPPORTED_OPERATION(1001),
  62. RESULT_ERROR_BAD_PARAMETER(1002);
  63. private final int id;
  64. ResultCode(int id) {
  65. this.id = id;
  66. }
  67. public static ResultCode fromInt(int id) {
  68. switch (id) {
  69. case 0:
  70. return RESULT_OK;
  71. case 100:
  72. return RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  73. case 101:
  74. return RESULT_FATAL_ERROR_DATA_STORE_FAILED;
  75. case 102:
  76. return RESULT_FATAL_ERROR_INTERNAL;
  77. case 1000:
  78. return RESULT_ERROR_NETWORK_NOT_FOUND;
  79. case 1001:
  80. return RESULT_ERROR_UNSUPPORTED_OPERATION;
  81. case 1002:
  82. return RESULT_ERROR_BAD_PARAMETER;
  83. default:
  84. throw new RuntimeException("Unhandled value: " + id);
  85. }
  86. }
  87. public boolean isFatal(int id) {
  88. return (id > 100 && id < 1000);
  89. }
  90. }