Shop OBEX P1 Docs P2 Docs Learn Events
Spinneret SD Card usage problem- help please? — Parallax Forums

Spinneret SD Card usage problem- help please?

RforbesRforbes Posts: 281
edited 2012-12-01 04:45 in Accessories
Hey all,

I'm trying to understand the HTTPServer object better, and I'm definitely making some headway. But I am getting myself stumped here.
PUB Main | packetSize, id, i, reset, j, temp
  '' HTTP Service

  GetSntp(3)

  repeat

    repeat until fifoSocket == 0        <================================== does this = 0 when there are no requests to serve up a file?
      
      SDCard.changeDirectory(@approot)
      bytefill(@rxdata, 0, RxTx_BUFFER)

      if(debug)
        'pst..str(string(13, "----- Start of Request----------------------------",13))
        QueueDump
      else
        pause(DELAY)
        
      ' Pop the next socket handle 
      id := DequeueSocket
      if(id < 0)
        'pst..str(string(13,"Id < 0"))
        'pst..dec(id)                                  
        'pst..char(13)
        nextPUB Main | packetSize, id, i, reset, j, temp

Where I've listed this <========== in the code is my question. Is this variable equal to 0 when no one is trying to get or post to the server?

The reason I'm asking, is because I need to run a method in another cog that uses the SDcard to write and append static *.xml files. I'm trying to think of a way to ensure that I don't try to use any of the sdcard methods from two cogs at once. I did that today and it made some really interesting files and folders on the sdcard... heh. Had to format it and start over.

So, if fifosocket is equal to 0 when there is no traffic, I can use that as a means to tell my other cog that it's ok to go ahead and write or append files to the sd card. Or at least it's ok to start at that instant.

I hope this question makes sense.

Thanks in advance!
Robert

Comments

  • Mike GMike G Posts: 2,702
    edited 2012-11-30 18:29
    So, if fifosocket is equal to 0 when there is no traffic, I can use that as a means to tell my other cog that it's ok to go ahead and write or append files to the sd card. Or at least it's ok to start at that instant.
    Kinda' fifosocket = 0 means no pending socket requests at the time fifosocket was checked. But a socket request can come along at any time. It sounds like you want to halt any SD IO from the server process when another parallel process wants exclusive SD access.

    You'll have to halt the sockets. The following snippet of code will save the run-time TCP socket IDs and halt socket id. If you wanted to halt all 4 sockets you would have to invoke SetTcpSocketMaskById four times. Once for each socket.
        'save current socket state
        tempMask := tcpMask
        SetTcpSocketMaskById(id, 0)    
    

    On SD IO completion, we'll have to put the a single socket back in operation.
        tcpMask := tempMask        'reset the socket
        InitializeSocket(id)
    

    Keep in mind that halting all the sockets mean HTTP requests can be missed.
  • RforbesRforbes Posts: 281
    edited 2012-11-30 18:42
    Mike- Wow, ok. So here's how I'd use your snippet above?
    PRI LogData|Bcnt,tempMask
    {{
      Every 15 minutes we want to log the latest values in an xml file. 
    }}  
    
    
        'save current socket state
      tempMask := tcpMask
      SetTcpSocketMaskById(0, 0)
    
      If (Minute==3) OR (Minute==57) OR (Minute==59) OR (Minute==1)
        repeat while oneshot==0
          SDCard.changeDirectory(@approot)                   'This one re-writes data with the most current values.
          SDCard.openFile(@logfile, "W")
          SDCard.writeString(String("<xml>",13))             
          SDCard.writeString(String("<root>", 13))
          repeat Bcnt from 0 to 44
            SDCard.writeString(String("<IOS"))
            SDCard.writeDec(Bcnt)
            SDCard.writeString(String(">"))
            SDCard.writeDec(IOS[Bcnt])
            SDCard.writeString(String("</IOS"))
            SDCard.writeDec(Bcnt)
            SDCard.writeString(String(">"))
            SDCard.writeString(String(13))
          SDCard.writeString(String("</root>", 13))
          SDCard.writeString(String("</xml>", 13))
          SDCard.closeFile
          oneshot:=1
      Else
        oneshot:=0
    
      tcpMask := tempMask        'reset the socket
      InitializeSocket(0)
    

    By doing this, if I try to call up a web page when I'm running this Logdata method- will the webserver just wait and then process the request once I reset the socket? If I used it for all 4 sockets that is (instead of just one as shown)
  • Mike GMike G Posts: 2,702
    edited 2012-11-30 18:45
    It is also possible to place a code block above repeat until fifoSocket == 0 line in Main.
      repeat
        'current process
        repeat until fifoSocket == 0
    
    This action will take control until completion making any socket request wait.
  • RforbesRforbes Posts: 281
    edited 2012-11-30 18:54
    Dang!!! Right on- Something like this is what I was thinking of doing, but I don't have access to my spinneret this evening to play with it and try stuff out.
      repeat
    
        repeat until lockout== 0       'This will hold us up until the datalogging is completed.
                                       'Method Datalog sets it to 1 as soon as it starts, and
                                       'sets it back to 0 when complete.
        
        repeat until fifoSocket == 0
    

    But this still seems like it won't really work exactly right. I think I'd still need to do something similar in the datalog method to ensure it doesn't start writing a file at the instant the dispatcher method is fetching one to serve up.
  • Mike GMike G Posts: 2,702
    edited 2012-12-01 04:45
    But this still seems like it won't really work exactly right. I think I'd still need to do something similar in the datalog method to ensure it doesn't start writing a file at the instant the dispatcher method is fetching one to serve up.
    Have the data log process set a flag when the process is ready to write. It's the same principle as the fifoSocket flag.
      repeat
        if(isLogEvent)
          'process the data log
        repeat until fifoSocket == 0
    

    Keep in mind the data log process will block socket Rx processing. There are 4 Rx socket buffers. Once the 4 Rx buffers are in use any subsequent client requests can produce unexpected requests depending on the persistence of the client.
Sign In or Register to comment.