Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.net.*;
- import java.io.*;
- import java.rmi.NotBoundException;
- import java.rmi.RemoteException;
- import java.rmi.registry.LocateRegistry;
- import java.util.LinkedHashMap;
- public class TCPServerImpl extends java.rmi.server.UnicastRemoteObject implements TCPServer{
- static RMIServer RMI = null;
- public TCPServerImpl() throws java.rmi.RemoteException{
- super();
- }
- public static void main(String args[]){
- try {
- rmiConnection();
- int number=0;
- try {
- int serverPort = 6000;
- System.out.println("Listening on port 6000!");
- ServerSocket listenSocket = new ServerSocket(serverPort);
- System.out.println("LISTEN SOCKET="+listenSocket);
- while(true) {
- Socket clientSocket = listenSocket.accept(); // BLOQUEANTE
- System.out.println("CLIENT_SOCKET (created at accept())="+clientSocket);
- number++;
- new Connection(clientSocket, number);
- }
- } catch(IOException e) {
- System.out.println("Listen:" + e.getMessage());
- }
- } catch (Exception e) {
- System.out.println("Exception in main: " + e);
- }
- }
- static void rmiConnection(){
- try {
- TCPServerImpl.RMI = (RMIServer) LocateRegistry.getRegistry(7000).lookup("iBei");
- } catch (RemoteException | NotBoundException e1) {
- e1.printStackTrace();
- }
- }
- }
- class Connection extends Thread {
- PrintWriter out;
- BufferedReader in = null;
- private Socket clientSocket;
- private int thread_number;
- public Connection (Socket newClientSocket, int number) {
- this.thread_number = number;
- try{
- this.clientSocket = newClientSocket;
- this.in = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));
- this.out = new PrintWriter(this.clientSocket.getOutputStream(), true);
- this.start();
- }catch(IOException e){System.out.println("Connection:" + e.getMessage());}
- }
- //=============================
- public void run(){
- String resposta;
- try{
- while(true){
- //an echo server
- String data = in.readLine();
- System.out.println("T["+thread_number + "] Recebeu: "+data);
- parseUserInput(data);
- //resposta=String.valueOf(RMI.register("Dinis","dinis","dinis"));
- //out.writeUTF(resposta);
- }
- }catch(EOFException e){System.out.println("EOF:" + e);
- }catch(IOException e){System.out.println("IO:" + e);}
- }
- private void parseUserInput(String data){
- String[] aux;
- LinkedHashMap<String, String> parsedInput = new LinkedHashMap<String, String>();
- aux = data.split(",");
- for (String field : aux) {
- String[] split = field.split(":");
- String firstSubString = split[0].trim();
- String secondSubString = split[1].trim();
- parsedInput.put(firstSubString, secondSubString);
- }
- System.out.println(parsedInput);
- chosenType(parsedInput);
- }
- private void chosenType(LinkedHashMap<String, String> parsedInput){
- String type = parsedInput.get("type");
- switch(type){
- case "login":
- login(parsedInput);
- break;
- case "status":
- break;
- case "item_list":
- System.out.println("123");
- break;
- case "register":
- register(parsedInput);
- break;
- case "create_auction":
- create_auction(parsedInput);
- break;
- case "search_auction":
- System.out.println("123");
- break;
- case "detail_auction":
- System.out.println("123");
- break;
- case "my_auctions":
- System.out.println("123");
- break;
- case "bid":
- System.out.println("123");
- break;
- case "edit_auction":
- System.out.println("123");
- break;
- case "message":
- System.out.println("123");
- break;
- case "online_users":
- System.out.println("123");
- break;
- }
- }
- //type : login , username : pierre , password : omidyar
- //TODO: CHECK IF USER IS ALREADY LOGGED
- private void login(LinkedHashMap<String, String> parsedInput){
- String username, password;
- username = parsedInput.get("username");
- password = parsedInput.get("password");
- try {
- if(TCPServerImpl.RMI.login(username, password)){
- out.println("type : login , ok : true");
- } else {
- out.println("type : login , ok : false");
- }
- } catch (IOException e) {
- try {
- System.out.println("Connection with problems...");
- Thread.sleep(5000);
- } catch (InterruptedException e1) {
- e1.printStackTrace();
- }
- TCPServerImpl.rmiConnection();
- login(parsedInput);
- //System.out.println("Crashei-me todo!!!!!!");
- //Problems with rmi connection
- }
- }
- // type : register , username : pierre , password : omidyar
- private void register(LinkedHashMap<String, String> parsedInput){
- String username, password;
- username = parsedInput.get("username");
- password = parsedInput.get("password");
- try {
- if(TCPServerImpl.RMI.register(username, password)){
- out.println("type : register , ok : true");
- } else {
- out.println("type : register , ok : false");
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- private void create_auction(LinkedHashMap<String, String> parsedInput){
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement