Demo entry 6747689

Car Client

   

Submitted by anonymous on Jun 05, 2018 at 19:58
Language: Python. Code size: 4.4 kB.

import socket
import threading, sys, string
import Queue
import cPickle as pickle
from time import sleep

import car_v2 as car
import usrp_spectrum_sense as usp
CAR_NO = 1


class ClientReceive(threading.Thread):
    def __init__(self, sock):
        threading.Thread.__init__(self)
        self.sock = sock
        self.car_q = Queue.Queue()
        self.usp_q = Queue.Queue()

        self.send_to_server = ClientToServer(self.sock)
        self.send_to_server.start()

        self.usp_send = ControlUSRP(self.send_to_server.send_q, self.usp_q)
        self.car_send = ControlCAR(self.send_to_server.send_q, self.car_q)
        sleep(2)
        self.car_send.start()
        self.usp_send.start()

    def run(self):
        while True:
            data = recvN(self.sock)
            if data["type"] == "command":
                if data["data"] == "EXIT":
                    self.car_q.put("EXIT")
                    self.usp_q.put("EXIT")
                    self.car_send.join()
                    self.usp_send.join()
                    break
            elif data["type"] == "car":
                self.car_q.put(data["data"])
            elif data["type"] == "usrp":
                self.usp_q.put(data["data"])


class ControlCAR(threading.Thread):
    def __init__(self, send_q, car_q):
        threading.Thread.__init__(self)
        self.car_q = car_q
        self.car = car.car(send_q)

    def run(self):
        while True:
            data = self.car_q.get()
            if data == "EXIT":
                self.car.go(['T'])
                return
            self.car.go(data)


class ControlUSRP(threading.Thread):
    def __init__(self, send_q, usp_q):
        threading.Thread.__init__(self)
        self.usp_q = usp_q
        self.tb = usp.my_top_block()
        self.tb.start()
        self.send_q = send_q
        self.car_loc = 0

    def get_car_location(self):
        return self.car_loc

    def set_car_location(self, new_loc):
        self.car_loc = new_loc
        print "Car location: %d" % self.car_loc

    def run(self):
        ready = False
        while True:
            try:
                data_receive = self.usp_q.get_nowait()
                if data_receive == "EXIT":
                    return
                if data_receive == "START":
                    ready = True
                if type(data_receive) == int:
                    self.set_car_location(data_receive)
            except Queue.Empty:
                pass
            if not ready:  # not ready to collect data
                sleep(0.1)
                continue
            data = usp.main_loop(self.tb)
            data["location"] = str(self.get_car_location())
            self.send_q.put({"type": "usrp", "data": data})


class ClientToServer(threading.Thread):
    def __init__(self, sock):
        threading.Thread.__init__(self)
        self.sock = sock
        self.send_q = Queue.Queue()

    def run(self):
        while True:
            message = self.send_q.get()
            sendN(self.sock, message)
            if message["data"] == "EXIT":
                return


def recvN(sock):
    totaln = ""
    totalrn = 0
    while totalrn < 8:
        oncen = sock.recv(8 - totalrn)
        totaln += oncen
        totalrn = len(totaln)

    n = int(totaln)

    #totalcontent = ""
    totalreceived = 0
    first = True
    while totalreceived < n:
        oncecontent = sock.recv(n - totalreceived)
        if first:
            totalcontent = oncecontent
            first = False
        else:
            totalcontent += oncecontent
        totalreceived = len(totalcontent)

    return pickle.loads(totalcontent)


def sendN(sock, data):
    data_send = pickle.dumps(data)
    n = len(data_send)
    n = str(n)
    while len(n) < 8:
        n = '0' + n
    sock.sendall(n)
    sock.sendall(data_send)


def client(server_address):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    print "connecting to %s:%s" % server_address

    sock.connect(server_address)

    sock.sendall(pickle.dumps(CAR_NO))

    thread = ClientReceive(sock)
    thread.start()


def main(ip, port):
    server_address = (ip, port)
    client(server_address)


if __name__ == "__main__":
    main("202.120.38.6", 9091)

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).