int invokeFileEng(char *connectionId,char *filename){
  int success ;
  int sockfd ;
  int conid = atoi(connectionId);
  sockfd = getSockDscpConn(conid);
  
  success =fileProcess(sockfd,filename,conid);


  //get the socket id and send to the file processing program 
 
}
  /*Get the delimited data and save it in a strcture in the memory
    The data that reaches the client or server is processed in this 
    method */
  void processCmd(char *rcvddata,int socket,int bytes_rcvd){
    
    char *ipdata;
    char *arg;
    char *iprecdata;
    char *argz;
    char *field;
    int strdlen = strlen(rcvddata);
    int strreclen ;
    ipdata =(char *)malloc((strdlen + 1)* sizeof(char)); 
    strcpy(ipdata,rcvddata) ;
    int count = 0;
    int idatacnt = 0;
    char *dataarr[50];
    char *tokens[3];
    int leng;
    int ifunction ;
    int iIndex;
    int oIndex = 0 ;
    //char hport[];
    int iLoop;

    //printf("\n Entry point data:%s",ipdata);
    arg = strtok(ipdata,rdelimiter);

    while(arg){
     //printf("\n Entry point2:%s",arg);
     if(strcmp(arg,"REG") == 0){

      ifunction = REGISTER_CMD ;
      //clear the list and build new
      clearClientList(&headClientNode,0);
      
    }else if(strcmp(arg,"CON") == 0){

      ifunction =  CONNECT_CMD ;

    }else if(strcmp(arg,"UP") == 0){

      ifunction = UPLOAD_CMD ;
      oIndex = 0 ;

    }else if(strcmp(arg,"DOW") == 0){

      ifunction = DOWNLOAD_CMD ;

    }else if(strcmp(arg,"E1")== 0){

      printf("--------File error : File not found at remote peer");
 
    }

    if(ifunction == CONNECT_CMD && count == 1){

      
      char ips[INET6_ADDRSTRLEN] ;
      char *rip = ips ;
      updateConnctdPort(socket,arg,&headConnectdNode,rip);
      printf("\n-------Connected to Peer :%s",ips);

    }

    if(ifunction == UPLOAD_CMD){
     strreclen = strlen(arg); 
     dataarr[oIndex] = (char *)malloc((strreclen + 1)* sizeof(char));
     strcpy(dataarr[oIndex],arg);
     //printf("\n Entry point4:%s",dataarr[oIndex]);
     oIndex++ ;

    }

    if(ifunction == DOWNLOAD_CMD){
     strreclen = strlen(arg);
     dataarr[oIndex] = (char *)malloc((strreclen + 1)* sizeof(char));
     strcpy(dataarr[oIndex],arg);
     oIndex++ ;
    }

    if(ifunction == REGISTER_CMD ){
    strreclen = strlen(arg);
    //iprecdata = (char *)malloc((strreclen + 1)* sizeof(char));
    //strcpy(iprecdata,arg);
    dataarr[oIndex] = (char *)malloc((strreclen + 1)* sizeof(char));
    strcpy(dataarr[oIndex],arg);
    //printf("\n Entry point4:%s",dataarr[oIndex]);
      
      oIndex++ ;
    }

    count++ ;
    arg = strtok(NULL,rdelimiter) ;
  }
if(ifunction == REGISTER_CMD){
  for(iLoop = 0;iLoop < oIndex;iLoop++){

    //printf("Row data is:%s",dataarr[iLoop]);
    char *rec = strtok(dataarr[iLoop],fdelimiter);
      //rec = strtok(dataarr[iLoop],fdelimiter);
    iIndex = 0 ;
    while(rec){

      tokens[iIndex] = strdup(rec);
      if(iIndex == 4){
        //Call method to add to the clients to server list
        //printf("\n %s, %s, %s,%s",tokens[0],tokens[1],tokens[2],tokens[3]);
        addClientstoList(&headClientNode,tokens[0],tokens[1],tokens[2],atoi(tokens[3]),1);
          
      }
      iIndex++ ;
      rec = strtok(NULL,fdelimiter);

    }

  }
}
    
   if(ifunction == REGISTER_CMD){
     listactpeers(0,headClientNode,0);
   }

   if(ifunction == UPLOAD_CMD){

    //call multiple receive
    //fileReceive1(dataarr[1],dataarr[2],socket);
    if(oIndex ==3){

      dataarr[oIndex] = (char *)malloc((strreclen + 1)* sizeof(char));
      strcpy(dataarr[oIndex],"\0");
    }    
    fileReceive(dataarr[1],dataarr[2],dataarr[3],socket,bytes_rcvd);
    //printf("\n  THe sample data is%s :%s :%s",dataarr[1],dataarr[2],dataarr[3]);
   }
   
   
   if(ifunction == DOWNLOAD_CMD){
     
    int connectId = getConnectionId(socket);
    int success = 0;
    success = fileProcess(socket,dataarr[1],connectId);

    //error in reading file send it to client
    if(success == 0){

     send(socket,"E1@@@E",7,0);

    }

   } 

   if(ifunction == CONNECT_CMD){
     
          
   }
}
Пример #3
0
void browser::tagParsing(WCHAR* message, HDC hdc, HWND mainhWnd, int yPos)
{
	WCHAR tempIp[MAXLEN] = { '\0', };
	WCHAR tempPort[MAXLEN] = { '\0', };

	if (ip != NULL && port != NULL)
	{
		lstrcpyW(tempIp, ip);
		lstrcpyW(tempPort, port);
	}
	initialize();
	if (ip != NULL && port != NULL)
	{
		lstrcpyW(ip, tempIp);
		lstrcpyW(port, tempPort);
	}

	unsigned int i = 0;
	int tagFlag = 0; //tag시작을 알림
	int outTagFlag = 0; //태그의 종료를 알리는 변수
	int headerFlag = 0;
	int tempBodyFlag = 0; //body안에서 tag의 시작을 알림
	int bodyFlag = 0; //body의 시작을 알림
	int fontHeight = 0;
	int styleFlag = 0;
	
	int htempX = 0;
	int htempY = 0;

	WCHAR htempUri[MAXLEN] = { '\0', };
	WCHAR* tempMessage = NULL;
	wstring fileName = TEXT("");
	wstring tempTag = TEXT(""); //tag내용 저장
	wstring body = TEXT(""); //tag사이의 내용 담기
	wstring hTag[] = { TEXT("<h1>"), TEXT("<h2>"), TEXT("<h3>"), TEXT("<h4>"), TEXT("<h5>"), TEXT("<h6>") };
	RECT rect = {0, };
	HFONT hFont = 0, oldFont = 0;

	GetClientRect(mainhWnd, &rect);

	x = rect.left;
	y = rect.top;

	y += 100 - yPos;



	//메시지만큼 배열 생성
	tempMessage = (WCHAR*)malloc(sizeof(WCHAR) * (lstrlenW(message) + 1));

	memset(tempMessage, '\0', (lstrlenW(message) + 1));

	lstrcpyW(tempMessage, message);

	//에러일 경우 
	if (lstrcmpW(tempMessage, TEXT("HTTP/1.1 404 Not Found")) == 0)
	{
		TextOut(hdc, x, y, tempMessage, lstrlenW(tempMessage));
		return;
	}
	else if (lstrcmpW(tempMessage, TEXT("HTTP/1.1 400 Bad Request")) == 0)
	{
		TextOut(hdc, x, y, tempMessage, lstrlenW(tempMessage));
		return;
	}
	
	while (1)
	{
		//body가 나왔을때의 동작 정의
		if (tempMessage[i] == '<' && bodyFlag == 1)
		{
			tempTag += tempMessage[i];
			i++;
			tagFlag = 1;
			continue;
		}

		if (tempMessage[i] == '>' && bodyFlag == 1)
		{
			tempTag += tempMessage[i];
			if (outTagFlag == 0) // </~>이 아닐때
			{
				/////////////////////////////////////////////////////////////////////
				const WCHAR* p = NULL;
				p = wcsstr(tempTag.c_str(), TEXT("img src"));
				/* 이미지 파일 이름 찾기*/
				if (p != NULL)
				{
					//파일이 나왔을 때 그 전 바디 내용 출력
					
					TextOut(hdc, x, y, body.c_str(), body.length());

					x += (body.length() * 10);

					int pivot = 0;
					for (int j = 0; tempTag[j] != NULL; j++)
					{
						if (tempTag[j] != '\"' && pivot == 1)
						{
							fileName += tempTag[j];
						}
						if (tempTag[j] == '\"' && pivot == 0)
						{
							j++;
							fileName += tempTag[j];
							pivot = 1;
						}
						else if (tempTag[j] == '\"' && pivot == 1)
						{
							break;
						}
					}
					//파일 처리 함수
					fileProcess(fileName, hdc);

					//변수 초기화
					fileName = TEXT("");
					body = TEXT("");
					tempTag = TEXT("");
					tagFlag = 0;
					i++;
					continue;
				}
				/////////////////////////////////////////////////////////////////
				

				////////////////////////////////////////////////////////////////
				p = wcsstr(tempTag.c_str(), TEXT("form action"));
				if (p != NULL) //form action 일 때
				{
					WCHAR formTag[MAXLEN] = { '\0', };
					lstrcpyW(formTag, tempTag.c_str());
					p = wcstok(formTag, TEXT("\""));
					p = wcstok(NULL, TEXT("\""));
					printf("%s\n", p); //파일
					p = wcstok(NULL, TEXT("\""));
					p = wcstok(NULL, TEXT("\""));
					printf("%s\n", p); //메시지
				}
				////////////////////////////////////////////////////////////////

				////////////////////////////////////////////////////////////////

				p = wcsstr(tempTag.c_str(), TEXT("input type"));
				if (p != NULL)
				{
					
					TextOut(hdc, x, y, body.c_str(), body.length());

					WCHAR inputTag[MAXLEN] = { '\0', };
					lstrcpyW(inputTag, tempTag.c_str());
					p = wcstok(inputTag, TEXT("="));
					p = wcstok(NULL, TEXT(" "));//edit
					p = wcstok(NULL, TEXT("\""));
					p = wcstok(NULL, TEXT("\""));//name
				}


				//////////////////////////////////////////////////////


				p = wcsstr(tempTag.c_str(), TEXT("a href"));
				if (p != NULL)
				{
					TextOut(hdc, x, y, body.c_str(), body.length());

					x += (body.length() * 10);

					WCHAR inputTag[MAXLEN] = { '\0', };
					lstrcpyW(inputTag, tempTag.c_str());
					p = wcstok(inputTag, TEXT("\""));
					p = wcstok(NULL, TEXT("\""));

					htempX = x;
					htempY = y;					
					wcscpy(htempUri, p);

					tagStack.push(TEXT("<a>"));
				}

				//////////////////////////////////////////////
				//h~태그일때 h 확인

				
				for (int j = 0; j < sizeof(hTag) / sizeof(hTag[0]); j++)
				{
					if (lstrcmpW(tempTag.c_str(), hTag[j].c_str()) == 0) //h태그라 판단
					{
						switch (j)
						{
						case 0:
							fontHeight = 40;
							break;
						case 1:
							fontHeight = 35;
							break;
						case 2:
							fontHeight = 30;
							break;
						case 3:
							fontHeight = 25;
							break;
						case 4:
							fontHeight = 20;
							break;
						case 5:
							fontHeight = 15;
							break;
						}
						hFont = CreateFont(fontHeight, 0, 0, 0, 0, 0, 0, 0, DEFAULT_CHARSET, 0, 0, 0,
							VARIABLE_PITCH | FF_ROMAN, TEXT("Impect"));
						oldFont = (HFONT)SelectObject(hdc, hFont);

						tagStack.push(tempTag);
					}
				}
				
				//<br>, <h~>, ...등등 일 때 정의
				if (lstrcmpW(tempTag.c_str(), TEXT("<br>")) == 0)
				{
					TextOut(hdc, x, y, body.c_str(), body.length());
					x = rect.left;
					y += 30;
				}
				
				body = TEXT("");
				tempTag = TEXT("");
				outTagFlag = 0;
				tagFlag = 0;
				i++;
				continue;

			}
			else//새로나온 태그가 end tag일 때
			{
				//</br>, </body> 일 때 정의
				if (lstrcmpW(tempTag.c_str(), TEXT("<br>")) == 0)
				{
					
					TextOut(hdc, x, y, body.c_str(), body.length());
					y += 30;
					tempTag = TEXT("");
					body = TEXT("");
					outTagFlag = 0;
					i++;
					continue;
				}
				///////////////////////////////////////////////////////

				if (lstrcmpW(tempTag.c_str(), TEXT("<p>")) == 0)
				{
					TextOut(hdc, x, y, body.c_str(), body.length());
					y += 30;
					tempTag = TEXT("");
					body = TEXT("");
					outTagFlag = 0;
					i++;
					continue;
				}


				if (!tagStack.empty())
				{
					if (lstrcmpW(tagStack.top().c_str(), tempTag.c_str()) == 0)
					{
						//body짝일 때
						if (lstrcmpW(tempTag.c_str(), TEXT("<body>")) == 0)
						{
							TextOut(hdc, x, y, body.c_str(), body.length());
							bodyFlag = 0;
							tagStack.pop();
						}
						//h~ 짝일 때
						for (int j = 0; j < sizeof(hTag) / sizeof(hTag[0]); j++)
						{
							if (lstrcmpW(tempTag.c_str(), hTag[j].c_str()) == 0) //h태그라 판단
							{
								TextOut(hdc, x, y, body.c_str(), body.length());
								y += fontHeight;
								fontHeight = 0;
								SelectObject(hdc, oldFont);
								DeleteObject(hFont);
								tagStack.pop();
							}
						}

						if (lstrcmpW(tempTag.c_str(), TEXT("<a>")) == 0)
						{
							SetTextColor(hdc, RGB(0, 0, 255));
							TextOut(hdc, x, y, body.c_str(), body.length());

							struct hyperLink tempLink = { 0, };
							tempLink.x = htempX;
							tempLink.y = htempY;
							tempLink.width = body.length()*20;
							tempLink.height = 20;
							
							wsprintfW(tempLink.uri,TEXT("%s:%s/%s"),ip, port, htempUri);

							linker.push_back(tempLink);

							htempX = 0;
							htempY = 0;
							memset(htempUri, '\0', MAXLEN);

							x += (body.length() * 15);
							SetTextColor(hdc, RGB(0, 0, 0));
							tagStack.pop();
						}
					}
					else// 짝이 안 맞아서 종료 
					{
						break;
					}
				}
			}

			tempTag = TEXT("");
			tagFlag = 0;
			body = TEXT("");
			outTagFlag = 0;
			i++;
			continue;
		}
		
		/////////////////////////////////////////////////

		if (tempMessage[i] == '<' && bodyFlag == 0)
		{
			tagFlag = 1;
			tempTag += tempMessage[i];
			i++;
			continue;
		}
		if (tempMessage[i] == '>' && bodyFlag == 0)
		{
			tempTag += tempMessage[i];

			if (outTagFlag == 0)
			{

				if (lstrcmpW(tempTag.c_str(), TEXT("<body>")) == 0) //body인지 판별
				{
					bodyFlag = 1;
					tagStack.push(tempTag);
				}
				else if (lstrcmpW(tempTag.c_str(), TEXT("<center>")) == 0) //center인지 판별
				{
					x = (rect.top + rect.right) / 2;
					tagStack.push(tempTag);
				}
				else if (lstrcmpW(tempTag.c_str(), TEXT("<title>")) == 0)
				{
					//no action
					tagStack.push(tempTag);
				}
				else if (lstrcmpW(tempTag.c_str(), TEXT("<style>")) == 0)
				{
					//no action
					tagStack.push(tempTag);
				}
				else if (lstrcmpW(tempTag.c_str(), TEXT("<span>")) == 0)
				{
					//no action
					tagStack.push(tempTag);
				}
				else if (lstrcmpW(tempTag.c_str(), TEXT("<br>")) == 0)
				{
					y += 30;
					x = rect.left;
				}
				else if (lstrcmpW(tempTag.c_str(), TEXT("<li>")) == 0)
				{
					tagStack.push(tempTag);
				}
				else 
				{
					tagStack.push(tempTag);
				}
				

			}
			else //순서쌍 확인 
			{
				if (!tagStack.empty())
				{
					if (lstrcmpW(tagStack.top().c_str(), tempTag.c_str()) == 0)
					{

						if (lstrcmpW(tagStack.top().c_str(), TEXT("<title>")) == 0) //title 짝
						{
							SetWindowTextW(mainhWnd, body.c_str()); //타이틀의 내용으로 window 타이틀 변경
							tagStack.pop();
						}
						else if (lstrcmpW(tagStack.top().c_str(), TEXT("<center>")) == 0) //center 짝
						{
							x = rect.top;
							//SetTextAlign(hdc, TA_LEFT);
							tagStack.pop();
						}
						else if (lstrcmpW(tagStack.top().c_str(), TEXT("<style>")) == 0)
						{
							styleParsing(body, mainhWnd);
							tagStack.pop();
						}
						else if (lstrcmpW(tagStack.top().c_str(), TEXT("<span>")) == 0)
						{
							TextOut(hdc, x, y, body.c_str(), body.length());
							x += (body.length() * 16);
							tagStack.pop();

						}
						else if (lstrcmpW(tagStack.top().c_str(), TEXT("<li>")) == 0)
						{
							TextOut(hdc, x, y, body.c_str(), body.length());
							x = rect.left;
							y += 30;
							tagStack.pop();
						}else if (lstrcmpW(tagStack.top().c_str(), TEXT("<html>")) == 0)
						{
							break;
						}
					}
				}
			}

			tempTag = TEXT("");
			body = TEXT("");
			tagFlag = 0;
			outTagFlag = 0;
			i++;
			continue;
		}
		if (tagFlag == 0) //tag가 아니면 body 변수에 모든 내용 저장
		{
			body += tempMessage[i];
			i++;
			continue;
		}
		if (tagFlag == 1) //태그 내용 저장
		{
			if (tempMessage[i] == '/')
			{
				tagFlag = 1;
				outTagFlag = 1; //끝 태그
				i++;
				continue;
			}
			if (tempMessage[i] >= 'A' && tempMessage[i] <= 'Z') //소문자로 만들기
			{
				tempTag += tempMessage[i] + 32;
			}
			else
			{
				tempTag += tempMessage[i];
			}
			i++;
			continue;
		}
		////////////////////////////////////////////////////
		i++;
	}
}