Add files via upload

This commit is contained in:
MatMasIt 2021-06-17 01:47:29 +02:00 committed by GitHub
parent 0ca4ebb0cd
commit d27fa84ab4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 1016 additions and 0 deletions

25
README.md Normal file
View File

@ -0,0 +1,25 @@
# WallPost
A Java Swing application which allows to post articles on a remote "wall"
Includes:
* SignUp/SignIn
* Posting stories
* Reading stories
* Multi-threaded server
* State persistence
Employs a custom protocol in communication ("FOLLOWS protocol" to exchange variables)
> **Notice**
> This is just a demonstrative application
> It lacks basic functionalities as editing and upvoting, and relies on terrible storage and authentication, in fact it does not even use SSL or TLS
> Be aware of this
* WallPost: The client
* WallPostServer: The server

11
WallPost/WallPost.iml Normal file
View 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>

View File

@ -0,0 +1,5 @@
public class HeaderNotFoundException extends Exception{
public HeaderNotFoundException(){
super("Header not found");
}
}

35
WallPost/src/Post.java Normal file
View File

@ -0,0 +1,35 @@
public class Post {
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;
}
}

View File

@ -0,0 +1,5 @@
import java.util.ArrayList;
public class PostList extends ArrayList<Post>{
}

View File

@ -0,0 +1,230 @@
import javax.swing.*;
import java.io.*;
import java.lang.reflect.Array;
import java.net.Socket;
import java.rmi.ServerError;
import java.util.ArrayList;
public class ServerConnection {
private Socket socket;
private InputStreamReader i;
private BufferedReader in;
private OutputStreamWriter osw;
private BufferedWriter bw;
private PrintWriter out;
private ArrayList<String> headers=new ArrayList<String>();
private ArrayList<String> values=new ArrayList<String>();
private JFrame window;
public ServerConnection(JFrame window) throws IOException {
this.window=window;
socket= new Socket("mascmt.ddns.net",70);
i= new InputStreamReader(socket.getInputStream());
in = new BufferedReader(i);
osw= new OutputStreamWriter(socket.getOutputStream());
bw =new BufferedWriter(osw);
out= new PrintWriter(bw, true);
out.println("HELLO");
}
public ArrayList<ArrayList<String>> sendData(String action) throws IOException, ServerErrorException {//lenght of Arralist[] is 2 as in headers, values
ArrayList<ArrayList<String>> hv=new ArrayList<ArrayList<String>>();
hv.add(new ArrayList<String>());
hv.add(new ArrayList<String>());
out.println("HELLO AGAIN");
out.println("ACTION " + action);
for (int c = 0; c < headers.size(); c++) {
out.println("FOLLOWS " + headers.get(c).replaceAll("FOLLOWS","follows").replaceAll("END","end"));
out.println(values.get(c).replaceAll("FOLLOWS","follows").replaceAll("END","end"));
}
out.println("END");
String line="",temp="";
if(in.readLine().equals("ERROR")){
while(true){
line=in.readLine();
if(line.equals("END")){
String title=in.readLine();
String type=in.readLine();
if(in.readLine().equals("QUIT")){
throw new ServerErrorException(title,temp,in.readLine(),true);
}
else{
throw new ServerErrorException(title,temp,in.readLine(),false);
}
}
else{
temp+=line;
}
}
}
int flNum=0;
do{
line=in.readLine();
if(line.startsWith("FOLLOWS")){
hv.get(0).add(line.split(" ",2)[1]);
if(flNum!=0){
hv.get(1).add(temp);
}
temp="";
flNum++;
}
else if(line.equals("END")){
hv.get(1).add(temp);
}
else{
temp+=line;
}
}while (!line.equals("END"));
return hv;
}
public void init(){
headers.clear();
values.clear();
}
public void bindParam(String h,String v){
headers.add(h);
values.add(v);
}
public ArrayList<ArrayList<String>> handledGetData(String action){
ArrayList<ArrayList<String>> s=new ArrayList<ArrayList<String>>(2);
try {
s=this.sendData(action);
} catch (IOException e) {
JOptionPane.showMessageDialog(window,
"Connection error",
"Could not connect to the server",
JOptionPane.ERROR_MESSAGE);
} catch (ServerErrorException e) {
handleServerErrorInGUI(e.title,e.message,e.errorType,e.mustQuit);
}
System.out.println(s.toString());
return s;
}
public void handleServerErrorInGUI(String title, String text, String type, boolean mustQuit){
if(type.equals("INFO")){
JOptionPane.showMessageDialog(this.window, title, text,JOptionPane.INFORMATION_MESSAGE);
}
else if(type.equals("WARNING")){
JOptionPane.showMessageDialog(this.window,
title,
text,
JOptionPane.WARNING_MESSAGE);
}
else if(type.equals("PLAIN")){
JOptionPane.showMessageDialog(this.window,
title,
text,
JOptionPane.PLAIN_MESSAGE);
}
else{//ERROR
JOptionPane.showMessageDialog(this.window,
title,
text,
JOptionPane.ERROR_MESSAGE);
}
if(mustQuit) {
System.exit(1);
}
}
public String valueByHeader(ArrayList<ArrayList<String>> resp,String header) throws HeaderNotFoundException {
for(int i=0;i<resp.get(0).size();i++){
if(resp.get(0).get(i).equals(header)){
return resp.get(1).get(i);
}
}
throw new HeaderNotFoundException();
}
public boolean signIn(String username, String password){
this.init();
this.bindParam("username",username);
this.bindParam("password",password);
ArrayList<ArrayList<String>> li= this.handledGetData("signIn");
try {
return this.valueByHeader(li,"status").trim().equals("OK");
} catch (HeaderNotFoundException e) {
return false;
}
}
public boolean signUp(String username, String password){
this.init();
this.bindParam("username",username);
this.bindParam("password",password);
ArrayList<ArrayList<String>> li= this.handledGetData("signUp");
try {
return this.valueByHeader(li,"status").trim().equals("OK");
} catch (HeaderNotFoundException e) {
return false;
}
}
public PostList fetchPostsFromServer(){
this.init();
ArrayList<ArrayList<String>> li =this.handledGetData("listPosts");
PostList p= new PostList();
int count=0;
for(int i=0; i<li.get(0).size();i++){
System.out.println(li.get(0).get(i));
if(li.get(0).get(i).startsWith("POST-Id")){//POST-Id-0
try {
Post tp= new Post(this.valueByHeader(li,"POST-Title-"+String.valueOf(count)),"empty",this.valueByHeader(li,"POST-Author-"+String.valueOf(count)),this.valueByHeader(li,"POST-Date-"+String.valueOf(count)),Integer.parseInt(this.valueByHeader(li,"POST-Id-"+String.valueOf(count))));
p.add(tp);
} catch (HeaderNotFoundException e) {
e.printStackTrace();
}
count++;
}
}
return p;
}
public Post fetchPostBodyIfEmpty(PostList list, int index){
if(index<0){
return null;
}
Post a=list.get(index);
System.out.println(a.toString());
if(a.getBody().equals("empty")){
this.init();
this.bindParam("POST-Id", String.valueOf(a.getId()));
ArrayList<ArrayList<String>> li=this.handledGetData("getPostBody");
String body="";
try {
body=this.valueByHeader(li,"body");
} catch (HeaderNotFoundException e) {
body="An error occurred while loading the post";
}
a.setBody(body);
list.set(index,a);
}
return a;
}
public void publishPost(String title,String body){
this.init();
this.bindParam("POST-Title",title);
this.bindParam("POST-Body",body);
this.handledGetData("sendPost");
}
/*
NOT SUPPORTED BY SERVER
@Deprecated
public Post getPost(int Id) throws HeaderNotFoundException {
this.init();
this.bindParam("POST-Id",String.valueOf(Id));
ArrayList<ArrayList<String>> li=this.handledGetData("getPost");
Post p= new Post(this.valueByHeader(li,"POST-Title"),this.valueByHeader(li,"POST-Body"),this.valueByHeader(li,"POST-Author"),this.valueByHeader(li,"POST-Date"),Integer.parseInt(this.valueByHeader(li,"POST-Id")));
return p;
}
*/
}

View File

@ -0,0 +1,13 @@
import javax.swing.*;
public class ServerErrorException extends Exception{
public String message,errorType, title;
public boolean mustQuit;
public ServerErrorException(String title, String message, String errorType, boolean mustQuit){
super("The server issued an error");
this.message=message;
this.title=title;
this.errorType = errorType;
this.mustQuit=mustQuit;
}
}

231
WallPost/src/Window.java Normal file
View File

@ -0,0 +1,231 @@
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.io.IOException;
public class Window extends JFrame implements ActionListener {
private JPanel auth, list, viewOne, mine;
private JLabel labelUsername, labelPassword, postHeader;
private JTextField username, titleField;
private JPasswordField password;
private JButton signIn, signUp, make, send, backList,backList2;
private JScrollPane TablePane, textViewPane, textEditPane,postHeaderPane;
private JTable table;
private JTextArea view, write;
private DefaultTableModel tb;
private ServerConnection server;
private PostList PL;
private Post CP;
public Window() throws IOException {
initComponents();
server= new ServerConnection(this);
PL=null;
}
public void initComponents(){
auth=new JPanel();
labelUsername= new JLabel("Username");
labelPassword=new JLabel("Password");
username = new JTextField(15);
password = new JPasswordField(15);
signIn=new JButton("Sign In");
signUp=new JButton("Sign Up");
auth.setLayout(new GridLayout(3,2));
auth.add(labelUsername);
auth.add(username);
auth.add(labelPassword);
auth.add(password);
auth.add(signIn);
auth.add(signUp);
list=new JPanel();
Object[][] data = {};
String[] columnNames = {"User","Title","Date"};
tb=new DefaultTableModel(data,columnNames);
table = new JTable(tb);
TablePane= new JScrollPane(table);
make=new JButton("Write");
list.setLayout(new GridLayout(2,1));
list.add(TablePane);
list.add(make);
send= new JButton("Send");
this.setLayout(new FlowLayout());
//this.add(list,new GridBagConstraints());
viewOne= new JPanel();
view= new JTextArea(15,50);
backList2= new JButton("Back To list");
textViewPane= new JScrollPane(view);
postHeader=new JLabel("Title, details");
postHeaderPane=new JScrollPane(postHeader, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
viewOne.setLayout(new GridLayout(3,1));
viewOne.add(postHeaderPane);
viewOne.add(textViewPane);
viewOne.add(backList2);
mine=new JPanel();
titleField=new JTextField(15);
titleField.setForeground(Color.GRAY);
titleField.setText("Title");
titleField.setForeground(Color.GRAY);
titleField.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
if (titleField.getText().equals("Title")) {
titleField.setText("");
titleField.setForeground(Color.BLACK);
}
}
@Override
public void focusLost(FocusEvent e) {
if (titleField.getText().isEmpty()) {
titleField.setForeground(Color.GRAY);
titleField.setText("Title");
}
}
});
write=new JTextArea(15,50);
write.setForeground(Color.GRAY);
write.setText("Body");
write.setForeground(Color.GRAY);
write.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
if (write.getText().equals("Body")) {
write.setText("");
write.setForeground(Color.BLACK);
}
}
@Override
public void focusLost(FocusEvent e) {
if (write.getText().isEmpty()) {
write.setForeground(Color.GRAY);
write.setText("Body");
}
}
});
textEditPane= new JScrollPane(write);
backList= new JButton("Back To list");
mine.setLayout(new GridLayout(4,1));
mine.add(titleField);
mine.add(textEditPane);
mine.add(send);
mine.add(backList);
signIn.addActionListener(this);
signUp.addActionListener(this);
make.addActionListener(this);
backList.addActionListener(this);
backList2.addActionListener(this);
send.addActionListener(this);
this.add(auth);
this.add(list);
this.add(viewOne);
this.add(mine);
list.setVisible(false);
viewOne.setVisible(false);
mine.setVisible(false);
//this.add(auth,new GridBagConstraints());
table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent event) {
// do some actions here, for example
// print first column value from selected row
CP=server.fetchPostBodyIfEmpty(PL,table.getSelectedRow());
if (CP!= null) {
postHeader.setText(CP.getTitle() + ", written by " + CP.getAuthor() + " at " + CP.getDate());
view.setText(CP.getBody());
list.setVisible(false);
viewOne.setVisible(true);
}
}
});
}
public static void main(String args[]) throws IOException {
Window window= new Window();
window.setSize(600,1000);
window.setTitle("WallPost");
window.setVisible(true);
}
public void updateTable(PostList a) {
tb.setRowCount(0);//Imposta il numero di righe a 0 eliminando tutte le righe presenti
for (int i = 0; i < a.size(); i++) {
Object[] row = {a.get(i).getAuthor(),a.get(i).getTitle(), a.get(i).getDate()};//Crea riga temporanea
tb.addRow(row);//aggiungi riga
}
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(signIn)){
if(server.signIn(username.getText(),new String(password.getPassword()))) {
PL=server.fetchPostsFromServer();
this.updateTable(PL);
auth.setVisible(false);
list.setVisible(true);
}
else{
JOptionPane.showMessageDialog(this,
"Wrong credentials",
"Cannot sign in",
JOptionPane.WARNING_MESSAGE);
}
}
else if(e.getSource().equals(signUp)){
if(server.signUp(username.getText(),new String(password.getPassword()))) {
updt();
auth.setVisible(false);
list.setVisible(true);
}
else{
JOptionPane.showMessageDialog(this,
"User Taken",
"Cannot sign up",
JOptionPane.WARNING_MESSAGE);
}
}
else if(e.getSource().equals(make)){
list.setVisible(false);
mine.setVisible(true);
}
else if(e.getSource().equals(backList)||e.getSource().equals(backList2)){
updt();
viewOne.setVisible(false);
mine.setVisible(false);
list.setVisible(true);
}
else if(e.getSource().equals(send)){
server.publishPost(titleField.getText(),write.getText());
updt();
mine.setVisible(false);
list.setVisible(true);
}
}
public void updt(){
PL=server.fetchPostsFromServer();
this.updateTable(PL);
}
}

View 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>

View File

@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: WallPostServer

View File

@ -0,0 +1,5 @@
public class HeaderNotFoundException extends Exception{
public HeaderNotFoundException(){
super("Header not found");
}
}

View File

@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: WallPostServer

View 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();
}
}
}
}

View 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;
}
}

View 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;
}
}

View 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;
}
}

View 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();
}
}
}
}
}

View 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;
}
}

View 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;
}
}

View 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();
}
}
}