Skip to content

Commit

Permalink
finished the server but untried
Browse files Browse the repository at this point in the history
  • Loading branch information
janusle committed May 3, 2012
1 parent 046add9 commit aebc916
Showing 1 changed file with 99 additions and 7 deletions.
106 changes: 99 additions & 7 deletions server/multiServer.java
@@ -1,7 +1,7 @@
import java.io.*;
import java.net.*;
import java.util.*;

import java.text.SimpleDateFormat;

class clientInfo{

Expand Down Expand Up @@ -37,38 +37,122 @@ public boolean equals( clientInfo ci ){

}

public class multiServer{

class TimeInfo{

public ArrayList<clientInfo> addresses;
public String time;


public TimeInfo( ArrayList<clientInfo> addresses, String time){

this.addresses = addresses;
this.time = time;

}


}



public class MultiServer{

private final int PORT = 40302;
private ArrayList<clientInfo> addresses = new ArrayList<clientInfo>();
private Timer timer = null;

public static void main(String []args){

new MultiServer().run(); //runing server;

}

class Sender extends TimerTask{

private String getCurrentTime(){

Calendar currentDate = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss dd:MM:yyyy");
return formatter.format( currentDate.getTime() );

}

public void run() {

if( addresses.size() > 0 ){

clientInfo ci;
InetAddress ip;
int port;

for(int i=0; i<addresses.size(); i++ ){

try{

ci = addresses.get(i); // get first client
ip = ci.getAddress();
port = ci.getPort();
Socket s = new Socket( ip, port );
PrintWriter out = new PrintWriter( s.getOutputStream(), true );
String time = this.getCurrentTime();
out.print( new TimeInfo( addresses, time ) );
out.close();
s.close();
System.out.println("Time is sent to " + ip.getHostAddress() + ":" + port );
return;
}
catch(Exception e)
{
//System.err.println("Fail to connect to " + ip.getHostAddress() + ":" + port );
continue;
}

}
System.err.println("Failed to send time to client, will resend it one minute after");

}
else{ // no subscription

System.out.println("No subscription, will try one minute after");

}

}
}
// end of inner class Sender

public void run(){

try{

ServerSocket ss = new ServerSocket(PORT);
System.out.println("Server is started...");

while(true){

Socket cs = ss.accept();
BufferedReader in = new BufferedReader( new InputStreamReader( cs.getInputStream() ) );
String line;
int index;

if ( ( line = in.readLine() ) != null ){

if( line.equals("subscribe") ){

InetAddress ia = cs.getInetAddress();
int port = cs.getPort();
clientInfo ci = new clientInfo( ia, port );
if ( ( int index = addresses.indexOf( ci ) ) == -1 ){

if ( ( index = addresses.indexOf( ci ) ) == -1 ){

addresses.add( ci );
System.out.println("New subscriber from " + ia.getHostAddress() );
if ( this.timer == null ){
timer = new Timer();
timer.schedule( new Sender(), 60 * 1000 );
System.out.println("Timer is set");
}

}
else{ //duplicated subscribe
Expand All @@ -81,7 +165,7 @@ else if ( line.equals("unsubscribe") ){

InetAddress ia = cs.getInetAddress();
int port = cs.getPort();
if ( ( int index = addresses.indexOf( new clientInfo( ia, port) )) != -1 ){ //remove client
if ( ( index = addresses.indexOf( new clientInfo( ia, port) )) != -1 ){ //remove client
addresses.remove( index );

}
Expand All @@ -91,9 +175,17 @@ else if ( line.equals("unsubscribe") ){

}
}
else{ // invalid request

InetAddress ia = cs.getInetAddress();
System.err.println("Invalid request from " + ia.getHostAddress() );

}
}


in.close();
cs.close();

}

}
Expand Down

0 comments on commit aebc916

Please sign in to comment.