Document

Peer-to-Peer Connections
using JXTA
情報科学部ディジタルメディア学科
n00k1042 山崎 剛
Peer-to-peer Connections
PDP: Peer Discovery Protocol


他のpeerへリソースなど(Advertisement)を公開
公開されたAdvertisementの取得
PBP: Pipe Binding Protocol




Pipeは他のpeerとのコミュニケーションに使用
Pipe Advertisementの公開(publish)
Pipe Advertisementの検索・獲得(discovery/get)
入出力Pipeの作成
1. アドバータイズメント公開
Peer
Pipe
Adv.
2. アドバータイズメント検索
3.応答からPipe Adv.取得
4.Pipe入出力でMessageやり取り
(Peer間で共通)
Pipe
Peer
Peer-to-peer Connections(Cont.)
1. アドバータイズメント作成 ( Pipe Advertisementを公開するpeerのみ )

Get Discovery Service from NetPeerGroup


Creating Pipe Advertisement


discSvc = netPeerGourp.getDiscoveryService();
PipeAdvertisement pipeAdv= (PipeAdvertisement)
AdvertisementFactory.newAdvertisement(PipeAdvertisement.getAdvertise
mentType);
publish Pipe Advertisement (Local/Remote)


discSvc.publish(pipeAdv, DiscoveryService.ADV);
discSvc.remotePublish(pipeAdv, DiscoveryService.ADV);
1. アドバータイズメント公開(publish)
Peer
Pipe
Adv.
Peer
Peer-to-peer Connections(Cont.)
2. アドバータイズメント検索 ( Pipe Advertisementを公開するpeerのみ )
Get Discovery Service from NetPeerGroup

discSvc = netPeerGourp.getDiscoveryService();

Discovering Pipe Advertisement (Remote:相手のPeerのPipeAdv.検索)

discSvc.getRemoteAdvertisements(null, DiscoveryService.ADV, null, null, 5, this);

3. 応答からPipeAdvertisementを取得

Creating Pipe – Get Pipe Advertisement from response

Peer
public void discoveryEvent(DiscoveryEvent evt) {
DiscoveryResponseMsg res = evt.getResponse(); // 応答の取得
…
// 応答がPipeAdvertisementだった場合
if (adv instanceof PipeAdvertisement)
pipeAdv = (PipeAdvertisement) adv;
}
2. アドバータイズメント検索
(Discovery)
Pipe
Adv.
3.応答からPipe Adv.取得
Pipe
Peer
Peer-to-peer Connections(Cont.)
4. 入出力Pipeの作成 (両方のPeerで行う)



Creating Pipe(input/output)

pipeSvc = netPeerGroup.getPipeService();

inputPipe = pipeSvc.createInputPipe(pipeAdv,this);
(このクラスはPipeMsgListenerをインプリメントする必要がある)

outputPipe = pipeSvc.createOutputPipe(pipeAdv,TIME_TO_WAIT);
Send Message

Message msg = pipeSvc.createMessage(); …

outputPipe.send(msg);
Receive Message (inputPipeのPipeMsgEventを受け取る)

public void pipeMsgEvent(PipeMsgEvent evt)
{ Message msg= evt.getMessage(); … }
Peer
Pipe 4.Pipe入出力でMessageやり取り
Pipe
Adv.
Peer
Example:Stream Socket Connections
(TCP:transmission control protocol)
CloseWindowAndExit.java
The basic life cycle of a server is:
ServerTCP.java
•A creation of a ServerSocket
-- ServerSocket server = new ServerSocket(5000,100);
•Listen for incoming connection
-- Socket connection = server.accept();
•Receive data
-- DataInputStream input = new DataInputStream(connection.getInputStream());
•Send data
-- DataOutputStream output = new DataOutputStream(connection.getOutputStream());
input.readUTF();
output.writeUTF(“connected!”);
•Close the connection
-- connection.close();
•Wait for the next connection.
What a client does:
ClientTCP.java
•Connect to a remote machine -- Socket client = new Socket(InetAddress.getLocalHost(),5000);
•Receive data
-- DataInputStream input = new DataInputStream(client.getInputStream());
input.readUTF();
•Send data
-- DataOutputStream output = new DataOutputStream(client.getOutputStream());
output.writeUTF(“Thanks!”);
•Close a connection
-- client.close();
Socket connection
Output.writeUTF
Client
Input.readUTF(
)
Input.readUTF(
)
Output.writeUTF
Socket client
5000
Server