1 00:00:05,810 --> 00:00:10,490 Now that we have our chat protocol set up, let's start working on implementing our client. 2 00:00:12,090 --> 00:00:16,140 Our chat is going to read from Standard N and then send it to the server. 3 00:00:16,140 --> 00:00:21,060 So let's start to add this logic to our client file. 4 00:00:21,060 --> 00:00:23,640 So client dot press. 5 00:00:23,640 --> 00:00:30,000 So as usual, the first thing that we want to do is add in a couple of the use statements of dependencies 6 00:00:30,000 --> 00:00:31,230 that we're going to need. 7 00:00:31,740 --> 00:00:45,390 So we'll need async and then everything inside the prelude, we'll use chat, utils, self and chat 8 00:00:45,390 --> 00:00:46,080 result. 9 00:00:46,320 --> 00:00:50,970 Now if you named your project something other than chat, that's what you will place there. 10 00:00:53,010 --> 00:01:00,600 And then we're going to use async, standard, IO and net. 11 00:01:03,820 --> 00:01:07,270 And now we know that the client is going to send messages. 12 00:01:07,270 --> 00:01:14,800 So we are going to create an asynchronous function called send that will accept one argument, a mutable 13 00:01:14,800 --> 00:01:22,660 TCP stream, and then the function will return a chat result so we can set that up async send. 14 00:01:24,850 --> 00:01:36,550 Mutable send variable of net TCP stream and we're going to return a chat result. 15 00:01:38,850 --> 00:01:44,670 So our chat is going to have a few options and we want the user to be aware of them so we can just go 16 00:01:44,670 --> 00:01:46,140 ahead and print them out. 17 00:01:46,770 --> 00:01:49,020 So we'll say print line. 18 00:01:52,860 --> 00:02:01,650 And we will have our options and we'll have join chat. 19 00:02:03,950 --> 00:02:09,860 Post chat message and then. 20 00:02:10,880 --> 00:02:12,260 To exit the chat. 21 00:02:12,260 --> 00:02:25,130 You'll do control D on Unix or control Z on windows to close the. 22 00:02:26,310 --> 00:02:26,970 Connection. 23 00:02:29,580 --> 00:02:29,910 All right. 24 00:02:29,910 --> 00:02:35,820 So now that we have that out the way, we are going to take the standard input and then wrap it into 25 00:02:35,820 --> 00:02:41,120 a buffer reader so that our data is now contained inside a buffer. 26 00:02:41,130 --> 00:02:53,400 So we'll say let mute options equals IO buff reader for buffer reader a new one on the standard n. 27 00:02:57,080 --> 00:02:58,040 Lines. 28 00:02:59,630 --> 00:03:08,240 So we call lines so that the users input is processed on a line by line basis, since it is processed 29 00:03:08,240 --> 00:03:09,750 line by line. 30 00:03:09,770 --> 00:03:15,590 We can start setting up logic to parse each line and match it on a value in our client enum. 31 00:03:16,190 --> 00:03:20,940 We will want our logic to send the value to the server if it's successfully matches. 32 00:03:20,960 --> 00:03:26,300 Otherwise, we want it to generate an error and continue in a loop waiting for the next command. 33 00:03:26,690 --> 00:03:29,810 So let's start working on that logic. 34 00:03:29,810 --> 00:03:40,190 So why let some option result equals option? 35 00:03:42,280 --> 00:03:43,120 Next. 36 00:03:44,740 --> 00:03:45,430 Oh, wait. 37 00:03:46,480 --> 00:03:47,630 Let opt. 38 00:03:47,650 --> 00:03:49,300 Equal option. 39 00:03:49,300 --> 00:03:50,020 Result. 40 00:03:50,880 --> 00:03:51,210 My. 41 00:03:52,210 --> 00:03:55,720 Have been in air and now we will say let request. 42 00:03:55,720 --> 00:04:03,880 So our IQ match pass input on a reference to an option. 43 00:04:07,350 --> 00:04:14,070 Some requests where we return our request or none. 44 00:04:15,310 --> 00:04:18,520 Where we continue. 45 00:04:22,020 --> 00:04:24,120 And then we'll say Utils. 46 00:04:25,930 --> 00:04:26,770 Sinned. 47 00:04:26,770 --> 00:04:27,310 And what did I. 48 00:04:27,340 --> 00:04:31,960 Name my I have not sinned. 49 00:04:31,960 --> 00:04:43,360 We'll say sinned, Jason and mute sinned and request dot await. 50 00:04:48,130 --> 00:04:51,010 And then send flush. 51 00:04:52,190 --> 00:04:52,950 Dot. 52 00:04:52,990 --> 00:04:53,560 Oh, wait. 53 00:04:53,960 --> 00:04:56,840 So we have a lot going on in here, so. 54 00:04:58,700 --> 00:05:04,520 We created a function in here called parse input. 55 00:05:04,700 --> 00:05:10,520 This function is going to allow us to get the values our user passes in and then match on them so that 56 00:05:10,520 --> 00:05:12,920 we know what we want our user to do. 57 00:05:13,160 --> 00:05:15,050 So let's set up that function. 58 00:05:16,200 --> 00:05:22,080 But before we start setting up past input, we're actually going to need a helper function that is going 59 00:05:22,080 --> 00:05:25,800 to give us our values and take out the white space accordingly. 60 00:05:25,950 --> 00:05:28,770 So we'll call this function get value. 61 00:05:29,040 --> 00:05:31,680 So let's, let's set up get value first. 62 00:05:31,800 --> 00:05:42,030 So we'll say get value mute input reference to a string slice. 63 00:05:42,030 --> 00:05:49,170 And then we're going to return an option with references to a string slice. 64 00:05:51,320 --> 00:05:59,390 So we are returning to string slices inside our option because we want the command such as post message 65 00:05:59,390 --> 00:06:03,920 or joint chat, which will be the first value we're going to get from the user. 66 00:06:04,040 --> 00:06:10,900 And then we want the remaining string to be the to be the rest of the string that the user passed in. 67 00:06:10,910 --> 00:06:14,600 So this is actually a pretty straightforward implementation. 68 00:06:14,600 --> 00:06:18,020 So we'll say our input equals to input dot. 69 00:06:18,980 --> 00:06:20,030 Trim start. 70 00:06:22,480 --> 00:06:24,700 And if we hover over this, we can see what it does. 71 00:06:24,700 --> 00:06:28,180 It's going to return a string slice with leading white space removed. 72 00:06:28,210 --> 00:06:28,810 Great. 73 00:06:29,170 --> 00:06:30,190 That's what we need. 74 00:06:30,700 --> 00:06:31,960 If our input. 75 00:06:34,320 --> 00:06:35,310 Is empty. 76 00:06:37,180 --> 00:06:38,980 Then we want to return on. 77 00:06:44,430 --> 00:06:47,250 Otherwise we're going to match on input. 78 00:06:47,580 --> 00:06:48,390 Find 79 00:06:51,450 --> 00:06:54,780 char is whitespace. 80 00:06:56,840 --> 00:07:01,370 And then some white space. 81 00:07:03,310 --> 00:07:04,930 Overturn some. 82 00:07:07,870 --> 00:07:16,790 An input from the beginning all the way to the white space. 83 00:07:17,400 --> 00:07:18,730 Like how it did that. 84 00:07:22,460 --> 00:07:23,270 There you go. 85 00:07:24,200 --> 00:07:30,020 And then the remainder is going to be input from white space. 86 00:07:31,000 --> 00:07:31,780 To the end. 87 00:07:35,270 --> 00:07:40,790 And then in the event of none, we returned some. 88 00:07:42,890 --> 00:07:43,770 Input. 89 00:07:45,880 --> 00:07:47,950 With nothing following. 90 00:07:50,080 --> 00:07:55,660 So this is just going to allow us to get the first value and our argument. 91 00:07:56,470 --> 00:07:59,680 The user gives us and then just start. 92 00:08:00,690 --> 00:08:06,030 Breaking down the command that they are putting in. 93 00:08:07,170 --> 00:08:10,220 So now we can start implementing our parsing input. 94 00:08:12,210 --> 00:08:18,840 And these two functions will make more sense when you actually see the arguments that our user is going 95 00:08:18,840 --> 00:08:22,020 to put in to our program. 96 00:08:29,260 --> 00:08:36,970 And then now we will create a tuple which is going to be our input and our remainder, which is why 97 00:08:36,970 --> 00:08:38,410 we returned back. 98 00:08:39,990 --> 00:08:40,740 To. 99 00:08:41,460 --> 00:08:43,560 Reference to string slices. 100 00:08:43,680 --> 00:08:48,510 And now we will say, get value of our line. 101 00:08:51,560 --> 00:08:55,250 And then if input equals join. 102 00:08:55,250 --> 00:08:57,560 So remember that. 103 00:08:58,630 --> 00:09:00,730 Is an option for our client. 104 00:09:01,270 --> 00:09:03,280 So if input equals join. 105 00:09:05,020 --> 00:09:07,210 Let chat. 106 00:09:08,680 --> 00:09:10,240 And then we'll say remainder. 107 00:09:12,510 --> 00:09:13,230 Equal. 108 00:09:14,760 --> 00:09:19,830 Get value on the remainder. 109 00:09:22,050 --> 00:09:24,330 And then if remainder. 110 00:09:25,670 --> 00:09:27,270 Not equal trim. 111 00:09:27,590 --> 00:09:29,570 Start is empty. 112 00:09:32,470 --> 00:09:33,400 Return? 113 00:09:33,700 --> 00:09:34,210 None. 114 00:09:37,440 --> 00:09:38,550 Otherwise. 115 00:09:41,760 --> 00:09:45,600 Return some client 116 00:09:49,590 --> 00:09:50,460 join. 117 00:09:51,970 --> 00:09:57,400 And now we set up our join and we have our chat name. 118 00:09:59,390 --> 00:10:05,990 Which is going to be atomic reference counter and then chat dot. 119 00:10:08,580 --> 00:10:09,480 To string. 120 00:10:17,450 --> 00:10:23,270 And then else if input equals post. 121 00:10:24,740 --> 00:10:31,400 So if you can kind of see what we're doing here now we're handling if it's a post given to us from our 122 00:10:31,400 --> 00:10:32,060 user. 123 00:10:32,510 --> 00:10:34,010 Now we're going to handle that logic. 124 00:10:34,010 --> 00:10:37,280 So we'll say Let chat. 125 00:10:38,630 --> 00:10:39,500 Remainder. 126 00:10:40,920 --> 00:10:43,950 Get value remainder. 127 00:10:43,980 --> 00:10:46,860 And this is going to be very similar to what we just did. 128 00:10:49,870 --> 00:10:54,880 So we'll say let message equals remainder dot trim start. 129 00:10:57,520 --> 00:10:58,630 To string. 130 00:11:02,740 --> 00:11:09,160 And then we're going to return some client post. 131 00:11:13,220 --> 00:11:14,330 Chat name. 132 00:11:15,460 --> 00:11:22,300 Is atomic reference counter to a new chat dot to string. 133 00:11:25,240 --> 00:11:27,010 And then we want our message. 134 00:11:30,050 --> 00:11:34,130 Which is atomic inference counter to our message. 135 00:11:38,340 --> 00:11:40,050 And now otherwise. 136 00:11:42,840 --> 00:11:47,040 We must have some kind of input that we were not able to match on. 137 00:11:47,040 --> 00:11:51,090 So we have unrecognized input. 138 00:11:53,680 --> 00:12:04,540 And lets print out whatever the user gave us and we will return none. 139 00:12:05,350 --> 00:12:07,580 And then just to make this pretty. 140 00:12:08,700 --> 00:12:08,980 Oop. 141 00:12:11,050 --> 00:12:13,630 Expected one of the following. 142 00:12:13,630 --> 00:12:14,920 So online. 143 00:12:15,910 --> 00:12:16,870 12. 144 00:12:20,660 --> 00:12:27,110 Let request equals and then now line 35. 145 00:12:29,160 --> 00:12:30,720 If not in. 146 00:12:31,590 --> 00:12:35,940 So now let's take everything. 147 00:12:36,210 --> 00:12:36,750 Line up. 148 00:12:36,750 --> 00:12:37,170 Good. 149 00:12:37,770 --> 00:12:38,610 So awesome. 150 00:12:39,520 --> 00:12:41,800 So we have in here. 151 00:12:42,770 --> 00:12:45,470 A call to utils. 152 00:12:45,470 --> 00:12:47,210 Send JSON. 153 00:12:48,570 --> 00:12:50,360 But we haven't implemented that yet. 154 00:12:50,370 --> 00:12:53,670 So let's begin doing that in the next lecture.