Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cse550
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Winston Jodjana
cse550
Commits
d94f151c
Commit
d94f151c
authored
2 years ago
by
Mengqi Chen
Browse files
Options
Downloads
Patches
Plain Diff
main thread initial layout
parent
2a33ec9d
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
assignment1/partb/550server.cpp
+36
-39
36 additions, 39 deletions
assignment1/partb/550server.cpp
assignment1/partb/threadpool.h
+2
-0
2 additions, 0 deletions
assignment1/partb/threadpool.h
with
38 additions
and
39 deletions
assignment1/partb/550server.cpp
+
36
−
39
View file @
d94f151c
...
...
@@ -7,6 +7,7 @@
#include
<cstring>
#include
<sys/poll.h>
#include
<fcntl.h>
#include
<unordered_map>
#include
"server_util.h"
#include
"threadpool.h"
...
...
@@ -37,6 +38,7 @@ int main(int argc, char **argv) {
int
num_fds
=
1
;
int
poll_timeout
=
-
1
;
// -1 is same as infinite timeout
bool
resize_events_arr
=
false
;
unordered_map
<
int
,
Work
*>
worker_to_client
;
// read end of a worker -> work to do map
// Check for valid number of arguments
if
(
argc
!=
3
)
...
...
@@ -137,33 +139,57 @@ int main(int argc, char **argv) {
// TODO: clean up;
continue
;
}
Work
*
thread_work
=
new
Work
;
thread_work
->
filepath
=
req_file_path
;
// pipe_fds[1] is write end of pipe
thread_work
->
pipe_fd
=
pipe_fds
[
1
];
thread_work
->
written
=
0
;
thread_work
->
client_fd
=
new_client_fd
;
// add pollfd
events
[
num_fds
].
fd
=
new_client_fd
;
events
[
num_fds
].
fd
=
pipe_fds
[
0
]
;
events
[
num_fds
].
events
=
POLLIN
;
num_fds
++
;
// dispatch thread
HttpServerTask
*
worker_task
=
new
HttpServerTask
(
HttpServer_ThrFn
);
worker_task
->
work
=
thread_work
;
worker_to_client
[
pipe_fds
[
0
]]
=
thread_work
;
tp
.
Dispatch
(
worker_task
);
}
else
{
// this is probably a worker thread done with the work
Work
*
thread_work
=
worker_to_client
[
events
[
i
].
fd
];
if
(
thread_work
==
nullptr
)
{
cerr
<<
" worker_to_client mapping corrupted for fd: "
<<
events
[
i
].
fd
<<
"
\n
"
;
// TODO: need some action
}
// return data to client
int
written_bytes
=
WrappedWrite
(
thread_work
->
client_fd
,
(
unsigned
char
*
)(
thread_work
->
buffer
),
thread_work
->
written
);
if
(
written_bytes
!=
thread_work
->
written
)
{
cerr
<<
" actual written: "
<<
written_bytes
<<
", expected: "
<<
thread_work
->
written
<<
"
\n
"
;
}
// clean up resource: pipes, client socket, work struct
close
(
thread_work
->
client_fd
);
close
(
events
[
i
].
fd
);
close
(
thread_work
->
pipe_fd
);
delete
thread_work
;
events
[
i
].
fd
=
-
1
;
resize_events_arr
=
true
;
}
}
if
(
resize_events_arr
)
{
// at least 1 event has been deleted, need to resize
if
(
resize_events_arr
)
{
resize_events_arr
=
false
;
for
(
int
i
=
0
;
i
<
num_fds
;
i
++
)
{
if
(
events
[
i
].
fd
==
-
1
)
{
for
(
int
j
=
i
;
j
<
num_fds
;
j
++
)
{
for
(
int
i
=
0
;
i
<
num_fds
;
i
++
)
{
if
(
events
[
i
].
fd
==
-
1
)
{
for
(
int
j
=
i
;
j
<
num_fds
;
j
++
)
{
events
[
j
].
fd
=
events
[
j
+
1
].
fd
;
}
i
--
;
...
...
@@ -171,35 +197,6 @@ int main(int argc, char **argv) {
}
}
}
// TODO: initialize shared data structure
// TODO: modify logic to add threapool
// TODO: use server_socket class
/*
1) accept client connection
2) initialize stuff for communication with threadpool
3) read from client and write to thread
4) get back info from thread
*/
// read from client fd and write to stdout
while
(
1
)
{
int
rlen
=
WrappedRead
(
client_fd
,
buf
,
BUFFER_SIZE
);
// check for reading failure
// on error, clean up and exit with failure
if
(
rlen
<=
0
)
{
break
;
}
int
wlen
=
WrappedWrite
(
STDOUT_FILENO
,
buf
,
rlen
);
// check that we wrote the same amount as we read in
// on error, clean up and exit with failure
if
(
wlen
!=
rlen
)
{
break
;
}
}
}
return
EXIT_SUCCESS
;
...
...
This diff is collapsed.
Click to expand it.
assignment1/partb/threadpool.h
+
2
−
0
View file @
d94f151c
...
...
@@ -79,7 +79,9 @@ class HttpServerTask : public ThreadPool::Task {
struct
Work
{
string
filepath
;
char
buffer
[
FILE_BUFFER_SIZE
];
// Buffer for main thread to read file contents from worker thread
int
written
;
// Number of bytes (char) that the worker thread has written to the buffer
int
pipe_fd
;
// Write end for the pipe. Used to notify main thread that the worker is done writing the file contents.
int
client_fd
;
// Client that this work is for
};
#endif // THREADPOOL_H_
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment