ログインしてさらにmixiを楽しもう

コメントを投稿して情報交換!
更新通知を受け取って、最新情報をゲット!

Javaの課題丸投げコミュのJXTA

  • mixiチェック
  • このエントリーをはてなブックマークに追加
JXTA1.0で書かれているのですが、これをJXTA2.3.7でもエラーが出ないように直さなければなりません。
よろしくお願いします。


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Enumeration;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;

import net.jxta.peergroup.PeerGroup;
import net.jxta.peergroup.PeerGroupFactory;
import net.jxta.exception.PeerGroupException;
import net.jxta.discovery.DiscoveryService;
import net.jxta.discovery.DiscoveryListener;
import net.jxta.discovery.DiscoveryEvent;
import net.jxta.protocol.DiscoveryResponseMsg;
import net.jxta.document.Advertisement;
import net.jxta.protocol.PipeAdvertisement;
import net.jxta.document.AdvertisementFactory;
import net.jxta.document.MimeMediaType;
import net.jxta.pipe.PipeService;
import net.jxta.pipe.InputPipe;
import net.jxta.pipe.OutputPipe;
import net.jxta.pipe.PipeMsgListener;
import net.jxta.pipe.PipeMsgEvent;
import net.jxta.endpoint.Message;
import net.jxta.endpoint.MessageElement;
import net.jxta.id.IDFactory;


/**
* 簡単なチャットアプリケーション
*/
public class SimpleJxtaChat extends JFrame implements DiscoveryListener, PipeMsgListener, ActionListener {

private static final int TIME_TO_WAIT = 1000;
private static final String PIPE_NAME = "JxtaChatPipe";
private static final String MSG_TAG_SENDER = "Sender";
private static final String MSG_TAG_MESSAGE = "Message";

private JTextArea textArea;
private JButton okBtn;
private JTextField textField;

// NetPeerGroup
private PeerGroup netPeerGroup;

// DiscoveryService
private DiscoveryService discSvc;

// PipeService
private PipeService pipeSvc;

// Inputパイプ
private InputPipe inputPipe;

// Outputパイプ
private OutputPipe outputPipe;

// コンストラクタ
public SimpleJxtaChat( String title ) {

setTitle(title);
setBounds( 10, 10, 300, 200);

textArea = new JTextArea();
textArea.setEditable(false);
JScrollPane pane = new JScrollPane(textArea);
JButton okBtn = new JButton("OK");
okBtn.addActionListener(this);

textField = new JTextField( "", 20 );
JPanel panel = new JPanel();
panel.add(textField);
panel.add(okBtn);

getContentPane().setLayout(new BorderLayout());
getContentPane().add(pane, BorderLayout.CENTER);
getContentPane().add(panel, BorderLayout.SOUTH);
}

// メインルーチン
public static void main( String[] args ) {

SimpleJxtaChat app = new SimpleJxtaChat("SimpleJxtaChat");

app.addWindowListener( new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0);
}
} );

app.startJxta();

if( args.length > 0 ) {
if( args[0].equals("true") )
app.initJxtaChat(true);
}
else {
app.initJxtaChat(false);
}

app.setVisible(true);
}

// JXTAの開始
public void startJxta() {

try {
netPeerGroup = PeerGroupFactory.newNetPeerGroup();
setTitle( "SimpleJxtaChat - " + netPeerGroup.getPeerName() );
}
catch(PeerGroupException e) {
System.out.println("cannot create NetPeerGroup.");
e.printStackTrace();
System.exit(1);
}
}

// チャットアプリケーションの初期化
public void initJxtaChat( boolean createAdv ) {

discSvc = netPeerGroup.getDiscoveryService();

try {
// アドバタイズメントを削除
discSvc.flushAdvertisements( null, DiscoveryService.ADV );
}
catch( IOException ioe ) {
ioe.printStackTrace();
System.err.println("error while flushing the advertisements.");
}

if(createAdv) {
createPipeAdv();
}
else {
// パイプのアドバタイズメントを検索
discSvc.getRemoteAdvertisements( null
, DiscoveryService.ADV
, null
, null
, 5
, this );
}
}

// パイプのアドバタイズメントを作成
public void createPipeAdv() {

// パイプのアドバタイズメントを作成
PipeAdvertisement pipeAdv = (PipeAdvertisement)AdvertisementFactory.newAdvertisement(
PipeAdvertisement.getAdvertisementType() );

// パイプ名をセット
pipeAdv.setName(PIPE_NAME);

// パイプIDをセット
pipeAdv.setPipeID( IDFactory.newPipeID( netPeerGroup.getPeerGroupID() ) );

// パイプ種別をセット
pipeAdv.setType( PipeService.PropagateType );

// アドバタイズメントを公開
try {
discSvc.publish( pipeAdv, DiscoveryService.ADV );
}
catch( IOException e ) {
e.printStackTrace();
}
discSvc.remotePublish( pipeAdv, DiscoveryService.ADV );

// パイプを作成
createPipes(pipeAdv);
}

// パイプを作成
public void createPipes( PipeAdvertisement pipeAdv ) {

if (pipeAdv != null) {
// NetPeerGroupからパイプサービスを取得
pipeSvc = netPeerGroup.getPipeService();

// InputパイプおよびOutputパイプを作成
try {
inputPipe = pipeSvc.createInputPipe( pipeAdv, this );
outputPipe = pipeSvc.createOutputPipe( pipeAdv, TIME_TO_WAIT );
}
catch( IOException ioe ) {
System.err.println("error while creating input or output pipe.");
}
}
}

// discoveryEventメソッドの実装
public void discoveryEvent( DiscoveryEvent evt ) {

try{
PipeAdvertisement pipeAdv = null;
DiscoveryResponseMsg response = evt.getResponse();
Enumeration en = response.getResponses();

while ( en.hasMoreElements() ) {
// レスポンスからアドバタイズメントを取得
String strAdv = (String)en.nextElement();
InputStream is = new ByteArrayInputStream(strAdv.getBytes());

Advertisement adv = AdvertisementFactory.newAdvertisement( new MimeMediaType("text/xml"), is );
if( adv instanceof PipeAdvertisement ) {
pipeAdv = (PipeAdvertisement)adv;
if(pipeAdv != null) {
if( pipeAdv.getName().equals(PIPE_NAME) && pipeAdv.getType().compareTo(PipeService.PropagateType) == 0 ) {
createPipes(pipeAdv);
}
}
}

}
}
catch( Exception e ){
System.out.println("error while executing discoveryEvent method.");
}
}

// メッセージを送信
public void sendMsg( String message ) {

String sender = netPeerGroup.getPeerName();
Message msg = pipeSvc.createMessage();
MessageElement messageElement = null;

try{

// 送信者名追加
messageElement = msg.newMessageElement( MSG_TAG_SENDER
, new MimeMediaType( "text/plain" )
, new ByteArrayInputStream( sender.getBytes() ) );
msg.addElement( messageElement );

// メッセージ本体追加
messageElement = msg.newMessageElement( MSG_TAG_MESSAGE
, new MimeMediaType( "text/plain" )
, new ByteArrayInputStream( message.getBytes() ) );
msg.addElement( messageElement );

// メッセージ送信
outputPipe.send(msg);
}
catch( IOException ioe ) {
System.err.println("error while sending a message.");
}

}

// pipeMsgEventメソッドの実装
public void pipeMsgEvent( PipeMsgEvent evt ) {

String sender = "";
String msg = "";

try {
// PipeMsgEventオブジェクトからMessageオブジェクトを取得
Message message = evt.getMessage();

Enumeration names = message.getNames();

// 送信者とメッセージをXML要素から取得
while (names.hasMoreElements()) {
String name = (String)names.nextElement();
if(name.compareTo(MSG_TAG_SENDER) == 0) {
InputStream inputStream = (message.getElement(name)).getStream();
byte[] bytes = new byte[inputStream.available()];
inputStream.read(bytes);
sender = new String(bytes);
}
else if(name.compareTo(MSG_TAG_MESSAGE) == 0) {
InputStream inputStream = (message.getElement(name)).getStream();
byte[] bytes = new byte[inputStream.available()];
inputStream.read(bytes);
msg = new String(bytes);
}
}

// メッセージを表示
textArea.append( "<"+ sender +">: " + msg + "\n" );
}
catch( IOException ioe ) {
ioe.printStackTrace();
System.err.println("error while reading a message");
}
}

// チャットアプリケーションを終了
public void stopChatApp() {

// InputPipeを閉じる
inputPipe.close();

System.exit(0);
}

// actionPerformedメソッドの実装
public void actionPerformed( ActionEvent evt ) {

if( evt.getActionCommand().equals("OK") ) {
if( outputPipe != null ) {
sendMsg( textField.getText() );
textField.setText("");
}
}
else {
if( inputPipe != null )
stopChatApp();
}
}

}

コメント(0)

mixiユーザー
ログインしてコメントしよう!

Javaの課題丸投げ 更新情報

Javaの課題丸投げのメンバーはこんなコミュニティにも参加しています

星印の数は、共通して参加しているメンバーが多いほど増えます。

人気コミュニティランキング