Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.net.*;
- import java.rmi.RemoteException;
- import java.util.ArrayList;
- import java.util.Scanner;
- public class MulticastServer extends Thread {
- private static ArrayList<Pessoa> bdPessoas = new ArrayList<>();
- private static ArrayList<Musica> bdMusicas = new ArrayList<>();
- private static ArrayList<Album> bdAlbuns = new ArrayList<>();
- private static ArrayList<Artista> bdArtistas = new ArrayList<>();
- private String MULTICAST_ADDRESS = "224.3.2.1";
- private int PORT = 4321;
- private long SLEEP_TIME = 5000;
- private static int porto;
- //escrever nos ficheiros
- public static void escreveFicheiro(String fich, Object obj){
- ObjectOutputStream ooS = null;
- try{
- FileOutputStream foS = new FileOutputStream(fich);
- ooS = new ObjectOutputStream(foS);
- ooS.writeObject(obj);
- ooS.flush();
- }catch (IOException e) {
- System.out.println("Erro a abrir o ficheiro " + fich);
- }finally {
- if(ooS != null){
- try {
- ooS.close();
- } catch (IOException e) {
- System.out.println("Erro a fechar o ficheiro " + fich);
- }
- }
- }
- }
- //funcao que serve para pesquisar musicas por nome de musica
- public Musica pesquisaMusicas(Musica musica){
- for(Musica m : bdMusicas){
- if(m.getNome().equals(musica.getNome())){
- return musica;
- }
- }
- System.out.println("Musica nao se encontra no sistema!");
- return null;
- }
- //pesquisar musicas por album
- public Album pesquisaMusicaAlb(String nome){
- for(Album a : bdAlbuns){
- if(a.getNome().equals(nome)){
- return a;
- }
- }
- System.out.println("Album nao esta no sistema!");
- return null;
- }
- public static int checkLogin(String username, String pass){
- for(Pessoa p : bdPessoas){
- if(p.getUsername().equals(username) && p.getPassword().equals(pass)){
- return 1;
- }
- }
- return 0;
- }
- public static Pessoa pesquisaPessoas(String username){
- for (Pessoa p : bdPessoas){
- if(p.getUsername().equals(username)){
- return p;
- }
- }
- System.out.println("Pessoa nao esta no sistema!");
- return null;
- }
- public static boolean signUp(Pessoa p){
- if(pesquisaPessoas(p.getUsername())==null){
- bdPessoas.add(p);
- return true;
- }
- else
- return false;
- }
- public static Artista pesquisaArtistas(String nome){
- for (Artista a: bdArtistas){
- if(a.getNome().equals(nome)){
- return a;
- }
- }
- return null;
- }
- public static Boolean addArtist(Artista a){
- if(pesquisaArtistas(a.getUsername())==null){
- bdArtistas.add(a);
- return true;
- }
- else
- return false;
- }
- public static void sendResposta(String msg) throws IOException {
- byte[] buf = new byte[1000];
- DatagramPacket recv = new DatagramPacket(buf, buf.length);
- DatagramSocket s = null;
- try {
- s = new DatagramSocket(4000);
- } catch (SocketException e) {
- e.printStackTrace();
- }
- DatagramPacket reply = new DatagramPacket(recv.getData(),
- recv.getLength(), recv.getAddress(), recv.getPort());
- s.send(reply);
- }
- public static void escolheOpcao(String comando) throws IOException {
- String user = new String();
- String password = new String();
- String tipo = new String();
- Pessoa newPessoa = new Pessoa();
- Artista newArtista=new Artista();
- try {
- if (comando != null && comando != "\n" && comando != "") {
- String parts[] = comando.split(" ; ");
- String funcao[] = parts[0].split(" / ");
- tipo = funcao[1];
- if (funcao[1].equals("login") && funcao != null) {
- String username[] = parts[1].split(" / ");
- user = username[1].trim();
- String[] passwordArray = parts[2].split("/");
- password = passwordArray[1].trim();
- }
- if (funcao[1].equals("sign up") && funcao != null) {
- //type / sign up ; item_count / 2 ; name / gui ; username / gui ; password / ola ; mail / g@hot.com
- String[] infoArray = new String[4];
- String[] f;
- int count=0;
- for (int i=2; i<parts.length; i++){
- f=parts[i].split(" / ");
- infoArray[count] =f[1].trim();
- count++;
- }
- newPessoa.setNome(infoArray[0]);
- newPessoa.setUsername(infoArray[1]);
- newPessoa.setPassword(infoArray[2]);
- newPessoa.setMail(infoArray[3]);
- }
- if (funcao[1].equals("add artist") && funcao != null) {
- //type / add artist ; item_count / 4 ; name / gui ; username / gui ; password / ola ; mail / g@hot.com
- String[] infoArtist = new String[4];
- String[] f;
- int count=0;
- for (int i=2; i<parts.length; i++){
- f=parts[i].split(" / ");
- infoArtist[count] =f[1].trim();
- count++;
- }
- newArtista.setNome(infoArtist[0]);
- newArtista.setUsername(infoArtist[1]);
- newArtista.setPassword(infoArtist[2]);
- newArtista.setMail(infoArtist[3]);
- }
- if (funcao[1].equals("remove artist") && funcao != null) {
- //type / remove artist ; item_count / 2 ; name / gui ; username / gui ; password / ola ;
- }
- }
- }catch (ArrayIndexOutOfBoundsException exception){}
- switch (tipo){
- case "login":
- if (checkLogin(user, password) == 1){
- pesquisaPessoas(user).setLog(true);
- }
- //envia resposta
- //sendResposta("type / status ; logged / on ; msg / Welcome to DropMusic");
- break;
- case "sign up":
- System.out.println("welcome to sign up");
- if(signUp(newPessoa)==true){
- System.out.println("registado com sucesso");
- } else
- System.out.println("nao foi possivel registar");
- //envia resposta
- //sendResposta("type / status ; logged / on ; msg / Welcome to DropMusic");
- break;
- case "add artist":
- System.out.println("welcome to add artist");
- if (addArtist(newArtista)==true){
- System.out.println("Artista registado com sucesso");
- } else
- System.out.println("nao foi possivel registar");
- //envia resposta
- //sendResposta("type / status ; logged / on ; msg / Welcome to DropMusic");
- break;
- }
- }
- public static void main(String[] args) {
- MulticastServer server = new MulticastServer();
- //server.start();
- System.out.println("Escolha o porto a iniciar ([4000], [3000], [2000])");
- while(true){
- Scanner sc = new Scanner(System.in);
- porto = sc.nextInt();
- if(porto == 4000 || porto == 3000 || porto == 2000)
- break;
- else{
- System.out.println("Porto invalido");
- continue;
- }
- }
- //thread que recebe mensagens do rmi
- new Thread() { //THREAD QUE TRATA DO MULTICAST (recebe mensagens)
- public void run() {
- MulticastSocket s = null;
- try {
- s = new MulticastSocket(porto);
- s.joinGroup(InetAddress.getByName("224.3.2.1"));
- } catch (IOException e) {
- e.printStackTrace();
- }
- byte[] buf = new byte[1000];
- DatagramPacket recv = new DatagramPacket(buf, buf.length);
- try {
- while (true) {
- s.receive(recv);
- String msgNew = new String(recv.getData(), 0, recv.getLength());
- escolheOpcao(msgNew);
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }.start();
- }
- public MulticastServer() {
- super("Server " + (long) (Math.random() * 1000));
- }
- public void run() {
- MulticastSocket socket = null;
- long counter = 0;
- System.out.println(this.getName() + " running...");
- try {
- socket = new MulticastSocket(); // create socket without binding it (only for sending)
- while (true) {
- String message = this.getName() + " packet " + counter++;
- byte[] buffer = message.getBytes();
- InetAddress group = InetAddress.getByName(MULTICAST_ADDRESS);
- DatagramPacket packet = new DatagramPacket(buffer, buffer.length, group, PORT);
- socket.send(packet);
- try { sleep((long) (Math.random() * SLEEP_TIME)); } catch (InterruptedException e) { }
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- socket.close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement