-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Encapsulated socket handling for Media Server/Client. MediaServer and…
… MediaClient now ready for LAN testing
- Loading branch information
1 parent
ed277c5
commit b655e0e
Showing
13 changed files
with
312 additions
and
263 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.mercurys.almostfinished; | ||
|
||
import com.mercurys.threads.ReadMediaThread; | ||
|
||
import java.io.*; | ||
import java.net.Socket; | ||
import java.util.Scanner; | ||
|
||
public class ClientSocketHandler { | ||
|
||
private Socket socket; | ||
private DataOutputStream outputStream; | ||
private ReadMediaThread incomingReaderThread; | ||
private MessageSender messageSender; | ||
|
||
public ClientSocketHandler(String address, int port) throws IOException { | ||
connectToServer(address, port); | ||
initialiseIO(); | ||
} | ||
|
||
private void connectToServer(String address, int port) throws IOException { | ||
this.socket = new Socket(address, port); | ||
System.out.println("Connected to server! [" + this.socket.getRemoteSocketAddress() + "]"); | ||
} | ||
|
||
private void initialiseIO() throws IOException { | ||
this.outputStream = new DataOutputStream(this.socket.getOutputStream()); | ||
PrintWriter writer = new PrintWriter(this.outputStream, true); | ||
messageSender = new MessageSender(new Scanner(System.in), writer, outputStream); | ||
} | ||
|
||
public void talkToServer() throws IOException { | ||
initialiseReaderThread(); | ||
messageSender.sendMessagesToOtherUser(); | ||
closeConnection(); | ||
} | ||
|
||
private void initialiseReaderThread() throws IOException { | ||
incomingReaderThread = new ReadMediaThread(this.socket.getInputStream(), | ||
"M_Host"); | ||
incomingReaderThread.start(); | ||
} | ||
|
||
private void closeConnection() throws IOException { | ||
incomingReaderThread.exitThread(); | ||
this.socket.close(); | ||
this.outputStream.close(); | ||
System.out.println("Closing connection... Goodbye!"); | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.mercurys.almostfinished; | ||
|
||
import java.io.IOException; | ||
import java.util.Scanner; | ||
|
||
public class MediaClient { | ||
|
||
ClientSocketHandler clientSocketHandler; | ||
|
||
private MediaClient(final String address, final int port) { | ||
try { | ||
clientSocketHandler = new ClientSocketHandler(address, port); | ||
} catch (final IOException u) { | ||
u.printStackTrace(); | ||
} | ||
} | ||
|
||
public static void main(final String[] args) { | ||
final MediaClient client = new MediaClient(getHostAddress(), 4444); | ||
client.startTalking(); | ||
System.out.println("Thank you for using Project Mercurys!"); | ||
} | ||
|
||
private static String getHostAddress() { | ||
final Scanner sc = new Scanner(System.in); | ||
System.out.println("Enter server host address [IP] or press 1 for localhost:"); | ||
final String hostAddress = sc.next(); | ||
return (hostAddress.equals("1"))? "192.168.0.151" : hostAddress; | ||
} | ||
|
||
private void startTalking() { | ||
try { | ||
clientSocketHandler.talkToServer(); | ||
} catch (final IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.mercurys.almostfinished; | ||
|
||
import java.io.IOException; | ||
|
||
public class MediaServer { | ||
|
||
private ServerSocketHandler serverSocketHandler; | ||
|
||
private MediaServer(final int port) { | ||
try { | ||
serverSocketHandler = new ServerSocketHandler(port); | ||
} catch (final IOException e) { | ||
System.out.println("Exception occurred!"); | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public static void main(final String[] args) { | ||
final MediaServer mediaServer = new MediaServer(4444); | ||
mediaServer.startTalking(); | ||
System.out.println("Thank you for using Project Mercurys!"); | ||
} | ||
|
||
private void startTalking() { | ||
try { | ||
serverSocketHandler.talkToClient(); | ||
} catch (final IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.mercurys.almostfinished; | ||
|
||
import com.mercurys.encryption.Encryption; | ||
|
||
import java.io.*; | ||
import java.util.Scanner; | ||
|
||
public class MessageSender { | ||
private final Scanner scanner; | ||
private final Encryption encryption = new Encryption(); | ||
private final PrintWriter writer; | ||
private final DataOutputStream outputStream; | ||
|
||
public MessageSender(Scanner scanner, PrintWriter writer, DataOutputStream outputStream) { | ||
this.scanner = scanner; | ||
this.writer = writer; | ||
this.outputStream = outputStream; | ||
} | ||
|
||
public void sendMessagesToOtherUser() throws IOException { | ||
String outGoingLine; | ||
while (!(outGoingLine = scanner.nextLine()).equals("-x-")) { | ||
sendMessage(outGoingLine); | ||
} | ||
writer.println(new Encryption().encrypt("Connection closed by server.")); | ||
} | ||
|
||
public void sendMessage(String outGoingLine) throws IOException { | ||
if (outGoingLine.startsWith("/image")) { | ||
this.handleImageUpload(outGoingLine.substring(7)); | ||
} else { | ||
writer.println(encryption.encrypt(outGoingLine)); | ||
} | ||
} | ||
|
||
public void handleImageUpload(final String imageFileName) throws IOException { | ||
ImageHandler imageHandler = new ImageHandler(outputStream); | ||
writer.println(new Encryption().encrypt("/image incoming!")); | ||
imageHandler.sendImageFileAsByteStream(imageFileName); | ||
System.out.println("[M. Console]: Image sent!"); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.mercurys.almostfinished; | ||
|
||
import com.mercurys.threads.ReadMediaThread; | ||
|
||
import java.io.*; | ||
import java.net.*; | ||
import java.util.Scanner; | ||
|
||
public class ServerSocketHandler { | ||
|
||
private final ServerSocket serverSocket; | ||
private Socket socket; | ||
private DataOutputStream outputStream; | ||
private MessageSender messageSender; | ||
private ReadMediaThread incomingReaderThread; | ||
|
||
public ServerSocketHandler(int serverPort) throws IOException { | ||
serverSocket = new ServerSocket(); | ||
initialiseServerSocket(serverPort); | ||
acceptClientRequest(); | ||
initialiseIO(); | ||
} | ||
|
||
private void initialiseServerSocket(int serverPort) throws IOException { | ||
String hostAddress = "192.168.0.151"; //replace with one's own LAN IP address | ||
serverSocket.bind(new InetSocketAddress(hostAddress, serverPort)); | ||
System.out.println(""" | ||
Server initiated. | ||
Waiting for client..."""); | ||
} | ||
|
||
private void acceptClientRequest() throws IOException { | ||
this.socket = serverSocket.accept(); | ||
System.out.println("Client accepted!"); | ||
} | ||
|
||
private void initialiseIO() throws IOException { | ||
this.outputStream = new DataOutputStream(this.socket.getOutputStream()); | ||
PrintWriter writer = new PrintWriter(this.outputStream, true); | ||
messageSender = new MessageSender(new Scanner(System.in), writer, outputStream); | ||
} | ||
|
||
public void talkToClient() throws IOException { | ||
initialiseReaderThread(); | ||
messageSender.sendMessagesToOtherUser(); | ||
closeConnection(); | ||
} | ||
|
||
private void initialiseReaderThread() throws IOException { | ||
incomingReaderThread = new ReadMediaThread(this.socket.getInputStream(), | ||
"M_Client"); | ||
incomingReaderThread.start(); | ||
} | ||
|
||
private void closeConnection() throws IOException { | ||
incomingReaderThread.exitThread(); | ||
this.socket.close(); | ||
this.outputStream.close(); | ||
System.out.println("Closing connection... Goodbye!"); | ||
} | ||
} |
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
Oops, something went wrong.