You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
defmoduleKV.Bucketdo@doc""" Starts a new bucket. """defstart_linkdoAgent.start_link(fn->%{}end)enddefget(bucket,key)doAgent.get(bucket,&Map.get(&1,key))enddefput(bucket,key,value)doAgent.update(bucket,&Map.put(&1,key,value))enddefdelete(bucket,key)doAgent.get_and_update(bucket,&Map.pop(&1,key))endend
# lib/kv/registry.exdefmoduleKV.RegistrydouseGenServer## Client API@doc""" Starts the registry. """defstart_linkdoGenServer.start_link(__MODULE__,:ok,[])end@doc""" Looks up the bucket pid for `name` stored in `server`. Returns `{:ok, pid}` if the bucket exists, `:error` otherwise. """deflookup(server,name)doGenServer.call(server,{:lookup,name})end@doc""" Ensures there is a bucket associated to the given `name` in `server`. """defcreate(server,name)doGenServer.cast(server,{:create,name})end@doc""" Stops the registry. """defstop(server)doGenServer.stop(server)end## Server callbacksdefinit(:ok)donames=%{}refs=%{}{:ok,{names,refs}}enddefhandle_call({:lookup,name},_from,{names,_}=state)do{:reply,Map.fetch(names,name),state}enddefhandle_cast({:create,name},{names,refs})doifMap.has_key?(names,name)do{:noreply,{names,refs}}else{:ok,pid}=KV.Bucket.start_linkref=Process.monitor(pid)refs=Map.put(refs,ref,name)names=Map.put(names,name,pid){:noreply,{names,refs}}endenddefhandle_info({:DOWN,ref,:process,_pid,_reason},{names,refs})do{name,refs}=Map.pop(refs,ref)names=Map.delete(names,name){:noreply,{names,refs}}enddefhandle_info(_msg,state)do{:noreply,state}endend
defmoduleKV.BucketTestdouseExUnit.Case,async: truesetupdo{:ok,bucket}=KV.Bucket.start_link{:ok,bucket: bucket}endtest"sample"doassertfoo==nilendtest"sample",%{bucket: bucket}do# `bucket` is now the bucket from the setup blockendend
# apps/kv_server/lib/kv_server.exdefmoduleKVServerdorequireLoggerdefaccept(port)do{:ok,socket}=:gen_tcp.listen(port,[:binary,packet: :line,active: false,reuseaddr: true])Logger.info"Accepting connections on port #{port}"loop_acceptor(socket)enddefploop_acceptor(socket)do{:ok,client}=:gen_tcp.accept(socket)serve(client)loop_acceptor(socket)enddefpserve(socket)dosocket|>read_line()|>write_line(socket)serve(socket)enddefpread_line(socket)do{:ok,data}=:gen_tcp.recv(socket,0)dataenddefpwrite_line(line,socket)do:gen_tcp.send(socket,line)endend
Start an IEx session inside app kv_server by running iex -S mix.
iex> KVServer.accept(40400)
In new terminal, test the server using telnet.
$ telnet 127.0.0.1 4040
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
hello
hello
Start the server by typing mix run --no-halt in the terminal and test it using telnet. Now, even if the client gets killed, whole server will still crash, but a new one will start right away.
If you try to connent multiple clients to this server at the same time, it won't work.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters