Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Caco
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor 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
Vytor Calixto
Caco
Commits
d486321b
Commit
d486321b
authored
9 years ago
by
Vytor Calixto
Browse files
Options
Downloads
Plain Diff
Merge branch 'putServer' into put
Conflicts: definitions.h dirFunctions.cpp dirFunctions.h
parents
93355d56
16a56139
No related branches found
No related tags found
1 merge request
!1
Lento
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
Protocol.cpp
+11
-5
11 additions, 5 deletions
Protocol.cpp
dirFunctions.cpp
+27
-0
27 additions, 0 deletions
dirFunctions.cpp
dirFunctions.h
+6
-0
6 additions, 0 deletions
dirFunctions.h
server.cpp
+27
-3
27 additions, 3 deletions
server.cpp
with
71 additions
and
8 deletions
Protocol.cpp
+
11
−
5
View file @
d486321b
...
...
@@ -27,7 +27,7 @@ bool Protocol::sendMessages(int socket) {
}
bool
Protocol
::
sendMessage
(
int
socket
,
int
index
)
{
cout
<<
"message: "
<<
messages
[
index
]
<<
endl
;
cout
<<
"message
sent
: "
<<
messages
[
index
]
<<
endl
;
vector
<
BYTE
>
message
=
messages
[
index
].
getMessage
();
unsigned
char
*
msg
=
reinterpret_cast
<
unsigned
char
*>
(
message
.
data
());
int
status
=
send
(
socket
,
msg
,
messages
[
index
].
getMessageSize
(),
0
);
...
...
@@ -88,7 +88,7 @@ int Protocol::setData(vector<BYTE> data, int type){
int
Protocol
::
recvMessage
(
int
sockt
){
BYTE
dataRec
[
MAXSIZE
+
4
];
int
r
=
recv
(
sockt
,
dataRec
,
MAXSIZE
+
4
,
0
);
cout
<<
bitset
<
8
>
(
dataRec
[
0
])
<<
"|"
<<
bitset
<
8
>
(
dataRec
[
1
])
<<
"|"
<<
bitset
<
8
>
(
dataRec
[
2
])
<<
"|"
<<
bitset
<
8
>
(
dataRec
[
3
])
<<
"|
\t
"
;
cout
<<
"begin: "
<<
bitset
<
8
>
(
dataRec
[
0
])
<<
endl
;
cout
<<
"recv response: "
<<
r
<<
endl
;
if
(
dataRec
[
0
]
!=
BEGIN
){
return
NOISE
;
...
...
@@ -99,7 +99,6 @@ int Protocol::recvMessage(int sockt){
int
dataSize
=
size
<
MINSIZE
?
MINSIZE
:
size
;
msg
.
setBitFields
(
dataRec
[
0
],
dataRec
[
1
],
dataRec
[
2
],
dataRec
[
dataSize
+
3
]);
cout
<<
"Sequence:"
<<
msg
.
sequence
.
to_ulong
()
<<
"
\t
"
;
BYTE
msgData
[
size
];
memcpy
(
msgData
,
dataRec
+
3
,
size
);
msg
.
data
.
insert
(
msg
.
data
.
end
(),
msgData
,
msgData
+
size
);
...
...
@@ -107,6 +106,7 @@ int Protocol::recvMessage(int sockt){
messages
.
push_back
(
msg
);
cout
<<
"Tipo:"
<<
(
int
)
msg
.
type
.
to_ulong
()
<<
endl
;
cout
<<
"message received: "
<<
msg
<<
endl
;
if
(
!
msg
.
checkParity
()){
return
INCONSISTENT
;
}
...
...
@@ -190,6 +190,7 @@ int Protocol::receive(int sockt, int type, int window, bool dataEndable){
}
status
=
recvMessage
(
sockt
);
cout
<<
"receive status:"
<<
status
<<
endl
;
cout
<<
"sequence: "
<<
messages
.
back
().
sequence
.
to_ulong
()
<<
" next: "
<<
nextSequence
<<
endl
;
if
(
status
==
NOISE
){
continue
;
}
else
if
(
status
==
type
)
{
...
...
@@ -209,11 +210,16 @@ int Protocol::receive(int sockt, int type, int window, bool dataEndable){
nextSequence
=
(
messages
.
back
().
sequence
.
to_ulong
()
+
1
)
%
(
MAXSIZE
+
1
);
}
}
else
if
(
status
==
ERROR
)
{
string
str
(
messages
.
back
().
data
.
begin
(),
messages
.
back
().
data
.
end
());
cout
<<
"ERROR: "
<<
getDataAsString
()
<<
endl
;
cout
<<
"ERROR: "
<<
messages
.
back
().
getDataAsString
()
<<
endl
;
return
-
1
;
}
}
while
(
status
!=
end
);
if
(
dataEndable
){
response
.
reset
();
vector
<
BYTE
>
val
(
1
,(
BYTE
)
messages
.
back
().
sequence
.
to_ulong
());
response
.
setData
(
val
,
ACK
);
response
.
sendMessages
(
sockt
);
}
return
0
;
}
...
...
This diff is collapsed.
Click to expand it.
dirFunctions.cpp
+
27
−
0
View file @
d486321b
#include
<iostream>
#include
<fstream>
#include
<errno.h>
#include
<unistd.h>
#include
<string.h>
...
...
@@ -6,6 +7,7 @@
#include
<stdio.h>
//popen
#include
<sys/stat.h>
#include
<fstream>
#include
<sys/statvfs.h>
using
namespace
std
;
...
...
@@ -31,6 +33,11 @@ string ls(string args){
return
output
;
}
string
getWorkingPath
(){
char
temp
[
1024
];
return
(
getcwd
(
temp
,
1024
)
?
string
(
temp
)
:
string
(
""
));
}
bool
fexists
(
string
path
)
{
struct
stat
buffer
;
return
(
stat
(
path
.
c_str
(),
&
buffer
)
==
0
);
...
...
@@ -40,3 +47,23 @@ int filesize(string path) {
ifstream
in
(
path
,
ifstream
::
ate
|
ifstream
::
binary
);
return
in
.
tellg
();
}
bool
hasEnoughSpace
(
int
size
){
struct
statvfs
fsData
;
string
path
=
getWorkingPath
();
statvfs
(
path
.
c_str
(),
&
fsData
);
int
freeSpace
=
fsData
.
f_bsize
*
fsData
.
f_bfree
;
return
(
freeSpace
>
size
);
}
void
writeFile
(
string
path
,
vector
<
BYTE
>
data
){
cout
<<
"path: "
<<
path
<<
endl
;
string
strData
(
data
.
begin
(),
data
.
end
());
ofstream
file
(
path
);
if
(
file
.
is_open
()){
file
<<
strData
;
file
.
close
();
}
else
cout
<<
"Unable to open file"
;
}
This diff is collapsed.
Click to expand it.
dirFunctions.h
+
6
−
0
View file @
d486321b
...
...
@@ -13,4 +13,10 @@ bool fexists(string path);
int
filesize
(
string
path
);
bool
hasEnoughSpace
(
int
size
);
string
getWorkingPath
();
void
writeFile
(
string
path
,
vector
<
BYTE
>
data
);
#endif
This diff is collapsed.
Click to expand it.
server.cpp
+
27
−
3
View file @
d486321b
...
...
@@ -19,8 +19,7 @@ int main(){
cout
<<
"Recebeu CD
\n
"
;
cout
<<
"CD: "
<<
receiveProtocol
.
getDataAsString
()
<<
endl
;
cd
(
receiveProtocol
.
getDataAsString
());
vector
<
BYTE
>
val
(
1
,(
BYTE
)
0
);
sendProtocol
.
setData
(
val
,
OK
);
sendProtocol
.
setData
(
vector
<
BYTE
>
(
1
,(
BYTE
)
0
),
OK
);
sendProtocol
.
sendMessage
(
sockt
,
0
);
}
else
if
(
status
==
LS
){
cout
<<
"protocol data: "
<<
receiveProtocol
.
getDataAsString
()
<<
endl
;
...
...
@@ -31,9 +30,34 @@ int main(){
sendProtocol
.
transmit
(
sockt
,
WAIT_STOP
);
cout
<<
"finished transmit"
<<
endl
;
}
else
if
(
status
==
PUT
){
//TODO
string
fileName
=
receiveProtocol
.
getDataAsString
();
cout
<<
"fileName: "
<<
fileName
<<
endl
;
sendProtocol
.
setData
(
vector
<
BYTE
>
(
1
,(
BYTE
)
0
),
OK
);
sendProtocol
.
sendMessage
(
sockt
,
0
);
receiveProtocol
.
reset
();
receiveProtocol
.
receive
(
sockt
,
SIZE
,
WAIT_STOP
,
false
);
cout
<<
"fileSize: "
<<
receiveProtocol
.
getDataAsString
()
<<
endl
;
int
fileSize
=
stoi
(
receiveProtocol
.
getDataAsString
());
sendProtocol
.
reset
();
if
(
hasEnoughSpace
(
fileSize
)){
sendProtocol
.
setData
(
vector
<
BYTE
>
(
1
,(
BYTE
)
0
),
OK
);
sendProtocol
.
sendMessage
(
sockt
,
0
);
}
else
{
sendProtocol
.
setData
(
vector
<
BYTE
>
(
1
,(
BYTE
)
SPACE_ERR
),
ERROR
);
sendProtocol
.
sendMessage
(
sockt
,
0
);
}
receiveProtocol
.
reset
();
receiveProtocol
.
receive
(
sockt
,
DATA
,
SLIDING
,
true
);
cout
<<
"conteudo: "
<<
receiveProtocol
.
getDataAsString
()
<<
endl
;
writeFile
(
getWorkingPath
()
+
"/"
+
fileName
,
receiveProtocol
.
getData
());
}
else
if
(
status
==
GET
){
//TODO
}
else
if
(
status
==
ENDTX
){
sendProtocol
.
reset
();
vector
<
BYTE
>
val
(
1
,(
BYTE
)
receiveProtocol
.
getMessages
().
back
().
sequence
.
to_ulong
());
sendProtocol
.
setData
(
val
,
ACK
);
sendProtocol
.
sendMessages
(
sockt
);
}
}
catch
(
char
const
*
strException
){
cout
<<
"Erro:"
<<
strException
<<
endl
;
...
...
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