-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Requirement for project | ||
- the project will be able to receive data from mobile phones by socket server. | ||
- The socket server will be TCP server if it's possible | ||
- There will be two type of data coming from the phone.The first type is images of web page user are watching and the second is the position of eye gaze of the user. | ||
- the format of the image data will be jpeg because it will cause lesser data space and improve load time. | ||
- database of this project is PostgreSQL. | ||
|
||
# Socket Expected Behavior | ||
- After connection success. Will request for the image of the webpage. | ||
- after getting the image of the webpage the server will save data in the filesystem in folder ./img and save path to the database.database will generate ID with auto increment and send back to the socket server. | ||
- socket server would reply id of the image in the database to mobile if all of the operations succeeded. | ||
- after initialization complete mobile will send gaze point(x, y) along with the id of the image to ensure that gaze point. |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import asyncio | ||
import uvloop | ||
|
||
class Server(): | ||
|
||
def __init__(self, host, port): | ||
self.host = host | ||
self.port = port | ||
|
||
async def handle_client(self, reader, writer): | ||
writer.close() | ||
return | ||
|
||
def start(self, loop): | ||
# socket_server = socket.socket() | ||
# socket_server.bind((self.host, self.port)) | ||
# socket_server.listen(5) | ||
# socket_server.setblocking(False) | ||
coro = asyncio.start_server(self.handle_client, self.host, self.port, loop=loop) | ||
self.server = loop.run_until_complete(coro) | ||
|
||
def close(self): | ||
self.server.close() | ||
self.server | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) | ||
server = Server("127.0.0.1", 8888) | ||
loop = asyncio.get_event_loop() | ||
server.start(loop) | ||
try: | ||
loop.run_forever() | ||
except KeyboardInterrupt: | ||
server.close() | ||
loop.run_until_complete(server.server.wait_closed()) | ||
loop.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import socket | ||
def is_server_running(host, port): | ||
args = socket.getaddrinfo(host, port, socket.AF_INET, socket.SOCK_STREAM) | ||
for family, socktype, proto, _, sockaddr in args: | ||
s = socket.socket(family, socktype, proto) | ||
try: | ||
s.connect(sockaddr) | ||
except socket.error: | ||
return False | ||
else: | ||
s.close() | ||
return True |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import unittest | ||
import asyncio | ||
import uvloop | ||
|
||
from src.util import is_server_running | ||
from src.server import Server | ||
|
||
|
||
class TestSocketServer(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self.host = "127.0.0.1" | ||
self.port = 8888 | ||
self.server = Server(self.host, self.port) | ||
self.loop = asyncio.get_event_loop() | ||
self.server.start(self.loop) | ||
|
||
def test_init_server(self): | ||
self.assertEqual(self.server.host, "127.0.0.1") | ||
self.assertEqual(self.server.port, 8888) | ||
|
||
def test_server_is_running(self): | ||
self.assertTrue(is_server_running(self.host, self.port)) | ||
|
||
def tearDown(self): | ||
self.server.close() | ||
if self.loop.is_running(): | ||
self.loop.close() | ||
# self.loop.close() | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |