erlang

erlang - how to limit message queue or emulate it?

▼魔方 西西 提交于 2019-12-05 12:17:13
Right now I am doing Process ! Message, but as i googled a bit, a message queue size is only limited to memory. I have a tree of processes where leaves generate messages and feed up to the root and i need to limit queue or switch to some another method of doing the same. more of it, sometimes Process gets messages from one leaf and sometimes from two leaves. In the second case I need different finite queues for eave leaf. There are no built-in mechanisms to limit the size of the message queue for a process. The usual solution to this problem in erlang is to introduce a flow control protocol

In Erlang how to get Client's ip and port?

谁说我不能喝 提交于 2019-12-05 12:11:51
In the following code , server is listening to port 2345. After accepting connection from client it returns {ok, Socket} start() -> {ok, Listen} = gen_tcp:listen(2345, [binary, {packet, 4}, {reuseaddr, true}, {active, true}]), {ok, Socket} = gen_tcp:accept(Listen). I want to get client's IP and port, how can I get them by analyzing socket? Use inet:peername/1 . The description of the function from documentation: peername(Socket) -> {ok, {Address, Port}} | {error, posix()} Types: Socket = socket() Address = ip_address() Port = integer() >= 0 Returns the address and port for the other end of a

Showing full expected and value information when ?_assertEqual fails

谁都会走 提交于 2019-12-05 11:28:02
I'm coding a unit test where a (rather lengthy) binary is generated, and I want to assert that the generated binary equals the one I expect to be generated. I'm running eunit through " rebar eunit ". Thing is, when this assertion fails, the output is abreviated with " ... ", and I want to see the complete output so I can spot where the difference is. I'm now using " ?debugFmt() " as a temporary solution, but I'd like to know if there's an alternative to it (a config option or argument somewhere that can be applied to " ?_assertEqual() " so the output is only shown when the assertion fails).

Erlang : nested cases

强颜欢笑 提交于 2019-12-05 11:23:26
I'm very new to Erlang. I tried to find out if a list index is out of bounds (before trying it) so i wanted to do an if clause with something like if lists:flatlength(A) < DestinationIndex .... I discovered that those function results cannot be used in if guards so i used case instead. This results in a nested case statement case Destination < 1 of true -> {ok,NumberOfJumps+1}; false -> case lists:flatlength(A) < Destination of true -> doSomething; false -> case lists:member(Destination,VisitedIndices) of true -> doSomething; false -> doSomethingElse end end end. I found this bad in terms of

Easy way of loading project's Rebar dependencies in Erlang shell

爷,独闯天下 提交于 2019-12-05 11:07:25
问题 I've got a project that uses Rebar as build tool. When developing, I would like all my app dependencies that are specified in Rebar.config be compiled & loaded in the shell as easy as possible. I'm using the Erlang shell in Emacs. What's a quick way to do this? 回答1: I'm not using Emacs so I may miss the Emacs-specific side of your question, but when I want an Erlang shell with all my rebar dependencies loaded, I use: erl -pa ebin deps/*/ebin 回答2: ./rebar shell should load all your

What does the “head mismatch” compiler error mean?

假装没事ソ 提交于 2019-12-05 11:06:21
问题 I tries to write code to print Z character. zzzzzzz z z z z z zzzzzzz But when I compile this code, it throws D:\erlang\graphics>erlc zeez2.erl d:/erlang/graphics/zeez2.erl:19: head mismatch d:/erlang/graphics/zeez2.erl:6: function zeez/3 undefined I can't fixed this error. I didn't find what wrong in my could. Does one please suggest me. Thank you. -module(zeez2). -export([main/0]). main() -> L = 8, zeez( false ,1, L). % line 6 zeez(true, M,M) -> init:stop(); zeez(false, M, N) -> io:format("

Erlang gen_tcp connect question

折月煮酒 提交于 2019-12-05 10:34:50
Simple question... This code .. client() -> SomeHostInNet = "localhost" % to make it runnable on one machine {ok, Sock} = gen_tcp:connect(SomeHostInNet, 5678, [binary, {packet, 0}]), ok = gen_tcp:send(Sock, "Some Data"), ok = gen_tcp:close(Sock). is very clear except that I don't quite understand what [binary, {packet,0}] means ? Any one cares to explain ? MadSeb As per the gen_tcp:connect documentation: [binary, {packet, 0}] is the list of options that's passed to the connect function. binary means that the data that is sent/received on the socket is in binary format (as opposed to, say, list

erlang mnesia - illegal record info

随声附和 提交于 2019-12-05 10:29:02
I am trying to have a function that ensures the table I need is already created and if not to create it. Here's the sample: ensure_table_exists(Table, MnesiaTables, Nodes) -> case lists:member(Table, MnesiaTables) of true -> throw({error, db_might_have_already_been_created}); false -> mnesia:create_table(Table, [{disc_copies, Nodes}, {attributes, record_info(fields, Table)}]), ok end. The issue is that when compiling I get the error: illegal record info . It might have to do that record_info is resolved at compile time or that the second argument to record info should actually be a record that

Tracing the action of consuming messages from mailbox in Erlang

♀尐吖头ヾ 提交于 2019-12-05 10:14:05
I went through the documentation of the trace/3 BIF in Erlang. However, one observation I have made is that it cannot be used for tracing the consuming of messages from the mailbox. The flag 'receive' only traces when messages are added to a process's mailbox. Is there any way one can trace events such as reading from the mailbox using the receive construct? If not, is there any reason why this isn't possible? It seems very strange that one can trace most kind of events in a program and the reading of messages from a mailbox is not traceable. There is no such tool. You can only hope for call

Erlang: What is most-wrong with this trie implementation?

左心房为你撑大大i 提交于 2019-12-05 09:57:54
Over the holidays, my family loves to play Boggle. Problem is, I'm terrible at Boggle. So I did what any good programmer would do: wrote a program to play for me. At the core of the algorithm is a simple prefix trie , where each node is a dict of references to the next letters. This is the trie:add implementation: add([], Trie) -> dict:store(stop, true, Trie); add([Ch|Rest], Trie) -> % setdefault(Key, Default, Dict) -> % case dict:find(Key, Dict) of % { ok, Val } -> { Dict, Val } % error -> { dict:new(), Default } % end. { NewTrie, SubTrie } = setdefault(Ch, dict:new(), Trie), NewSubTrie = add