Commit 12f87b02 authored by Matti Pulkkinen's avatar Matti Pulkkinen
Browse files

merged directory structure

parent f19a2478
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>dtek1066-ryhmis</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
package fi.utu.dtek1066;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
System.out.println("yo");
SocketHandler socketHandler = null;
StreamHandler streamHandler = null;
try {
socketHandler = new SocketHandler();
} catch (IOException e) {
System.err.println("Failed to create SocketHandler object.");
e.printStackTrace();
System.exit(1);
}
try {
streamHandler = socketHandler.openStreamHandler();
} catch (IOException e) {
System.err.println("Failed to open StreamHandler.");
e.printStackTrace();
System.exit(1);
}
int t = 0;
try {
t = streamHandler.readInt();
System.err.println("Received t = " + t);
} catch (IOException e) {
System.err.println("Failed to read int through ObjectInputStream.");
e.printStackTrace();
}
if (t < 2 || t > 10) {
System.err.println("Illegal value t, sending response...");
try {
streamHandler.writeInt(-1);
} catch (IOException e) {
System.out.println("Failed to send response for illegal value.");
e.printStackTrace();
System.exit(2);
}
System.err.println("Response sent, terminating...");
System.exit(2);
}
}
}
//Testi
package fi.utu.dtek1066;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
// TODO generalize the interface so that a connection can be opened by giving an address and a
// remote port;
public class SocketHandler {
private static final int REMOTEPORT = 3126;
private static final int LOCALPORT = 6666;
private static final int TIMEOUTMILLIS = 5000;
private static final int MAXRETRIES = 5;
private static final InetAddress REMOTEHOST = bytesToAddress(new byte[] {127, 0, 0, 1});
/**
* Turns an array of bytes into an InetAddress.
*
* @param bytes The array to be converted.
* @return The InetAddress which is the result of this conversion, or null if the conversion
* fails.
*/
private static InetAddress bytesToAddress(byte[] bytes) {
try {
return InetAddress.getByAddress(bytes);
} catch (UnknownHostException ex) {
System.err.println("Failed in creating InetAddress");
ex.printStackTrace();
System.exit(3);
}
return null;
} // bytesToAddress(byte[])
private ServerSocket serverSocket;
private Socket socket;
/**
* Creates a new SocketHandler.
*
* @throws IOException If creating the SocketHandler fails.
*/
public SocketHandler() throws IOException {
this.serverSocket = new ServerSocket(LOCALPORT);
this.serverSocket.setSoTimeout(TIMEOUTMILLIS);
} // SocketHandler()
/**
* Closes the resources handled by this SocketHandler.
*
* @throws IOException If closing either the Socket or the ServerSocket fails.
*/
public void close() throws IOException {
if (!this.socket.isClosed()) {
this.socket.close();
}
if (!this.serverSocket.isClosed()) {
this.serverSocket.close();
}
} // close()
/**
* Uses this SocketHandler to create a StreamHandler object that contains the streams from this
* SocketHandler's socket.
*
* @throws IOException if opening one of the sockets fails.
*/
public StreamHandler openStreamHandler() throws IOException {
int timesRetried = 0;
do {
this.sendPortNumber(REMOTEHOST, REMOTEPORT, LOCALPORT);
try {
this.socket = this.serverSocket.accept();
try {
this.socket.setSoTimeout(TIMEOUTMILLIS);
} catch (SocketException e) {
System.err.println("Failed in setting timeout for socket.");
e.printStackTrace();
}
return new StreamHandler(new ObjectInputStream(this.socket.getInputStream()),
new ObjectOutputStream(this.socket.getOutputStream()));
} catch (SocketTimeoutException ex) {
System.err.println("Socket timed out, trying again...");
timesRetried++;
}
} while (timesRetried < MAXRETRIES);
if (timesRetried == MAXRETRIES) {
System.err.println("Failed to receive connection");
System.exit(1);
}
return null;
} // openStreamHandler()
/**
* Sends the port number localPort to the port remotePort at remoteHost
*
* @param remoteHost The machine to which the port number needs to be sent
* @param remotePort The port on remoteHost to send to
* @param localPort The local port number to be sent to remoteHost
* @throws IOException If the transmission fails
*/
private void sendPortNumber(InetAddress remoteHost, int remotePort, int localPort) {
try (DatagramSocket socket = new DatagramSocket(0)) {
String portNumber = Integer.toString(localPort);
byte[] portHolder = portNumber.getBytes();
DatagramPacket request =
new DatagramPacket(portHolder, portHolder.length, remoteHost, remotePort);
socket.send(request);
} catch (SocketException e) {
System.err.println("Failed in opening DatagramSocket.");
e.printStackTrace();
} catch (IOException e) {
System.err.println("Failed in sending request packet through UDP");
e.printStackTrace();
}
} // sendPort(InetAddress, int, int)
} // class SocketHandler
package fi.utu.dtek1066;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class StreamHandler {
private ObjectInputStream ois;
private ObjectOutputStream oos;
/**
* Packs the parameters ois and oos into a StreamHandler object.
*
* @param ois The ObjectInputStream to be placed in this object.
* @param oos The ObjectOutputStream to be placed in this object.
*/
public StreamHandler(ObjectInputStream ois, ObjectOutputStream oos) {
this.ois = ois;
this.oos = oos;
} // StreamHandler(ObjectInputStream, ObjectOutputStream)
/**
* Closes the streams handled by this object.
*
* @throws IOException If closing one of the streams fails.
*/
void close() throws IOException {
this.ois.close();
this.oos.close();
} // close()
/**
* Reads an int from the input stream contained in this object.
*
* @return The int read from the stream.
* @throws IOException If reading from the stream fails.
*/
int readInt() throws IOException {
return this.ois.readInt();
} // readInt()
/**
* Write an int to the output stream contained in this object.
*
* @param n The int to be written out.
* @throws IOException If writing to the stream fails.
*/
void writeInt(int n) throws IOException {
this.oos.writeInt(n);
this.oos.flush();
} // writeInt(int)
} // class StreamHandler
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment