Browse Source

Finished initial revision of single server

server
Thomas Johnson 3 years ago
parent
commit
c28b7d8e64
  1. 5
      server/EchoServer.java
  2. 238
      server/SingleServer.java

5
server/EchoServer.java

@ -10,6 +10,7 @@ import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Arrays;
public class EchoServer
{
@ -102,7 +103,7 @@ class EchoHandler implements ProtoHandler
}
try
{
dispatch.giveUserListing("echo_" + username, names);
dispatch.requestUserList("echo_" + username);
} catch (Exception e) {}
}
@ -112,7 +113,7 @@ class EchoHandler implements ProtoHandler
System.out.println("listing request " + username);
try
{
dispatch.requestUserList("echo_" + username);
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) {}
}

238
server/SingleServer.java

@ -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);
}
}
Loading…
Cancel
Save