⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mymessenger.java

📁 * This a simple tool to send/receive UDP packet based on a * free software developed by Neil Deas
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
          this.setDebug(!this.getDebug());
          MyMsg.setDebug(!MyMsg.getDebug());
	  		}
		  	else
			  {
  			  //Use the while loop to allow the space in the msg body.
	  	  	while(st.hasMoreTokens())
		  		{
					  DebugFlag = st.nextToken();
  					if(DebugFlag.equalsIgnoreCase("MSG"))
	  				{
		  			  MyMsg.setDebug(!MyMsg.getDebug());
			  		}
				  	else if(DebugFlag.equalsIgnoreCase("Msgr"))
					  {
					    this.setDebug(!this.getDebug());
  				  } 
	  			}	
		  	}
 			  myOutput(" ** DEBUG FLAG Changed !! ** ");
    		myOutput(" Msg Debug  : "+MyMsg.getDebug());
    		myOutput(" Msgr Debug : "+this.getDebug());
		  	Cmd="NULL";
        // to prevent the program to goto the myCommandHandler() function.
        InfoComplete = false;
			  this.myOutput("");
  		}
  		else if(Cmd.equalsIgnoreCase("Show"))
  		{
        InfoComplete=true;
  		  if(!st.hasMoreTokens())
  		  {
  		    ShowInvite=true;
  		    ShowRev=true;
  		    ShowSnd=true;
  		  }
  		  else
  		  {
  		    String ShowFlag;
  		    while(st.hasMoreTokens())
  		    {
  		      ShowFlag=st.nextToken();
  		      if(ShowFlag.equalsIgnoreCase("Invite"))
  		      {
  		        ShowInvite=true;
  		        ShowRev=false;
  		        ShowSnd=false;
  		      }
  		      else if(ShowFlag.equalsIgnoreCase("Rev"))
  		      {
              ShowInvite=false;
  		        ShowRev=true;
  		        ShowSnd=false;
  		      }
  		      else if(ShowFlag.equalsIgnoreCase("Snd"))
  		      {
              ShowInvite=false;
  		        ShowRev=false;
  		        ShowSnd=true;
  		      }
  		    }
  		  }
    	}
    	else if(Cmd.equalsIgnoreCase("Grab") || Cmd.equalsIgnoreCase("G"))
    	{
			  if(st.hasMoreTokens())
			  {
  				FileToWrite = st.nextToken();
	  			InfoComplete = true;
		  	}
			  else
		 		  this.myOutput("Usage: Grab <FileName>");
      }
	  	else
		  {
			  this.myOutput(" *** Start of Response *** ");
  			this.myOutput(" Command Not Recognized !!!!! ");
	  		this.myOutput(" Command Currently Supported is:");
		  	this.myOutput(" SendVrdn <FileName> ");
			  this.myOutput(" SendSip <FileName> ");
  			this.myOutput(" RespondVrdn <FileName> ");
	  		this.myOutput(" RespondSip <FileName> ");
		  	this.myOutput(" TandemSip <FileName> ");
			  this.myOutput(" MSg <String>    ");
  			this.myOutput(" *** End of Response *** ");
	  	}
    }
 	  return InfoComplete;
  }

  /**
   * Based on the info provided by Parser to determine what to do
   */
	private void myCommandHandler()
	{
			if(Cmd.equalsIgnoreCase("SendVrdn") || Cmd.equalsIgnoreCase("SV"))
			{
        MyMsg oMsg = MyMsg.getFromFile(FileToSend);
        printDebug("Calling  getFromFile to new a MyMsg Object");
        if(oMsg!=null)
        {
           printDebug("MyMsg Object successfully built!!");
           this.send(VRDNIP, Integer.parseInt(RmtPort), oMsg.toString().getBytes());
           LastSndMsg=oMsg;
        }
        Cmd="NULL";
			}
			else if(Cmd.equalsIgnoreCase("SendSip") || Cmd.equalsIgnoreCase("SS"))
			{
        MyMsg oMsg = MyMsg.getFromFile(FileToSend);
        if(oMsg != null)
        {
				  this.send(SIPIP, Integer.parseInt(RmtPort), oMsg.toString().getBytes());
          LastSndMsg=oMsg;
        }
				Cmd="NULL";
			}
			else if(Cmd.equalsIgnoreCase("RespondVrdn") || Cmd.equalsIgnoreCase("RV"))
			{
				MyMsg oMsg = MyMsg.getFromFile(FileToSend);
        if(oMsg != null)
        {
          MyMsgBuilder.addCallID(IncInviteMsg.getCallID(), oMsg);
				  this.send(VRDNIP, Integer.parseInt(RmtPort), oMsg.toString().getBytes());
          LastSndMsg=oMsg;
        }
				Cmd="NULL";
			}
			else if(Cmd.equalsIgnoreCase("RespondSip") || Cmd.equalsIgnoreCase("RS"))
			{
				MyMsg oMsg = MyMsg.getFromFile(FileToSend);
        if(oMsg != null)
        {
          printDebug("CALL-ID used: "+IncInviteMsg.getCallID());
          MyMsgBuilder.addCallID(IncInviteMsg.getCallID(), oMsg);
				  this.send(SIPIP, Integer.parseInt(RmtPort), oMsg.toString().getBytes());
          LastSndMsg=oMsg;
        }
				Cmd="NULL";
			}
			else if(Cmd.equalsIgnoreCase("TandemVrdn") || Cmd.equalsIgnoreCase("TV"))
			{
				MyMsg oMsg = MyMsg.getFromFile(FileToSend);
				if(oMsg != null)
				{
					MyMsgBuilder.addSDP(IncInviteMsg , oMsg);
					this.send(VRDNIP, Integer.parseInt(RmtPort), oMsg.toString().getBytes());
          LastSndMsg=oMsg;
			  }
				Cmd="NULL";
			}
			else if(Cmd.equalsIgnoreCase("TandemSip") || Cmd.equalsIgnoreCase("TS"))
			{
				MyMsg oMsg = MyMsg.getFromFile(FileToSend);
				if(oMsg != null)
				{
					MyMsgBuilder.addSDP(IncInviteMsg, oMsg);
					this.send(SIPIP, Integer.parseInt(RmtPort), oMsg.toString().getBytes());
          LastSndMsg=oMsg;
	  	  }
				Cmd="NULL";
			}
	  	else if(Cmd.equalsIgnoreCase("RespondSip100") || Cmd.equalsIgnoreCase("RS100"))
		  {
		    MyMsg oMsg = MyMsgBuilder.build100(IncInviteMsg);
		    if(oMsg != null)
		    {
		      this.send(SIPIP, Integer.parseInt(RmtPort), oMsg.toString().getBytes());
		    }
		    Cmd="NULL";
  		}
			else if(Cmd.equalsIgnoreCase("Invite") || Cmd.equalsIgnoreCase("Inv"))
			{
				MyMsg oMsg = MyMsgBuilder.buildInv();
				if(oMsg != null)
				{
					this.send(SIPIP, Integer.parseInt(RmtPort), oMsg.toString().getBytes());
			  }
				Cmd="NULL";
			}
	  	else if(Cmd.equalsIgnoreCase("SET"))
	  	{
	  	  if(FlagToSet.equalsIgnoreCase("AutoAckBye"))
	  	  {
	  	    AutoAckBye=!AutoAckBye;
	  	    myOutput("");
	  	    myOutput("Flag : AutoAckBye Is Now Set to : "+ AutoAckBye);
	  	  }
	  	  else if(FlagToSet.equalsIgnoreCase("AutoAckInv"))
	  	  {
	  	    AutoAckInv=!AutoAckInv;
	  	    myOutput("");
	  	    myOutput("Flag : AutoAckInv Is Now Set to : "+ AutoAckInv);
	  	  }
	  	  Cmd="NULL";
	    }
			else if(Cmd.equalsIgnoreCase("MSG"))
			{
				this.send(VRDNIP, Integer.parseInt(RmtPort), MsgToSend.getBytes());
				Cmd="NULL";
			}
			else if(Cmd.equalsIgnoreCase("Grab") || Cmd.equalsIgnoreCase("G"))
			{
				Cmd="NULL";
				if(LastRevMsg!=null)
  				myFileWriter(LastRevMsg.toString(), FileToWrite, true);
  			else
  			  myOutput("NULL MSG. Operation Aborted!!");
			}
			else if(Cmd.equalsIgnoreCase("Show"))
			{
        Cmd="NULL";
			  if(ShowInvite)
			  {
          this.myOutput("<<<<<<<<< SHOW >>>>>>>>>> ");
 			    this.myOutput("Last Received INVITE Msg :");
 			    if(IncInviteMsg != null)
 			      this.myOutput(IncInviteMsg.toString());
 			    else
 			      this.myOutput("NULL Msg");
			    this.myOutput("<<<<<<<<< SHOW >>>>>>>>>> ");
			  }
			  if(ShowRev)
			  {
          this.myOutput("<<<<<<<<< SHOW >>>>>>>>>> ");
 			    this.myOutput("Last Received Msg :");
 			    if(LastRevMsg != null)
 			      this.myOutput(LastRevMsg.toString());
 			    else
 			      this.myOutput("NULL Msg");
			    this.myOutput("<<<<<<<<< SHOW >>>>>>>>>> ");
			  }
			  if(ShowSnd)
			  {
          this.myOutput("<<<<<<<<< SHOW >>>>>>>>>> ");
 			    this.myOutput("Last Sent Msg :");
 			    if(LastSndMsg != null)
 			      this.myOutput(LastSndMsg.toString());
 			    else
 			      this.myOutput("NULL Msg");
			    this.myOutput("<<<<<<<<< SHOW >>>>>>>>>> ");
			  }
      }			
		return;
	}

  /**
   *    Parse the command line arguments.
   */
	private static String[] parseArguments(String[] a_Arguments)
	{
		String[] result = new String[6];
		result[5]="";

		List args = Arrays.asList(a_Arguments);
		Iterator iArg = args.iterator();
		while (iArg.hasNext())
		{
			String arg = (String)iArg.next();
			if ( null == result[0])
			{
				// Local host hasn't been seen yet
				result[0] = arg;
			}
			else if (null == result[1])
			{
				// Local port hasn't been seen yet
				result[1] = arg;
			}
			else if (null == result[2])
			{
				// VRDN GWC IP hasn't been seen yet
				result[2] = arg;
			}
			else if (null == result[3])
			{
				// remote port hasn't been seen yet
				result[3] = arg;
			}
			else if ( null == result[4])
			{
				// SIP GWC IP hasn't been set yet
				result[4] = arg;
			}
			else
			{
				// Oops!  Too Many Arguments specified
				displayHelp();
			}
		}
    // Make sure we have everything.
		for( int i = 0; i < 5 ; i++)
		{
			if (null == result[i])
			{
				displayHelp();
			}
		}
		return result;
	}

	/**
	 * Pring debug statements, if debugging has been turned on.
	 */
	private void printDebug(String statement)
	{
		if(m_Debugging)
		{
			System.out.println(statement);
		}
	}


  /**
  * The method is defined here so that we can have a single method
  * to handle all system output 
  */
	private void myOutput(String a_Str)
	{
		System.out.println(a_Str);
		myFileWriter(a_Str);
	}


  /**
  * The method is defined here so that we can have a single method
  * to handle all file output.
  */
	private void myFileWriter(String a_Str)
	{
		try
		{
			byte[] buf=null;
			try
			{		
				BufferedInputStream buffer = new BufferedInputStream( new FileInputStream("MYLOG") );
				int ilength = buffer.available();
				buf = new byte[ilength];
				buffer.read(buf, 0, ilength);
				buffer.close();
			}
			catch(FileNotFoundException e)
			{
				FileOutputStream dummy = new FileOutputStream("MYLOG");
				dummy.close();
			}
			
			byte[] LineFeed = new byte[2];
			LineFeed[0]=0x0d;
			LineFeed[1]=0x0a;
			String LineF=new String(LineFeed);
			String outputStr=null;
			if(!(buf == null))
				outputStr = new String(buf);
			else
			{
				outputStr = new String("New File Created on: ");
		    Calendar Cc=Calendar.getInstance();
  		  String MyTimestamp=Cc.get(Calendar.YEAR)+"-"+(Cc.get(Calendar.MONTH)+1)+"-"+Cc.get(Calendar.DAY_OF_MONTH)+" "+Cc.get(Calendar.HOUR_OF_DAY)+":"+Cc.get(Calendar.MINUTE)+":"+Cc.get(Calendar.SECOND);
				outputStr = outputStr + MyTimestamp + LineF;
			}
			outputStr = outputStr+a_Str+LineF;
			BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream("MYLOG") );
			bos.write(outputStr.getBytes());
			bos.flush();
			bos.close();
		}
		catch(IOException e)
		{
			System.err.println(e);
		}
	}

	private void myFileWriter(String a_Str, String a_FileName, boolean b_Compact)
	{
		try
		{
			byte[] buf=null;
			try
			{		
				BufferedInputStream buffer = new BufferedInputStream( new FileInputStream(a_FileName) );
				int ilength = buffer.available();
				buf = new byte[ilength];
				buffer.read(buf, 0, ilength);
				buffer.close();
			}
			catch(FileNotFoundException e)
			{
				FileOutputStream dummy = new FileOutputStream(a_FileName);
				dummy.close();
			}
			
			byte[] LineFeed = new byte[2];
			LineFeed[0]=0x0d;
			LineFeed[1]=0x0a;
			String LineF=new String(LineFeed);
			String outputStr=null;
			if(!(buf == null))
			{
				outputStr = new String(buf);
				
			}
			else
			{
				if(!b_Compact)
				{
					outputStr = new String("New File Created on: ");
		    	Calendar Cc=Calendar.getInstance();
  		  	String MyTimestamp=Cc.get(Calendar.YEAR)+"-"+(Cc.get(Calendar.MONTH)+1)+"-"+Cc.get(Calendar.DAY_OF_MONTH)+" "+Cc.get(Calendar.HOUR_OF_DAY)+":"+Cc.get(Calendar.MINUTE)+":"+Cc.get(Calendar.SECOND);
					outputStr = outputStr + MyTimestamp + LineF;
				}
				else
					outputStr = new String("");
			}
			outputStr = outputStr+a_Str+LineF;
			BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream(a_FileName) );
			bos.write(outputStr.getBytes());
			bos.flush();
			bos.close();
		}
		catch(IOException e)
		{
			System.err.println(e);
		}
	}

  /**
   * Print a usage summary.
   */
	private static void displayHelp()
	{
		System.out.println(
			"Usage: MyMessenger <local-host> <local-port> <vrdn-host> <remote-port> <sip-host>\n");

		System.exit(0);
	}

}
/***************************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -