Next lesson playing in 5 seconds

  • Overview
  • Transcript

3.2 OTP and GenServer

The Open Telephony Project (OTP) was an initiative to make telecom companies independent from hardware suppliers. Nowadays it is used in almost every Erlang and Elixir project (not to mention in many telecoms networks worldwide). In this lesson, we're going to learn about the GenServer, one of the most important components of OTP.

3.2 OTP and GenServer

Hi and welcome back to get started with Elixir. In this lesson we are going to talk about OTP, one of the most important parts of the Erlang standard library and the GenServer. The open Telecom platform was released by Ericsson in the late 90's as Erlang/OTP, that nowadays, it powers most of the telecommunication infrastructure on the planet. The OTP framework itself is a collection of libraries and standards that help you build applications. Since OTP is huge, we will only learn about the most important component of it, starting with the GenServer. GenServer is a module that helps creating client/server relation. Like with processors in the last lesson, you can start a GenServer with a start or start_link functions. Parsing in the module that will handle any callbacks, some arguments, and some options. If the server starts it successfully, it will return a tuple containing ok and to servers pid.. Let's look at the callback module. You have to call use GenServer in the module to enable functionality. Here is an example of wallet. First let's have a look at how you would use it and then look at the implementation. First you need to start your GenServer by parsing the right module and in our case an initial cash balance. Then you can use GenServer that cost and provide the wallet and the message. First we put money into the wallet, say 10 bucks. Then we take money out of it. Cast doesn't expect a return value, therefore it doesn't wait for the call function to complete. Finally, there's that does wait for the function to return. In our case, we parse the message amount to request the current amount that is in the wallet. Let's look at the implementation. The wallet module has an init function, that receives the initial amount you have in the wallet. Init, has to return a tuple of ok, and the initial state of the server. That will be stored in the process, if the server was started successfully. Then we have two functions handle_cast and handle_call. They respond to the invocation of their counterparts. Handle_cast put in will add money to the amount that is stored by the state and take out or subtracted. A handle_cast or call method has to return either reply or no reply and a new state. In our case we will return no reply in the cast functions and reply in the call function, since the caller expects a return value. There is an improvement to be made. Since the fact that we are using GenServer as an implementation detail, it is better to hide behind some public API. To do this, we have to create wrapper functions or client functions. Instead of having to use GenServer.start, we are going to create our own start function, the .start. Instead of naming the module explicitly, we can use a macro. Finally, we have to return the wallet. Then we have put in and take out, that call the cast functions on the GenServer. Parsing the wallet and money parameters. Finally they have amount that returns the current amount of money that's inside the wallet by using Let's see how we would call it in the console. I can create a new wallet by calling Wallet.start, and then put some money in, Or take some money out. Finally I can check the amount that's left in my wallet. To recap, almost every project in Erlang or Elixir uses OTP. A GenServer can be used if you need a client/server structure. The GenServer is started with start and start_link like processors. The functions require a call back module. You can either use call or cast to interact with a GenServer. The corresponding functions in the in the call back module are handle_call and handle_cast. In the nest lesson we are going to talk about supervisors that processors. See you there.

Back to the top