-1

我正在尝试创建一个代表 NXCP 消息的类(它是与监控系统 NetXMS 一起使用的协议),我正在尝试对其进行修改http://wiki.netxms.org/wiki/Communication_Protocol_Reference_Guide

..这是我到目前为止的代码

NXCP.inc.php:

<?php
class message_nxcp {

        public $message;
        public $header;
        public $payload;
        public $padding;

        function header ($name,$ID,$eof,$de,$ro,$eos,$c )
        {   //CODE
            $sql= "select value from nxcp_code where name = '$name'" ;
            $code=mysql_query($sql);
            //FLAG
            //$sql1="select Value from flag where name = '$flag'"; $flag=mysql_query($sql1);
            if     ($eof=true){ $flag =0x0002;}
            elseif ($de=true) {$flag = 0x0004;}
            elseif ($eos=true) {$flag = 0x0008;}
            elseif ($ro=true) {$flag = 0x0010;}
            elseif ($c=true) {$flag = 0x0020;
                              $payload= ' ';}
            //SIZE
            $size=strlen($message); 
            //ID
            $this->id= $ID;
            // HEADER
            $header= $code.$flag.$size.$id.$df_count;
        }

        function payload ($var_id,$d_type,$value)
        {   //VARIABLE id
            $sql= "select value from nxcp_code where name = '$var_id'" ;
            $vid=mysql_query($sql);
            //data type  ,padding ,data  
                if ($d_type = 'integer')
                {   $data_type =0;
                    $padding1= 00;
                    $data=$value;
                }
            if ($d_type ='string' )
                {   $data_type =1;
                    $padding1= 00;
                    $data= strlen($value).$value;
                }
            if ($d_type = 'int64')
                { $data_type =2;
                    $padding1=00 ;
                    $data=$value;
                }
            if ($d_type ='int32')
                {   $data_type =3;
                    $padding1= '';
                    $data=$value;
                }
            if ($d_type ='binary')
                {   $data_type =4;
                    $padding1= 00;
                    $data=strlen($value).$value;
                }
            if ($d_type ='float')
                {   $data_type =5;
                    $padding1= 00;
                    $data=$value;
                }

        $payload=$vid.$data_type.$padding1.$data ;      
        }

        function padding ()
        {
            $i =0 ;
            $pad = 262160 - (strlen($header)+strlen($payload));
            if ($pad > 0)
            {
            for ($i=0 ; $i<$pad ; $i++)
                    {
                    $padding= $padding.'0' ; 
                    }
            }

        }

        function message()
         {  $message = $header.$payload.$padding;
            return $message;
         }
    }
?>

然后我message_nxcp在 new.php 中创建我的类的一个新对象:

<?php

require_once('NXCP.inc.php');

$message=new message_nxcp;
$message->header('CMD_LOGIN' ,'1',true,false,false,false,false);
$message->payload('VID_LOGIN_NAME','string','admin');
$message->padding();
$message->message();
echo $message;

?>

任何帮助,并提前感谢

4

1 回答 1

0

首先我不知道如何实现 NXCP 消息类,但我看到了代码中的基本问题。

所以所有这些和这里的解释:

  <?php
  class message_nxcp {

    //use private keyword (i think you does not want to reach this variables directly from outside of the class
    private $message;
    private $header;
    private $payload;
    private $padding;
    //
    private $id; //somewhere down i saw you use it so declared here

    function header ($name,$ID,$eof,$de,$ro,$eos,$c )
    {   //CODE
        $sql= "select value from nxcp_code where name = '$name'" ;

        /* Have you already connected to database with `mysql_connect` function? */
        $res=mysql_query($sql); 
        /* the type of $res is mysqlResult (not a string), you should fetch that like this: */
        //we should examine , is there error in the query
        if( !$res ) throw new Exception(mysql_error(),mysql_errno());
        $codeArray = mysql_fetch_assoc($res); //This fetch the first row of the result to an array with numeric indexes.
        //Here you should examine is the result empty
        if( !$codeArray ) throw new Exception('Code not found'); //somevhere you should catch it          
        $code = $codeArray[0]; 

        //FLAG
        //$sql1="select Value from flag where name = '$flag'"; $flag=mysql_query($sql1);
        if     ($eof=true){ $flag =0x0002;}
        elseif ($de=true) {$flag = 0x0004;}
        elseif ($eos=true) {$flag = 0x0008;}
        elseif ($ro=true) {$flag = 0x0010;}
        elseif ($c=true) {$flag = 0x0020; 
                          // $this keyword
                          $this->payload= ' ';}
        //SIZE
        // $this keyword
        $size=strlen($this->message); 
        //ID
        $this->id= $ID;
        // HEADER
        /* the this keyword missing */
        $this->header= $code.$flag.$size.$id.$df_count;
    }

    function payload ($var_id,$d_type,$value)
    {   //VARIABLE id
        /* do the same as the previus function*/
        $sql= "select value from nxcp_code where name = '$var_id'" ;
        $vid=mysql_query($sql);
        // see the example in the previus function

        //data type  ,padding ,data  
            if ($d_type = 'integer')
            {   $data_type =0;
                $padding1= 00;
                $data=$value;
            }
        if ($d_type ='string' )
            {   $data_type =1;
                $padding1= 00;
                $data= strlen($value).$value;
            }
        if ($d_type = 'int64')
            { $data_type =2;
                $padding1=00 ;
                $data=$value;
            }
        if ($d_type ='int32')
            {   $data_type =3;
                $padding1= '';
                $data=$value;
            }
        if ($d_type ='binary')
            {   $data_type =4;
                $padding1= 00;
                $data=strlen($value).$value;
            }
        if ($d_type ='float')
            {   $data_type =5;
                $padding1= 00;
                $data=$value;
            }
    // $this keyword missing, in php you must use this to access class variables
    $this->payload=$vid.$data_type.$padding1.$data ;      
    }

    function padding ()
    {
        $i =0 ;
        //$this keyword
        $pad = 262160 - (strlen($this->header)+strlen($this->payload));
        if ($pad > 0)
        {
        for ($i=0 ; $i<$pad ; $i++)
                {
                //$this keyword
                $this->padding= $this->padding.'0' ; 
                }
        }

    }

    function message()
     {  
         //$this keyword
        $this->message = $this->header.$this->payload.$this->padding;
        return $this->message;
     }
?>

以及其他代码的问题:

     <?php

         require_once('NXCP.inc.php');
         try{
              $message=new message_nxcp();  //() missed
              $message->header('CMD_LOGIN' ,'1',true,false,false,false,false);
              $message->payload('VID_LOGIN_NAME','string','admin');
              $message->padding();
              echo $message->message();  //echo the message
         }catch(Exception $e){  //if we throw exceptions, somewhere here we should catch them
               //when you debuging:
               print '<pre>'.$e->getMessage()."\n".$e->getTraceAsString().'</pre>';
               //in production mode, we dont want to show users the details, just this: 
               print 'Error while processing the request';
               exit;

         }
      ?>

其他一些事情:

mysqli_*不使用mysql_*mysql_*将在 php 中弃用)或者如果您需要 Databasde Access 的 OOP 解决方案,您可以使用PDO

阅读以下页面:

http://php.net/manual/en/language.oop5.php,http://php.net/manual/en/book.mysqli.php,http://php.net/manual/en/language _ _ _ exceptions.phphttp ://php.net/manual/en/book.pdo.php

于 2013-04-11T19:15:19.293 回答