Skip to content

Commit

Permalink
bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
AboudyKreidieh committed Jan 1, 2019
1 parent 535038f commit 2f7c2dd
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
2 changes: 1 addition & 1 deletion flow/utils/aimsun/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ def get_vehicle_tracking_info(self, veh_id):
tracking_info.PreviousSpeed,
tracking_info.TotalDistance,
tracking_info.SystemGenerationT,
tracking_info.SectionEntranceT,
tracking_info.SystemEntranceT,
tracking_info.SectionEntranceT,
tracking_info.CurrentStopTime,
tracking_info.stopped,
Expand Down
63 changes: 60 additions & 3 deletions tests/dummy_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,53 @@
runner script. Used for testing purposes.
"""
import flow.utils.aimsun.constants as ac
from flow.utils.aimsun.run import send_message, retrieve_message
# from flow.utils.aimsun.run import send_message, retrieve_message
from thread import start_new_thread
import socket
import struct

PORT = 9999


def send_message(conn, in_format, values):
"""Send a message to the client.
Parameters
----------
conn : socket.socket
socket for server connection
in_format : str
format of the input structure
values : tuple of Any
commands to be encoded and issued to the client
"""
packer = struct.Struct(format=in_format)
packed_data = packer.pack(*values)
conn.send(packed_data)


def retrieve_message(conn, out_format):
"""Retrieve a message from the client.
Parameters
----------
conn : socket.socket
socket for server connection
out_format : str or None
format of the output structure
Returns
-------
Any
received message
"""
unpacker = struct.Struct(format=out_format)
try:
data = conn.recv(unpacker.size)
unpacked_data = unpacker.unpack(data)
finally:
pass
return unpacked_data


def threaded_client(conn):
Expand Down Expand Up @@ -48,5 +94,16 @@ def threaded_client(conn):
else:
send_message(conn, in_format='i', values=(-1001,))

# close the connection
conn.close()

while True:
# tcp/ip connection from the aimsun process
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind(('localhost', PORT))

# connect to the Flow instance
server_socket.listen(10)
c, address = server_socket.accept()

# start the threaded process
start_new_thread(threaded_client, (c,))
8 changes: 7 additions & 1 deletion tests/fast_tests/test_aimsun_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import flow.config as cofig
import flow.utils.aimsun.constants
from flow.utils.aimsun.api import FlowAimsunAPI
import unittest
import os
import subprocess
import numpy as np


Expand Down Expand Up @@ -36,6 +39,9 @@ class TestDummyAPI(unittest.TestCase):

def test_getter_methods(self):
# start the server's process
subprocess.Popen([
os.path.join(cofig.AIMSUN_SITEPACKAGES, "bin/python2.7"),
os.path.join(cofig.PROJECT_PATH, 'tests/dummy_server.py')])

# create the FlowAimsunKernel object
kernel_api = FlowAimsunAPI(port=9999)
Expand Down Expand Up @@ -86,7 +92,7 @@ def test_getter_methods(self):
self.assertEqual(tracking_info.PreviousSpeed, 13)
self.assertEqual(tracking_info.TotalDistance, 14)
self.assertEqual(tracking_info.SystemGenerationT, 15)
self.assertEqual(tracking_info.SectionEntranceT, 16)
self.assertEqual(tracking_info.SystemEntranceT, 16)
self.assertEqual(tracking_info.SectionEntranceT, 17)
self.assertEqual(tracking_info.CurrentStopTime, 18)
self.assertEqual(tracking_info.stopped, 19)
Expand Down

0 comments on commit 2f7c2dd

Please sign in to comment.