📄 jxtasocketlinuxwifi.java
字号:
package edu.uiuc.cs.cs327.linuxwifi.services;
import java.io.*;
import net.jxta.document.AdvertisementFactory;
import net.jxta.document.MimeMediaType;
import net.jxta.exception.PeerGroupException;
import java.io.FileInputStream;
import net.jxta.peergroup.PeerGroup;
import net.jxta.peergroup.PeerGroupFactory;
import net.jxta.protocol.PipeAdvertisement;
import net.jxta.socket.JxtaSocket;
import edu.uiuc.cs.cs327.linuxwifi.app.*;
/**
* Utilize the JxtaSocket by Reading in socket adv
* and attempts to bind to a JxtaServerSocket
* Passes a MusicProfile as a Serialized object
*/
public class JxtaSocketLinuxwifi extends Thread{
private PeerGroup netPeerGroup = null;
private PipeAdvertisement pipeAdv;
private JxtaSocket socket=null;
private String masterName = "Pearl Jam";
private MusicProfile searchProfile;
private WifiMessage outGoingMessage;
private AppAPI _appAPI;
public JxtaSocketLinuxwifi (WifiMessage msg, PipeAdvertisement pa, PeerGroup pg, AppAPI app)
{
pipeAdv = pa;
netPeerGroup = pg;
outGoingMessage = msg;
_appAPI=app;
}
public void sendTestMessage (JxtaSocket socket) {
if(socket != null)
{
try {
if(outGoingMessage.getmessageType() == WifiMessage.FileTransferRequest)
{
((FileTransferRequest)outGoingMessage).getFileInfo().setPipeAdvertisement(null);
}
System.out.println("Sending test message");
// get the socket output stream
ObjectOutputStream out = new ObjectOutputStream (socket.getOutputStream());
// send the searchProfile MusicProfile Object over the socket
System.out.println("sending search request");
out.writeObject(outGoingMessage);
out.flush();
out.close();
System.out.println("search request sent");
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
boolean eof=false;
while( (!eof) && (in != null) )
{
// this call should block until bits are avail.
try{
WifiMessage wifiResponse = (WifiMessage)in.readObject();
if(wifiResponse.getmessageType() == WifiMessage.SearchResponse)
{
SearchResponse sr = (SearchResponse) wifiResponse;
FileInfo fi = sr.getFileInfo();
System.out.println( "requester got: " + fi.getFileName() );
fi.setPipeAdvertisement(pipeAdv);
_appAPI.fireFileInfoReceivedEvent(fi);
}
else if(wifiResponse.getmessageType() == WifiMessage.FileTransferResponse)
{
System.out.println("Recvd File Transfer Response");
FileTransferResponse ftr = (FileTransferResponse) wifiResponse;
System.out.println("Requester: Filename: " + ftr.getFileName());
FileInfo response = new FileInfo("incoming.mp3",
// Should be replaced by the download file path in the config screen
// f.getCanonicalPath(),
"c:\\linuxwifi\\recieve.mp3",
true);
System.out.println("Requester created a fileinfo");
response.saveFile(ftr.getBytes());
System.out.println("Requester saved the file");
/*
InputStream in = socket.getInputStream();
int read;
byte [] headerBuf = new byte[headerSize];
// this call should block until bits are avail.
read = in.read(headerBuf, 0, headerSize);
System.out.println("received "+read+" bytes");
downloadFileInfo.setFileStatus(10);
FileTransferResponseHeader ftrh = new FileTransferResponseHeader(headerBuf);
byte [] payloadBuf = new byte[ftrh.PayLoadSize()];
read = in.read(payloadBuf, 0, ftrh.PayLoadSize());
System.out.println("received "+read+" bytes");
in.close();
out.close();
socket.close();
FileTransferResponse ftrsp = new FileTransferResponse(downloadFileInfo.getFileName(), payloadBuf);
downloadFileInfo.saveFile(ftrsp.getBytes());
downloadFileInfo.setFileStatus(100);
downloadFileInfo.setFileState(FileInfo.COMPLETED);
*/
}
}
catch (Exception e) {
System.out.println("failed to read in search response");
e.printStackTrace();
eof = true;
}
}
} catch (IOException ie) {
ie.printStackTrace();
}
}
}
/**
* wait for msgs
*/
public void run() {
try{
JxtaSocket js = new JxtaSocket(getnetPeerGroup(), getpipeAdv());
System.out.println("created socket");
setsocket( js );
sendTestMessage(socket);
}
catch (Exception e) {System.out.println("ha ha ha");}
}
public void setProfile(String name)
{
masterName = name;
}
/**
* Starts jxta
*/
public 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);
}
}
/*GET & SET methods for (JxtaServerSocket) serverSocket attribute*/
public JxtaSocket getsocket(){
return socket;
}
public void setsocket(JxtaSocket s){
socket = s;
}
/*GET & SET methods for (PipeAdvertisement) pipeadv attribrute*/
public PipeAdvertisement getpipeAdv(){
return pipeAdv;
}
public void setpipeAdv(PipeAdvertisement p){
pipeAdv = p;
}
/*GET & SET methods for (PipeAdvertisement) pipeadv attribrute*/
public PeerGroup getnetPeerGroup(){
return netPeerGroup;
}
public void setnetPeerGroup(PeerGroup g){
netPeerGroup = g;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -