|
| 1 | +/* |
| 2 | + This is free and unencumbered software released into the public domain. |
| 3 | +
|
| 4 | + Anyone is free to copy, modify, publish, use, compile, sell, or |
| 5 | + distribute this software, either in source code form or as a compiled |
| 6 | + binary, for any purpose, commercial or non-commercial, and by any |
| 7 | + means. |
| 8 | +
|
| 9 | + In jurisdictions that recognize copyright laws, the author or authors |
| 10 | + of this software dedicate any and all copyright interest in the |
| 11 | + software to the public domain. We make this dedication for the benefit |
| 12 | + of the public at large and to the detriment of our heirs and |
| 13 | + successors. We intend this dedication to be an overt act of |
| 14 | + relinquishment in perpetuity of all present and future rights to this |
| 15 | + software under copyright law. |
| 16 | +
|
| 17 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 | + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 | + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 20 | + IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 21 | + OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 22 | + ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 23 | + OTHER DEALINGS IN THE SOFTWARE. |
| 24 | +
|
| 25 | + For more information, please refer to <http://unlicense.org/> |
| 26 | +*/ |
| 27 | + |
| 28 | +#include <cassandra.h> |
| 29 | + |
| 30 | + |
| 31 | +#include <stdio.h> |
| 32 | +#include <uv.h> |
| 33 | + |
| 34 | +void on_signal(uv_signal_t* handle, int signum) { |
| 35 | + uv_signal_stop(handle); |
| 36 | +} |
| 37 | + |
| 38 | +void on_host_listener(CassHostListenerEvent event, CassInet inet, void* data); |
| 39 | + |
| 40 | +void print_error(CassFuture* future) { |
| 41 | + const char* message; |
| 42 | + size_t message_length; |
| 43 | + cass_future_error_message(future, &message, &message_length); |
| 44 | + fprintf(stderr, "Error: %.*s\n", (int)message_length, message); |
| 45 | +} |
| 46 | + |
| 47 | +CassCluster* create_cluster(const char* hosts) { |
| 48 | + CassCluster* cluster = cass_cluster_new(); |
| 49 | + cass_log_set_level(CASS_LOG_DISABLED); |
| 50 | + cass_cluster_set_contact_points(cluster, hosts); |
| 51 | + cass_cluster_set_host_listener_callback(cluster, on_host_listener, NULL); |
| 52 | + return cluster; |
| 53 | +} |
| 54 | + |
| 55 | +CassError connect_session(CassSession* session, const CassCluster* cluster) { |
| 56 | + CassError rc = CASS_OK; |
| 57 | + CassFuture* future = cass_session_connect(session, cluster); |
| 58 | + |
| 59 | + cass_future_wait(future); |
| 60 | + rc = cass_future_error_code(future); |
| 61 | + if (rc != CASS_OK) { |
| 62 | + print_error(future); |
| 63 | + } |
| 64 | + cass_future_free(future); |
| 65 | + |
| 66 | + return rc; |
| 67 | +} |
| 68 | + |
| 69 | +void on_host_listener(CassHostListenerEvent event, CassInet inet, void* data) { |
| 70 | + char address[CASS_INET_STRING_LENGTH]; |
| 71 | + |
| 72 | + cass_inet_string(inet, address); |
| 73 | + if (event == CASS_HOST_LISTENER_EVENT_ADD) { |
| 74 | + printf("Host %s has been ADDED\n", address); |
| 75 | + } else if (event == CASS_HOST_LISTENER_EVENT_REMOVE) { |
| 76 | + printf("Host %s has been REMOVED\n", address); |
| 77 | + } else if (event == CASS_HOST_LISTENER_EVENT_UP) { |
| 78 | + printf("Host %s is UP\n", address); |
| 79 | + } else if (event == CASS_HOST_LISTENER_EVENT_DOWN) { |
| 80 | + printf("Host %s is DOWN\n", address); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +int main(int argc, char* argv[]) { |
| 85 | + CassCluster* cluster = NULL; |
| 86 | + CassSession* session = cass_session_new(); |
| 87 | + char* hosts = "127.0.0.1"; |
| 88 | + uv_loop_t loop; |
| 89 | + uv_signal_t signal; |
| 90 | + if (argc > 1) { |
| 91 | + hosts = argv[1]; |
| 92 | + } |
| 93 | + |
| 94 | + cluster = create_cluster(hosts); |
| 95 | + |
| 96 | + if (connect_session(session, cluster) != CASS_OK) { |
| 97 | + cass_cluster_free(cluster); |
| 98 | + cass_session_free(session); |
| 99 | + return -1; |
| 100 | + } |
| 101 | + |
| 102 | + uv_loop_init(&loop); |
| 103 | + uv_signal_init(&loop, &signal); |
| 104 | + uv_signal_start(&signal, on_signal, SIGINT); |
| 105 | + fprintf(stderr, "Press CTRL+C to exit ...\n"); |
| 106 | + uv_run(&loop, UV_RUN_DEFAULT); |
| 107 | + uv_loop_close(&loop); |
| 108 | + |
| 109 | + cass_cluster_free(cluster); |
| 110 | + cass_session_free(session); |
| 111 | + |
| 112 | + return 0; |
| 113 | +} |
0 commit comments