1
class SourcetoPort(Base):
    """"""
    __tablename__ = 'source_to_port'
    id = Column(Integer, primary_key=True)
    port_no        = Column(Integer)
    src_address    = Column(String,index=True)

    #----------------------------------------------------------------------
    def __init__(self, src_address,port_no):
        """"""
        self.src_address = src_address
        self.port_no     = port_no


  def act_like_switch (self, packet, packet_in):
    """
    Implement switch-like behavior.
    """
    # Learn the port for the source MAC
    #print "RECIEVED FROM PORT ",packet_in.in_port , "SOURCE ",packet.src
    # create a Session
    #Session = sessionmaker(bind=engine)
    #session = Session()
    self.mac_to_port[packet.src]=packet_in.in_port
    #if self.mac_to_port.get(packet.dst)!=None:
    print "count for dst",session.query(SourcetoPort).filter_by(src_address=str(packet.dst)).count(),str(packet.dst)
    #if session.query(SourcetoPort).filter_by(src_address=str(packet.dst)).count():
    if session.query(exists().where(SourcetoPort.src_address == str(packet.dst))).scalar() is not None:
           #send this packet
           print "got info from the database"
           q_res = session.query(SourcetoPort).filter_by(src_address=str(packet.dst)).first()
           self.send_packet(packet_in.buffer_id, packet_in.data,q_res.port_no, packet_in.in_port)
           #create a flow modification message
           msg = of.ofp_flow_mod()
           #set the fields to match from the incoming packet
           msg.match = of.ofp_match.from_packet(packet)
           #send the rule to the switch so that it does not query the controller again.
           msg.actions.append(of.ofp_action_output(port=q_res.port_no))
           #push the rule
           self.connection.send(msg)
    else:
           #flood this packet out as we don't know about this node.
           print "flooding the first packet"
           self.send_packet(packet_in.buffer_id, packet_in.data,
                       of.OFPP_FLOOD, packet_in.in_port)
           #self.matrix[(packet.src,packet.dst)]+=1      
           entry = SourcetoPort(src_address=str(packet.src) , port_no=packet_in.in_port)
           #add the record to the session object
           session.add(entry)
           #add the record to the session object
           session.commit()

我有这段代码。我替换了

#if session.query(SourcetoPort).filter_by(src_address=str(packet.dst)).count():

if session.query(exists().where(SourcetoPort.src_address == str(packet.dst))).scalar() is not None:

Now I am getting the following error.


  File "/home/karthik/pox/tutorial.py", line 86, in act_like_switch
    self.send_packet(packet_in.buffer_id, packet_in.data,q_res.port_no, packet_in.in_port)
AttributeError: 'NoneType' object has no attribute 'port_no'
^CINFO:core:Going down...

上面这段代码曾经用于计数查询。为什么现在它与存在查询一起使用。

4

1 回答 1

1

正如 francis-avila 在他对您的另一个问题的精彩回答中所解释的那样,逻辑上存在问题exists()...scalar() is not None。它返回 True 或 False - 所以它总是不返回 None。抱歉,昨天在 SQLAlchemy 中建议如何使用存在时是我的错误。

除此之外,您的代码是正确的,并且在更改使用exists()查询结果的逻辑后应该可以工作:

if session.query(exists().where(SourcetoPort.src_address == str(packet.dst))).scalar() is not None:

if session.query(exists().where(SourcetoPort.src_address == str(packet.dst))).scalar():

使用这里描述的 exists()...one()和处理异常的方法也可以。请记住,在 python 中处理异常总是更昂贵的操作(使用更多的 cpu 周期),然后只是进行条件检查。如果您的应用程序不是性能关键 - 使用 try/catch 异常处理就可以了。

于 2013-05-03T15:24:37.237 回答