Created
August 16, 2012 04:25
-
-
Save mwilc0x/3366818 to your computer and use it in GitHub Desktop.
The peer connector class
This file contains 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.InetAddress; | |
import java.net.ServerSocket; | |
import java.net.Socket; | |
import java.util.Arrays; | |
import java.util.logging.Logger; | |
/** | |
* The Class PeerUploader. | |
* | |
* This class is responsible for creating a connection with | |
* other peers by creating and running a server socket. | |
* | |
* @author Deepak, Mike, Josh | |
*/ | |
public class PeerConnect extends Thread{ | |
/** The Constant log. */ | |
public static final Logger log = Log2.getLogger(PeerUploader.class); | |
/** The peer object we will be adding to our list of peers */ | |
public Peer peer; | |
/** The is running. */ | |
public boolean isRunning = false; | |
/** 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; | |
/** the id of the peer we are connecting to */ | |
byte[] peerId; | |
/** The ip of the peer we are connecting to */ | |
String ip; | |
/** | |
* 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 handshake response here to get their info | |
InetAddress peerIP = socket.getInetAddress(); | |
ip = peerIP.toString(); | |
port = socket.getPort(); | |
peerId = getPeerID(response); | |
//Create the peer, initialize it and then add it to our list | |
peer = new Peer(peerId, port, ip, manager); | |
peer.init(); | |
manager.peers.add(peer); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
/** | |
* Grabs the byte array of the peer ID | |
* from the handshake response byte array | |
* | |
* @param byte[] the handshake byte array response | |
* | |
* @return byte[] the byte array of the peer ID | |
*/ | |
public byte[] getPeerID(byte[] response) { | |
byte[] peerID = new byte[20]; | |
System.arraycopy(response, 48, peerID, 0, 20); | |
return peerID; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment