Search code examples
erlangpid-controller

Erlang-Pid control


I have written a simple chat server in Erlang (without any sockets or ports, just between to message among multiple shells), but when I try to simulate it I have some problems. Almost every client function (like pm, say_to_all) in my implementation needs Chat_server-s Process ID. If I open chat_server and client in one shell, I can easily bound chat_server's process ID and access it if necessary, but problem comes up when I want to open another shell for client. look at the picture --> http://s018.radikal.ru/i501/1308/ee/a194aa8486ae.png how to access the process from 1-st shell (chat_server) from second shell (chat_client) ?


Solution

  • You could register your server globally under a certain name (http://erlang.org/doc/man/global.html#register_name-2). That way it would be possible for you to access the server from any shell within your chatsystem.

    Don't forget, that you need to connect the shells with net_adm:ping first, to let the shells know of globally registered names.

    And I can really recommend looking into gen_server (http://www.erlang.org/doc/man/gen_server.html) since it can really help when trying to organize a client-server-structure.

    Edit: Sorry maybe you also want an explanation for your problem. This is because every erlang-shell has its own environment with own variables etc. That means a second shell does not know about any variables of other shells.