Created
August 16, 2012 04:03
-
-
Save mwilc0x/3366672 to your computer and use it in GitHub Desktop.
peer connection class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.DataInputStream; | |
import java.io.DataOutputStream; | |
import java.io.IOException; | |
import java.net.ServerSocket; | |
import java.net.Socket; | |
import java.util.Arrays; | |
import java.util.logging.Logger; | |
/** | |
* The Class PeerUploader. | |
* | |
* @author Deepak, Mike, Josh | |
*/ | |
public class PeerConnect extends Thread{ | |
/** The Constant log. */ | |
public static final Logger log = Log2.getLogger(PeerUploader.class); | |
/** Server connection for the peer to connect to */ | |
public ServerSocket peerConnectSocket; | |
/** Socket we accept connection on */ | |
public Socket socket; | |
/** port to use to connect to the peer */ | |
public int port; | |
/** The manager */ | |
Manager manager; | |
/** The output stream we will be writing our data on */ | |
DataOutputStream os; | |
/** Input stream to read data on */ | |
DataInputStream is; | |
/** | |
* Instantiates a new peer connection. | |
* | |
* @param peer the peer | |
* @param manager the manager | |
*/ | |
PeerConnect(int port, Manager manager){ | |
this.port = port; | |
this.manager = manager; | |
try { | |
peerConnectSocket = new ServerSocket(port); | |
RUBTClient.log("Established server socket with peer on port " + port); | |
} catch (Exception e) { | |
RUBTClient.log("Couldn't establish server socket with peer on port " + port); | |
} | |
} | |
/* (non-Javadoc) | |
* @see java.lang.Thread#run() | |
*/ | |
public void run(){ | |
try { | |
socket = peerConnectSocket.accept(); | |
is = new DataInputStream(socket.getInputStream()); | |
os = new DataOutputStream(socket.getOutputStream()); | |
os.write(Peer.generateHandshake(manager.peerId, manager.torrentInfo.info_hash.array())); | |
os.flush(); | |
byte[] response = new byte[68]; | |
this.socket.setSoTimeout(10000); | |
is.readFully(response); | |
this.socket.setSoTimeout(130000); | |
log.info("Handshake Response: " + Arrays.toString(response)); | |
//Parse the response here to get all the peer info | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment