C programming
writing a (concurrent) chat server. The chat server
maintains
channels, each containing a list of messages. A client can post or
retrieve messages from a given
channel.
create the data structures used to manage the channels and
their
associated messages. The type definitions are included in
storage.h, and you'll implement several
methods in storage.c. The channels are stored as a singly-linked
list. Each element of the list conforms to
the channel_t type, which includes:
- name – A unique string identifying the channel. You must ensure
that creating multiple channels
with the same name is impossible.
- next – A pointer to the next channel.
- head/tail – The head and tail of a linked list that includes the
messages associated with this
channel
- last_msg – The id of the last message plus 1. Initially, last_msg
is 0.
As indicated above, for each channel, there is a list of messages
that are stored. Each message is defined
in the message_t type and includes the following members:
- id – An unique identifier for the message. Note that for a given
channel, each message has a
unique identifier. Furthermore, your implementation must ensure
that the message's id is
created incrementally (i.e., 0, 1, 2, and so on) and the biggest
message id in the channel is one
less than the value of last_msg of the channel.
- text – A string including the text of the message.
- next – A pointer to the next message of the channel.
To manipulate these two data structures, implement the following
API in Storage.c:
- channel_list_t *get_channels() – returns a pointer of type
channel_list_t that specifies the head
and tail of the channels list. I have already implemented this
method for you.
- channel_t *create_channel(channel_list_t *channels, const char
*name) – this method creates a
new channel with the supplied name and adds it to the list. If a
channel with channel_name
already exists, a pointer to that channel is returned.
- channel_t *get_channel(channel_list_t *channels, const char
*name) – the method returns a
channel based on its name. If the channel does not exist, NULL is
returned.
- void add_message(channel_t *channel, const char *text) – the
method will create a message_t
that contains the provided text as the text field. Then, the
created message is added to the list of
messages for the specified channel. Note that you must ensure
that the message's id is created
according to the constraints specified above.
- message_t *get_message(channel_t *channel, message_id_t id) – the
method searches the
channel's messages, and if it finds one with the supplied id, that
message is returned. If no
matching message is found, then NULL is returned.
- void dump(channel_list_t *channels) – Prints the channels and the
associated messages.
- void free_channels(channel_list_t *channels) – frees all the
channels and their associated
message
You can add locks as needed to the data
structures(storage.h)
You can access the channels list at any point by calling
channel_list_t *get_channels():
Storage.c contains:
#include "storage.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
channel_list_t *channel_list = NULL;
channel_list_t *get_channels() {
if (channel_list == NULL) {
channel_list = (channel_list_t *)
malloc(sizeof(channel_list_t));
channel_list->head = NULL;
channel_list->tail = NULL;
}
return channel_list;
}
channel_t *create_channel(channel_list_t *channels, const char
*name) {
return NULL;
}
channel_t *get_channel(channel_list_t *channels, const char
*name) {
return NULL;
}
void free_channels(channel_list_t *channels) {}
void add_message(channel_t *channel, const char *text) {
}
message_t *get_message(channel_t *channel, message_id_t id)
{
}
void dump(channel_list_t *channels) {
for (channel_t *c = channels->head; c != NULL; c =
c->next) {
printf("Channel %s\n",
c->name);
for (message_t *m = c->head; m
!= NULL; m = m->next) {
printf("\t[%lu]:
%s\n", m->id, m->text);
}
printf("\n\n");
}
}
Storage.h contains:
#include <stdint.h>
typedef uint64_t message_id_t;
typedef char* channel_id_t;
typedef struct message {
message_id_t id;
const char *text;
struct message *next;
} message_t;
typedef struct channel {
channel_id_t name;
struct channel *next;
message_t *head;
message_t *tail;
message_id_t last_msg;
} channel_t;
typedef struct channel_list {
channel_t *head;
channel_t *tail;
} channel_list_t;
// returns a channel_list_t that contains the head and tail of the
list storing the channels
channel_list_t *get_channels();
// creates a channel whose name is given by channel name.
// if a channel with that name already exists, a pointer to it is
returned
channel_t *create_channel(channel_list_t *channels, const char
*name);
// retrieves a channel by name.
// if a channel with that name does not exist, NULL is
returned.
channel_t *get_channel(channel_list_t *channels, const char
*name);
// frees all the channels and their messages
void free_channels(channel_list_t *channels);
// create a message_t that contains the provided text as the
text field and adds it to channel/
// IMPORTANT: you must efforce the following invariats:
// 1) for a given channel, two messages cannnot have the same
id
// 2) message_ids are allocated incrementally starting from 0
// 3) the biggest message id is one less than the value of last_msg
of the channel
//
void add_message(channel_t *channel, const char *text);
// searches the messages of channel and if it finds one that has
the supplied id, the it is returned.
// If no matching message is found, then NULL is returned.
message_t *get_message(channel_t *channel, message_id_t id);
// prints the channels and the message
void dump(channel_list_t *channels);
C programming writing a (concurrent) chat server. The chat server maintains channels, each containing a list of messages
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
C programming writing a (concurrent) chat server. The chat server maintains channels, each containing a list of messages
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!