Source Code

Here You can find lots of example of how use the H-N2N framewor in order to built your applications.

First of all, lets start with a "Hello World" example. In this example lets create a simple client-server application, a chat. You can download this source in a eclipse project at download page.

Step 1 - Creating a Client

Client.java

import br.lavid.hn2n.client.UserClient;
import br.lavid.hn2n.net.Package;
 
public class Client {
 
    public static void main(String args[]) {
        UserClient userClient = new UserClient("localhost",3693);
 
        while(!userClient.isConected());
        userClient.getCommunication().getConnectionOfControl().attach(new ClientProcess());
 
        System.out.println("###############################");
        System.out.println("#          ChibaChat          #");
        System.out.println("###############################");
        String msg;
        do {
            System.out.println("# ");
             msg = LeTeclado.leString();
 
            Package pacote = new Package(1000,new MChat(msg));
            userClient.getCommunication().sendMessage(pacote);
        } while(!msg.equals("sair"));
    }
}

Step 2 - Creating a Server

Server.java

import br.lavid.hn2n.server.DefaultServer;
 
public class Server {
 
    public static void main(String args[]) {
        DefaultServer server = new DefaultServer("localhost",3693);
        ServerProcess process = new ServerProcess();
        server.addSlaveProcess(process, -1);
        server.start();
    }
}

Step 3 - Creating a Message

MChat.java

import br.lavid.hn2n.net.Message;
 
public class MChat extends Message {
 
    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 1L;
 
    private String msg;
 
    public MChat(String msg) {
        this.msg = msg;
    }
 
    public String getMsg() {
        return msg;
    }
 
    public void setMsg(String msg) {
        this.msg = msg;
    }
 
}

Step 4 - Creating a Message Processor

ClientProcess.java

import br.lavid.hn2n.client.UserClient;
import br.lavid.hn2n.net.CommunicationLink;
import br.lavid.hn2n.net.LinkTCP;
import br.lavid.hn2n.net.Package;
import br.lavid.hn2n.process.LinkObserver;
 
public class ClientProcess implements LinkObserver{
 
    public boolean update(CommunicationLink link, Package pacote) {
        UserClient userClient = (UserClient)((LinkTCP)link).getSlaveNetProcess();
        String msg = ((MChat)pacote.getContent()).getMsg();
        if(!msg.equals("RECEBIDA")) {
            System.out.println("- "+msg);
            Package pck = new Package(1000,new MChat("RECEBIDA"));
            userClient.getCommunication().sendMessage(pck);
        }else
            System.out.println("- "+msg);
        return false;
    }
 
}

ServerProcess.java

import br.lavid.hn2n.net.CommunicationLink;
import br.lavid.hn2n.net.LinkTCP;
import br.lavid.hn2n.net.Package;
import br.lavid.hn2n.process.LinkObserver;
import br.lavid.hn2n.server.SlaveServer;
 
public class ServerProcess implements LinkObserver {
 
    public boolean update(CommunicationLink link, Package pacote) {
        SlaveServer slave = (SlaveServer)((LinkTCP)link).getSlaveNetProcess();
        System.out.println("Servidor recebeu:"+((MChat)pacote.getContent()).getMsg());
        slave.getGroupServer().relay(pacote, slave);
        return false;
    }
 
}

Step 5 - Creating a Class to read from keyboard

import java.io.BufferedReader;
import java.io.InputStreamReader;
 
/**
 * Classe para le uma String do teclado
 * @author aquiles
 *
 */
public class LeTeclado {
 
    /**
     * Metodo para ler a string do teclado
     * @return
     */
    public static String leString() {
        try {
            BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
            return b.readLine();
        }catch(Exception e ) {
            e.printStackTrace();
        }
        return null;
    }
 
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.