int main(int argc, char *argv[])
{
	Display *disp = new Display();
  title("Server", '=');
  try
  {
	  string client_port;
	  if (argc > 1)
	  {
		  client_port = argv[1];
	  }
	int clientport=atoi(client_port.c_str());
    Verbose v(true);
    SocketSystem ss;
    SocketListener sl(clientport,Socket::IP6);
    ClientHandler cp;
    sl.start(cp);
    Verbose::show("press key to exit: ", always);
    std::cout.flush();
    std::cin.get();
  }
  catch (std::exception& ex)
  {
	  disp->Exeception(ex);
  }
}
// Writes to the file in the server..
string Receiver::writeToFile(std::string filemsg, bool type,Socket &si)
{
	Display *disp = new Display();
	Message *m = new Message();
	char filestart = m->getFileStart(filemsg);
	string filename = m->getFileName(filemsg),fullpath= "../UploadedFolder/" + filename,msgbody = m->getmsgBody(filemsg),msgstr="";
	std::ofstream o;
	try
	{
		if (type == true)
		{
			if (filestart == '1')
			{
				o.open(fullpath);
				o << msgbody << endl;
			}
			else if (filemsg.find("Body:endfile") != string::npos)
			{
				o.close();
				string uploadpath = "../UploadedFolder/" + m->getFileName(filemsg);
				disp->show("\n**************************************************\n");
				disp->show("Requirement 1\n");
				string uploadstring = "Recieved File: " + m->getFileName(filemsg)+" And stored at path: " +uploadpath;
				disp->show(uploadstring);
				string pmsg = "Sending Acknowledgement to Client with address: " + m->getsendAddr(filemsg);
				disp->show(pmsg);
				disp->show("\n**************************************************\n");
				msgstr = setMessage(filemsg);
			}
			else
			{
				o.open(fullpath,ios::app);
				if (o.is_open())
					o << msgbody << endl;
			}

		}
		return msgstr;
	}
	catch (exception ex)
	{
		disp->Exeception(ex);
		return "";
	}
}
// Starts the receiver
string Receiver::start(string msg1,Socket &si)
{
	try
		{
			    Message *msgobj = new Message();
				msgobj->setMessage(msg1);
				string msgstr="";
				if (msg1.find("command:uploadfile") != std::string::npos)
					msgstr=writeToFile(msg1, type,si);
				if (msg1.find("command:download") != std::string::npos)
					msgstr=downloadMessage(msg1, msgobj,si);
				else if (msg1.find("command:filesearch") != std::string::npos)
					msgstr = filesearchMessage(msg1, msgobj, si);
				else if (msg1.find("command:textsearch") != std::string::npos)
				{
					string result = "";
					result = performTextSearch(msg1, result);
					msgstr = result;
					//si.sendString(result);
				}
				else if (msg1.find("command:retrieve_categories") != std::string::npos)
				{
					string result = "";
					result=performRetrieveCategories(msg1,result);
					msgstr = result;
					//si.sendString(result);
				}
				else if (msg1.find("command:retrieve_files") != std::string::npos)
				{
					string result = "";
					result = performRetrieveFiles(msg1, result);
					msgstr = result;
					//si.sendString(result);
				}
				return msgstr;
		}

		catch (std::exception& ex)
		{
			Display *disp = new Display();
			disp->Exeception(ex);
			return "";
		}
		
}