File tree 3 files changed +62
-2
lines changed
3 files changed +62
-2
lines changed Original file line number Diff line number Diff line change 2
2
bin /tracker
3
3
bin /files
4
4
bin /pieces
5
+ bin /t_server
5
6
* .totient
6
7
* ~
7
8
* .o
Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ LDFLAGS=-lzmqpp -lzmq -lpthread -lsfml-audio -luuid
4
4
SOURCE =./src
5
5
TARGET =./bin
6
6
7
- all : dir $(TARGET ) /peer $(TARGET ) /tracker
7
+ all : dir $(TARGET ) /peer $(TARGET ) /tracker $( TARGET ) /t_server
8
8
9
9
dir :
10
10
mkdir -p $(TARGET ) /files
@@ -16,6 +16,9 @@ $(TARGET)/peer: $(SOURCE)/peer.cc
16
16
$(TARGET ) /tracker : $(SOURCE ) /tracker.cc
17
17
$(CC ) $(CFLAGS ) $(LDFLAGS ) -o $(TARGET ) /tracker $(SOURCE ) /tracker.cc
18
18
19
+ $(TARGET ) /t_server : $(SOURCE ) /t_server.cc
20
+ $(CC ) $(CFLAGS ) $(LDFLAGS ) -o $(TARGET ) /t_server $(SOURCE ) /t_server.cc
21
+
19
22
20
23
clean :
21
- rm -rf $(TARGET ) /peer $(TARGET ) /tracker
24
+ rm -rf $(TARGET ) /peer $(TARGET ) /tracker $( TARGET ) /t_server
Original file line number Diff line number Diff line change
1
+ #include < set>
2
+ #include < string>
3
+ #include < unordered_map>
4
+ #include < zmqpp/zmqpp.hpp>
5
+ #include " utils.cc"
6
+
7
+ using namespace std ;
8
+ using namespace zmqpp ;
9
+
10
+ int main (int argc, char **argv) {
11
+ if (argc < 2 ) {
12
+ cout << " Usage " << argv[0 ] << " port " << endl;
13
+ exit (1 );
14
+ }
15
+
16
+ string port = argv[1 ];
17
+ cout << string_color (" Server running on port: " + port + " \n " );
18
+
19
+ context ctx;
20
+ socket frontend (ctx, socket_type::rep);
21
+ frontend.connect (" tcp://localhost:" + port);
22
+
23
+ poller pol;
24
+ pol.add (frontend);
25
+
26
+ while (true ) {
27
+ if (pol.poll ()) {
28
+ if (pol.has_input (frontend)) {
29
+ message request;
30
+ frontend.receive (request);
31
+ string filename;
32
+ request >> filename;
33
+ filename = " ./totient/" + filename;
34
+ message response;
35
+ if (file_exists (filename)) {
36
+ ifstream file;
37
+ response << " OK" ;
38
+ file.open (filename);
39
+ file.seekg (0 , file.end );
40
+ int length = file.tellg ();
41
+ file.seekg (0 , file.beg );
42
+ char *data = new char [length];
43
+ file.read (data, length);
44
+ string chunk (data, file.gcount ());
45
+ response << chunk;
46
+ file.close ();
47
+ delete[] data;
48
+ } else {
49
+ response << " NF" ;
50
+ }
51
+ frontend.send (response);
52
+ }
53
+ }
54
+ }
55
+
56
+ }
You can’t perform that action at this time.
0 commit comments