Add files via upload
This commit is contained in:
11
WallPostServer/WallPostServer.iml
Normal file
11
WallPostServer/WallPostServer.iml
Normal file
@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
Binary file not shown.
@ -0,0 +1,3 @@
|
||||
Manifest-Version: 1.0
|
||||
Main-Class: WallPostServer
|
||||
|
5
WallPostServer/src/HeaderNotFoundException.java
Normal file
5
WallPostServer/src/HeaderNotFoundException.java
Normal file
@ -0,0 +1,5 @@
|
||||
public class HeaderNotFoundException extends Exception{
|
||||
public HeaderNotFoundException(){
|
||||
super("Header not found");
|
||||
}
|
||||
}
|
3
WallPostServer/src/META-INF/MANIFEST.MF
Normal file
3
WallPostServer/src/META-INF/MANIFEST.MF
Normal file
@ -0,0 +1,3 @@
|
||||
Manifest-Version: 1.0
|
||||
Main-Class: WallPostServer
|
||||
|
73
WallPostServer/src/PermanentSorage.java
Normal file
73
WallPostServer/src/PermanentSorage.java
Normal file
@ -0,0 +1,73 @@
|
||||
import java.io.*;
|
||||
import java.net.Socket;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class PermanentSorage extends Thread{
|
||||
public UserList getUList() {
|
||||
return UList;
|
||||
}
|
||||
|
||||
public PostList getPList() {
|
||||
return PList;
|
||||
}
|
||||
|
||||
private UserList UList;
|
||||
private PostList PList;
|
||||
private FileInputStream FIn=null;
|
||||
private ObjectInputStream ObjIn=null;
|
||||
private boolean firstTime;
|
||||
public PermanentSorage(UserList UList, PostList PList) {
|
||||
this.UList=UList;
|
||||
this.PList=PList;
|
||||
try {
|
||||
File f = new File("SAVE.DAT");
|
||||
if(!f.exists()) {
|
||||
this.firstTime= f.createNewFile();
|
||||
}
|
||||
else {
|
||||
FIn = new FileInputStream(f);
|
||||
ObjIn = new ObjectInputStream(FIn);
|
||||
}
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
public void loadFromDisk(){
|
||||
try {
|
||||
if(!firstTime) {
|
||||
SaveContainer c = (SaveContainer) ObjIn.readObject();
|
||||
this.UList = c.getUl();
|
||||
this.PList = c.getPl();
|
||||
ObjIn.close();
|
||||
FIn.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void run(){
|
||||
while(true) {
|
||||
try {
|
||||
System.out.println("STARTING SAVE");
|
||||
FileOutputStream FOut= new FileOutputStream(new File("SAVE.DAT"));
|
||||
ObjectOutputStream ObjOut= new ObjectOutputStream(FOut);
|
||||
ObjOut.writeObject(new SaveContainer(UList,PList));
|
||||
FOut.close();
|
||||
Thread.sleep(60000);//10 sec
|
||||
System.out.println("END SAVE");
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
37
WallPostServer/src/Post.java
Normal file
37
WallPostServer/src/Post.java
Normal file
@ -0,0 +1,37 @@
|
||||
import java.io.Serializable;
|
||||
|
||||
public class Post implements Serializable {
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public String getBody() {
|
||||
return body;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public String getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
private String title, body,author, date;
|
||||
private int id;
|
||||
public void setBody(String body) {
|
||||
this.body = body;
|
||||
}
|
||||
|
||||
public Post(String title, String body, String author, String date, int id){
|
||||
this.title=title;
|
||||
this.body=body;
|
||||
this.author=author;
|
||||
this.date=date;
|
||||
this.id=id;
|
||||
}
|
||||
}
|
18
WallPostServer/src/PostList.java
Normal file
18
WallPostServer/src/PostList.java
Normal file
@ -0,0 +1,18 @@
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Random;
|
||||
|
||||
public class PostList extends ArrayList<Post> implements Serializable {
|
||||
public int getNewId(){
|
||||
int u;
|
||||
Random r = new Random();
|
||||
u=r.nextInt();
|
||||
for(int i=0;i<this.size();i++){
|
||||
u=r.nextInt();
|
||||
if(this.get(i).getId()==u){
|
||||
return this.getNewId();
|
||||
}
|
||||
}
|
||||
return u;
|
||||
}
|
||||
}
|
27
WallPostServer/src/SaveContainer.java
Normal file
27
WallPostServer/src/SaveContainer.java
Normal file
@ -0,0 +1,27 @@
|
||||
import java.io.Serializable;
|
||||
|
||||
public class SaveContainer implements Serializable {
|
||||
public UserList getUl() {
|
||||
return ul;
|
||||
}
|
||||
|
||||
public void setUl(UserList ul) {
|
||||
this.ul = ul;
|
||||
}
|
||||
|
||||
public PostList getPl() {
|
||||
return pl;
|
||||
}
|
||||
|
||||
public void setPl(PostList pl) {
|
||||
this.pl = pl;
|
||||
}
|
||||
|
||||
private UserList ul;
|
||||
private PostList pl;
|
||||
public SaveContainer(UserList ul, PostList pl){
|
||||
this.ul=ul;
|
||||
this.pl=pl;
|
||||
}
|
||||
|
||||
}
|
201
WallPostServer/src/ServerThread.java
Normal file
201
WallPostServer/src/ServerThread.java
Normal file
@ -0,0 +1,201 @@
|
||||
import java.io.*;
|
||||
import java.net.Socket;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
|
||||
public class ServerThread extends Thread {
|
||||
protected Socket socket;
|
||||
protected UserList UList;
|
||||
protected PostList PList;
|
||||
protected ArrayList<String> headers, values;
|
||||
public ServerThread(Socket clientSocket,UserList UList,PostList PList) {
|
||||
this.socket = clientSocket;
|
||||
this.UList=UList;
|
||||
this.PList=PList;
|
||||
}
|
||||
/*
|
||||
|
||||
ERROR
|
||||
TITLE
|
||||
END
|
||||
TEXT
|
||||
END
|
||||
END
|
||||
END
|
||||
|
||||
|
||||
ERROR
|
||||
TITLE
|
||||
END
|
||||
TEXT
|
||||
END
|
||||
QUIT
|
||||
QUIT
|
||||
|
||||
*/
|
||||
private String sanitize(String a){
|
||||
return a.replaceAll("FOLLOWS","follows").replaceAll("END","END");
|
||||
}
|
||||
private synchronized UserList accessUserList(){
|
||||
return this.UList;
|
||||
}
|
||||
private synchronized PostList accessPostList(){
|
||||
return this.PList;
|
||||
}
|
||||
public String valueByHeader(String header) throws HeaderNotFoundException {
|
||||
for(int i=0;i<headers.size();i++){
|
||||
if(headers.get(i).equals(header)){
|
||||
return values.get(i);
|
||||
}
|
||||
}
|
||||
throw new HeaderNotFoundException();
|
||||
}
|
||||
public void run() {
|
||||
InputStream inp = null;
|
||||
BufferedReader in = null;
|
||||
DataOutputStream out = null;
|
||||
try {
|
||||
inp = socket.getInputStream();
|
||||
in = new BufferedReader(new InputStreamReader(inp));
|
||||
out = new DataOutputStream(socket.getOutputStream());
|
||||
} catch (IOException e) {
|
||||
return;
|
||||
}
|
||||
String line,action=null,name="USER";
|
||||
headers= new ArrayList<String>();
|
||||
values= new ArrayList<String>();
|
||||
while (true) {
|
||||
try {
|
||||
line = in.readLine();
|
||||
if ((line == null) || line.equalsIgnoreCase("QUIT")) {
|
||||
socket.close();
|
||||
return;
|
||||
} else {
|
||||
if(line.startsWith("ACTION")) {
|
||||
headers.clear();
|
||||
values.clear();
|
||||
action=line.split(" ", 2)[1];
|
||||
while(!line.equals("END")){
|
||||
if(line.startsWith("FOLLOWS")){
|
||||
headers.add(line.split(" ", 2)[1]);
|
||||
}
|
||||
else{
|
||||
if(!line.startsWith("ACTION")) {
|
||||
values.add(line);
|
||||
}
|
||||
}
|
||||
line=in.readLine();
|
||||
}
|
||||
}
|
||||
if(line.equals("END")){
|
||||
if(action.equals("signIn")){
|
||||
UserList u=accessUserList();
|
||||
boolean access= u.signIn(new User(valueByHeader("username"),valueByHeader("password")));
|
||||
out.writeBytes("OK\n");
|
||||
out.writeBytes("FOLLOWS status\n");
|
||||
if(access){
|
||||
out.writeBytes("OK\n");
|
||||
out.writeBytes("END\n");
|
||||
name=valueByHeader("username");
|
||||
}
|
||||
else{
|
||||
out.writeBytes("NO\n");
|
||||
out.writeBytes("END\n");
|
||||
|
||||
}
|
||||
}
|
||||
else if(action.equals("signUp")){
|
||||
UserList u=accessUserList();
|
||||
boolean access= u.signUp(new User(valueByHeader("username"),valueByHeader("password")));
|
||||
out.writeBytes("OK\n");
|
||||
out.writeBytes("FOLLOWS status\n");
|
||||
if(access){
|
||||
out.writeBytes("OK\n");
|
||||
out.writeBytes("END\n");
|
||||
name=valueByHeader("username");
|
||||
}
|
||||
else{
|
||||
out.writeBytes("NO\n");
|
||||
out.writeBytes("END\n");
|
||||
|
||||
}
|
||||
}
|
||||
else if(name.equals("DEFAULT")){
|
||||
out.writeBytes("ERROR\n" +
|
||||
"Authentication Error\n" +
|
||||
"END\n" +
|
||||
"You must log in" +
|
||||
"END\n" +
|
||||
"QUIT\n" +
|
||||
"QUIT\n");
|
||||
}
|
||||
else if(action.equals("listPosts")){
|
||||
|
||||
PostList p= accessPostList();
|
||||
out.writeBytes("OK\n");
|
||||
for(int i=0;i<p.size();i++){
|
||||
//Post c= p.get(i);
|
||||
out.writeBytes("FOLLOWS POST-Id-"+String.valueOf(i) +"\n"+
|
||||
sanitize(String.valueOf(p.get(i).getId()))+"\n" +
|
||||
"FOLLOWS POST-Title-"+String.valueOf(i) +"\n"+
|
||||
sanitize(p.get(i).getTitle())+"\n" +
|
||||
"FOLLOWS POST-Author-"+String.valueOf(i) +"\n"+
|
||||
sanitize(p.get(i).getAuthor())+"\n" +
|
||||
"FOLLOWS POST-Date-"+String.valueOf(i) +"\n"+
|
||||
sanitize(p.get(i).getDate())+"\n");
|
||||
}
|
||||
out.writeBytes("END\n");
|
||||
}
|
||||
else if(action.equals("getPostBody")){
|
||||
PostList p= accessPostList();
|
||||
Post a= new Post("Error","Post not found","Server","01/01/1970",-1);
|
||||
for(int i=0;i<p.size();i++){
|
||||
if(p.get(i).getId()==Integer.parseInt(this.valueByHeader("POST-Id"))){
|
||||
a=p.get(i);
|
||||
out.writeBytes("OK\n");
|
||||
out.writeBytes("FOLLOWS body\n");
|
||||
out.writeBytes(a.getBody()+"\n");
|
||||
out.writeBytes("END\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if(action.equals("sendPost")){
|
||||
int ID=this.accessPostList().getNewId();
|
||||
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
|
||||
Date date = new Date();
|
||||
Post p= new Post(this.valueByHeader("POST-Title"),this.valueByHeader("POST-Body"),name,dateFormat.format(date),ID);
|
||||
PostList pl= accessPostList();
|
||||
pl.add(p);
|
||||
out.writeBytes("OK\n");
|
||||
out.writeBytes("FOLLOWS status\n");
|
||||
out.writeBytes("OK\n");
|
||||
out.writeBytes("END\n");
|
||||
}
|
||||
|
||||
}
|
||||
//out.writeBytes(line + "\n\r");
|
||||
out.flush();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
} catch (HeaderNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
try {
|
||||
out.writeBytes("ERROR\n" +
|
||||
"Request Error\n" +
|
||||
"END\n" +
|
||||
"The server received a malformed request\n" +
|
||||
"END\n" +
|
||||
"END\n" +
|
||||
"END\n");
|
||||
} catch (IOException ioException) {
|
||||
ioException.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
25
WallPostServer/src/User.java
Normal file
25
WallPostServer/src/User.java
Normal file
@ -0,0 +1,25 @@
|
||||
import java.io.Serializable;
|
||||
|
||||
public class User implements Serializable {
|
||||
private String username, password;
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
public User(String username, String password){
|
||||
this.username=username;
|
||||
this.password=password;
|
||||
}
|
||||
}
|
24
WallPostServer/src/UserList.java
Normal file
24
WallPostServer/src/UserList.java
Normal file
@ -0,0 +1,24 @@
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class UserList extends ArrayList<User> implements Serializable{
|
||||
public boolean signUp(User a){
|
||||
for(int i=0;i<this.size();i++){
|
||||
if(this.get(i).getUsername().equals(a.getUsername())){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
this.add(a);
|
||||
return true;
|
||||
}
|
||||
public boolean signIn(User a){
|
||||
|
||||
for(int i=0;i<this.size();i++){
|
||||
if(this.get(i).getUsername().equals(a.getUsername()) && this.get(i).getPassword().equals(a.getPassword())){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
34
WallPostServer/src/WallPostServer.java
Normal file
34
WallPostServer/src/WallPostServer.java
Normal file
@ -0,0 +1,34 @@
|
||||
import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
|
||||
public class WallPostServer {
|
||||
|
||||
static final int PORT = 5555;
|
||||
public static void main(String args[]) {
|
||||
ServerSocket serverSocket = null;
|
||||
Socket socket = null;
|
||||
UserList UList= new UserList();
|
||||
PostList PList= new PostList();
|
||||
/* PermanentSorage ps=new PermanentSorage(UList,PList);
|
||||
ps.loadFromDisk();
|
||||
UList=ps.getUList();
|
||||
PList=ps.getPList();
|
||||
ps.start();*/
|
||||
try {
|
||||
serverSocket = new ServerSocket(PORT);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
|
||||
}
|
||||
while (true) {
|
||||
try {
|
||||
socket = serverSocket.accept();
|
||||
} catch (IOException e) {
|
||||
System.out.println("I/O error: " + e);
|
||||
}
|
||||
// new thread for a client
|
||||
new ServerThread(socket,UList,PList).start();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user