Skip to content

Commit 5671e1d

Browse files
author
SNU Outliers
committed
MA MU RI
1 parent 62da8c3 commit 5671e1d

7 files changed

Lines changed: 43 additions & 44 deletions

File tree

‎cache/lru_cache.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ def get(self, key):
1616
try:
1717
value = self.cache.pop(key)
1818
self.cache[key] = value
19-
print("LRU Cache Hit!!!")
19+
#print("LRU Cache Hit!")
2020
return value
2121
except KeyError:
22-
print("LRU Cache Miss!!!")
22+
#print("LRU Cache Miss!")
2323
return -1
2424

2525
def set(self, key, value):

‎compare/node_app.js‎

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
var http = require('http');
22
var fs = require('fs');
3-
var path = require('path');
4-
3+
54
http.createServer(function (req, res) {
6-
var filePath = path.join('../resources', req.url);
7-
var stat = fs.statSync(filePath);
8-
9-
res.writeHead(200, {
10-
'Content-Type': '*/*',
11-
'Content-Length': stat.size
5+
if (req.url == '/') {
6+
res.writeHead(200, {'Content-Type':'*/*'});
7+
res.write('');
8+
res.end();
9+
return;
10+
}
11+
fs.readFile('../resources' + req.url, 'utf-8', function (err,data) {
12+
res.writeHead(200, {'Content-Type': '*/*'});
13+
res.write(data);
14+
res.end();
1215
});
13-
14-
var readStream = fs.createReadStream(filePath);
15-
// We replaced all the event handlers with a simple call to readStream.pipe()
16-
readStream.pipe(res);
1716
}).listen(8080);
18-

‎env/production.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# TODO : 아마존 인스턴스 생성 후 수정할 것
2-
host : 'ec2-13-125-65-86.ap-northeast-2.compute.amazonaws.com'
2+
host : 'ec2-52-79-135-109.ap-northeast-2.compute.amazonaws.com'
33
port : 8080
44
buffer_size : 8192

‎event_loop.py‎

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import os
22
from queue import Queue
33
from threading import Thread
4-
4+
import threading
55
from http.status import *
6-
6+
import time
77
from cache.cache import *
88
from http.http_response import HTTPResponse
99
from selector import sel
1010
from utils.event_loop_app_exception import EventLoopAppException
11+
import socket
1112

12-
NUM_OF_THREADS = 4
13+
NUM_OF_THREADS = 1
1314

1415
class EventLoop:
1516

@@ -21,7 +22,6 @@ def __init__(self, event_queue, cache_policy=LRU):
2122
for i in range(NUM_OF_THREADS):
2223
t = Thread(target=self.read)
2324
t.start()
24-
print("Thread " + str(i) + " started!")
2525

2626
def start(self):
2727
while True:
@@ -35,7 +35,6 @@ def execute(self):
3535
if event.is_disk_io():
3636
# Check whether the requested data is cached.
3737
cached_response_bytes = self.cache.get(event.request_uri)
38-
3938
if cached_response_bytes != -1:
4039
event.response_bytes = cached_response_bytes
4140
event.disk_io = False
@@ -49,44 +48,47 @@ def execute(self):
4948

5049
@staticmethod
5150
def close_or_keep_alive(event):
52-
print("Event_connection_info:" + str(event.connection))
51+
# print("Event_connection_info:" + str(event.connection))
5352
if event.connection == 'keep-alive':
54-
print("Event Connnection is keep alive!")
53+
# print("Event Connnection is keep alive!")
5554
pass
5655
else:
5756
sel.unregister(event.CLIENT_SOCKET)
5857
event.CLIENT_SOCKET.close()
59-
print("Connection from client is closed.")
58+
# print("Connection from client is closed.")
6059

6160
def send_event(self, event):
62-
print("Event sending started!")
61+
# print("Event sending started!")
62+
# begin = time.time()
6363
bytes_to_send = HTTPResponse.respond(HTTP_200_OK, event)
6464
event.CLIENT_SOCKET.setblocking(True)
6565
event.CLIENT_SOCKET.sendall(bytes_to_send)
66-
print("Event Sent!")
66+
# end = time.time()
67+
# print('Send elapsed time: ' + str(end - begin))
68+
# print("Event Sent!")
6769

6870
def read(self):
6971
while True:
7072
self.read_aux()
7173

7274
def read_aux(self):
7375
event = self.disk_io_queue.get(block=True, timeout=None)
74-
print("after getting event")
7576
event = self.process_disk_io(event)
7677
# self.disk_io_queue.task_done()
77-
self.event_queue.enqueue(event)
78+
self.send_event(event)
79+
# self.event_queue.enqueue(event)
7880

7981
def process_disk_io(self, event):
80-
print("Read file from disk.")
82+
# begin = time.time()
8183
try:
8284
with open(os.path.dirname(__file__) + '/resources' + event.request_uri, 'rb') as f:
8385
event.response_bytes = f.read()
8486
# Newly read file is inserted into cache.
8587
self.cache.set(event.request_uri, event.response_bytes)
8688
except:
8789
raise EventLoopAppException(HTTP_404_NOT_FOUND, 'File does not exist. Cannot process event', event)
88-
90+
# end = time.time()
91+
# print('Disk I/O elapsed time: ' + str(end-begin))
8992
event.disk_io = False
90-
print("Done reading file from disk.")
9193
return event
9294

‎event_loop_app.py‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,10 @@ def accept_client(self, sock, mask):
3737
CLIENT_SOCKET.setblocking(False)
3838
# CLIENT_SOCKET.setsockopt(SOL_SOCKET, SO_KEEPALIVE, 1)
3939
sel.register(CLIENT_SOCKET, selectors.EVENT_READ, self.run)
40-
print('[INFO][%s] A client(%s) is connected.' % (ctime(), ADDR_INFO))
40+
#print('[INFO][%s] A client(%s) is connected.' % (ctime(), ADDR_INFO))
4141

4242
def run(self, CLIENT_SOCKET, mask):
43-
# data = CLIENT_SOCKET.recv(self.BUFFER_SIZE)
44-
data = CLIENT_SOCKET.recv(512)
43+
data = CLIENT_SOCKET.recv(self.BUFFER_SIZE)
4544

4645
# Receive data from the socket. The return value is a bytes object representing the data received.
4746
# The maximum amount of data to be received at once is specified by bufsize.
@@ -52,7 +51,7 @@ def run(self, CLIENT_SOCKET, mask):
5251
if data_size == 0:
5352
sel.unregister(CLIENT_SOCKET)
5453
CLIENT_SOCKET.close()
55-
print('Connection from client is disconnected.')
54+
# print('Connection from client is disconnected.')
5655
else:
5756
if decoded_data[-2:] != '\r\n':
5857
print('Bad Request(too long HTTP header)')
@@ -61,7 +60,7 @@ def run(self, CLIENT_SOCKET, mask):
6160
error_event.CLIENT_SOCKET = CLIENT_SOCKET
6261
raise EventLoopAppException(HTTP_400_BAD_REQUEST, 'Bad Request(too long HTTP header)', error_event)
6362
else:
64-
print('[INFO][%s] Received data from client.' % ctime())
63+
# print('[INFO][%s] Received data from client.' % ctime())
6564
parser = HTTPParser()
6665
event = parser.parse(decoded_data) # Request turned into event.
6766
event.CLIENT_SOCKET = CLIENT_SOCKET
@@ -82,7 +81,7 @@ def start(self):
8281
while True:
8382
try:
8483
events = sel.select() # standby here
85-
print("Current number of sockets that have things to read:" + str(len(events)))
84+
# print("Current number of sockets that have things to read:" + str(len(events)))
8685
for key, mask in events:
8786
callback = key.data
8887
# print(callback.__name__)

‎event_queue.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ def __init__(self):
55
self._queue = Queue()
66

77
def enqueue(self, event):
8-
print("Event is enqueued. Current event queue length: " + str(self.size()))
8+
# print("Event is enqueued. Current event queue length: " + str(self.size()))
99
self._queue.put(event)
1010

1111
def dequeue(self):
12-
print("Event is dequeued. Current event queue length: " + str(self.size()))
12+
# print("Event is dequeued. Current event queue length: " + str(self.size()))
1313
return self._queue.get(block=True, timeout=None)
1414

1515
def size(self):

‎http/http_parser.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def parse(self, request):
2727
# general-header, request-header, entity-header
2828
# especially, get 'Connection' info.
2929
info_dict = {}
30-
print('Additional info: ' + str(additional_info))
30+
# print('Additional info: ' + str(additional_info))
3131
for info in additional_info:
3232
if ': ' not in info:
3333
continue
@@ -40,10 +40,10 @@ def parse(self, request):
4040
# print('accept_ranges:' + self.accept_ranges)
4141

4242
# Check whether data is inserted correctly
43-
print('Method: ' + self.method)
44-
print('Request-URI: ' + self.request_uri)
45-
print('HTTP-Version: ' + self.protocol)
46-
print('Connection: ' + self.connection)
43+
#print('Method: ' + self.method)
44+
#print('Request-URI: ' + self.request_uri)
45+
#print('HTTP-Version: ' + self.protocol)
46+
#print('Connection: ' + self.connection)
4747

4848
# Check whether requested uri needs disk_io or not.
4949
return Event(self.method, self.request_uri, disk_io=(self.request_uri != '/'), connection = self.connection)

0 commit comments

Comments
 (0)