Main.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import java.io.*;
  2. import java.util.*;
  3. import com.erlport.erlang.term.*;
  4. class State implements Serializable {
  5. Integer times;
  6. public State() {
  7. times = 0;
  8. }
  9. public Integer incr() {
  10. times += 1;
  11. return times;
  12. }
  13. @Override
  14. public String toString() {
  15. return String.format("State(times: %d)", times);
  16. }
  17. }
  18. public class Main {
  19. public static Object init() {
  20. System.err.printf("Initiate driver...\n");
  21. // [{"topics", ["t/#", "t/a"]}]
  22. List<Object> topics = new ArrayList<Object>();
  23. topics.add(new Binary("t/#"));
  24. topics.add(new Binary("test/#"));
  25. List<Object> actionOpts = new ArrayList<Object>();
  26. actionOpts.add(Tuple.two(new Atom("topics"), topics));
  27. Object[] actions0 = new Object[] {
  28. Tuple.three("client_connect", "Main", "on_client_connect"),
  29. Tuple.three("client_connack", "Main", "on_client_connack"),
  30. Tuple.three("client_connected", "Main", "on_client_connected"),
  31. Tuple.three("client_disconnected", "Main", "on_client_disconnected"),
  32. Tuple.three("client_authenticate", "Main", "on_client_authenticate"),
  33. Tuple.three("client_check_acl", "Main", "on_client_check_acl"),
  34. Tuple.three("client_subscribe", "Main", "on_client_subscribe"),
  35. Tuple.three("client_unsubscribe", "Main", "on_client_unsubscribe"),
  36. Tuple.three("session_created", "Main", "on_session_created"),
  37. Tuple.three("session_subscribed", "Main", "on_session_subscribed"),
  38. Tuple.three("session_unsubscribed", "Main", "on_session_unsubscribed"),
  39. Tuple.three("session_resumed", "Main", "on_session_resumed"),
  40. Tuple.three("session_discarded", "Main", "on_session_discarded"),
  41. Tuple.three("session_takeovered", "Main", "on_session_takeovered"),
  42. Tuple.three("session_terminated", "Main", "on_session_terminated"),
  43. Tuple.four("message_publish", "Main", "on_message_publish", actionOpts),
  44. Tuple.four("message_delivered", "Main", "on_message_delivered", actionOpts),
  45. Tuple.four("message_acked", "Main", "on_message_acked", actionOpts),
  46. Tuple.four("message_dropped", "Main", "on_message_dropped", actionOpts)
  47. };
  48. List<Object> actions = new ArrayList<Object>(Arrays.asList(actions0));
  49. State state = new State();
  50. //Tuple state = new Tuple(0);
  51. // {0 | 1, [{HookName, CallModule, CallFunction, Opts}]}
  52. return Tuple.two(0, Tuple.two(actions, state));
  53. }
  54. public static void deinit() {
  55. }
  56. // Callbacks
  57. public static void on_client_connect(Object connInfo, Object props, Object state) {
  58. System.err.printf("[Java] on_client_connect: connInfo: %s, props: %s, state: %s\n", connInfo, props, state);
  59. }
  60. public static void on_client_connack(Object connInfo, Object rc, Object props, Object state) {
  61. System.err.printf("[Java] on_client_connack: connInfo: %s, rc: %s, props: %s, state: %s\n", connInfo, rc, props, state);
  62. }
  63. public static void on_client_connected(Object clientInfo, Object state) {
  64. System.err.printf("[Java] on_client_connected: clientinfo: %s, state: %s\n", clientInfo, state);
  65. }
  66. public static void on_client_disconnected(Object clientInfo, Object reason, Object state) {
  67. System.err.printf("[Java] on_client_disconnected: clientinfo: %s, reason: %s, state: %s\n", clientInfo, reason, state);
  68. }
  69. public static Object on_client_authenticate(Object clientInfo, Object authresult, Object state) {
  70. System.err.printf("[Java] on_client_authenticate: clientinfo: %s, authresult: %s, state: %s\n", clientInfo, authresult, state);
  71. return Tuple.two(0, true);
  72. }
  73. public static Object on_client_check_acl(Object clientInfo, Object pubsub, Object topic, Object result, Object state) {
  74. System.err.printf("[Java] on_client_check_acl: clientinfo: %s, pubsub: %s, topic: %s, result: %s, state: %s\n", clientInfo, pubsub, topic, result, state);
  75. return Tuple.two(0, true);
  76. }
  77. public static void on_client_subscribe(Object clientInfo, Object props, Object topic, Object state) {
  78. System.err.printf("[Java] on_client_subscribe: clientinfo: %s, props: %s, topic: %s, state: %s\n", clientInfo, props, topic, state);
  79. }
  80. public static void on_client_unsubscribe(Object clientInfo, Object props, Object topic, Object state) {
  81. System.err.printf("[Java] on_client_unsubscribe: clientinfo: %s, props: %s, topic: %s, state: %s\n", clientInfo, props, topic, state);
  82. }
  83. // Sessions
  84. public static void on_session_created(Object clientInfo, Object state) {
  85. System.err.printf("[Java] on_session_created: clientinfo: %s, state: %s\n", clientInfo, state);
  86. }
  87. public static void on_session_subscribed(Object clientInfo, Object topic, Object opts, Object state) {
  88. System.err.printf("[Java] on_session_subscribed: clientinfo: %s, topic: %s, subopts: %s, state: %s\n", clientInfo, topic, opts, state);
  89. }
  90. public static void on_session_unsubscribed(Object clientInfo, Object topic, Object state) {
  91. System.err.printf("[Java] on_session_unsubscribed: clientinfo: %s, topic: %s, state: %s\n", clientInfo, topic, state);
  92. }
  93. public static void on_session_resumed(Object clientInfo, Object state) {
  94. System.err.printf("[Java] on_session_resumed: clientinfo: %s, state: %s\n", clientInfo, state);
  95. }
  96. public static void on_session_discarded(Object clientInfo, Object state) {
  97. System.err.printf("[Java] on_session_discarded: clientinfo: %s, state: %s\n", clientInfo, state);
  98. }
  99. public static void on_session_takeovered(Object clientInfo, Object state) {
  100. System.err.printf("[Java] on_session_takeovered: clientinfo: %s, state: %s\n", clientInfo, state);
  101. }
  102. public static void on_session_terminated(Object clientInfo, Object reason, Object state) {
  103. System.err.printf("[Java] on_session_terminated: clientinfo: %s, reason: %s, state: %s\n", clientInfo, reason, state);
  104. }
  105. // Messages
  106. public static Object on_message_publish(Object message, Object state) {
  107. System.err.printf("[Java] on_message_publish: message: %s, state: %s\n", message, state);
  108. return Tuple.two(0, message);
  109. }
  110. public static void on_message_dropped(Object message, Object reason, Object state) {
  111. System.err.printf("[Java] on_message_dropped: message: %s, reason: %s, state: %s\n", message, reason, state);
  112. }
  113. public static void on_message_delivered(Object clientInfo, Object message, Object state) {
  114. System.err.printf("[Java] on_message_delivered: clientinfo: %s, message: %s, state: %s\n", clientInfo, message, state);
  115. }
  116. public static void on_message_acked(Object clientInfo, Object message, Object state) {
  117. System.err.printf("[Java] on_message_acked: clientinfo: %s, message: %s, state: %s\n", clientInfo, message, state);
  118. }
  119. }