-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathunified_voice.c
159 lines (132 loc) · 4.6 KB
/
unified_voice.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include "memory_silo.h"
#include "io_socket.h"
// Unified Voice Structure
typedef struct {
int id;
int memory_silo_id;
int io_socket_id;
} unified_voice_t;
// Unified Voice Functions
unified_voice_t* unified_voice_init(int memory_silo_id);
void unified_voice_read(int io_socket_id, void* buffer, int length);
void unified_voice_write(int io_socket_id, void* buffer, int length);
unified_voice_t* get_unified_voice(int io_socket_id);
// IO Socket Functions
void io_socket_init(int* io_socket_id);
void io_socket_read(int io_socket_id, void* buffer, int length);
void io_socket_write(int io_socket_id, void* buffer, int length);
// Diagnosis Window Functions
void diagnosis_window_init(int io_socket_id);
void diagnosis_window_update(int io_socket_id, void* buffer, int length);
void diagnosis_window_close(int io_socket_id);
// Unified Voice Initialization
unified_voice_t* unified_voice_init(int memory_silo_id) {
unified_voice_t* unified_voice = malloc(sizeof(unified_voice_t));
if (!unified_voice) {
perror("Failed to allocate memory for unified voice");
exit(EXIT_FAILURE);
}
unified_voice->id = 1;
unified_voice->memory_silo_id = memory_silo_id;
// Initialize the IO socket
io_socket_init(&unified_voice->io_socket_id);
return unified_voice;
}
void unified_voice_read(int io_socket_id, void* buffer, int length) {
unified_voice_t* unified_voice = get_unified_voice(io_socket_id);
memory_silo_read(unified_voice->memory_silo_id, buffer, length);
}
void unified_voice_write(int io_socket_id, void* buffer, int length) {
unified_voice_t* unified_voice = get_unified_voice(io_socket_id);
memory_silo_write(unified_voice->memory_silo_id, buffer, length);
}
// Get Unified Voice Function
unified_voice_t* get_unified_voice(int io_socket_id) {
unified_voice_t* unified_voice = malloc(sizeof(unified_voice_t));
if (!unified_voice) {
perror("Failed to allocate memory for unified voice");
exit(EXIT_FAILURE);
}
unified_voice->id = 1;
unified_voice->memory_silo_id = 1;
unified_voice->io_socket_id = io_socket_id;
return unified_voice;
}
// IO Socket Initialization
void io_socket_init(int* io_socket_id) {
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1) {
perror("Error creating socket");
exit(EXIT_FAILURE);
}
*io_socket_id = sock;
// Set up the server address
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(8080);
inet_pton(AF_INET, "127.0.0.1", &server_addr.sin_addr);
// Bind the socket to the server address
if (bind(sock, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) {
perror("Error binding socket");
close(sock);
exit(EXIT_FAILURE);
}
// Listen for incoming connections
if (listen(sock, 1) == -1) {
perror("Error listening on socket");
close(sock);
exit(EXIT_FAILURE);
}
}
void io_socket_read(int io_socket_id, void* buffer, int length) {
if (read(io_socket_id, buffer, length) == -1) {
perror("Error reading from IO socket");
}
}
void io_socket_write(int io_socket_id, void* buffer, int length) {
if (write(io_socket_id, buffer, length) == -1) {
perror("Error writing to IO socket");
}
}
// Diagnosis Window Functions
void diagnosis_window_init(int io_socket_id) {
// Initialize the diagnosis window
// (Implementation detail)
}
void diagnosis_window_update(int io_socket_id, void* buffer, int length) {
// Update the diagnosis window
io_socket_read(io_socket_id, buffer, length);
// (Implementation detail to update the window)
}
void diagnosis_window_close(int io_socket_id) {
close(io_socket_id);
}
int main() {
// Initialize the unified voice
unified_voice_t* unified_voice = unified_voice_init(1);
// Initialize the diagnosis window
diagnosis_window_init(unified_voice->io_socket_id);
// Main loop
while (1) {
void* buffer = malloc(1024);
if (!buffer) {
perror("Failed to allocate buffer");
break;
}
io_socket_read(unified_voice->io_socket_id, buffer, 1024);
diagnosis_window_update(unified_voice->io_socket_id, buffer, 1024);
io_socket_write(unified_voice->io_socket_id, buffer, 1024);
free(buffer);
}
// Close the diagnosis window
diagnosis_window_close(unified_voice->io_socket_id);
free(unified_voice);
return 0;
}