C-Borg Tutorial.


  • Primitive Chat Application In C-Borg
    1. The Idea
    2. The Server
    3. The Client
    4. Demonstration



    Back to general Turtorial



    1. The Idea
    2. The idea is to make a very primitive chat-application that uses a remote function call to send and receive message. The application consists of a Server and a number of Clients.
      The Clients all login on this server and then send messages to it.
      The Server remembers wich clients are logged in and sends the messages to all these clients.

      There is no error-detection, nor will agents that are gone be logged out.

      There are 2 files used : server.cborg and client.cborg (use save link as)

      Good Luck !

    3. The Server
    4. The Server is implemented as follows :

      
            or(p,q):p(true,q);
            not(p):p(false,true);
      
            users[10]:void;
            nbrusers:0;
      
            login(remotedict):: {
                nbrusers := nbrusers + 1;
                users[nbrusers] := remotedict;
                chatsend("User" + text(nbrusers) + " logged in") };
      
            chatsend(message):: {
                for(i:1,or(not(is_void(users[i])), i < nbrusers), i:=i+1,
                     {tmp : users[i]; tmp.chatreceive(message)} )  }    

      First we define a the logical functions or and not. We need these in our program and they did not seem to exist.
      They are defined as in the logical functions section of the syllabus.

      Next 2 variables are defined. nbrusers counts the number of users currently logged in, users[10] defines an array in wich the references to the remote agents will be put.

      There are 2 functions : login(remotedict) and chatsend(message)
      The login(remotedict) function is used to register the client in the chatserver. It is used only 1 time upon connecting to the chatserver.
      chatsend(message) is used to send messages. The function is called remotly and sends the message to all the clients.
      It uses a method on the client side (wich is also remotly called) to deliver the message. This function is : chatreceive(message).

    5. The Client
    6. The implementation of the Client :

         
         chatreceive(message):display(message);
         a:remotedict("Master/server");
         a->login(agentself);    

      The chatreceive(message) function is used in the chatserver.
      We first create a reference to the server and call it a.
      Next we create a link to our-self and tell the server to login with this reference to yourself.
      All done, you can send messages using a.chatsend("Your message here")

    7. Demonstration
    8. How does it work ?

      First we start the Master Agent System. On this system we will load the Chatserver. So you need to open the server.cborg in this agent system and then you need to evaluate the entire document. You will get something like :
      Fig 1.1 Master Agent System with Chatserver.
      Next we need to create a client on an agent system. This could be on this system or on an other system. Anyways, we will get on with the action.
      Fig 1.2 Sub Agent System with Chatclient.
      You need to evaluate this code. You will get a message that you are logged in, and then you can use

      
               a.chatsend("Your message here")    


    © 2000 Jannes Pockele jpockele@vub.ac.be