2016-09-23 17:10:15 -07:00
|
|
|
#ifndef REDIS_H
|
|
|
|
#define REDIS_H
|
|
|
|
|
2016-09-13 18:54:26 -07:00
|
|
|
#include "db.h"
|
|
|
|
#include "object_table.h"
|
2016-09-29 21:12:06 -07:00
|
|
|
#include "task_log.h"
|
2016-09-13 18:54:26 -07:00
|
|
|
|
|
|
|
#include "hiredis/hiredis.h"
|
|
|
|
#include "hiredis/async.h"
|
2016-09-20 17:02:56 -07:00
|
|
|
#include "uthash.h"
|
2016-09-29 21:12:06 -07:00
|
|
|
#include "utarray.h"
|
2016-09-20 17:02:56 -07:00
|
|
|
|
|
|
|
typedef struct {
|
2016-10-29 15:22:33 -07:00
|
|
|
/** Unique ID for this service. */
|
2016-09-20 17:02:56 -07:00
|
|
|
int service_id;
|
2016-10-29 15:22:33 -07:00
|
|
|
/** IP address and port of this service. */
|
2016-09-23 22:53:58 -07:00
|
|
|
char *addr;
|
2016-10-29 15:22:33 -07:00
|
|
|
/** Handle for the uthash table. */
|
2016-09-20 17:02:56 -07:00
|
|
|
UT_hash_handle hh;
|
|
|
|
} service_cache_entry;
|
2016-09-13 18:54:26 -07:00
|
|
|
|
2016-10-29 15:22:33 -07:00
|
|
|
struct db_handle {
|
|
|
|
/** String that identifies this client type. */
|
2016-09-13 18:54:26 -07:00
|
|
|
char *client_type;
|
2016-10-29 15:22:33 -07:00
|
|
|
/** Unique ID for this client within the type. */
|
2016-09-13 18:54:26 -07:00
|
|
|
int64_t client_id;
|
2016-10-29 15:22:33 -07:00
|
|
|
/** Redis context for this global state store connection. */
|
2016-09-13 18:54:26 -07:00
|
|
|
redisAsyncContext *context;
|
2016-10-29 15:22:33 -07:00
|
|
|
/** Redis context for "subscribe" communication.
|
2016-09-29 21:12:06 -07:00
|
|
|
* Yes, we need a separate one for that, see
|
|
|
|
* https://github.com/redis/hiredis/issues/55 */
|
|
|
|
redisAsyncContext *sub_context;
|
2016-10-29 15:22:33 -07:00
|
|
|
/** The event loop this global state store connection is part of. */
|
2016-09-13 18:54:26 -07:00
|
|
|
event_loop *loop;
|
2016-10-29 15:22:33 -07:00
|
|
|
/** Index of the database connection in the event loop */
|
2016-09-20 22:40:35 -07:00
|
|
|
int64_t db_index;
|
2016-10-29 15:22:33 -07:00
|
|
|
/** Cache for the IP addresses of services. */
|
2016-09-20 17:02:56 -07:00
|
|
|
service_cache_entry *service_cache;
|
2016-10-29 15:22:33 -07:00
|
|
|
/** Redis context for synchronous connections.
|
2016-09-20 17:02:56 -07:00
|
|
|
* Should only be used very rarely, it is not asynchronous. */
|
|
|
|
redisContext *sync_context;
|
2016-10-29 15:22:33 -07:00
|
|
|
/** Data structure for callbacks that needs to be freed. */
|
2016-09-29 21:12:06 -07:00
|
|
|
UT_array *callback_freelist;
|
2016-09-13 18:54:26 -07:00
|
|
|
};
|
|
|
|
|
2016-10-29 15:22:33 -07:00
|
|
|
void redis_object_table_get_entry(redisAsyncContext *c,
|
|
|
|
void *r,
|
|
|
|
void *privdata);
|
2016-09-13 18:54:26 -07:00
|
|
|
|
|
|
|
void object_table_lookup_callback(redisAsyncContext *c,
|
|
|
|
void *r,
|
|
|
|
void *privdata);
|
2016-09-20 22:40:35 -07:00
|
|
|
|
2016-10-29 15:22:33 -07:00
|
|
|
/*
|
|
|
|
* ==== Redis object table functions ====
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Lookup object table entry in redis.
|
|
|
|
*
|
|
|
|
* @param callback_data Data structure containing redis connection and timeout
|
|
|
|
* information.
|
|
|
|
* @return Void.
|
|
|
|
*/
|
|
|
|
void redis_object_table_lookup(table_callback_data *callback_data);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an entry to the object table in redis.
|
|
|
|
*
|
|
|
|
* @param callback_data Data structure containing redis connection and timeout
|
|
|
|
* information.
|
|
|
|
* @return Void.
|
|
|
|
*/
|
|
|
|
void redis_object_table_add(table_callback_data *callback_data);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Subscribe to learn when a new object becomes available.
|
|
|
|
*
|
|
|
|
* @param callback_data Data structure containing redis connection and timeout
|
|
|
|
* information.
|
|
|
|
* @return Void.
|
|
|
|
*/
|
|
|
|
void redis_object_table_subscribe(table_callback_data *callback_data);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ==== Redis task table function =====
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add or update task log entry with new scheduling information.
|
|
|
|
*
|
|
|
|
* @param callback_data Data structure containing redis connection and timeout
|
|
|
|
* information.
|
|
|
|
* @return Void.
|
|
|
|
*/
|
|
|
|
void redis_task_log_publish(table_callback_data *callback_data);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback invoked when the replya from the task push command is received.
|
|
|
|
*
|
|
|
|
* @param c Redis context.
|
|
|
|
* @param r Reply (not used).
|
|
|
|
* @param privdata Data associated to the callback.
|
|
|
|
* @return Void.
|
|
|
|
*/
|
|
|
|
void redis_task_log_publish_push_callback(redisAsyncContext *c,
|
|
|
|
void *r,
|
|
|
|
void *privdata);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback invoked when the replya from the task publish command is received.
|
|
|
|
*
|
|
|
|
* @param c Redis context.
|
|
|
|
* @param r Reply (not used).
|
|
|
|
* @param privdata Data associated to the callback.
|
|
|
|
* @return Void.
|
|
|
|
*/
|
|
|
|
void redis_task_log_publish_publish_callback(redisAsyncContext *c,
|
|
|
|
void *r,
|
|
|
|
void *privdata);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Subscribe to updates of the task log.
|
|
|
|
*
|
|
|
|
* @param callback_data Data structure containing redis connection and timeout
|
|
|
|
* information.
|
|
|
|
* @return Void.
|
|
|
|
*/
|
|
|
|
void redis_task_log_subscribe(table_callback_data *callback_data);
|
|
|
|
|
|
|
|
#endif /* REDIS_H */
|