3 changed files with 391 additions and 1 deletions
@ -0,0 +1,151 @@ |
|||
package server; |
|||
|
|||
import proto.ProtoHandler; |
|||
import proto.ProtoIn; |
|||
import proto.ProtoOut; |
|||
|
|||
import java.io.DataInputStream; |
|||
import java.io.DataOutputStream; |
|||
import java.io.IOException; |
|||
import java.net.ServerSocket; |
|||
import java.net.Socket; |
|||
import java.util.ArrayList; |
|||
import java.util.Arrays; |
|||
|
|||
public class EchoServer |
|||
{ |
|||
public EchoServer(int port) |
|||
{ |
|||
ServerSocket ss; |
|||
try |
|||
{ |
|||
ss = new ServerSocket(port); |
|||
} catch (IOException e) |
|||
{ |
|||
e.printStackTrace(); |
|||
return; |
|||
} |
|||
while (true) |
|||
{ |
|||
try |
|||
{ |
|||
Socket sock = ss.accept(); |
|||
System.out.println("accepted"); |
|||
DataInputStream dis = new DataInputStream(sock.getInputStream()); |
|||
DataOutputStream dos = new DataOutputStream(sock.getOutputStream()); |
|||
ProtoOut pos = new ProtoOut(dos); |
|||
new ProtoIn(dis, new EchoHandler(pos)).init(); |
|||
} catch (IOException e) |
|||
{ e.printStackTrace(); } |
|||
} |
|||
} |
|||
|
|||
public static void main(String args[]) |
|||
{ |
|||
int port = 25519; |
|||
if (args.length > 0) |
|||
{ |
|||
try |
|||
{ |
|||
port = Integer.parseInt(args[0]); |
|||
} catch (Exception e) {} |
|||
} |
|||
new EchoServer(port); |
|||
} |
|||
} |
|||
|
|||
class EchoHandler implements ProtoHandler |
|||
{ |
|||
private ProtoOut dispatch; |
|||
|
|||
public EchoHandler(ProtoOut dispatch) |
|||
{ |
|||
this.dispatch = dispatch; |
|||
} |
|||
|
|||
@Override |
|||
public void handleError(String username, byte[] msg) |
|||
{ |
|||
System.out.println("error " + username); |
|||
try |
|||
{ |
|||
dispatch.sendError("echo_" + username, msg); |
|||
} catch (Exception e) {} |
|||
} |
|||
|
|||
@Override |
|||
public void handleJoin(String username) |
|||
{ |
|||
System.out.println("join " + username); |
|||
try |
|||
{ |
|||
dispatch.sendJoin("echo_" + username); |
|||
} catch (Exception e) {} |
|||
} |
|||
|
|||
@Override |
|||
public void handlePart(String username) |
|||
{ |
|||
System.out.println("part " + username); |
|||
try |
|||
{ |
|||
dispatch.sendPart("echo_" + username); |
|||
} catch (Exception e) {} |
|||
} |
|||
|
|||
@Override |
|||
public void handleListing(String username, ArrayList<String> names) |
|||
{ |
|||
System.out.println("listing " + username); |
|||
for (String n: names) |
|||
{ |
|||
System.out.println(" " + n); |
|||
} |
|||
try |
|||
{ |
|||
dispatch.requestUserList("echo_" + username); |
|||
} catch (Exception e) {} |
|||
} |
|||
|
|||
@Override |
|||
public void handleListingRequest(String username) |
|||
{ |
|||
System.out.println("listing request " + username); |
|||
try |
|||
{ |
|||
dispatch.giveUserListing("echo_" + username, new ArrayList<String>(Arrays.asList(username, "echo_" + username, "you", "me", "Clint Eastwood", "Ben Shapiro", "The Libs", "Facts", "Logic"))); |
|||
} catch (Exception e) {} |
|||
} |
|||
|
|||
@Override |
|||
public void handleMessage(String username, String message) |
|||
{ |
|||
System.out.println("message " + username + ": " + message); |
|||
try |
|||
{ |
|||
dispatch.sendMessage("echo_" + username, message); |
|||
} catch (Exception e) {} |
|||
} |
|||
|
|||
@Override |
|||
public void handleFile(String username, String filename, byte[] data) |
|||
{ |
|||
System.out.println("file " + username + " " + filename); |
|||
try |
|||
{ |
|||
dispatch.sendFile("echo_" + username, filename, data); |
|||
} catch (Exception e) {} |
|||
} |
|||
|
|||
@Override |
|||
public void handleUnknownCommand(int length, byte id, String username) |
|||
{ |
|||
System.out.println("Unknown command " + id + " from " + username); |
|||
} |
|||
|
|||
@Override |
|||
public void handleClose() |
|||
{ |
|||
System.out.println("closed"); |
|||
} |
|||
} |
@ -0,0 +1,238 @@ |
|||
package server; |
|||
|
|||
import proto.ProtoHandler; |
|||
import proto.ProtoIn; |
|||
import proto.ProtoOut; |
|||
|
|||
import java.io.DataInputStream; |
|||
import java.io.DataOutputStream; |
|||
import java.io.IOException; |
|||
import java.net.ServerSocket; |
|||
import java.net.Socket; |
|||
import java.util.ArrayList; |
|||
import java.util.HashSet; |
|||
import java.util.Set; |
|||
import java.util.stream.Collectors; |
|||
|
|||
public class SingleServer |
|||
{ |
|||
Set<ClientHandler> clients; |
|||
|
|||
SingleServer(int port) |
|||
{ |
|||
ServerSocket ss; |
|||
this.clients = new HashSet<ClientHandler>(); |
|||
try |
|||
{ |
|||
ss = new ServerSocket(port); |
|||
} catch (IOException e) |
|||
{ |
|||
e.printStackTrace(); |
|||
return; |
|||
} |
|||
while (true) |
|||
{ |
|||
try |
|||
{ |
|||
Socket sock = ss.accept(); |
|||
System.out.println("accepted"); |
|||
DataInputStream dis = new DataInputStream(sock.getInputStream()); |
|||
DataOutputStream dos = new DataOutputStream(sock.getOutputStream()); |
|||
ProtoOut pos = new ProtoOut(dos); |
|||
ProtoIn pin = new ProtoIn(dis, new ClientHandler(pos, clients)); |
|||
pin.init(); |
|||
} catch (IOException e) |
|||
{ e.printStackTrace(); } |
|||
} |
|||
} |
|||
|
|||
public static void main(String args[]) |
|||
{ |
|||
int port = 25519; |
|||
if (args.length > 0) |
|||
{ |
|||
try |
|||
{ |
|||
port = Integer.parseInt(args[0]); |
|||
} catch (Exception e) {} |
|||
} |
|||
new SingleServer(port); |
|||
} |
|||
} |
|||
|
|||
class ClientHandler implements ProtoHandler |
|||
{ |
|||
public String username; |
|||
public ProtoOut out; |
|||
private Set<ClientHandler> fellows; |
|||
|
|||
public ClientHandler(ProtoOut out, Set<ClientHandler> fellows) |
|||
{ |
|||
this.username = null; |
|||
this.fellows = fellows; |
|||
this.fellows.add(this); |
|||
this.out = out; |
|||
} |
|||
|
|||
@Override |
|||
public void handleError(String username, byte[] msg) |
|||
{ |
|||
checkName(username); |
|||
} |
|||
|
|||
@Override |
|||
public void handleJoin(String username) |
|||
{ |
|||
if (updateName(username)) |
|||
{ |
|||
synchronized(fellows) |
|||
{ |
|||
for (ClientHandler fellow: fellows) |
|||
{ |
|||
if (fellow != this && fellow.username != null) |
|||
try |
|||
{ |
|||
fellow.out.sendJoin(username); |
|||
} catch (IOException e) |
|||
{ } |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public void handlePart(String username) |
|||
{ |
|||
if (this.username == username) |
|||
{ |
|||
this.username = null; |
|||
synchronized(fellows) |
|||
{ |
|||
for (ClientHandler fellow : fellows) |
|||
{ |
|||
if (fellow != this && fellow.username != null) |
|||
try |
|||
{ |
|||
fellow.out.sendPart(username); |
|||
} catch (IOException e) |
|||
{ |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public void handleListing(String username, ArrayList<String> names) |
|||
{ |
|||
// who did this? who broke the protocol? |
|||
checkName(username); |
|||
} |
|||
|
|||
@Override |
|||
public void handleListingRequest(String username) |
|||
{ |
|||
if (checkName(username)) { |
|||
try |
|||
{ |
|||
synchronized (fellows) |
|||
{ |
|||
out.giveUserListing(username, fellows.stream().map((f) -> f.username).filter((x) -> x != null).collect(Collectors.toList())); |
|||
} |
|||
} catch (IOException e) {} |
|||
|
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public void handleMessage(String username, String message) |
|||
{ |
|||
if (checkName(username)) |
|||
{ |
|||
synchronized(fellows) |
|||
{ |
|||
for (ClientHandler fellow: fellows) |
|||
{ |
|||
if (fellow != this && fellow.username != null) |
|||
try |
|||
{ |
|||
fellow.out.sendMessage(username, message); |
|||
} catch (IOException e) |
|||
{ } |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public void handleFile(String username, String filename, byte[] data) |
|||
{ |
|||
if (checkName(username)) |
|||
{ |
|||
synchronized(fellows) |
|||
{ |
|||
for (ClientHandler fellow: fellows) |
|||
{ |
|||
if (fellow != this && fellow.username != null) |
|||
try |
|||
{ |
|||
fellow.out.sendFile(username, filename, data); |
|||
} catch (IOException e) |
|||
{ } |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public void handleUnknownCommand(int length, byte id, String username) |
|||
{ |
|||
if (checkName(username)) |
|||
{ |
|||
try |
|||
{ |
|||
out.sendError(username, new byte[0]); |
|||
} catch (IOException e) {} |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public void handleClose() |
|||
{ |
|||
if (this.username != null) |
|||
{ |
|||
synchronized(fellows) |
|||
{ |
|||
this.username = null; |
|||
for (ClientHandler fellow : fellows) |
|||
{ |
|||
if (fellow != this && fellow.username != null) |
|||
try |
|||
{ |
|||
fellow.out.sendPart(username); |
|||
} catch (IOException e) |
|||
{ |
|||
} |
|||
} |
|||
} |
|||
} |
|||
this.fellows.remove(this); |
|||
} |
|||
|
|||
private boolean updateName(String username) |
|||
{ |
|||
if (this.username == null) |
|||
{ |
|||
this.username = username; |
|||
return true; |
|||
} |
|||
return checkName(username); |
|||
} |
|||
|
|||
private boolean checkName(String username) |
|||
{ |
|||
if (this.username == null) |
|||
return false; |
|||
return this.username.equals(username); |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue