示例#1
0
 // -----------------------------------------------------------
 public static void sendArgs(Socket s, String[] args, int numArgs, MessageType type)
     throws GenericException {
   Message m = new Message(type);
   m.addPayload(args[0].getBytes());
   try {
     m.send(s);
   } catch (IOException e) {
     throw new GenericException(e.getMessage());
   }
   for (int i = 1; i < numArgs; i++) {
     Message rr = Message.receive(s);
     if (rr.getMessageType() != MessageType.SEND_NEXT_PARAM) {
       throw new GenericException(
           "Got type: "
               + rr.getMessageType().toString()
               + " when "
               + MessageType.SEND_NEXT_PARAM.toString()
               + " was expected");
     }
     m = new Message(MessageType.NEXT_PARAM_RQT);
     m.addPayload(args[i].getBytes());
     try {
       m.send(s);
     } catch (IOException e) {
       throw new GenericException(e.getMessage());
     }
   }
 }
  public void createArenaFromSelection(Player pl, String name) {
    FileConfiguration c = SettingsManager.getInstance().getSystemConfig();
    // SettingsManager s = SettingsManager.getInstance();

    WorldEditPlugin we = p.getWorldEdit();
    Selection sel = we.getSelection(pl);
    if (sel == null) {
      Message.send(pl, ChatColor.RED + "You must make a WorldEdit Selection first!");
      return;
    }
    Location max = sel.getMaximumPoint();
    Location min = sel.getMinimumPoint();

    /* if(max.getWorld()!=SettingsManager.getGameWorld() || min.getWorld()!=SettingsManager.getGameWorld()){
    	Message.send(pl, ChatColor.RED+"Wrong World!");
    	return;
    }*/
    SettingsManager.getInstance().getSpawns().set(("spawns." + name), null);
    c.set("system.arenas." + name + ".world", max.getWorld().getName());
    c.set("system.arenas." + name + ".x1", max.getBlockX());
    c.set("system.arenas." + name + ".y1", max.getBlockY());
    c.set("system.arenas." + name + ".z1", max.getBlockZ());
    c.set("system.arenas." + name + ".x2", min.getBlockX());
    c.set("system.arenas." + name + ".y2", min.getBlockY());
    c.set("system.arenas." + name + ".z2", min.getBlockZ());
    c.set("system.arenas." + name + ".enabled", false);
    c.set("system.arenas." + name + ".min", 3);
    c.set("system.arenas." + name + ".max", 4);
    SettingsManager.getInstance().saveSystemConfig();
    hotAddArena(name);
    Message.send(pl, ChatColor.GREEN + "Arena " + name.toUpperCase() + " succesfully added");
  }
示例#3
0
 // -----------------------------------------------------------
 public static GetFileReturnType getFileResponse(Socket s) throws GenericException {
   // GOELogger log = new GOELogger("/home/arbab/storageLogs/ws.log");
   // System.out.println("INITIALIZED THE LOGGER");
   GetFileReturnType r = new GetFileReturnType();
   Message m = Message.receive(s);
   byte[] payload;
   // IP
   if (m.getMessageType() != MessageType.GET_FILE_RSP)
     throw new GenericException("Error, received invalid type: " + m.getMessageType());
   else payload = m.getPayload();
   r.IP = payload.toString();
   System.out.println("IP: " + r.IP);
   // Port
   m = new Message(MessageType.SEND_NEXT_PARAM);
   try {
     m.send(s);
   } catch (IOException e) {
     throw new GenericException("error " + e);
   }
   m = Message.receive(s);
   if (m.getMessageType() != MessageType.NEXT_PARAM_RQT) {
     throw new GenericException("Error, received invalid type: " + m.getMessageType());
   } else {
     payload = m.getPayload();
     r.Port = (payload[0] << 8) + payload[1];
   }
   // tid
   m = new Message(MessageType.SEND_NEXT_PARAM);
   try {
     m.send(s);
   } catch (IOException e) {
     throw new GenericException("error " + e);
   }
   m = Message.receive(s);
   if (m.getMessageType() != MessageType.NEXT_PARAM_RQT) {
     throw new GenericException("Error, received invalid type: " + m.getMessageType());
   } else {
     payload = m.getPayload();
     // r.tid = (long)(payload[0] << 24) + (long)(payload[1] << 16) +
     // (long)(payload[2] << 8) + (long)payload[3];
     // log.write("%%%%%% " + (long)payload[0] + " " + (long)payload[1] + " " +
     // (long)payload[2] + " " + (long)payload[3] + "\n", false);
     // log.write( (0x000000FF & ((long)payload[0])) + "\n",false);
     // log.write( (0x000000FF & ((long)payload[1])) + "\n",false);
     // log.write( (0x000000FF & ((long)payload[2])) + "\n",false);
     // log.write( (0x000000FF & ((long)payload[3])) + "\n",false);
     r.tid =
         (0x000000FF & ((long) payload[0] << 24))
             + (0x000000FF & ((long) payload[1] << 16))
             + (0x000000FF & ((long) payload[2] << 8))
             + (0x000000FF & ((long) payload[3]));
   }
   return r;
 }
示例#4
0
 // -----------------------------------------------------------
 public static void getPutFileRequest(Socket s, String[] args, int numArgs, byte type)
     throws GenericException {
   byte[] myPayload = new byte[4];
   myPayload[0] = (byte) ((numArgs >> 24) & 0xff);
   myPayload[1] = (byte) ((numArgs >> 16) & 0xff);
   myPayload[2] = (byte) ((numArgs >> 8) & 0xff);
   myPayload[3] = (byte) (numArgs & 0xff);
   Message m = new Message(MessageType.findType(type));
   m.addPayload(myPayload);
   try {
     m.send(s);
     m = Message.receive(s);
     if (m.getMessageType() != MessageType.SEND_NEXT_PARAM) {
       System.out.println("Error, unexpected type returned");
       throw new GenericException("Received wrong type");
     }
   } catch (IOException e) {
     throw new GenericException(e.getMessage());
   }
   try {
     sendArgs(s, args, numArgs, MessageType.NEXT_PARAM_RQT);
   } catch (GenericException e) {
     throw new GenericException("Error " + e);
   }
 }
示例#5
0
 private void processMessage(
     final String channel,
     final String message,
     final String sender,
     final String login,
     final String hostname) {
   try {
     logsDao.logMessage(Logs.Type.MESSAGE, sender, channel, message);
     if (isValidSender(sender)) {
       List<Message> responses = new ArrayList<Message>();
       for (final String startString : startStrings) {
         if (responses != null && message.startsWith(startString)) {
           String content = message.substring(startString.length()).trim();
           while (content.charAt(0) == ':' || content.charAt(0) == ',') {
             content = content.substring(1).trim();
           }
           responses.addAll(getResponses(channel, sender, login, hostname, content));
         }
       }
       if (responses.isEmpty()) {
         responses.addAll(getChannelResponses(channel, sender, login, hostname, message));
       }
       for (Message response : responses) {
         response.send(this);
       }
     } else {
       if (log.isInfoEnabled()) {
         log.info("ignoring " + sender);
       }
     }
   } catch (Exception e) {
     log.error(e.getMessage(), e);
     throw new RuntimeException(e.getMessage());
   }
 }
示例#6
0
 // -----------------------------------------------------------
 public static void deleteDirectoryRequest(
     Socket s, String username, String password, String directory) throws GenericException {
   try {
     Message m = new Message(MessageType.DELETE_DIR_RQT);
     byte[] myPayload = {0, 0, 0, 3};
     m.addPayload(myPayload);
     m.send(s);
     Message rr = Message.receive(s);
     if (rr.getMessageType() != MessageType.SEND_NEXT_PARAM) {
       throw new GenericException(
           "Got type: "
               + rr.getMessageType().toString()
               + " when "
               + MessageType.SEND_NEXT_PARAM.toString()
               + " was expected");
     }
     m = new Message(MessageType.NEXT_PARAM_RQT);
     m.addPayload(username.getBytes());
     m.send(s);
     rr = Message.receive(s);
     if (rr.getMessageType() != MessageType.SEND_NEXT_PARAM) {
       throw new GenericException(
           "Got type: "
               + rr.getMessageType().toString()
               + " when "
               + MessageType.SEND_NEXT_PARAM.toString()
               + " was expected");
     }
     m = new Message(MessageType.NEXT_PARAM_RQT);
     m.addPayload(password.getBytes());
     m.send(s);
     rr = Message.receive(s);
     if (rr.getMessageType() != MessageType.SEND_NEXT_PARAM) {
       throw new GenericException(
           "Got type: "
               + rr.getMessageType().toString()
               + " when "
               + MessageType.SEND_NEXT_PARAM.toString()
               + " was expected");
     }
     m = new Message(MessageType.NEXT_PARAM_RQT);
     m.addPayload(directory.getBytes());
     m.send(s);
   } catch (IOException e) {
     throw new GenericException(e.getMessage());
   }
 }
示例#7
0
 // -----------------------------------------------------------
 public static void copyFileRequest(Socket s, String username, String origName, String copyName)
     throws GenericException {
   try {
     Message m = new Message(MessageType.COPY_FILE_RQT);
     byte[] myPayload = {0, 0, 0, 3};
     m.addPayload(myPayload);
     m.send(s);
     Message rr = Message.receive(s);
     if (rr.getMessageType() != MessageType.SEND_NEXT_PARAM) {
       throw new GenericException(
           "Got type: "
               + rr.getMessageType().toString()
               + " when "
               + MessageType.SEND_NEXT_PARAM.toString()
               + " was expected");
     }
     m = new Message(MessageType.NEXT_PARAM_RQT);
     m.addPayload(username.getBytes());
     m.send(s);
     rr = Message.receive(s);
     if (rr.getMessageType() != MessageType.SEND_NEXT_PARAM) {
       throw new GenericException(
           "Got type: "
               + rr.getMessageType().toString()
               + " when "
               + MessageType.SEND_NEXT_PARAM.toString()
               + " was expected");
     }
     m = new Message(MessageType.NEXT_PARAM_RQT);
     m.addPayload(origName.getBytes());
     m.send(s);
     rr = Message.receive(s);
     if (rr.getMessageType() != MessageType.SEND_NEXT_PARAM) {
       throw new GenericException(
           "Got type: "
               + rr.getMessageType().toString()
               + " when "
               + MessageType.SEND_NEXT_PARAM.toString()
               + " was expected");
     }
     m = new Message(MessageType.NEXT_PARAM_RQT);
     m.addPayload(copyName.getBytes());
     m.send(s);
   } catch (IOException e) {
     throw new GenericException(e.getMessage());
   }
 }
 public void addPlayer(Player p, String g) {
   Game game = getGame(g);
   if (game == null) {
     Message.send(p, ChatColor.RED + "Game does not exist!");
     return;
   }
   getGame(g).addPlayer(p);
 }
 public void removeFromOtherQueues(Player p, String id) {
   for (Game g : getGames()) {
     if (g.isInQueue(p) && g.getID() != id) {
       g.removeFromQueue(p);
       Message.send(p, ChatColor.GREEN + "Removed from the queue in arena " + g.getID());
     }
   }
 }
示例#10
0
 // -----------------------------------------------------------
 // send a terminateRequest message
 public static void terminateRequest(Socket s) throws IOException {
   byte[] b = new byte[4];
   Message m = new Message(MessageType.TERM_RQT);
   b[0] = 0;
   b[1] = 0;
   b[2] = 0;
   b[3] = 0;
   m.addPayload(b);
   m.send(s);
 }
示例#11
0
 public static void sendArg(Socket s, byte[] arg, MessageType type) throws GenericException {
   Message m = new Message(type);
   m.addPayload(arg);
   try {
     m.send(s);
   } catch (IOException e) {
     throw new GenericException(e.getMessage());
   }
   Message rr = Message.receive(s);
   if (rr.getMessageType() != MessageType.SEND_NEXT_PARAM) {
     System.out.println("Got a " + rr.getMessageType() + " message");
     throw new GenericException(
         "Got type: "
             + rr.getMessageType().toString()
             + " when "
             + MessageType.SEND_NEXT_PARAM.toString()
             + " was expected");
   }
 }
示例#12
0
 // -----------------------------------------------------------
 // ---------------SERVICE MANAGEMENT FUNCTIONS----------------
 // send an initRequest message
 public static void initRequest(Socket s, int numServs) throws GenericException {
   try {
     System.out.println("Sending init");
     Message m = new Message(MessageType.INIT_RQT);
     // 2 byte payload, maximum of 65536 'servers' per web service
     byte[] Payload =
         new byte[] {
           (byte) (numServs >> 24),
           (byte) (numServs >> 16),
           (byte) (numServs >> 8),
           (byte) (numServs)
         };
     m.addPayload(Payload);
     m.send(s);
     System.out.println("Sent init");
   } catch (IOException e) {
     throw new GenericException(e.getMessage());
   }
 }
示例#13
0
 // -----------------------------------------------------------
 public static listFilesRequestType listFilesResponse(Socket s) throws GenericException {
   byte[] payload;
   int numFiles;
   Message m = Message.receive(s);
   if (m.getMessageType() != MessageType.LIST_FILES_RSP)
     throw new GenericException("Error, received invalid type: " + m.getMessageType());
   else {
     payload = m.getPayload();
     numFiles = (payload[0] << 24) + (payload[1] << 16) + (payload[2] << 8) + payload[3];
   }
   System.out.println(numFiles + " files to be received");
   listFilesRequestType fileList = new listFilesRequestType(numFiles);
   for (int i = 0; i < numFiles; i++) {
     // m.setMessageType(MessageType.SEND_NEXT_PARAM);
     m = new Message(MessageType.SEND_NEXT_PARAM);
     try {
       m.send(s);
     } catch (IOException e) {
       throw new GenericException("error " + e);
     }
     m = Message.receive(s);
     if (m.getMessageType() != MessageType.FILE_NAME_RQT
         && m.getMessageType() != MessageType.DIR_NAME_RQT) {
       throw new GenericException("Error, received invalid type: " + m.getMessageType());
     } else {
       payload = m.getPayload();
       fileList.name[i] = byteArrayToString(payload);
       if (m.getMessageType() == MessageType.FILE_NAME_RQT) {
         fileList.isDir[i] = false;
         System.out.println("File: " + fileList.name[i]);
       } else {
         fileList.isDir[i] = true;
         System.out.println("Directory: " + fileList.name[i]);
       }
     }
   } // end for
   return (fileList);
 }
示例#14
0
 // ------------------------------------------------------------
 private boolean respondToDatagram(
     DatagramPacket receivePacket,
     ServerDescription[] validServers,
     boolean addToFile,
     int expectedServers)
     throws IOException {
   boolean serverAdded = false;
   StorageServer serv = new StorageServer();
   try {
     if (receivePacket.getData()[0] == MessageType.ADD_SERV_RQT.getType()) {
       extractPacketData(serv, receivePacket);
       int servNo = findServer(validServers, serv.IP, serv.port);
       int index = servAlreadyConnected(serv);
       /*
        * if(expectedServers == 0) { Message m = new
        * Message(MessageType.COPY_FILE_RQT); m.send(serv.sock); return(false);
        * } else if(expectedServers == 1) { Message m = new
        * Message(MessageType.COPY_FILE_RSP); m.send(serv.sock); return(false);
        * } else if(expectedServers == 2) { Message m = new
        * Message(MessageType.DELETE_DIR_RQT); m.send(serv.sock);
        * return(false); } else { Message m = new
        * Message(MessageType.DELETE_DIR_RSP); m.send(serv.sock); }
        * if(expectedServers > 2) { return false; }
        */
       if (servNo > 0 || addToFile) {
         if (index >= 0) {
           System.out.println("Reconnection of a server, removing stale values");
           serverList.remove(index);
         }
         Message m = new Message(MessageType.SEND_NEXT_PARAM);
         m.send(serv.sock);
         m = Message.receive(serv.sock);
         if (m.getMessageType() != MessageType.NEXT_PARAM_RQT) {
           m = new Message(MessageType.ERR_UNEXPECTED_CLT);
           m.send(serv.sock);
           return (false);
         } else {
           byte[] payload = new byte[2];
           payload = m.getPayload();
           serv.httpPort = payload[0] & 0xff;
           serv.httpPort <<= 8;
           serv.httpPort += payload[1] & 0xff;
           if (servNo > 0) {
             serv.servNo = servNo;
           }
           if (servNo < 0) {
             try {
               validServers[expectedServers].IP = ServerDescription.IPStrToByteArr(serv.IP);
               validServers[expectedServers].port = serv.port;
               validServers[expectedServers].serverNumber = findMinUnusedServNum(validServers);
               serv.servNo = validServers[expectedServers].serverNumber;
               addToFile(serv, validServers[expectedServers].serverNumber);
               serverAdded = true;
             } catch (GenericException e) {
               m = new Message(MessageType.ERR_UNEXPECTED_CLT);
               m.send(serv.sock);
               return (false);
             }
           }
           // By default, external IP = internal IP
           serv.externalName = serv.IP;
           Iterator itr = ipPairs.iterator();
           // if we find an external IP that is associated with the
           // internal IP for this server, change the external IP
           // address for this server to match
           while (itr.hasNext()) {
             String[] pair = (String[]) itr.next();
             // mylog.write("pair[0]: " + pair[0] + "servIP: " + serv.IP +
             // "\n", true);
             if (pair[0].equals(serv.IP)) {
               // mylog.write("found ext int match\n", true);
               serv.externalName = pair[1];
               /*
                * Runtime runtime = Runtime.getRuntime(); String[] args = new
                * String[]{"sh", "-c", "ssh -f -i " + keyfile + " root@" + gwIP
                * + " \"perl stoptcppr.pl " + serv.externalIP + "\""}; Process
                * proc = runtime.exec(args); args = new String[]{"sh", "-c",
                * "ssh -f -i " + keyfile + " root@" + gwIP +
                * " \"perl tcppr.pl " + serv.externalIP + " " + serv.IP +
                * "\""}; proc = runtime.exec(args);
                */
               break;
             }
           }
           serverList.add(serv);
           m = new Message(MessageType.ADD_SERV_RSP);
           payload[0] = (byte) ((servNo >> 8) & 0xff);
           payload[1] = (byte) (servNo & 0xff);
           m.addPayload(payload);
           m.send(serv.sock);
         }
       } else {
         Message m = new Message(MessageType.ERR_UNEXPECTED_CLT);
         m.send(serv.sock);
       }
     }
     return (serverAdded);
   } catch (IOException e) {
     Message m = new Message(MessageType.INTERNAL_SERVER_ERR);
     m.send(serv.sock);
     throw new IOException(e);
   } catch (GenericException e) {
     Message m = new Message(MessageType.INTERNAL_SERVER_ERR);
     m.send(serv.sock);
     throw new IOException(e.getMessage());
   }
 }