package javaapplication29;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.net.ServerSocket;
import java.net.Socket;
public class Server
{
public static void main(String[] args) throws IOException
{
ServerSocket serverSocket = null;
boolean listening = true;
try
{
serverSocket = new ServerSocket(7777);
}
catch (IOException e)
{
System.err.println("Could not listen on port: 7777");
System.exit(1);
}
System.out.println("Multi server ready");
Socket socket = null;
while (listening)
{
try
{
socket = serverSocket.accept();
new EchoThread(socket).start();
}
catch (IOException e)
{
System.err.println("Accept failed.");
System.exit(1);
}
}
serverSocket.close();
}
}
class EchoThread extends Thread
{
// Library lib = new Library();
private Socket socket = null;
public EchoThread(Socket socket)
{
this.socket = socket;
}
public void run()
{
System.out.println("enter to run1");
Socket incomingConnection = null;
incomingConnection = socket;
ResultSet rs;
PrintWriter out;
BufferedReader in;
String inputLine = null;
try
{
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
do {
inputLine = in.readLine();
System.out.println("the input is----:"+inputLine);
String [] newinputLine = null;
newinputLine = inputLine.split("#");
System.out.println(newinputLine[0]);
// /*
// * Add Movie
// * add's new movie to database
// */
// if (newinputLine[0].matches("add movie")){
// lib.addItem(newinputLine[1],newinputLine[2],Integer.parseInt(newinputLine[3]),newinputLine[4],Integer.parseInt(newinputLine[5]),newinputLine[6],newinputLine[7],newinputLine[8],newinputLine[9],Integer.parseInt(newinputLine[10]));
// out.println(inputLine);
// }
// /*
// * Add Song
// * add's new song to database
// */
// else if (newinputLine[0].matches("add song")){
// lib.addItem(newinputLine[1],newinputLine[2],Integer.parseInt(newinputLine[3]),newinputLine[4],Integer.parseInt(newinputLine[5]),newinputLine[6],newinputLine[7],newinputLine[8],newinputLine[9],Integer.parseInt(newinputLine[10]));
// out.println(inputLine);
//
// }
// /*
// * Search by Movies Year
// * returns movie between years ("year 1" and "year 2")
// */
// else if (newinputLine[0].matches("search movies by year")){
// String ReturnString="";
//
// System.out.println(newinputLine[1]);
// rs = lib.searchByYearMovie(Integer.parseInt(newinputLine[1]),Integer.parseInt(newinputLine[2]));
// System.out.println(" first ReturnString:" + ReturnString);
// try {
// while (rs.next()) {
// ReturnString = ReturnString + rs.getString(1) + "#" + rs.getString(2) + "#" + rs.getString(3) + "#" + rs.getString(4) + "#" + rs.getString(5) + "#" + rs.getString(6) + "#" + rs.getString(7) + "#" + rs.getString(8) + "#" + rs.getString(9) + "#" + rs.getString(10) + "@";
// System.out.println("while ReturnString:" + ReturnString);
// }
// } catch (SQLException ex) {
// System.out.println("catch no resaultset");
// Logger.getLogger(EchoThread.class.getName()).log(Level.SEVERE, null, ex);
// }
// System.out.println(ReturnString);
// out.println(ReturnString);
// System.out.println("search Movies by year ends");
// }
// /*
// * Search Songs by Year
// * returns songs between years ("year 1" and "year 2")
// */
// else if (newinputLine[0].matches("search songs by year")){
//
// String ReturnString="";
// System.out.println(" first ReturnString:" + ReturnString);
// System.out.println("newinputLine[1] : "+newinputLine[1]);
// rs = lib.searchByYearSong(Integer.parseInt(newinputLine[1]),Integer.parseInt(newinputLine[2]));
//
// try {
// while (rs.next()) {
// ReturnString = ReturnString + rs.getString(1) + "#" + rs.getString(2) + "#" + rs.getString(3) + "#" + rs.getString(4) + "#" + rs.getString(5) + "#" + rs.getString(6) + "#" + rs.getString(7) + "#" + rs.getString(8) + "#" + rs.getString(9) + "#" + rs.getString(10) + "@";
// System.out.println("while ReturnString:" + ReturnString);
// }
// } catch (SQLException ex) {
// System.out.println("catch no resaultset");
// Logger.getLogger(EchoThread.class.getName()).log(Level.SEVERE, null, ex);
// }
// System.out.println(ReturnString);
//
//
// out.println(ReturnString);
// System.out.println("search Songs by year ends");
// }
// /*
// * Search movie
// * returns all the movies
// */
// else if (newinputLine[0].matches("search movie")){
// String ReturnString="";
// System.out.println(" first ReturnString:" + ReturnString);
// rs = lib.ShowAllMovies();
// try {
// while (rs.next()) {
// ReturnString = ReturnString + rs.getString(1) + "#" + rs.getString(2) + "#" + rs.getString(3) + "#" + rs.getString(4) + "#" + rs.getString(5) + "#" + rs.getString(6) + "#" + rs.getString(7) + "#" + rs.getString(8) + "#" + rs.getString(9) + "#" + rs.getString(10) + "@";
// System.out.println("while ReturnString:" + ReturnString);
// }
// } catch (SQLException ex) {
// System.out.println("catch no resaultset");
// Logger.getLogger(EchoThread.class.getName()).log(Level.SEVERE, null, ex);
// }
// System.out.println(ReturnString);
//
//
// out.println(ReturnString);
// System.out.println("search by name ends");
// }
// /*
// * Search Song
// * returns all the songs
// */
// else if (newinputLine[0].matches("search song")){
// String ReturnString="";
// System.out.println(" first ReturnString:" + ReturnString);
// rs = lib.ShowAllSongs();
//
// try {
// while (rs.next()) {
// ReturnString = ReturnString + rs.getString(1) + "#" + rs.getString(2) + "#" + rs.getString(3) + "#" + rs.getString(4) + "#" + rs.getString(5) + "#" + rs.getString(6) + "#" + rs.getString(7) + "#" + rs.getString(8) + "#" + rs.getString(9) + "#" + rs.getString(10) + "@";
// System.out.println("while ReturnString:" + ReturnString);
// }
// } catch (SQLException ex) {
// System.out.println("catch no resaultset");
// Logger.getLogger(EchoThread.class.getName()).log(Level.SEVERE, null, ex);
// }
// out.println(ReturnString);
// System.out.println("search by name ends");
// }
// /*
// * Search Movies by Category
// * returns all the movies with the same category
// */
// else if (newinputLine[0].matches("search movies by category")){
// String ReturnString="";
// System.out.println(" first ReturnString:" + ReturnString);
// System.out.println(newinputLine[1]);
// rs = lib.searchByCatogorMovie(newinputLine[1]);
//
// try {
// while (rs.next()) {
// ReturnString = ReturnString + rs.getString(1) + "#" + rs.getString(2) + "#" + rs.getString(3) + "#" + rs.getString(4) + "#" + rs.getString(5) + "#" + rs.getString(6) + "#" + rs.getString(7) + "#" + rs.getString(8) + "#" + rs.getString(9) + "#" + rs.getString(10) + "@";
// System.out.println("while ReturnString:" + ReturnString);
// }
// } catch (SQLException ex) {
// System.out.println("catch no resaultset");
// Logger.getLogger(EchoThread.class.getName()).log(Level.SEVERE, null, ex);
// }
// System.out.println(ReturnString);
// out.println(ReturnString);
// System.out.println("Search Movies by Category ends");
//
// }
// /*
// * Search Movies by Name
// * returns all the movies with the same name
// */
// else if (newinputLine[0].matches("search movies by name")){
// String ReturnString="";
// System.out.println(" first ReturnString:" + ReturnString);
// System.out.println(newinputLine[1]);
// rs = lib.searchByNameMovie(newinputLine[1]);
//
// try {
// while (rs.next()) {
// ReturnString = ReturnString + rs.getString(1) + "#" + rs.getString(2) + "#" + rs.getString(3) + "#" + rs.getString(4) + "#" + rs.getString(5) + "#" + rs.getString(6) + "#" + rs.getString(7) + "#" + rs.getString(8) + "#" + rs.getString(9) + "#" + rs.getString(10) + "@";
// System.out.println("while ReturnString:" + ReturnString);
// }
// } catch (SQLException ex) {
// System.out.println("catch no resaultset");
// Logger.getLogger(EchoThread.class.getName()).log(Level.SEVERE, null, ex);
// }
// System.out.println(ReturnString);
// out.println(ReturnString);
// }
//
// /*
// * Search Song by Name
// * returns all the song with the same name
// */
// else if (newinputLine[0].matches("search Song by name")){
// String ReturnString="";
// System.out.println(" first ReturnString:" + ReturnString);
// System.out.println(newinputLine[1]);
// rs = lib.searchByNameSong(newinputLine[1]);
//
// try {
// while (rs.next()) {
// ReturnString = ReturnString + rs.getString(1) + "#" + rs.getString(2) + "#" + rs.getString(3) + "#" + rs.getString(4) + "#" + rs.getString(5) + "#" + rs.getString(6) + "#" + rs.getString(7) + "#" + rs.getString(8) + "#" + rs.getString(9) + "#" + rs.getString(10) + "@";
// System.out.println("while ReturnString:" + ReturnString);
// }
// } catch (SQLException ex) {
// System.out.println("catch no resaultset");
// Logger.getLogger(EchoThread.class.getName()).log(Level.SEVERE, null, ex);
// }
// System.out.println(ReturnString);
// out.println(ReturnString);
// }
//
// /*
// * Search Songs by Category
// * returns all the songs with the same Category
// */
// else if (newinputLine[0].matches("search songs by category")){
//
// String ReturnString="";
// System.out.println(" first ReturnString:" + ReturnString);
// System.out.println(newinputLine[1]);
// rs = lib.searchByCatogorSong(newinputLine[1]);
//
// try {
// while (rs.next()) {
// ReturnString = ReturnString + rs.getString(1) + "#" + rs.getString(2) + "#" + rs.getString(3) + "#" + rs.getString(4) + "#" + rs.getString(5) + "#" + rs.getString(6) + "#" + rs.getString(7) + "#" + rs.getString(8) + "#" + rs.getString(9) + "#" + rs.getString(10) + "@";
// System.out.println("while ReturnString:" + ReturnString);
// }
// } catch (SQLException ex) {
// System.out.println("catch no resaultset");
// Logger.getLogger(EchoThread.class.getName()).log(Level.SEVERE, null, ex);
// }
// System.out.println(ReturnString);
// out.println(ReturnString);
// System.out.println("Search Songs by Category ends");
//
// }
// /*
// * Delite All
// * delets all movie and Song
// */
// else if (newinputLine[0].matches("delete all")){
// lib.removeAll();
// System.out.println("deta base clear");
// out.println(inputLine);
// }
// /*
// * Delite by Name
// * delets one movie by name(works on the id)
// */
// else if (newinputLine[0].matches("delete by name")){
// System.out.println("enter to delete by name" );
// lib.removeItem(newinputLine[1], Integer.parseInt(newinputLine[2]));
// out.println(inputLine);
// }
// /*
// * Send Movie
// * send the file of the movie to server
// */
// else if (newinputLine[0].matches("sends movie")){
// }
// /*
// * Send Song
// * send the file of the song to server
// */
// else if (newinputLine[0].matches("sends song")){
// }
// /*
// * Get Movie
// * download the movie from server
// */
// else if (newinputLine[0].matches("get movie")){
// }
// /*
// * Get Song
// * download the song from server
// */
// else if (newinputLine[0].matches("get song")){
// }
// /*
// * Edit Song
// * Edit Song or Movie to Data Base
// */
// else if (newinputLine[0].matches("edit items")){
// System.out.println( newinputLine[0]);
// lib.editItem(newinputLine[1],newinputLine[2],newinputLine[3],Integer.parseInt(newinputLine[4]),newinputLine[5],Integer.parseInt(newinputLine[6]),newinputLine[7],newinputLine[8],newinputLine[9],newinputLine[10],Integer.parseInt(newinputLine[11]));
// out.println(inputLine);
// }
/*
* copy file - here is the problem
* Edit Song or Movie to Data Base
*/
System.out.println( "working");
handleConnection(socket);
// out.println(inputLine);
}
while(inputLine.toUpperCase().equals("END")==false);
out.close();
in.close();
socket.close();
}
catch (IOException e)
{
System.out.println("couldn't read from connection");
}
}
/**
* Interacts with the client Socket to send the contents of the requested
* file to the client.
*
* @param incomingConnection
*/
public void handleConnection(Socket connectionToHandle) {
new Thread(new ConnectionHandler(connectionToHandle)).start();
}
}