Skip to content

Commit

Permalink
libpupvm: remove duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
wdlkmpx committed Aug 17, 2020
1 parent d3700ab commit 2487c74
Show file tree
Hide file tree
Showing 22 changed files with 93 additions and 1,058 deletions.
4 changes: 4 additions & 0 deletions libpupvm/common-includes.h
@@ -1,6 +1,9 @@
//common-includes.h
//Common library header file

#ifndef __LIBPUPVM_COMMON_H
#define __LIBPUPVM_COMMON_H

#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
Expand All @@ -20,3 +23,4 @@
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "pup-volume-monitor"

#endif
77 changes: 1 addition & 76 deletions libpupvm/conv.c
@@ -1,83 +1,8 @@
//conv.c or conv.h
//Highlevel organisation of data into conversations

#ifndef PUP_VM_H_INSIDE
//conv.c
# include "common-includes.h"
#include "common-includes.h"

#else // !PUP_VM_H_INSIDE
//conv.h

typedef struct
{
GPid process_id;
gpointer host_id;
} PupConvID;

typedef struct
{
GObject parent;
PupSock *sock;
gulong sorter_cb_tag;
gulong hup_cb_tag;
GHashTable *self_convs;
GHashTable *other_convs;
GPid process_id;
gboolean assumes_primary;
guint32 allocator;
gpointer def_cb;
gpointer def_cb_data;
} PupConvMgr;

typedef struct
{
GObjectClass parent;
} PupConvMgrClass;

//FILE_HEADER_SUBST:gobject_macro_gen PUP_CONV_MGR PupConvMgr pup_conv_mgr pup

typedef struct
{
PupConvMgr *cmgr;
PupConvID conv_id;
gboolean first;
gpointer cb;
gpointer cb_data;
gboolean closed;
gpointer close_cb;
gpointer close_cb_data;
} PupConv;


typedef enum
{
PUP_CONV_NEW = 0,
PUP_CONV_MSG = 1,
PUP_CONV_END = 2
} PupConvMsgs;

typedef enum
{
PUP_CONV_BREAK = 1 << 0,
PUP_CONV_FREE = 1 << 1,
PUP_CONV_NOREMOVE = 1 << 2
} PupConvCloseFlags;

typedef void (*PupConvCB) (PupConv *conv, PSDataParser *rcvd_data,
gboolean is_new, gpointer user_data, gpointer conv_user_data);

typedef void (*PupConvCloseCB) (PupConv *conv, gpointer user_data);

#endif // PUP_VM_H_INSIDE

//FILE_HEADER_END


#ifndef PUP_VM_H_INSIDE
G_DEFINE_TYPE(PupConvMgr, pup_conv_mgr, G_TYPE_OBJECT);
#else
GType pup_conv_mgr_get_type();
#endif

static void pup_conv_mgr_class_init(PupConvMgrClass *klass)
{
Expand Down
33 changes: 6 additions & 27 deletions libpupvm/conv.h
@@ -1,13 +1,5 @@
//conv.c or conv.h
//Highlevel organisation of data into conversations

#ifndef PUP_VM_H_INSIDE
//conv.c
# include "common-includes.h"

#else // !PUP_VM_H_INSIDE
//conv.h

typedef struct
{
GPid process_id;
Expand All @@ -34,18 +26,12 @@ typedef struct
GObjectClass parent;
} PupConvMgrClass;

//FILE_HEADER_SUBST:gobject_macro_gen PUP_CONV_MGR PupConvMgr pup_conv_mgr pup
#define PUP_TYPE_CONV_MGR (pup_conv_mgr_get_type())
#define PUP_CONV_MGR(ptr) \
(G_TYPE_CHECK_INSTANCE_CAST((ptr), PUP_TYPE_CONV_MGR, PupConvMgr))
#define PUP_IS_CONV_MGR(ptr) \
(G_TYPE_CHECK_INSTANCE_TYPE((ptr), PUP_TYPE_CONV_MGR))
#define PUP_CONV_MGR_CLASS(ptr) \
(G_TYPE_CHECK_CLASS_CAST((ptr), PUP_TYPE_CONV_MGR, PupConvMgrClass))
#define PUP_IS_CONV_MGR_CLASS(ptr) \
(G_TYPE_CHECK_CLASS_TYPE((ptr), PUP_TYPE_CONV_MGR))
#define PUP_CONV_MGR_GET_CLASS(ptr) \
(G_TYPE_INSTANCE_GET_CLASS((ptr), PUP_TYPE_CONV_MGR, PupConvMgrClass))
#define PUP_TYPE_CONV_MGR (pup_conv_mgr_get_type())
#define PUP_CONV_MGR(ptr) (G_TYPE_CHECK_INSTANCE_CAST((ptr), PUP_TYPE_CONV_MGR, PupConvMgr))
#define PUP_IS_CONV_MGR(ptr) (G_TYPE_CHECK_INSTANCE_TYPE((ptr), PUP_TYPE_CONV_MGR))
#define PUP_CONV_MGR_CLASS(ptr) (G_TYPE_CHECK_CLASS_CAST((ptr), PUP_TYPE_CONV_MGR, PupConvMgrClass))
#define PUP_IS_CONV_MGR_CLASS(ptr) (G_TYPE_CHECK_CLASS_TYPE((ptr), PUP_TYPE_CONV_MGR))
#define PUP_CONV_MGR_GET_CLASS(ptr) (G_TYPE_INSTANCE_GET_CLASS((ptr), PUP_TYPE_CONV_MGR, PupConvMgrClass))
#define PUP_CONV_MGR_NAME "PupConvMgr"

typedef struct
Expand Down Expand Up @@ -80,9 +66,7 @@ typedef void (*PupConvCB) (PupConv *conv, PSDataParser *rcvd_data,

typedef void (*PupConvCloseCB) (PupConv *conv, gpointer user_data);

#endif // PUP_VM_H_INSIDE

/*Everything beyond this is automatically generated*/
void pup_conv_send_message(PupConv *conv,PSDataEncoder *data);
void pup_conv_set_close_callback(PupConv *conv,PupConvCloseCB callback,gpointer user_data);
void pup_conv_set_msg_queue(PupConv *conv,GQueue *msg_queue);
Expand All @@ -99,9 +83,4 @@ void pup_conv_mgr_hup_cb(PupSock *sock,gpointer data);
void pup_conv_mgr_sorter_cb(PupSock *sock,PupSockData *data,gpointer user_data);
PupConvMgr *pup_conv_mgr_new(PupSock *sock,PupConvCB def_callback,gpointer user_data);
void pup_conv_mgr_finalize(GObject *instance);
#if !(!defined(PUP_VM_H_INSIDE))
GType pup_conv_mgr_get_type();
#endif
#if !defined(PUP_VM_H_INSIDE)
G_DEFINE_TYPE(PupConvMgr,pup_conv_mgr,G_TYPE_OBJECT);
#endif
96 changes: 1 addition & 95 deletions libpupvm/core.c
@@ -1,102 +1,8 @@
//core.c or core.h
//Library's central portion

#ifndef PUP_VM_H_INSIDE
//core.c
# include "common-includes.h"
#include "common-includes.h"

#else // !PUP_VM_H_INSIDE
//core.h

typedef struct
{
gint8 *data;
guint rw_ptr;
guint len;
} PupSockBuffer;

typedef enum
{
PUPSOCK_ADDRESS = 1 << 0,
PUPSOCK_IS_SERVER = 1 << 1,
PUPSOCK_IS_CONNECTED = 1 << 2,
PUPSOCK_SAVE_READ_DATA = 1 << 3,
PUPSOCK_HAS_G_MAIN = 1 << 4,

PUPSOCK_READONLY = PUPSOCK_ADDRESS | PUPSOCK_IS_SERVER
| PUPSOCK_IS_CONNECTED | PUPSOCK_HAS_G_MAIN,
PUPSOCK_BOOLEAN = PUPSOCK_IS_SERVER | PUPSOCK_IS_CONNECTED
| PUPSOCK_HAS_G_MAIN | PUPSOCK_SAVE_READ_DATA
} PupSockProperty;

typedef enum
{
PUPSOCK_ACCEPT = 0,
PUPSOCK_DATA_RECVD = 1,
PUPSOCK_HUP = 2,

PUPSOCK_N_EVENTS = 3
} PupSockEvent;

typedef struct
{
//Base object
GObject parent;

//Destroy params
gboolean attempt_flush;
gdouble timeout;

//Basic things
int fd;
GPollFD gfd;
char *address;

//Buffers
GQueue *write_buf;
GQueue *read_data;
PupSockBuffer *read_buf;

//Property bit mask
guint props;

//events
guint event_source;
} PupSock;

typedef struct
{
GObjectClass parent;

guint accept_signal_id;
guint data_recvd_signal_id;
guint hup_signal_id;
} PupSockClass;

//FILE_HEADER_SUBST:gobject_macro_gen PUP_SOCK PupSock pup_sock pup

typedef struct
{
GSource parent;
PupSock *sock;
} PupSockEventSource;

typedef gboolean (*pup_sock_common_callback)
(PupSock *sock, gpointer user_data);

typedef gboolean (*pup_sock_common_callback_data)
(PupSock *sock, gpointer cb_data, gpointer user_data);


#endif // PUP_VM_H_INSIDE

//FILE_HEADER_END

#ifndef PUP_VM_H_INSIDE
G_DEFINE_TYPE(PupSock, pup_sock, G_TYPE_OBJECT);
#else
GType pup_sock_get_type();
#endif

static void pup_sock_class_init(PupSockClass *klass)
{
Expand Down
34 changes: 6 additions & 28 deletions libpupvm/core.h
@@ -1,13 +1,5 @@
//core.c or core.h
//Library's central portion

#ifndef PUP_VM_H_INSIDE
//core.c
# include "common-includes.h"

#else // !PUP_VM_H_INSIDE
//core.h

typedef struct
{
gint8 *data;
Expand Down Expand Up @@ -73,18 +65,12 @@ typedef struct
guint hup_signal_id;
} PupSockClass;

//FILE_HEADER_SUBST:gobject_macro_gen PUP_SOCK PupSock pup_sock pup
#define PUP_TYPE_SOCK (pup_sock_get_type())
#define PUP_SOCK(ptr) \
(G_TYPE_CHECK_INSTANCE_CAST((ptr), PUP_TYPE_SOCK, PupSock))
#define PUP_IS_SOCK(ptr) \
(G_TYPE_CHECK_INSTANCE_TYPE((ptr), PUP_TYPE_SOCK))
#define PUP_SOCK_CLASS(ptr) \
(G_TYPE_CHECK_CLASS_CAST((ptr), PUP_TYPE_SOCK, PupSockClass))
#define PUP_IS_SOCK_CLASS(ptr) \
(G_TYPE_CHECK_CLASS_TYPE((ptr), PUP_TYPE_SOCK))
#define PUP_SOCK_GET_CLASS(ptr) \
(G_TYPE_INSTANCE_GET_CLASS((ptr), PUP_TYPE_SOCK, PupSockClass))
#define PUP_TYPE_SOCK (pup_sock_get_type())
#define PUP_SOCK(ptr) (G_TYPE_CHECK_INSTANCE_CAST((ptr), PUP_TYPE_SOCK, PupSock))
#define PUP_IS_SOCK(ptr) (G_TYPE_CHECK_INSTANCE_TYPE((ptr), PUP_TYPE_SOCK))
#define PUP_SOCK_CLASS(ptr) (G_TYPE_CHECK_CLASS_CAST((ptr), PUP_TYPE_SOCK, PupSockClass))
#define PUP_IS_SOCK_CLASS(ptr) (G_TYPE_CHECK_CLASS_TYPE((ptr), PUP_TYPE_SOCK))
#define PUP_SOCK_GET_CLASS(ptr) (G_TYPE_INSTANCE_GET_CLASS((ptr), PUP_TYPE_SOCK, PupSockClass))
#define PUP_SOCK_NAME "PupSock"

typedef struct
Expand All @@ -100,9 +86,6 @@ typedef gboolean (*pup_sock_common_callback_data)
(PupSock *sock, gpointer cb_data, gpointer user_data);


#endif // PUP_VM_H_INSIDE

/*Everything beyond this is automatically generated*/
void pup_sock_set_destroy_params(PupSock *sock,gboolean attempt_flush,gdouble timeout);
guint pup_sock_add_to_g_main(PupSock *sock,GMainContext *context);
void pup_sock_event_source_finalize(GSource *source);
Expand All @@ -114,9 +97,4 @@ PupSock *pup_sock_new_from_fd(int fd);
void pup_sock_set_property(GObject *instance,guint property,const GValue *value,GParamSpec *pspec);
void pup_sock_get_property(GObject *instance,guint property,GValue *value,GParamSpec *pspec);
void pup_sock_finalize(GObject *instance);
#if !(!defined(PUP_VM_H_INSIDE))
GType pup_sock_get_type();
#endif
#if !defined(PUP_VM_H_INSIDE)
G_DEFINE_TYPE(PupSock,pup_sock,G_TYPE_OBJECT);
#endif
70 changes: 1 addition & 69 deletions libpupvm/protocol.c
@@ -1,74 +1,6 @@
//protocol.c or protocol.h
//Contains functions to help interpret binary data

#ifndef PUP_VM_H_INSIDE
//protocol.c
# include "common-includes.h"

#else // !PUP_VM_H_INSIDE
//protocol.h

typedef struct
{
PupSockData data;
gsize rw_ptr;
gboolean free_on_unref;
} PSDataParser;

typedef struct
{
GQueue *data_queue;
gsize len;
} PSDataEncoder;

typedef struct
{
gint8 *data;
gsize len;
gint flags;
} PSEncodeData;

typedef enum
{
PS_DATA_ENCODER_ALLOC = 1 << 0,
PS_DATA_ENCODER_PREPEND = 1 << 1,
PS_DATA_ENCODER_FREE = 1 << 2,
PS_DATA_ENCODER_IS_ENCODER = 1 << 3
} PSEncodeDataOptions;

typedef void (*PSDataEncodeFunc) (PSDataEncoder *encoder,
gpointer data, gpointer user_data);

typedef gboolean (*PSDataParseFunc) (PSDataParser *parser,
gpointer *key_return, gpointer *data_return,
gpointer user_data);

typedef gint (*PSDataIterFunc) (PSDataEncoder *encoder,
gpointer data_structure, PSDataEncodeFunc func,
gpointer user_data);

typedef gpointer (*PSDataAddFunc) (gpointer data_structure,
gpointer key, gpointer data);

# define ps_data_parser_parse_var(p, t) \
*((t *) ps_data_parser_parse_next_fixed_block_noalloc(p, sizeof(t)))

# define ps_data_parser_parse_array(p, t, l) \
(t *) ps_data_parser_parse_next_variable_block \
(p, sizeof(t), l)

# define ps_data_encoder_add_val(e, v, t, f) \
*((t *) ps_data_encoder_alloc_fixed_block(e, sizeof(t), f)) = v

# define ps_data_encoder_add_array(e, a, t, l, f) \
ps_data_encoder_add_variable_block \
(e, (gpointer) a, (l) * sizeof(t), f)

#endif // PUP_VM_H_INSIDE

//FILE_HEADER_END


#include "common-includes.h"

PSDataParser *ps_data_parser_new(gpointer data, gsize len,
gboolean free_data_on_destroy)
Expand Down

0 comments on commit 2487c74

Please sign in to comment.