So now let's start working on our chats map file.
And this is just going to allow us to keep track of all the chats our users have created.
So let's go ahead and add in some use statements that we're going to need
standard collections, hash map and standard sync, AAC and mutex.
And now we can create our chat tracker struc, which is going to be a mutex on a hash map.
That has a arc string and an arc.
Chat.
And our chat tracker is obviously a mutex protected hash map that is going to map chat names to the
actual chats.
So now we can start implementing a few methods on our chat tracker.
With the first one being new so we can say Pub F in new and we're going to return a chat tracker.
And we have a chat tracker mutex.
New.
And now we have a new hash map.
And that's all we need to do here.
So the next couple of methods that we're going to have are actually very similar to each other.
So we're going to have pub f an find and pub f in find or new.
Find is going to be used when we post since we know the chat should already exist and then find or new
will be when we join a chat.
If it exists, join it.
But if the chat does not exist, then we want to create a new one.
So let's start implementing find first.
So we're going to take a reference to self and then we're going to have a reference to a string and
then we're going to return an option to a an atomic reference countered counted chat.
So then we just have self lock.
Unwrap, get the name and then clone it.
Cloned.
There we go.
And now for find finder new it's going to be very similar.
So self name a reference counter string and we return a reference counter to chat and then lock dot
on wrap entry.
Lame duck clone.
Or insert with.
Ark new chat, new name, dot clue and let's make this follow convention cargo format so everything
looks good there.
So again, these two are pretty straightforward and what they are doing and that's all we need to do
in our chat maps file.
So now we can hop over to our connections and start working on handling our clients connection to the
server.
So let's go ahead and add in some statements that we know we're going to need.
So we'll have async standard IO Buff Reader.
Async standard net TCP stream
async standard prelude give us everything
async standard sync arc and then chat utils self
chat result.
And then chat client server.
So now we are going to create a function called handle pub async effin handle and handle is going to
take care of each incoming connection.
So our implementation for this will actually be pretty straightforward.
So we're going to have a socket of type HTPC, TCP stream chats, which is going to refer to our chat
tracker.
And now we want to return a chat result.
So one thing that we're going to need is a structure to handle outgoing messages.
We will take ownership of the TCP stream and then wrap it inside a mutex so that only one task can use
it at a time.
We will have two methods associated with it new and sent.
So let's go ahead and add these in.
So we're going to have a struct and we're going to call it leaving and it's going to be a mutex wrapped
around a TCP stream.
And we have a couple of methods associated with it, such as New and Send.
So let's go ahead and add in new and it's going to be TCP stream leaving.
And then in here we're going to return leaving with a new mutex around our TCP stream.
So now we're going to have an asynchronous function called Syn that refers to self and then pack it
of the server and it's going to return a chat result.
So now we need to get the lock self zero lock await.
And now we can.
Send JSON a reference to our loch to what our lock has.
And so we need to reference it and then pass in our packet and await it.
So if we look at what's going on here, we're taking our LOC and our packet and using the UTILS send
JSON.
And so we see we have leaving which is a mutable generic oh and then our packet, so everything is lining
up accordingly.
So now we want to lock, flush, await and then return.
So now we can implement our core logic inside of handle.
For our servers connection.
The logic that we're going to implement is actually going to look very similar to our client's messages
function.
Here.
So this is something that we've relatively have already done before.
So let's just go ahead and start implementing it.
So we have a.
Leaving socket.
So we have a new atomic reference counter of leaving of our leaving struct.
Excuse me.
And now we're going to create a buffered on our buff reader new on our socket.
And then from the client we want
to receive
buffered and then while let some three quest result equals from client next
await let request equals.
Crist result and now we will say let result equal match on request.
And this is where we've kind of already have done this before in our messages.
So we're going to say client join.
Chat name.
And I will say let chat equals chats finder new, which we had recently implemented.
And then on chat name.
So we want to see if that chat already exists and then
join it.
And now we can return.
Okay.
Now we have a client post for when we want to post messages.
We have chat name and message and we're going to match on chats, find and.
Chat name.
And then inside here we have some.
Chat.
Chat Dot post.
We want to post our message
return.
Okay.
Otherwise none.
And now we want to return.
Our air chat does not exist.
The chat name.
Start ending it off.
And then if let air message equals result, let report equals server error message.
So what we're doing here is we're really just tying everything in to what we have already implemented
and to our server's logic.
And I need to move this up to right there.
And then down here we return.
And.
And now.
That we have our connection implemented.
We can tie all of this logic in to Maine.
That way we can run our application.
So we will do that in the next lecture.