IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)

JXTA Project

  1. Sa mira
    Sa mira
    Bonsoir,
    j'ai vérifié le code plusieurs fois et j'ai pas trouvé l’erreur!!

    public class JxtaServerPipeExample2 extends javax.swing.JFrame {

    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration


    PeerGroup netPeerGroup = null;
    PipeAdvertisement pipeAdv;
    JxtaServerPipe serverPipe;
    private final static String SenderMessage = "pipe_tutorial";
    private JxtaBiDiPipe pipe;
    private Message msg;


    public JxtaServerPipeExample2() throws IOException {
    super("Bienvennue sur notre application de chat vous etes sur le coté serveur");
    initComponents();
    }


    @SuppressWarnings("unchecked")

    private void initComponents() {

    jTextField1 = new javax.swing.JTextField();
    jLabel1 = new javax.swing.JLabel();
    jButton1 = new javax.swing.JButton();
    jScrollPane1 = new javax.swing.JScrollPane();
    jTextArea1 = new javax.swing.JTextArea();
    jLabel2 = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);



    jLabel1.setFont(new java.awt.Font("Tahoma", 3, 24)); // NOI18N
    jLabel1.setForeground(new java.awt.Color(255, 0, 51));
    jLabel1.setText("HI, I'm a Client");

    jButton1.setText("ENVOYER");
    jButton1.setFont(new java.awt.Font("Comic Sans MS", Font.ITALIC, 24)); // NOI18N
    jButton1.setBackground(Color.blue);
    jButton1.setForeground(Color.white);

    jButton1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jButton1ActionPerformed(evt);
    }
    });

    jTextArea1.setColumns(20);
    jTextArea1.setRows(5);
    jTextArea1.setBackground(Color.red);
    jTextArea1.setForeground(Color.white);
    jTextArea1.setFont(new Font("Script MT Bold", Font.ITALIC,20));
    jScrollPane1.setViewportView(jTextArea1);

    jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("n.jpg"))); // NOI18N

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(100, 100, 100)
    .addComponent(jLabel1))
    .addGroup(layout.createSequentialGroup()
    .addGap(10, 10, 10)
    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 360, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGroup(layout.createSequentialGroup()
    .addGap(118, 118, 118)
    .addComponent(jButton1))
    .addGroup(layout.createSequentialGroup()
    .addGap(10, 10, 10)
    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 360, javax.swing.GroupLayout.PREFERRED_SIZE)))
    .addContainerGap(417, Short.MAX_VALUE))
    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(52, 52, 52)
    .addComponent(jLabel1)
    .addGap(102, 102, 102)
    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGroup(layout.createSequentialGroup()
    .addGap(134, 134, 134)
    .addComponent(jButton1))
    .addGroup(layout.createSequentialGroup()
    .addGap(87, 87, 87)
    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)))
    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
    );

    pack();
    }


    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

    Messenger out;
    Date date = new Date(System.currentTimeMillis());
    try {
    out = pipe.getMessenger();
    msg = new Message();
    msg.addMessageElement(null,new StringMessageElement(SenderMessage, jTextField1.getText(),null));
    System.out.println("Sending back 'Hello World'");
    out.sendMessage(msg);
    jTextArea1.setText(jTextArea1.getText() + "\n" + date.toString() + ":" + jTextField1.getText());
    jTextField1.setText("");
    jTextField1.setBackground(Color.red);
    jTextField1.setForeground(Color.white);
    jTextField1.setFont(new Font("Script MT Bold", Font.ITALIC,20));

    } catch (IOException ex) {
    Logger.getLogger(JxtaServerPipeExample2.class.getName()).log(Level.SEVERE, null, ex);
    }

    }


    public static void main(String args[]) throws IOException {

    final JxtaServerPipeExample2 eg = new JxtaServerPipeExample2();
    eg.startJxta();
    invokeLater(new Runnable() {
    public void run() {
    eg.setVisible(true);
    }
    });

    System.out.println("Reading in pipe.adv");
    try {
    FileInputStream is = new FileInputStream("C:\\Users\\Samira\\Desktop\\JXTA_Project\\Client\\pipe.adv");
    eg.pipeAdv = (PipeAdvertisement) AdvertisementFactory.newAdvertisement(MimeMediaType.XMLUTF8, is);
    is.close();
    eg.serverPipe = new JxtaServerPipe(eg.netPeerGroup, eg.pipeAdv);
    // we want to block until a connection is established
    //eg.serverPipe.setPipeTimeout(0);
    } catch (Exception e) {
    System.out.println("failed to read/parse pipe advertisement");
    e.printStackTrace();
    System.exit(-1);
    }
    // run on this thread
    eg.run();
    }

    private void receiveAndSendTestMessage(JxtaBiDiPipe pipe) {
    try {
    this.pipe = pipe;
    Message msg = pipe.getMessage(120000);
    // get the message element named SenderMessage
    MessageElement msgElement = msg.getMessageElement(null, SenderMessage);
    // Get message
    if (msgElement.toString() == null) {
    System.out.println("null msg received");
    } else {
    Date date = new Date(System.currentTimeMillis());
    System.out.println("Message received at :" + date.toString());
    System.out.println("Message created at :" + msgElement.toString());
    jTextArea1.setText(jTextArea1.getText() + "\n" + date.toString() + ":" + msgElement.toString());
    }


    } catch (Exception ie) {
    ie.printStackTrace();
    }
    }



    public void run() {

    System.out.println("starting ServerPipe");
    JxtaBiDiPipe bipipe = null;
    try {
    bipipe = serverPipe.accept();
    System.out.println("jjjjj");
    } catch (IOException ex) {
    Logger.getLogger(JxtaServerPipeExample2.class.getName()).log(Level.SEVERE, null, ex);
    }
    while (true) {
    try {

    if (bipipe != null) {
    System.out.println("BiDi Pipe created");
    receiveAndSendTestMessage(bipipe);
    }
    } catch (Exception e) {
    e.printStackTrace();
    return;
    }
    }
    }


    private void startJxta() {
    try {
    // create, and Start the default jxta NetPeerGroup
    netPeerGroup = PeerGroupFactory.newNetPeerGroup();
    } catch (PeerGroupException e) {
    // could not instanciate the group, print the stack and exit
    System.out.println("fatal error : group creation failure");
    e.printStackTrace();
    System.exit(1);
    }
    }


    }
Affichage des résultats 1 à 1 sur 1
Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo