1 00:00:06,040 --> 00:00:10,810 So now we're going to start setting up some logic for our server. 2 00:00:11,680 --> 00:00:19,540 So the first file that we're going to set up for our servers is our chat's file. 3 00:00:19,960 --> 00:00:23,080 So let's go ahead and start adding in some of our use statements. 4 00:00:23,770 --> 00:00:34,180 So we're going to use async standard task and then we're actually going to go ahead and start referencing 5 00:00:35,560 --> 00:00:42,340 another type in our connection crate, which we will set up a little bit later. 6 00:00:43,210 --> 00:00:54,520 And now we're going to also use an atomic reference counter, and we will start using our Tokyo broadcast 7 00:00:54,520 --> 00:00:55,180 crate. 8 00:00:56,560 --> 00:01:04,360 So as I said, we will implement the leaving type shortly, but we can see that we're not using Tokyo's 9 00:01:04,360 --> 00:01:10,870 broadcast and broadcasts is a channel that is going to carry our messages and send them to the members 10 00:01:10,870 --> 00:01:11,680 of our chat. 11 00:01:12,100 --> 00:01:16,660 So the first thing we need to do is create our chats struck. 12 00:01:17,620 --> 00:01:28,390 So we'll create our chat struck and say Name AAC stream and then we'll have our publisher, which is 13 00:01:28,390 --> 00:01:29,950 going to be a. 14 00:01:31,060 --> 00:01:35,260 Broadcast Cinder park 15 00:01:37,660 --> 00:01:38,980 string as well. 16 00:01:39,220 --> 00:01:46,870 So name is going to refer to the chat group and publisher uses broadcaster Cinder, which refers to 17 00:01:46,870 --> 00:01:50,260 the sending side of the chats broadcast channel. 18 00:01:50,740 --> 00:01:56,620 So with this struct setup, we can now implement a few methods. 19 00:01:56,740 --> 00:02:05,350 The first method is going to be new, which is going to create our broadcast channel and for now we'll 20 00:02:05,350 --> 00:02:08,620 just have it set to 1000 messages. 21 00:02:08,620 --> 00:02:10,750 So we're going to implement this. 22 00:02:11,500 --> 00:02:20,260 So we have new and it's going to contain our name, which is a reference counter to a string, and it's 23 00:02:20,260 --> 00:02:21,850 going to return. 24 00:02:22,930 --> 00:02:23,800 Chats. 25 00:02:24,880 --> 00:02:30,430 So we'll say publisher score equals. 26 00:02:31,270 --> 00:02:32,440 Broadcast. 27 00:02:34,200 --> 00:02:35,010 Channel. 28 00:02:35,250 --> 00:02:37,350 And then 1000 messages. 29 00:02:37,740 --> 00:02:47,760 So broadcast channel is going to return a publisher and a receiver, but we don't care about the receiver. 30 00:02:47,760 --> 00:02:51,180 So that's why we just went ahead and put an underscore there. 31 00:02:52,560 --> 00:02:55,500 And now we want to return our 32 00:02:58,200 --> 00:02:58,620 chat. 33 00:03:01,210 --> 00:03:01,960 So. 34 00:03:03,110 --> 00:03:05,450 As I said, we don't care for the receiver. 35 00:03:05,450 --> 00:03:08,000 So that was why we used an underscore. 36 00:03:08,000 --> 00:03:12,440 And now our next method is going to be called join. 37 00:03:14,180 --> 00:03:15,920 So we'll have join. 38 00:03:20,640 --> 00:03:28,770 It's going to have self and a atomic reference counter to a string. 39 00:03:30,850 --> 00:03:33,820 And we're going to return. 40 00:03:35,290 --> 00:03:36,130 Chet's. 41 00:03:39,880 --> 00:03:45,760 So now we're going to create a variable called receiver equals self dot. 42 00:03:45,940 --> 00:03:48,490 Publisher dot. 43 00:03:51,850 --> 00:04:00,100 Subscribe and now we will spawn a task and call a sub. 44 00:04:01,120 --> 00:04:01,870 Method. 45 00:04:03,480 --> 00:04:08,130 Say passing in our name. 46 00:04:13,250 --> 00:04:21,080 The receiver and leaving, which we passed in up there. 47 00:04:21,080 --> 00:04:25,790 So now we can begin setting up our. 48 00:04:26,700 --> 00:04:27,780 Sub function. 49 00:04:30,000 --> 00:04:32,530 So it's going to be an asynchronous function called sub. 50 00:04:35,390 --> 00:04:36,860 We're going to have a chat. 51 00:04:38,500 --> 00:04:43,510 Name of string. 52 00:04:43,990 --> 00:04:52,870 A mutable receiver of broadcast receiver. 53 00:04:57,240 --> 00:04:59,310 An atomic reference counter. 54 00:05:01,050 --> 00:05:09,210 To a string and then we will also have leaving, which is of type leaving. 55 00:05:09,210 --> 00:05:13,290 And again we will implement leaving and a second. 56 00:05:14,880 --> 00:05:24,510 So inside here, we're going to have a loop and we'll break down this function and a second in more 57 00:05:24,510 --> 00:05:25,230 detail. 58 00:05:25,860 --> 00:05:31,260 So we're going to have receiver dot receive. 59 00:05:33,390 --> 00:05:33,660 Dot. 60 00:05:33,720 --> 00:05:34,230 Wait. 61 00:05:35,880 --> 00:05:38,760 And then OC we have a message 62 00:05:42,480 --> 00:05:43,650 server. 63 00:05:46,580 --> 00:05:47,480 Message. 64 00:05:48,390 --> 00:05:52,020 So we need to bring our server into scope. 65 00:05:57,510 --> 00:06:00,870 Server message. 66 00:06:04,890 --> 00:06:05,910 Chat name. 67 00:06:07,640 --> 00:06:15,410 Chat name, clone and then message. 68 00:06:16,360 --> 00:06:17,780 Message got clone. 69 00:06:21,230 --> 00:06:23,180 And then if we receive. 70 00:06:24,640 --> 00:06:26,230 A certain air. 71 00:06:26,230 --> 00:06:32,780 In this case, we're going to receive an air of type lagged. 72 00:06:33,220 --> 00:06:39,910 So if our one of our one of our clients is connection is failing, then we want to be able to handle 73 00:06:39,910 --> 00:06:40,330 that. 74 00:06:41,380 --> 00:06:43,060 In this case, our server. 75 00:06:44,230 --> 00:06:44,800 Air. 76 00:06:47,920 --> 00:06:49,120 It's going to be. 77 00:06:53,710 --> 00:06:59,980 Dropped X amount of messages from the chat. 78 00:07:03,550 --> 00:07:05,150 Chat name. 79 00:07:08,720 --> 00:07:09,510 Do not need a. 80 00:07:10,840 --> 00:07:11,710 There. 81 00:07:14,510 --> 00:07:19,070 And now we receive an air of. 82 00:07:19,070 --> 00:07:21,740 Receive air. 83 00:07:23,090 --> 00:07:24,260 Closed. 84 00:07:28,300 --> 00:07:28,960 Break. 85 00:07:31,870 --> 00:07:32,800 All righty. 86 00:07:34,690 --> 00:07:42,220 And then if leaving Dotson, pack it away. 87 00:07:44,810 --> 00:07:47,840 What is your. 88 00:07:50,380 --> 00:07:51,340 Then break. 89 00:07:51,370 --> 00:07:51,930 Okay. 90 00:07:52,870 --> 00:07:57,160 So now let's break this function down. 91 00:07:57,280 --> 00:08:04,810 We have a loop that is receiving messages from the broadcasting channel, which then transmits them 92 00:08:04,810 --> 00:08:08,710 back to the client via a leaving. 93 00:08:09,550 --> 00:08:10,330 Variable. 94 00:08:11,290 --> 00:08:18,310 If our client is unable to keep up with the number of incoming messages so we're lagging, then it will 95 00:08:18,310 --> 00:08:22,790 report back to the client the amount of dropped messages which is in. 96 00:08:24,440 --> 00:08:32,060 If we received a closed air, the loop exits, which will then unsubscribe our user from the chat so 97 00:08:32,060 --> 00:08:34,100 that the connection is cleaned up. 98 00:08:35,150 --> 00:08:38,450 So overall, it is a pretty straightforward. 99 00:08:39,870 --> 00:08:41,160 Implementation. 100 00:08:42,030 --> 00:08:46,290 Once you just take a second to look at it and really break it down. 101 00:08:46,800 --> 00:08:54,390 So now we have one more method that we need to implement up here in chats, and that is post. 102 00:08:56,050 --> 00:09:10,390 So we're going to have a reference to our self and the message which is a type arc string and we will 103 00:09:10,390 --> 00:09:14,790 say self dot publisher, dot sin. 104 00:09:16,280 --> 00:09:17,110 Message. 105 00:09:17,120 --> 00:09:19,640 And in this case, we do not care. 106 00:09:19,790 --> 00:09:22,330 We do not care about the return value. 107 00:09:22,340 --> 00:09:25,160 So that's why we put an underscore here as well. 108 00:09:25,790 --> 00:09:36,050 So now that we have the chat file on the server implemented, we can start working on tracking our chat 109 00:09:36,050 --> 00:09:36,830 groups. 110 00:09:37,190 --> 00:09:38,750 In the next lecture.