1 00:00:05,710 --> 00:00:09,880 So now let's start working on our chats map file. 2 00:00:09,880 --> 00:00:17,290 And this is just going to allow us to keep track of all the chats our users have created. 3 00:00:17,320 --> 00:00:24,760 So let's go ahead and add in some use statements that we're going to need 4 00:00:27,160 --> 00:00:38,980 standard collections, hash map and standard sync, AAC and mutex. 5 00:00:39,190 --> 00:00:51,100 And now we can create our chat tracker struc, which is going to be a mutex on a hash map. 6 00:00:52,290 --> 00:00:59,640 That has a arc string and an arc. 7 00:01:01,340 --> 00:01:02,310 Chat. 8 00:01:05,070 --> 00:01:14,040 And our chat tracker is obviously a mutex protected hash map that is going to map chat names to the 9 00:01:14,040 --> 00:01:15,450 actual chats. 10 00:01:15,930 --> 00:01:20,790 So now we can start implementing a few methods on our chat tracker. 11 00:01:22,450 --> 00:01:36,400 With the first one being new so we can say Pub F in new and we're going to return a chat tracker. 12 00:01:39,070 --> 00:01:43,170 And we have a chat tracker mutex. 13 00:01:43,530 --> 00:01:44,290 New. 14 00:01:44,590 --> 00:01:48,640 And now we have a new hash map. 15 00:01:50,590 --> 00:01:52,720 And that's all we need to do here. 16 00:01:52,930 --> 00:01:58,030 So the next couple of methods that we're going to have are actually very similar to each other. 17 00:01:58,030 --> 00:02:07,570 So we're going to have pub f an find and pub f in find or new. 18 00:02:09,890 --> 00:02:19,010 Find is going to be used when we post since we know the chat should already exist and then find or new 19 00:02:19,040 --> 00:02:21,240 will be when we join a chat. 20 00:02:21,260 --> 00:02:23,600 If it exists, join it. 21 00:02:23,600 --> 00:02:27,920 But if the chat does not exist, then we want to create a new one. 22 00:02:27,920 --> 00:02:30,710 So let's start implementing find first. 23 00:02:30,740 --> 00:02:40,340 So we're going to take a reference to self and then we're going to have a reference to a string and 24 00:02:40,340 --> 00:02:49,970 then we're going to return an option to a an atomic reference countered counted chat. 25 00:02:52,280 --> 00:02:56,030 So then we just have self lock. 26 00:02:57,920 --> 00:03:07,580 Unwrap, get the name and then clone it. 27 00:03:10,830 --> 00:03:11,540 Cloned. 28 00:03:11,580 --> 00:03:12,150 There we go. 29 00:03:13,660 --> 00:03:18,070 And now for find finder new it's going to be very similar. 30 00:03:18,070 --> 00:03:36,610 So self name a reference counter string and we return a reference counter to chat and then lock dot 31 00:03:36,610 --> 00:03:38,890 on wrap entry. 32 00:03:40,590 --> 00:03:44,310 Lame duck clone. 33 00:03:47,970 --> 00:03:52,680 Or insert with. 34 00:03:55,410 --> 00:04:14,550 Ark new chat, new name, dot clue and let's make this follow convention cargo format so everything 35 00:04:14,550 --> 00:04:16,650 looks good there. 36 00:04:16,650 --> 00:04:22,920 So again, these two are pretty straightforward and what they are doing and that's all we need to do 37 00:04:22,920 --> 00:04:25,260 in our chat maps file. 38 00:04:25,470 --> 00:04:32,880 So now we can hop over to our connections and start working on handling our clients connection to the 39 00:04:32,880 --> 00:04:33,510 server. 40 00:04:34,200 --> 00:04:37,470 So let's go ahead and add in some statements that we know we're going to need. 41 00:04:37,860 --> 00:04:43,410 So we'll have async standard IO Buff Reader. 42 00:04:46,090 --> 00:04:54,430 Async standard net TCP stream 43 00:04:57,070 --> 00:05:04,390 async standard prelude give us everything 44 00:05:06,700 --> 00:05:18,010 async standard sync arc and then chat utils self 45 00:05:21,760 --> 00:05:23,350 chat result. 46 00:05:25,860 --> 00:05:32,310 And then chat client server. 47 00:05:33,960 --> 00:05:43,920 So now we are going to create a function called handle pub async effin handle and handle is going to 48 00:05:43,920 --> 00:05:46,740 take care of each incoming connection. 49 00:05:46,860 --> 00:05:51,330 So our implementation for this will actually be pretty straightforward. 50 00:05:51,630 --> 00:06:03,150 So we're going to have a socket of type HTPC, TCP stream chats, which is going to refer to our chat 51 00:06:03,360 --> 00:06:04,260 tracker. 52 00:06:06,610 --> 00:06:10,390 And now we want to return a chat result. 53 00:06:13,610 --> 00:06:21,260 So one thing that we're going to need is a structure to handle outgoing messages. 54 00:06:21,830 --> 00:06:29,090 We will take ownership of the TCP stream and then wrap it inside a mutex so that only one task can use 55 00:06:29,090 --> 00:06:29,960 it at a time. 56 00:06:30,530 --> 00:06:34,360 We will have two methods associated with it new and sent. 57 00:06:34,370 --> 00:06:36,710 So let's go ahead and add these in. 58 00:06:37,860 --> 00:06:46,320 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 59 00:06:46,320 --> 00:06:48,570 around a TCP stream. 60 00:06:49,620 --> 00:06:57,870 And we have a couple of methods associated with it, such as New and Send. 61 00:06:57,870 --> 00:07:07,740 So let's go ahead and add in new and it's going to be TCP stream leaving. 62 00:07:08,250 --> 00:07:16,710 And then in here we're going to return leaving with a new mutex around our TCP stream. 63 00:07:18,240 --> 00:07:25,680 So now we're going to have an asynchronous function called Syn that refers to self and then pack it 64 00:07:26,580 --> 00:07:33,720 of the server and it's going to return a chat result. 65 00:07:34,260 --> 00:07:43,080 So now we need to get the lock self zero lock await. 66 00:07:45,150 --> 00:07:46,860 And now we can. 67 00:07:48,730 --> 00:07:57,220 Send JSON a reference to our loch to what our lock has. 68 00:07:57,310 --> 00:08:06,750 And so we need to reference it and then pass in our packet and await it. 69 00:08:06,760 --> 00:08:14,350 So if we look at what's going on here, we're taking our LOC and our packet and using the UTILS send 70 00:08:14,350 --> 00:08:15,310 JSON. 71 00:08:15,730 --> 00:08:23,500 And so we see we have leaving which is a mutable generic oh and then our packet, so everything is lining 72 00:08:23,500 --> 00:08:24,910 up accordingly. 73 00:08:26,710 --> 00:08:35,830 So now we want to lock, flush, await and then return. 74 00:08:39,880 --> 00:08:44,680 So now we can implement our core logic inside of handle. 75 00:08:45,710 --> 00:08:47,840 For our servers connection. 76 00:08:48,110 --> 00:08:53,780 The logic that we're going to implement is actually going to look very similar to our client's messages 77 00:08:53,780 --> 00:08:54,620 function. 78 00:08:57,450 --> 00:08:58,170 Here. 79 00:08:58,900 --> 00:09:02,210 So this is something that we've relatively have already done before. 80 00:09:02,230 --> 00:09:07,510 So let's just go ahead and start implementing it. 81 00:09:08,620 --> 00:09:10,210 So we have a. 82 00:09:12,570 --> 00:09:15,000 Leaving socket. 83 00:09:21,090 --> 00:09:28,560 So we have a new atomic reference counter of leaving of our leaving struct. 84 00:09:28,560 --> 00:09:29,400 Excuse me. 85 00:09:29,910 --> 00:09:39,000 And now we're going to create a buffered on our buff reader new on our socket. 86 00:09:40,250 --> 00:09:44,600 And then from the client we want 87 00:09:47,210 --> 00:09:49,280 to receive 88 00:09:51,410 --> 00:10:09,410 buffered and then while let some three quest result equals from client next 89 00:10:11,780 --> 00:10:17,750 await let request equals. 90 00:10:18,890 --> 00:10:26,880 Crist result and now we will say let result equal match on request. 91 00:10:27,090 --> 00:10:31,710 And this is where we've kind of already have done this before in our messages. 92 00:10:31,860 --> 00:10:36,000 So we're going to say client join. 93 00:10:40,010 --> 00:10:41,150 Chat name. 94 00:10:44,480 --> 00:10:52,310 And I will say let chat equals chats finder new, which we had recently implemented. 95 00:10:52,430 --> 00:10:55,460 And then on chat name. 96 00:10:55,460 --> 00:11:00,080 So we want to see if that chat already exists and then 97 00:11:03,170 --> 00:11:04,190 join it. 98 00:11:07,430 --> 00:11:09,290 And now we can return. 99 00:11:09,680 --> 00:11:10,200 Okay. 100 00:11:12,740 --> 00:11:18,320 Now we have a client post for when we want to post messages. 101 00:11:18,680 --> 00:11:30,620 We have chat name and message and we're going to match on chats, find and. 102 00:11:34,710 --> 00:11:36,030 Chat name. 103 00:11:39,330 --> 00:11:43,480 And then inside here we have some. 104 00:11:44,430 --> 00:11:45,360 Chat. 105 00:11:50,840 --> 00:11:52,430 Chat Dot post. 106 00:11:53,030 --> 00:11:54,710 We want to post our message 107 00:11:57,200 --> 00:11:57,620 return. 108 00:11:57,620 --> 00:11:58,430 Okay. 109 00:11:59,000 --> 00:12:00,950 Otherwise none. 110 00:12:01,790 --> 00:12:05,660 And now we want to return. 111 00:12:05,660 --> 00:12:11,420 Our air chat does not exist. 112 00:12:12,050 --> 00:12:13,310 The chat name. 113 00:12:22,150 --> 00:12:24,490 Start ending it off. 114 00:12:25,330 --> 00:12:40,090 And then if let air message equals result, let report equals server error message. 115 00:12:40,090 --> 00:12:45,970 So what we're doing here is we're really just tying everything in to what we have already implemented 116 00:12:45,970 --> 00:12:51,130 and to our server's logic. 117 00:12:55,480 --> 00:13:00,670 And I need to move this up to right there. 118 00:13:00,790 --> 00:13:04,420 And then down here we return. 119 00:13:11,700 --> 00:13:12,570 And. 120 00:13:15,710 --> 00:13:16,760 And now. 121 00:13:17,570 --> 00:13:20,590 That we have our connection implemented. 122 00:13:20,600 --> 00:13:24,570 We can tie all of this logic in to Maine. 123 00:13:24,590 --> 00:13:27,710 That way we can run our application. 124 00:13:27,710 --> 00:13:30,650 So we will do that in the next lecture.