feat(boc): v1.0 - Complete Business Operations Center
- Go backend API with full CRUD for all modules - Rust analytics service with parallel processing - C runtime with POSIX shared memory IPC - PostgreSQL schema with 30+ tables - Redis cache, Kafka event streaming - WebSocket hub, automation engine - PDF generation, Resend email integration - JWT auth, multi-tenant - Docker Compose deployment - Nginx reverse proxy Refs: BOC-001
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
# Build stage
|
||||
FROM gcc:14 AS builder
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
# Copy source code
|
||||
COPY src ./src
|
||||
|
||||
# Build shared library
|
||||
RUN gcc -shared -fPIC -O3 -o libboc_ipc.so src/ipc.c \
|
||||
-lpthread -lrt
|
||||
|
||||
# Build static library
|
||||
RUN gcc -c -O3 -o ipc.o src/ipc.c && \
|
||||
ar rcs libboc_ipc.a ipc.o
|
||||
|
||||
# Final stage - minimal runtime
|
||||
FROM alpine:latest
|
||||
|
||||
RUN apk add --no-cache libc6-compat
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
# Copy libraries
|
||||
COPY --from=builder /app/libboc_ipc.so /usr/local/lib/
|
||||
COPY --from=builder /app/libboc_ipc.a /usr/local/lib/
|
||||
COPY --from=builder /app/src/ipc.h /usr/local/include/
|
||||
|
||||
# Update library cache
|
||||
RUN ldconfig /usr/local/lib || true
|
||||
|
||||
# Default command - keep container running for IPC
|
||||
CMD ["sh", "-c", "echo 'BOC C Runtime ready' && tail -f /dev/null"]
|
||||
Binary file not shown.
Binary file not shown.
Executable
BIN
Binary file not shown.
@@ -0,0 +1,363 @@
|
||||
#include "ipc.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <time.h>
|
||||
|
||||
// Shared memory implementation
|
||||
int boc_shm_create(const char* name, size_t size) {
|
||||
int fd = shm_open(name, O_CREAT | O_RDWR | O_EXCL, 0644);
|
||||
if (fd < 0) {
|
||||
if (errno == EEXIST) {
|
||||
// Already exists, try to open
|
||||
return boc_shm_open(name);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (ftruncate(fd, size) < 0) {
|
||||
close(fd);
|
||||
shm_unlink(name);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return fd;
|
||||
}
|
||||
|
||||
int boc_shm_open(const char* name) {
|
||||
int fd = shm_open(name, O_RDWR, 0644);
|
||||
return fd;
|
||||
}
|
||||
|
||||
void* boc_shm_map(int fd, size_t size) {
|
||||
void* addr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
if (addr == MAP_FAILED) {
|
||||
return NULL;
|
||||
}
|
||||
return addr;
|
||||
}
|
||||
|
||||
int boc_shm_unmap(void* addr, size_t size) {
|
||||
return munmap(addr, size);
|
||||
}
|
||||
|
||||
int boc_shm_destroy(const char* name) {
|
||||
return shm_unlink(name);
|
||||
}
|
||||
|
||||
// Ring buffer implementation
|
||||
int boc_ring_init(boc_ring_buffer_t* ring) {
|
||||
if (!ring) return -1;
|
||||
|
||||
ring->write_idx = 0;
|
||||
ring->read_idx = 0;
|
||||
ring->flags = 0;
|
||||
memset(ring->data, 0, BOC_RING_SIZE);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int boc_ring_write(boc_ring_buffer_t* ring, const void* data, size_t len) {
|
||||
if (!ring || !data || len == 0 || len > BOC_MAX_MSG_SIZE) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint64_t write_idx = ring->write_idx;
|
||||
uint64_t read_idx = ring->read_idx;
|
||||
|
||||
// Check available space (leave 1 byte gap to distinguish full from empty)
|
||||
uint64_t available = (read_idx > write_idx)
|
||||
? (read_idx - write_idx - 1)
|
||||
: (BOC_RING_SIZE - write_idx + read_idx - 1);
|
||||
|
||||
// Need space for length (4 bytes) + data
|
||||
size_t total_len = sizeof(uint32_t) + len;
|
||||
if (available < total_len) {
|
||||
return -1; // Buffer full
|
||||
}
|
||||
|
||||
// Write length prefix
|
||||
uint32_t len32 = (uint32_t)len;
|
||||
for (size_t i = 0; i < sizeof(uint32_t); i++) {
|
||||
ring->data[write_idx % BOC_RING_SIZE] = ((char*)&len32)[i];
|
||||
write_idx++;
|
||||
}
|
||||
|
||||
// Write data
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
ring->data[write_idx % BOC_RING_SIZE] = ((char*)data)[i];
|
||||
write_idx++;
|
||||
}
|
||||
|
||||
// Memory barrier to ensure data is written before updating index
|
||||
__sync_synchronize();
|
||||
ring->write_idx = write_idx;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int boc_ring_read(boc_ring_buffer_t* ring, void* data, size_t max_len) {
|
||||
if (!ring || !data || max_len == 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint64_t write_idx = ring->write_idx;
|
||||
uint64_t read_idx = ring->read_idx;
|
||||
|
||||
if (write_idx == read_idx) {
|
||||
return 0; // Empty
|
||||
}
|
||||
|
||||
// Read length prefix
|
||||
uint32_t len = 0;
|
||||
for (size_t i = 0; i < sizeof(uint32_t); i++) {
|
||||
((char*)&len)[i] = ring->data[read_idx % BOC_RING_SIZE];
|
||||
read_idx++;
|
||||
}
|
||||
|
||||
if (len > max_len) {
|
||||
return -1; // Buffer too small
|
||||
}
|
||||
|
||||
// Read data
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
((char*)data)[i] = ring->data[read_idx % BOC_RING_SIZE];
|
||||
read_idx++;
|
||||
}
|
||||
|
||||
// Memory barrier
|
||||
__sync_synchronize();
|
||||
ring->read_idx = read_idx;
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
bool boc_ring_empty(boc_ring_buffer_t* ring) {
|
||||
return ring->write_idx == ring->read_idx;
|
||||
}
|
||||
|
||||
uint64_t boc_ring_available(boc_ring_buffer_t* ring) {
|
||||
uint64_t write_idx = ring->write_idx;
|
||||
uint64_t read_idx = ring->read_idx;
|
||||
|
||||
if (write_idx >= read_idx) {
|
||||
return write_idx - read_idx;
|
||||
} else {
|
||||
return BOC_RING_SIZE - read_idx + write_idx;
|
||||
}
|
||||
}
|
||||
|
||||
// Message serialization
|
||||
size_t boc_msg_serialize(boc_msg_t* msg, char* buf, size_t buf_size) {
|
||||
if (!msg || !buf || buf_size < sizeof(boc_msg_t)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t total_size = sizeof(boc_msg_t) + msg->length;
|
||||
if (buf_size < total_size) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
memcpy(buf, msg, sizeof(boc_msg_t));
|
||||
if (msg->length > 0 && msg->payload) {
|
||||
memcpy(buf + sizeof(boc_msg_t), msg->payload, msg->length);
|
||||
}
|
||||
|
||||
return total_size;
|
||||
}
|
||||
|
||||
int boc_msg_deserialize(const char* buf, size_t len, boc_msg_t** msg) {
|
||||
if (!buf || len < sizeof(boc_msg_t) || !msg) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
boc_msg_t* header = (boc_msg_t*)buf;
|
||||
size_t total_size = sizeof(boc_msg_t) + header->length;
|
||||
|
||||
if (len < total_size) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
*msg = malloc(total_size);
|
||||
if (!*msg) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
memcpy(*msg, buf, total_size);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void boc_msg_free(boc_msg_t* msg) {
|
||||
free(msg);
|
||||
}
|
||||
|
||||
// High-level IPC channel
|
||||
struct boc_ipc_channel {
|
||||
char name[256];
|
||||
boc_ring_buffer_t* request_ring;
|
||||
boc_ring_buffer_t* response_ring;
|
||||
int shm_fd;
|
||||
void* shm_addr;
|
||||
size_t shm_size;
|
||||
};
|
||||
|
||||
boc_ipc_channel_t* boc_ipc_connect(const char* channel_name) {
|
||||
boc_ipc_channel_t* channel = calloc(1, sizeof(boc_ipc_channel_t));
|
||||
if (!channel) return NULL;
|
||||
|
||||
strncpy(channel->name, channel_name, sizeof(channel->name) - 1);
|
||||
|
||||
// Create shared memory for two ring buffers
|
||||
channel->shm_size = sizeof(boc_ring_buffer_t) * 2;
|
||||
|
||||
char shm_name[512];
|
||||
snprintf(shm_name, sizeof(shm_name), "/boc_ipc_%s", channel_name);
|
||||
|
||||
channel->shm_fd = boc_shm_create(shm_name, channel->shm_size);
|
||||
if (channel->shm_fd < 0) {
|
||||
free(channel);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
channel->shm_addr = boc_shm_map(channel->shm_fd, channel->shm_size);
|
||||
if (!channel->shm_addr) {
|
||||
close(channel->shm_fd);
|
||||
shm_unlink(shm_name);
|
||||
free(channel);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Initialize rings
|
||||
channel->request_ring = (boc_ring_buffer_t*)channel->shm_addr;
|
||||
channel->response_ring = (boc_ring_buffer_t*)(channel->shm_addr + sizeof(boc_ring_buffer_t));
|
||||
|
||||
boc_ring_init(channel->request_ring);
|
||||
boc_ring_init(channel->response_ring);
|
||||
|
||||
return channel;
|
||||
}
|
||||
|
||||
void boc_ipc_disconnect(boc_ipc_channel_t* channel) {
|
||||
if (!channel) return;
|
||||
|
||||
if (channel->shm_addr) {
|
||||
boc_shm_unmap(channel->shm_addr, channel->shm_size);
|
||||
}
|
||||
if (channel->shm_fd >= 0) {
|
||||
close(channel->shm_fd);
|
||||
}
|
||||
|
||||
char shm_name[512];
|
||||
snprintf(shm_name, sizeof(shm_name), "/boc_ipc_%s", channel->name);
|
||||
boc_shm_destroy(shm_name);
|
||||
|
||||
free(channel);
|
||||
}
|
||||
|
||||
int boc_ipc_send(boc_ipc_channel_t* channel, boc_msg_t* msg) {
|
||||
if (!channel || !msg) return -1;
|
||||
|
||||
char buf[BOC_MAX_MSG_SIZE];
|
||||
size_t len = boc_msg_serialize(msg, buf, sizeof(buf));
|
||||
if (len == 0) return -1;
|
||||
|
||||
return boc_ring_write(channel->request_ring, buf, len);
|
||||
}
|
||||
|
||||
int boc_ipc_recv(boc_ipc_channel_t* channel, boc_msg_t** msg, int timeout_ms) {
|
||||
if (!channel || !msg) return -1;
|
||||
|
||||
char buf[BOC_MAX_MSG_SIZE];
|
||||
|
||||
// Simple polling with timeout
|
||||
int waited = 0;
|
||||
while (waited < timeout_ms) {
|
||||
int len = boc_ring_read(channel->response_ring, buf, sizeof(buf));
|
||||
if (len > 0) {
|
||||
return boc_msg_deserialize(buf, len, msg);
|
||||
}
|
||||
|
||||
usleep(1000); // 1ms
|
||||
waited += 1;
|
||||
}
|
||||
|
||||
return -1; // Timeout
|
||||
}
|
||||
|
||||
// Analytics cache implementation
|
||||
int boc_cache_init(boc_analytics_cache_t* cache) {
|
||||
if (!cache) return -1;
|
||||
|
||||
cache->version = 1;
|
||||
for (int i = 0; i < BOC_CACHE_SIZE; i++) {
|
||||
cache->entries[i].valid = false;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static uint64_t hash_key(uint64_t key_hash) {
|
||||
return key_hash % BOC_CACHE_SIZE;
|
||||
}
|
||||
|
||||
int boc_cache_get(boc_analytics_cache_t* cache, uint64_t key_hash, double* value, double* trend) {
|
||||
if (!cache || !value || !trend) return -1;
|
||||
|
||||
uint64_t idx = hash_key(key_hash);
|
||||
boc_cache_entry_t* entry = &cache->entries[idx];
|
||||
|
||||
if (!entry->valid || entry->key_hash != key_hash) {
|
||||
return -1; // Not found
|
||||
}
|
||||
|
||||
// Check TTL
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
uint64_t now = ts.tv_sec;
|
||||
|
||||
if (now > entry->timestamp + entry->ttl_seconds) {
|
||||
entry->valid = false;
|
||||
return -1; // Expired
|
||||
}
|
||||
|
||||
*value = entry->value;
|
||||
*trend = entry->trend;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int boc_cache_set(boc_analytics_cache_t* cache, uint64_t key_hash, double value, double trend, uint32_t ttl) {
|
||||
if (!cache) return -1;
|
||||
|
||||
uint64_t idx = hash_key(key_hash);
|
||||
boc_cache_entry_t* entry = &cache->entries[idx];
|
||||
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
|
||||
entry->key_hash = key_hash;
|
||||
entry->value = value;
|
||||
entry->trend = trend;
|
||||
entry->timestamp = ts.tv_sec;
|
||||
entry->ttl_seconds = ttl;
|
||||
entry->valid = true;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void boc_cache_invalidate(boc_analytics_cache_t* cache, uint64_t key_hash) {
|
||||
if (!cache) return;
|
||||
|
||||
uint64_t idx = hash_key(key_hash);
|
||||
boc_cache_entry_t* entry = &cache->entries[idx];
|
||||
|
||||
if (entry->key_hash == key_hash) {
|
||||
entry->valid = false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
#ifndef BOC_IPC_H
|
||||
#define BOC_IPC_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// Shared memory ring buffer for high-throughput communication
|
||||
#define BOC_SHM_SIZE (1024 * 1024 * 16) // 16MB
|
||||
#define BOC_RING_SIZE (1024 * 64) // 64KB buffer
|
||||
#define BOC_MAX_MSG_SIZE 8192
|
||||
|
||||
typedef enum {
|
||||
BOC_MSG_ANALYTICS_REQUEST = 1,
|
||||
BOC_MSG_ANALYTICS_RESPONSE = 2,
|
||||
BOC_MSG_REPORT_REQUEST = 3,
|
||||
BOC_MSG_REPORT_RESPONSE = 4,
|
||||
BOC_MSG_EVENT = 5,
|
||||
BOC_MSG_HEARTBEAT = 6,
|
||||
} boc_msg_type_t;
|
||||
|
||||
typedef struct {
|
||||
uint32_t type;
|
||||
uint32_t length;
|
||||
uint64_t timestamp;
|
||||
uint64_t correlation_id;
|
||||
char payload[];
|
||||
} boc_msg_t;
|
||||
|
||||
typedef struct {
|
||||
volatile uint64_t write_idx;
|
||||
volatile uint64_t read_idx;
|
||||
volatile uint32_t flags;
|
||||
char data[BOC_RING_SIZE];
|
||||
} boc_ring_buffer_t;
|
||||
|
||||
// Shared memory API
|
||||
int boc_shm_create(const char* name, size_t size);
|
||||
int boc_shm_open(const char* name);
|
||||
void* boc_shm_map(int fd, size_t size);
|
||||
int boc_shm_unmap(void* addr, size_t size);
|
||||
int boc_shm_destroy(const char* name);
|
||||
|
||||
// Ring buffer API
|
||||
int boc_ring_init(boc_ring_buffer_t* ring);
|
||||
int boc_ring_write(boc_ring_buffer_t* ring, const void* data, size_t len);
|
||||
int boc_ring_read(boc_ring_buffer_t* ring, void* data, size_t max_len);
|
||||
bool boc_ring_empty(boc_ring_buffer_t* ring);
|
||||
uint64_t boc_ring_available(boc_ring_buffer_t* ring);
|
||||
|
||||
// Message serialization
|
||||
size_t boc_msg_serialize(boc_msg_t* msg, char* buf, size_t buf_size);
|
||||
int boc_msg_deserialize(const char* buf, size_t len, boc_msg_t** msg);
|
||||
void boc_msg_free(boc_msg_t* msg);
|
||||
|
||||
// High-level API for Go/Rust interop
|
||||
typedef struct boc_ipc_channel boc_ipc_channel_t;
|
||||
|
||||
boc_ipc_channel_t* boc_ipc_connect(const char* channel_name);
|
||||
void boc_ipc_disconnect(boc_ipc_channel_t* channel);
|
||||
int boc_ipc_send(boc_ipc_channel_t* channel, boc_msg_t* msg);
|
||||
int boc_ipc_recv(boc_ipc_channel_t* channel, boc_msg_t** msg, int timeout_ms);
|
||||
|
||||
// Analytics cache in shared memory
|
||||
typedef struct {
|
||||
uint64_t key_hash;
|
||||
double value;
|
||||
double trend;
|
||||
uint64_t timestamp;
|
||||
uint32_t ttl_seconds;
|
||||
bool valid;
|
||||
} boc_cache_entry_t;
|
||||
|
||||
#define BOC_CACHE_SIZE 1024
|
||||
|
||||
typedef struct {
|
||||
volatile uint32_t version;
|
||||
boc_cache_entry_t entries[BOC_CACHE_SIZE];
|
||||
} boc_analytics_cache_t;
|
||||
|
||||
int boc_cache_init(boc_analytics_cache_t* cache);
|
||||
int boc_cache_get(boc_analytics_cache_t* cache, uint64_t key_hash, double* value, double* trend);
|
||||
int boc_cache_set(boc_analytics_cache_t* cache, uint64_t key_hash, double value, double trend, uint32_t ttl);
|
||||
void boc_cache_invalidate(boc_analytics_cache_t* cache, uint64_t key_hash);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // BOC_IPC_H
|
||||
Reference in New Issue
Block a user