Social Network Site
SOCIAL NETWORK SITE USING RMI
1.Remote interface – SocialNetworkService
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.List;
public interface SocialNetworkService extends Remote {
String registerUser(String username) throws RemoteException;
String postMessage(String username, String content) throws RemoteException;
List<Post> getUserPosts(String username) throws RemoteException;
List<Post> getAllPosts() throws RemoteException;
}
2.Server-side implementation – SocialNetworkServiceImpl
import java.rmi.server.UnicastRemoteObject;
import java.rmi.RemoteException;
import java.util.*;
public class SocialNetworkServiceImpl extends UnicastRemoteObject implements SocialNetworkService {
private Set<String> users;
private List<Post> posts;
protected SocialNetworkServiceImpl() throws RemoteException {
users = new HashSet<>();
posts = new ArrayList<>();
}
public synchronized String registerUser(String username) throws RemoteException {
if (users.contains(username)) {
return "Username already exists!";
}
users.add(username);
return "User " + username + " registered successfully!";
}
public synchronized String postMessage(String username, String content) throws RemoteException {
if (!users.contains(username)) {
return "User not registered.";
}
posts.add(new Post(username, content));
return "Post added!";
}
public List<Post> getUserPosts(String username) throws RemoteException {
List<Post> userPosts = new ArrayList<>();
for (Post p : posts) {
if (p.getUsername().equals(username)) {
userPosts.add(p);
}
}
return userPosts;
}
public List<Post> getAllPosts() throws RemoteException {
return posts;
}
}
3.Server launcher – SocialNetworkServer
import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;
public class SocialNetworkServer {
public static void main(String[] args) {
try {
LocateRegistry.createRegistry(1099);
SocialNetworkService service = new SocialNetworkServiceImpl();
Naming.rebind("SocialNetworkService", service);
System.out.println("Social Network RMI Server is running...");
} catch (Exception e) {
System.out.println("Server error: " + e.getMessage());
}
}
}
4.Console-based client – SocialNetworkClient
import java.rmi.Naming;
import java.util.List;
public class SocialNetworkClient {
public static void main(String[] args) {
try {
SocialNetworkService service = (SocialNetworkService) Naming.lookup("rmi://localhost/SocialNetworkService");
System.out.println(service.registerUser("alice"));
System.out.println(service.registerUser("bob"));
System.out.println(service.postMessage("alice", "Hello, this is Alice!"));
System.out.println(service.postMessage("bob", "Hey, Bob here."));
System.out.println(service.postMessage("alice", "Loving this social site!"));
System.out.println("\n--- Alice's Posts ---");
List<Post> alicePosts = service.getUserPosts("alice");
for (Post post : alicePosts) {
System.out.println(post);
}
System.out.println("\n--- All Posts ---");
List<Post> allPosts = service.getAllPosts();
for (Post post : allPosts) {
System.out.println(post);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
5.Data model for posts – Post
import java.io.Serializable;
import java.time.LocalDateTime;
public class Post implements Serializable {
private static final long serialVersionUID = 1L;
private String username;
private String content;
private LocalDateTime timestamp;
public Post(String username, String content) {
this.username = username;
this.content = content;
this.timestamp = LocalDateTime.now();
}
public String getUsername() {
return username;
}
public String getContent() {
return content;
}
public LocalDateTime getTimestamp() {
return timestamp;
}
public String toString() {
return "[" + timestamp + "] @" + username + ": " + content;
}
}
6.Expected Output
Server
Client
Comments
Post a Comment