2013-05-02 45 views
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() 

我有此peice的code.I的取代误差与更换计数查询之后SQLAlchemy的存在查询

#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... 

用于与计数工作上面的代码段查询。为什么它现在使用存在查询。

+1

你的问题是什么? – Chris 2013-05-02 21:06:50

+0

@ChrisBain我已经更新了这个问题。 – liv2hak 2013-05-02 21:09:09

+1

exists()不返回True或False? – PepperoniPizza 2013-05-02 21:14:33

回答

1

正如francis-avila在他的辉煌answer to your other question中解释的那样,在逻辑上存在问题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(): 

方法存在()...一( )描述了here并且处理异常也会起作用。只要记住,处理异常总是更昂贵的操作(使用更多的CPU周期)在Python中,然后只是做条件检查。如果你的应用程序不是性能关键 - 使用try/catch异常处理就好了。