0

I send data to socket on one side every second, but I can read that data on another side in any moment. Here's the writer:

from settings import Config

filename = Config.NAVIGATION_SOCKET_FILE

client = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
client.settimeout(None)
while True:
    try:
        client.connect(filename)
        break
    except Exception:
        continue
messages = ["$GPRMC,125504.049,A,5542.2389,N,03741.6063,E,0.06,25.82,200906,,,*17",
            "$GPRMC,155604.049,A,5542.2389,N,03741.6063,E,0.06,25.82,200906,,,*19",]
while True:
    msg = random.choice(messages)
    client.send(msg)
    print msg
    time.sleep(1)

And here's reader:

navigation_socket = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
if os.path.exists(app.config['NAVIGATION_SOCKET_FILE']):
        os.remove(app.config['NAVIGATION_SOCKET_FILE'])
navigation_socket.bind(app.config['NAVIGATION_SOCKET_FILE'])

class NavigationInfo(restful.Resource):
    def get(self):
        msg = navigation_socket.recv(1024)
        regex = re.compile(r"^\$GPRMC,(?P<time>\d{6}\.\d{3}),(?P<status>A|V),"
                           r"(?P<latitude>\d{4}\.\d{4}),(?P<lat_n_s>N|S),"
                           r"(?P<longitude>\d{5}\.\d{4}),(?P<long_e_w>E|W),"
                           r"(?P<hor_speed>\d+.\d+),(?P<track_angle>\d+.\d+),"
                           r"(?P<date>\d{6}),(?P<magnetic_declination>\d+\.\d+)?,"
                           r"(?P<magnetic_decl_direction>\d)?,"
                           r"(?P<mode>A|D|E|N)?\*(?P<checksum>\d\d)")
        result = regex.match(msg)
        navigation_info = result.groupdict()
        return navigation_info

So the first problem is that writer just stops writing data to socket when buffer is full (at least that's what I see) and when I request data on the other side, it's too old.

Can just I store one value in buffer and then rewrite it? Or maybe I'm getting it all wrong?

Dmitrii Mikhailov
  • 5,053
  • 7
  • 43
  • 69
  • You may consider sleeping some seconds in your first while true, in case of an exception. (If connect raises, you'll stick at 100% CPU, for nothing). – Julien Palard Jul 12 '14 at 16:43
  • @JulienPalard thanks. But the main problem for me here is that I'm not able to get fresh data from socket. – Dmitrii Mikhailov Jul 12 '14 at 16:47

1 Answers1

0

I think that you are using the solution in reverse.

Instead of pushing messaging, while not pulling messages ?

Your server may look like:

  1. Wait for a connection
  2. Give a random message
  3. go to step 1

And your client may just connect to the server when he needs a message.

In your case, the connection is "opened all the time", in my solution the socket is opened only when needed. and closed right after the message is delivered.

Julien Palard
  • 8,736
  • 2
  • 37
  • 44
  • yw, don't hesitate to ask again if you're stuck. – Julien Palard Jul 12 '14 at 17:07
  • Could you provide any piece of code as example? I really don't now how to do that thing without using two unix sockets. And even if I have two, do I need to use threading to work with them concurrently? – Dmitrii Mikhailov Jul 13 '14 at 08:42
  • RTFM: https://docs.python.org/2/library/socketserver.html#socketserver-tcpserver-example (If you really want to stick to Unix Sockets, use UnixStreamServer but TCP is great too). – Julien Palard Jul 13 '14 at 09:05